Skip to content

Conversation

@yasomaru
Copy link
Contributor

  • 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
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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

Copy link
Contributor Author

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.
@yasomaru yasomaru requested a review from jba August 20, 2025 05:46
Copy link
Contributor

@jba jba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@jba jba merged commit 719f68d into modelcontextprotocol:main Aug 20, 2025
5 checks passed
yasomaru added a commit to yasomaru/go-sdk that referenced this pull request Aug 28, 2025
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants