Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ def call_tool(request)
unless tool
add_instrumentation_data(tool_name: tool_name, error: :tool_not_found)

return error_tool_response("Tool not found: #{tool_name}")
raise RequestHandlerError.new("Tool not found: #{tool_name}", request, error_type: :invalid_params)
end

arguments = request[:arguments] || {}
Expand All @@ -401,6 +401,8 @@ def call_tool(request)
end

call_tool_with_args(tool, arguments)
rescue RequestHandlerError
raise
rescue => e
report_exception(e, request: request)

Expand Down
22 changes: 11 additions & 11 deletions test/mcp/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ class Example < Tool
assert_match(/Internal error calling tool tool_with_faulty_schema: Unexpected schema error/, response[:result][:content][0][:text])
end

test "#handle tools/call returns error response with isError true for unknown tool" do
test "#handle tools/call returns JSON-RPC error for unknown tool" do
request = {
jsonrpc: "2.0",
method: "tools/call",
Expand All @@ -539,14 +539,14 @@ class Example < Tool
}

response = @server.handle(request)
assert_nil response[:error], "Expected no JSON-RPC error"
assert response[:result][:isError]
assert_equal "text", response[:result][:content][0][:type]
assert_equal "Tool not found: unknown_tool", response[:result][:content][0][:text]
assert_instrumentation_data({ method: "tools/call", tool_name: "unknown_tool", error: :tool_not_found })
assert_nil response[:result]
assert_equal(-32602, response[:error][:code])
assert_equal "Invalid params", response[:error][:message]
assert_includes response[:error][:data], "Tool not found: unknown_tool"
assert_instrumentation_data({ method: "tools/call", tool_name: "unknown_tool", error: :invalid_params })
end

test "#handle_json returns error response with isError true for unknown tool" do
test "#handle_json returns JSON-RPC error for unknown tool" do
request = JSON.generate({
jsonrpc: "2.0",
method: "tools/call",
Expand All @@ -558,10 +558,10 @@ class Example < Tool
})

response = JSON.parse(@server.handle_json(request), symbolize_names: true)
assert_nil response[:error], "Expected no JSON-RPC error"
assert response[:result][:isError]
assert_equal "text", response[:result][:content][0][:type]
assert_equal "Tool not found: unknown_tool", response[:result][:content][0][:text]
assert_nil response[:result]
assert_equal(-32602, response[:error][:code])
assert_equal "Invalid params", response[:error][:message]
assert_includes response[:error][:data], "Tool not found: unknown_tool"
end

test "#tools_call_handler sets the tools/call handler" do
Expand Down