Skip to content

fix: return isError for argument validation failures#2511

Merged
SamMorrowDrums merged 3 commits into
mainfrom
sammorrowdrums/redo-pr-2488-iserror-fix
May 20, 2026
Merged

fix: return isError for argument validation failures#2511
SamMorrowDrums merged 3 commits into
mainfrom
sammorrowdrums/redo-pr-2488-iserror-fix

Conversation

@SamMorrowDrums
Copy link
Copy Markdown
Collaborator

Re-applies #2488 by @blackwell-systems on top of the NewServerTool rename in #2510.

What

When tool argument unmarshalling fails (wrong types, malformed JSON), the constructors in pkg/inventory/server_tool.go previously 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 CallToolResult with IsError: true and the validation message instead, so the agent sees the problem and can retry with corrected arguments.

Affects:

Tests

Adds pkg/inventory/server_tool_test.go covering both constructors (invalid args → IsError: true) plus a success path.

Stacking & credit

Fixes #1952.
Closes #2488 (superseded).

Copilot AI review requested due to automatic review settings May 20, 2026 08:09
@SamMorrowDrums SamMorrowDrums requested a review from a team as a code owner May 20, 2026 08:09
- 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>
@SamMorrowDrums SamMorrowDrums force-pushed the sammorrowdrums/redo-new-server-tool-cleanup branch from 5b022ba to 39dcba6 Compare May 20, 2026 08:12
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.CallToolResult with IsError: true when json.Unmarshal of tool arguments fails in NewServerToolWithDeps and NewServerToolWithContextHandler.
  • 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>
@SamMorrowDrums SamMorrowDrums force-pushed the sammorrowdrums/redo-pr-2488-iserror-fix branch from ac0f024 to 4884d63 Compare May 20, 2026 08:14
Base automatically changed from sammorrowdrums/redo-new-server-tool-cleanup to main May 20, 2026 08:14
@SamMorrowDrums SamMorrowDrums merged commit 272d160 into main May 20, 2026
18 checks passed
@SamMorrowDrums SamMorrowDrums deleted the sammorrowdrums/redo-pr-2488-iserror-fix branch May 20, 2026 08:19
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.

MCP tool results should set is_error: true when validation fails

2 participants