-
Notifications
You must be signed in to change notification settings - Fork 75
Fix tool error handling to follow MCP spec #165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -258,37 +258,60 @@ def list_tools(request) | |
|
||
def call_tool(request) | ||
tool_name = request[:name] | ||
arguments = request[:arguments] || {} | ||
|
||
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] || {} | ||
add_instrumentation_data(tool_name:) | ||
|
||
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 | ||
begin | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a step in the right direction, but I think we can take this a few more steps further: This rescue should be at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atesgoral makes sense. This is how it should work now: Original (Everything was a Protocol Error)
Now - Protocol Error
Now - Tool error:
|
||
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}", | ||
}], | ||
error: true, | ||
).to_h | ||
end | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems better to keep it at its original position on line 267, since there's no need to process it until it becomes necessary.