Skip to content

fix: return isError for argument validation failures#2488

Open
blackwell-systems wants to merge 1 commit into
github:mainfrom
blackwell-systems:fix/is-error-validation
Open

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

Conversation

@blackwell-systems
Copy link
Copy Markdown

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
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