Skip to content

fix: return isError for argument validation failures#2488

Closed
blackwell-systems wants to merge 3 commits into
github:mainfrom
blackwell-systems:fix/is-error-validation
Closed

fix: return isError for argument validation failures#2488
blackwell-systems wants to merge 3 commits into
github:mainfrom
blackwell-systems:fix/is-error-validation

Conversation

@blackwell-systems
Copy link
Copy Markdown
Contributor

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: true with the validation error message instead of a Go error. Two sites in server_tool.go:

  • NewServerTool (line 135)
  • NewServerToolWithContextHandler (line 159)

Before:

if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil {
    return nil, err // protocol error: agent sees nothing
}

After:

if err := json.Unmarshal(req.Params.Arguments, &arguments); err != nil {
    return &mcp.CallToolResult{
        Content: []mcp.Content{&mcp.TextContent{Text: fmt.Sprintf("invalid arguments: %s", err)}},
        IsError: true,
    }, nil // tool execution error: agent sees the message and can retry
}

Tests

  • TestNewServerTool_InvalidArguments_ReturnsIsError
  • TestNewServerToolWithContextHandler_InvalidArguments_ReturnsIsError
  • TestNewServerTool_ValidArguments_Succeeds

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
@blackwell-systems blackwell-systems requested a review from a team as a code owner May 17, 2026 08:48
SamMorrowDrums
SamMorrowDrums previously approved these changes May 19, 2026
Copy link
Copy Markdown
Collaborator

@SamMorrowDrums SamMorrowDrums left a comment

Choose a reason for hiding this comment

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

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.

@SamMorrowDrums
Copy link
Copy Markdown
Collaborator

@blackwell-systems some lint failures.

@SamMorrowDrums
Copy link
Copy Markdown
Collaborator

@blackwell-systems still some similar lint failures. You can run locally to avoid discovering this in CI naturally. There are scripts in repo etc.

@SamMorrowDrums
Copy link
Copy Markdown
Collaborator

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.

SamMorrowDrums added a commit that referenced this pull request May 20, 2026
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 added a commit that referenced this pull request May 20, 2026
* 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>
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