fix: return isError for argument validation failures#2511
Merged
Conversation
- 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>
5b022ba to
39dcba6
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Updates the pkg/inventory ServerTool constructors so JSON argument unmarshalling failures are surfaced to MCP clients as tool execution errors (CallToolResult.IsError: true) instead of JSON-RPC protocol errors, enabling agents to see the validation message and retry with corrected arguments.
Changes:
- Return a
*mcp.CallToolResultwithIsError: truewhenjson.Unmarshalof tool arguments fails inNewServerToolWithDepsandNewServerToolWithContextHandler. - Add unit tests covering invalid-argument behavior for both constructors plus a valid-argument success path.
Show a summary per file
| File | Description |
|---|---|
| pkg/inventory/server_tool.go | Converts argument-unmarshal failures from returned Go errors into MCP-visible CallToolResult errors (IsError: true). |
| pkg/inventory/server_tool_test.go | Adds tests ensuring invalid arguments yield IsError: true (and handlers are not invoked), plus a success-case test. |
Copilot's findings
- Files reviewed: 5/5 changed files
- Comments generated: 0
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>
ac0f024 to
4884d63
Compare
Base automatically changed from
sammorrowdrums/redo-new-server-tool-cleanup
to
main
May 20, 2026 08:14
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.
Re-applies #2488 by @blackwell-systems on top of the
NewServerToolrename in #2510.What
When tool argument unmarshalling fails (wrong types, malformed JSON), the constructors in
pkg/inventory/server_tool.gopreviously returned(nil, err). The Go SDK converts a returned error into a JSON-RPC protocol error (-32603), which is invisible to agents and prevents them from self-correcting.This change returns a
CallToolResultwithIsError: trueand the validation message instead, so the agent sees the problem and can retry with corrected arguments.Affects:
NewServerToolWithDeps(wasNewServerToolprior to the rename in refactor: simplify NewServerTool naming #2510)NewServerToolWithContextHandlerTests
Adds
pkg/inventory/server_tool_test.gocovering both constructors (invalid args →IsError: true) plus a success path.Stacking & credit
NewServerTool→NewServerToolWithDeps). Targets that branch as base.Co-authored-byon the commit.Fixes #1952.
Closes #2488 (superseded).