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
10 changes: 4 additions & 6 deletions lib/mcp/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,11 @@ def call_tool(request)
end
end

begin
call_tool_with_args(tool, arguments)
rescue => e
report_exception(e, { request: request })
call_tool_with_args(tool, arguments)
rescue => e
report_exception(e, request: request)

error_tool_response("Internal error calling tool #{tool_name}: #{e.message}")
end
error_tool_response("Internal error calling tool #{tool_name}: #{e.message}")
end

def list_prompts(request)
Expand Down
30 changes: 30 additions & 0 deletions test/mcp/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,36 @@ class Example < Tool
assert_instrumentation_data({ method: "tools/call", tool_name: "tool_that_raises" })
end

test "#handle tools/call returns error response with isError true if input_schema raises an error during validation" do
tool = Tool.define(
name: "tool_with_faulty_schema",
title: "Tool with faulty schema",
description: "A tool with a faulty schema",
input_schema: { type: "object", properties: { message: { type: "string" } }, required: ["message"] },
) { Tool::Response.new("success") }

tool.input_schema.expects(:missing_required_arguments?).raises(RuntimeError, "Unexpected schema error")

server = Server.new(name: "test_server", tools: [tool])

request = {
jsonrpc: "2.0",
method: "tools/call",
params: {
name: "tool_with_faulty_schema",
arguments: { message: "test" },
},
id: 1,
}

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_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
request = {
jsonrpc: "2.0",
Expand Down