Skip to content

mcp: send handler errors that carry no code as Internal error - #1407

Merged
guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
talhayme:fix/1394-plain-handler-error-code
Oct 16, 2026
Merged

guglielmo-san merged 1 commit into
modelcontextprotocol:mainfrom
talhayme:fix/1394-plain-handler-error-code

Conversation

@talhayme

Copy link
Copy Markdown
Contributor

A request handler that fails with an ordinary error (no JSON-RPC code) answered the peer with error code 0. As suggested in #1394, handleReceive in mcp/shared.go now gives such an error the code -32603 (Internal error), right after the method handler returns. The message is unchanged, and the original error stays in the chain for errors.Is/errors.As. Errors that already carry a code keep it, and jsonrpc2.ErrNotHandled is left alone so it still becomes "method not found". handleReceive serves both sides, so a failing client handler (sampling, elicitation) now reaches the server as -32603 too.

This is a behavioral change, so MCPGODEBUG=nointernalerrorcode=1 restores code 0. The option is documented in docs/mcpgodebug.md under 1.9.0. TestHandlerErrorWithoutCode covers a server prompt handler and a client sampling handler, with plain, wrapped, coded and wrapped-coded errors, under both settings; without the change it fails with code 0.

Fixes #1394

AI disclosure: written with Claude Code; I reviewed the change and ran go test ./..., go vet ./..., staticcheck and the mcp tests under -race.

🤖 Generated with Claude Code

An error returned by a request handler without a JSON-RPC error code
reached the peer with code 0. handleReceive now gives such an error the
code -32603 (Internal error), keeping its message and keeping it in the
chain for errors.Is and errors.As. Errors that already carry a code, and
jsonrpc2.ErrNotHandled, are unchanged. The path is shared by both sides,
so client handlers (sampling, elicitation) get the same treatment.

MCPGODEBUG=nointernalerrorcode=1 restores code 0.

Fixes modelcontextprotocol#1394

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@guglielmo-san
guglielmo-san merged commit 3e9d812 into modelcontextprotocol:main Oct 16, 2026
10 checks passed
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: a handler error without a code reaches the peer as JSON-RPC error code 0

2 participants