-
Notifications
You must be signed in to change notification settings - Fork 284
improve error handling for tools and server methods #311
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
improve error handling for tools and server methods #311
Conversation
yasomaru
commented
Aug 17, 2025
- Fix tool error handling to properly distinguish between protocol errors and tool execution errors
- Return structured JSON-RPC errors directly for protocol-level issues
- Embed regular tool errors in CallToolResult as per MCP specification
- Improve server error responses for unknown prompts and tools
- Add comprehensive tests for both structured and regular error handling
- Fix tool error handling to properly distinguish between protocol errors and tool execution errors - Return structured JSON-RPC errors directly for protocol-level issues - Embed regular tool errors in CallToolResult as per MCP specification - Improve server error responses for unknown prompts and tools - Add comprehensive tests for both structured and regular error handling This change resolves TODO comments about improper error handling and ensures clients can properly handle different types of errors according to the MCP spec. Fixes: #TODO (error handling improvements)
mcp/tool_test.go
Outdated
| // Create a tool that returns a structured error | ||
| structuredErrorHandler := func(ctx context.Context, req *ServerRequest[*CallToolParamsFor[map[string]any]]) (*CallToolResultFor[any], error) { | ||
| return nil, &jsonrpc2.WireError{ | ||
| Code: -32603, // ErrInternal |
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.
Define this one too, same place.
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.
Done.
mcp/tool_test.go
Outdated
|
|
||
| var wireErr *jsonrpc2.WireError | ||
| if !errors.As(err, &wireErr) { | ||
| t.Fatalf("expected WireError, got %T: %v", err, err) |
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.
%T, %[1]%v
then you only need one arg
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.
Done.
- Introduced a constant for CodeInvalidParams to enhance readability and maintainability in error handling. - Updated server methods to use the new constant instead of hardcoded error codes. - Modified tests to validate the use of the new error code constant for structured JSON-RPC errors. This change aims to standardize error handling across the codebase and improve the clarity of error responses.
jba
left a comment
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.
Thanks!
…ocol#311) - Fix tool error handling to properly distinguish between protocol errors and tool execution errors - Return structured JSON-RPC errors directly for protocol-level issues - Embed regular tool errors in CallToolResult as per MCP specification - Improve server error responses for unknown prompts and tools - Add comprehensive tests for both structured and regular error handling