Skip to content

Commit c8a68da

Browse files
authored
Merge pull request #231 from koic/return_json_rpc_protocol_errors_for_unknown_tool_calls
Return JSON-RPC protocol errors for unknown tool calls
2 parents 6df8e4c + 15710ef commit c8a68da

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

lib/mcp/server.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def call_tool(request)
377377
unless tool
378378
add_instrumentation_data(tool_name: tool_name, error: :tool_not_found)
379379

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

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

403403
call_tool_with_args(tool, arguments)
404+
rescue RequestHandlerError
405+
raise
404406
rescue => e
405407
report_exception(e, request: request)
406408

test/mcp/server_test.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ class Example < Tool
527527
assert_match(/Internal error calling tool tool_with_faulty_schema: Unexpected schema error/, response[:result][:content][0][:text])
528528
end
529529

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

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

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

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

567567
test "#tools_call_handler sets the tools/call handler" do

0 commit comments

Comments
 (0)