fix: return isError instead of internal error for tool execution failures#258
Merged
sammcj merged 4 commits intoJun 8, 2026
Conversation
…ures Tool handler returns (nil, error) when Execute() fails, which causes mcp-go to respond with JSON-RPC -32603 internal error. MCP clients treat -32603 as a server crash and cannot recover from it. Agents need isError: true with a descriptive message to self-correct. Changed three error paths to return mcp.NewToolResultError() instead: - tool not found in registry - invalid argument type - tool execution failure (input validation, missing params, etc.) This matches the pattern already used in individual tools like get_library_documentation (line 104) which correctly returns mcp.NewToolResultError() for fetch failures. Affected tools: calculator, get_tool_help, internet_search, search_packages (all return -32603 on invalid input instead of isError with helpful messages). Found with mcp-assert (https://github.com/blackwell-systems/mcp-assert)
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the MCP tool handler so tool failures are returned as isError: true tool results (via mcp.NewToolResultError(...)) instead of propagating a Go error that mcp-go converts into JSON-RPC -32603 internal errors, improving client/agent recoverability.
Changes:
- Return
mcp.NewToolResultError(...), nilwhen a tool is missing from the registry. - Return
mcp.NewToolResultError(...), nilwhen tool call arguments are notmap[string]any. - Return
mcp.NewToolResultError(...), nilwhenExecute()fails.
Extract the inline tool-handler closure into newToolHandler so the error-handling behaviour is unit-testable, and add tests covering execution failure, unknown tool, and the success path - all asserting a failure surfaces as an isError result with a nil Go error (not a JSON-RPC -32603). Include the root package in the make test targets so these run in CI. Drop a no-op deferred closure left in the handler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
(nil, error)whenExecute()fails, causing mcp-go to respond with JSON-RPC-32603internal error-32603as a server crash and cannot recoverisError: truewith a descriptive message to self-correctChanged three error paths in
main.goto returnmcp.NewToolResultError()instead of(nil, fmt.Errorf(...)):This matches the pattern already used in individual tools like
get_library_documentation(line 104) which correctly returnsmcp.NewToolResultError()for fetch failures.Reproduction
Change
Three-line change in
main.go: replacereturn nil, fmt.Errorf(...)withreturn mcp.NewToolResultError(...), nilat all three error return sites in the tool handler.Found with mcp-assert.