mcp: return input validation errors as tool results, not JSON-RPC errors (#863)

This implements [SEP-1303](https://github.com/modelcontextprotocol/modelcontextprotocol/issues/1303).

Input validation errors (missing required fields, wrong types) during
tool calls
are now returned as `CallToolResult` with `IsError=true` instead of
JSON-RPC
`-32602` errors. This allows LLMs to see the validation error and
self-correct,
  per the MCP spec.
The existing pattern for handler-returned errors (lines 347-350 in
server.go)
already converts regular errors to tool results with `IsError=true`.
This change
applies the same pattern to argument schema validation and unmarshal
errors,
  resolving the `TODO(#450)` in the code.                   

Thanks to @iwyrkore for filing the issue and proposing the approach, and
  @findleyr for the cross-SDK analysis in the discussion.
  Fixes #450

Co-authored-by: Maciej Kisiel <mkisiel@google.com>
This commit is contained in:
Ravish Gupta 2026-03-31 01:14:36 -07:00 • committed by GitHub
commit 74d2751d88
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -676,12 +676,19 @@ func testToolForSchema[In, Out any](t *testing.T, tool *Tool, in string, out Out
}
result, err := goth(context.Background(), ctr)
if wantErrContaining != "" {
if err == nil {
t.Errorf("got nil error, want error containing %q", wantErrContaining)
} else {
// Input validation errors are returned as tool results with IsError=true,
// not as Go errors. Check both possibilities.
if err != nil {
if !strings.Contains(err.Error(), wantErrContaining) {
t.Errorf("got error %q, want containing %q", err, wantErrContaining)
}
} else if result != nil && result.IsError {
text := result.Content[0].(*TextContent).Text
if !strings.Contains(text, wantErrContaining) {
t.Errorf("got tool error %q, want containing %q", text, wantErrContaining)
}
} else {
t.Errorf("got no error, want error containing %q", wantErrContaining)
}
} else if err != nil {
t.Errorf("got error %v, want no error", err)