Merge pull request #165 from Jflick58/fix/make-tool-error-spec-compliant

Fix tool error handling to follow MCP spec
This commit is contained in:
Koichi ITO 2025-10-22 10:58:29 +09:00 • committed by GitHub
commit 61d48cd6d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -258,10 +258,17 @@ module MCP
def call_tool(request)
tool_name = request[:name]
tool = tools[tool_name]
unless tool
add_instrumentation_data(error: :tool_not_found)
raise RequestHandlerError.new("Tool not found #{tool_name}", request, error_type: :tool_not_found)
add_instrumentation_data(tool_name:, error: :tool_not_found)
return Tool::Response.new(
[{
type: "text",
text: "Tool not found: #{tool_name}",
}],
error: true,
).to_h
end
arguments = request[:arguments] || {}
@ -269,11 +276,14 @@ module MCP
if tool.input_schema&.missing_required_arguments?(arguments)
add_instrumentation_data(error: :missing_required_arguments)
raise RequestHandlerError.new(
"Missing required arguments: #{tool.input_schema.missing_required_arguments(arguments).join(", ")}",
request,
error_type: :missing_required_arguments,
)
missing = tool.input_schema.missing_required_arguments(arguments).join(", ")
return Tool::Response.new(
[{
type: "text",
text: "Missing required arguments: #{missing}",
}],
error: true,
).to_h
end
if configuration.validate_tool_call_arguments && tool.input_schema
@ -281,14 +291,27 @@ module MCP
tool.input_schema.validate_arguments(arguments)
rescue Tool::InputSchema::ValidationError => e
add_instrumentation_data(error: :invalid_schema)
raise RequestHandlerError.new(e.message, request, error_type: :invalid_schema)
return Tool::Response.new(
[{
type: "text",
text: e.message,
}],
error: true,
).to_h
end
end
begin
call_tool_with_args(tool, arguments)
rescue => e
raise RequestHandlerError.new("Internal error calling tool #{tool_name}", request, original_error: e)
report_exception(e, { request: request })
Tool::Response.new(
[{
type: "text",
text: "Internal error calling tool #{tool_name}: #{e.message}",
}],
error: true,
).to_h
end
end