fix: return isError for argument validation failures#2488
fix: return isError for argument validation failures#2488blackwell-systems wants to merge 3 commits into
Conversation
When tool argument unmarshalling fails (wrong types, malformed JSON),
return a tool execution error (isError: true) instead of a Go error
that becomes a protocol error (-32603). This allows agents to see the
validation error message in their context window and self-correct.
Previously, an agent sending {"owner": 12345} (integer) instead of
{"owner": "octocat"} (string) would get an opaque -32603 protocol
error with no message. Now it gets isError: true with
"invalid arguments: json: cannot unmarshal number..." which the model
can act on.
Two sites fixed in server_tool.go:
- NewServerTool (deps-based handler wrapper)
- NewServerToolWithContextHandler (context-based handler wrapper)
Tests added:
- TestNewServerTool_InvalidArguments_ReturnsIsError
- TestNewServerToolWithContextHandler_InvalidArguments_ReturnsIsError
- TestNewServerTool_ValidArguments_Succeeds
Fixes github#1952
SamMorrowDrums
left a comment
There was a problem hiding this comment.
LGTM. Worth noting that this is now the spec-recommended behaviour, not just a pragmatic choice.
The 2025-11-25 spec sharpened the Error Handling section since the older 2025-06-18 revision. It now draws the line at the CallToolRequest schema rather than the tool's inputSchema:
- Protocol Errors → "Malformed requests (requests that fail to satisfy [CallToolRequest schema])"
- Tool Execution Errors (
isError: true) → "Input validation errors (e.g., date in wrong format, value out of range)"
And it adds the rationale this PR is built on:
Tool Execution Errors contain actionable feedback that language models can use to self-correct and retry with adjusted parameters. Protocol Errors indicate issues with the request structure itself that models are less likely to be able to fix. Clients SHOULD provide tool execution errors to language models to enable self-correction.
json.Unmarshal failures into the typed argument struct are exactly the "input validation" case the spec calls out — so the previous return nil, err path was the wrong mechanism under the current spec, and this PR aligns the server with it.
Both call sites are updated symmetrically, the response shape is well-formed (TextContent + IsError: true, returning a nil Go error so the SDK doesn't also emit a JSON-RPC error), and the tests cover both handler variants plus the happy path.
|
@blackwell-systems some lint failures. |
|
@blackwell-systems still some similar lint failures. You can run locally to avoid discovering this in CI naturally. There are scripts in repo etc. |
05ba17d to
ebffad1
Compare
|
re-created pr with you as co-author, I do not have time for iterating on linter failures multiple times in a row. thank you for contribution. |
When tool argument unmarshalling fails (wrong types, malformed JSON), return a CallToolResult with IsError: true instead of a Go error. Returning a Go error is converted by the SDK into a JSON-RPC protocol error (-32603), which is invisible to agents and prevents self-correction. Returning IsError: true with the validation message lets agents see the problem and retry with corrected arguments. Affects: - NewServerToolWithDeps (was NewServerTool prior to the rename in #2510) - NewServerToolWithContextHandler Fixes #1952. Re-applies #2488 by @blackwell-systems on top of the NewServerTool rename. Co-authored-by: blackwell-systems <236632453+blackwell-systems@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* refactor: simplify NewServerTool naming - Rename NewServerToolWithRawContextHandler -> NewServerTool. This is the preferred constructor for raw mcp.ToolHandler tools because it avoids creating closures at registration time, which matters for per-request servers that re-register all tools on every request. - Rename deprecated generic NewServerTool[In, Out] -> NewServerToolWithDeps to free up the simpler name and make its closure-based nature explicit. The dynamic tools package is the only legitimate user of this constructor because DynamicToolDependencies differs from the standard ToolDependencies. - Remove deprecated NewServerToolFromHandler. Its only callers can use the new NewServerTool directly via context-injected deps. - Update all call sites in dependencies.go, dynamic_tools.go, and registry_test.go. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> * fix: return isError for argument validation failures When tool argument unmarshalling fails (wrong types, malformed JSON), return a CallToolResult with IsError: true instead of a Go error. Returning a Go error is converted by the SDK into a JSON-RPC protocol error (-32603), which is invisible to agents and prevents self-correction. Returning IsError: true with the validation message lets agents see the problem and retry with corrected arguments. Affects: - NewServerToolWithDeps (was NewServerTool prior to the rename in #2510) - NewServerToolWithContextHandler Fixes #1952. Re-applies #2488 by @blackwell-systems on top of the NewServerTool rename. Co-authored-by: blackwell-systems <236632453+blackwell-systems@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: blackwell-systems <236632453+blackwell-systems@users.noreply.github.com>
Fixes #1952.
Problem
When tool argument unmarshalling fails (wrong types, malformed JSON), the handler returns
(nil, err)which the SDK converts to a JSON-RPC protocol error (-32603). Agents cannot see protocol errors in their context window, so they cannot self-correct.Fix
Return
isError: truewith the validation error message instead of a Go error. Two sites inserver_tool.go:NewServerTool(line 135)NewServerToolWithContextHandler(line 159)Before:
After:
Tests
TestNewServerTool_InvalidArguments_ReturnsIsErrorTestNewServerToolWithContextHandler_InvalidArguments_ReturnsIsErrorTestNewServerTool_ValidArguments_Succeeds