Skip to content

Commit b29d522

Browse files
committed
Show exception messages for tool call errors
Claude Code sometimes only shows `Error: MCP error -32603: Internal error` when an unexpected exception occurs during a tool call. This change broadens the exception handling scope so the server can surface the exception message more often, making failures easier to diagnose.
1 parent ca93445 commit b29d522

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

lib/mcp/server.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,11 @@ def call_tool(request)
338338
end
339339
end
340340

341-
begin
342-
call_tool_with_args(tool, arguments)
343-
rescue => e
344-
report_exception(e, { request: request })
341+
call_tool_with_args(tool, arguments)
342+
rescue => e
343+
report_exception(e, request: request)
345344

346-
error_tool_response("Internal error calling tool #{tool_name}: #{e.message}")
347-
end
345+
error_tool_response("Internal error calling tool #{tool_name}: #{e.message}")
348346
end
349347

350348
def list_prompts(request)

test/mcp/server_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,36 @@ class Example < Tool
431431
assert_instrumentation_data({ method: "tools/call", tool_name: "tool_that_raises" })
432432
end
433433

434+
test "#handle tools/call returns error response with isError true if input_schema raises an error during validation" do
435+
tool = Tool.define(
436+
name: "tool_with_faulty_schema",
437+
title: "Tool with faulty schema",
438+
description: "A tool with a faulty schema",
439+
input_schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
440+
) { Tool::Response.new("success") }
441+
442+
tool.input_schema.expects(:missing_required_arguments?).raises(RuntimeError, "Unexpected schema error")
443+
444+
server = Server.new(name: "test_server", tools: [tool])
445+
446+
request = {
447+
jsonrpc: "2.0",
448+
method: "tools/call",
449+
params: {
450+
name: "tool_with_faulty_schema",
451+
arguments: { message: "test" },
452+
},
453+
id: 1,
454+
}
455+
456+
response = server.handle(request)
457+
458+
assert_nil response[:error], "Expected no JSON-RPC error"
459+
assert response[:result][:isError]
460+
assert_equal "text", response[:result][:content][0][:type]
461+
assert_match(/Internal error calling tool tool_with_faulty_schema: Unexpected schema error/, response[:result][:content][0][:text])
462+
end
463+
434464
test "#handle tools/call returns error response with isError true for unknown tool" do
435465
request = {
436466
jsonrpc: "2.0",

0 commit comments

Comments
 (0)