fix(server): send protocol errors on the wire with a single "MCP error <code>:" prefix (#659) - #662
Merged
Merged
Conversation
…r <code>:" prefix (#659) The SDK's Protocol copies a thrown error's .message verbatim into the JSON-RPC error and the client-side McpError constructor prefixes "MCP error <code>: " again, so every McpError thrown from a request handler reached SDK clients doubled ("MCP error -32602: MCP error -32602: …"). The dev proxy hid it with dedupeMcpErrorPrefix. Convert at the JSON-RPC boundary instead of at every throw site: WireMcpError is an McpError whose .message is the bare detail (class, code, data and stack preserved), and tools/call rethrows through toWireError; the prompt and resource handlers throw it directly. Upstream code keeps throwing McpError/DebugError, whose prefixed .message is right in logs, and the existing instanceof/code/message-contains tests hold. Adds an in-memory Server/Client test pinning the exact client-side message for tools/call argument validation, an unknown tool, a wrapped internal error, an unknown prompt and an unknown resource. Closes #659 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DP1W22KJbkHZi4ueAtMFPF
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DP1W22KJbkHZi4ueAtMFPF
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #659.
Every
McpErrorthrown from a request handler reached SDK clients asMCP error -32602: MCP error -32602: <reason>. The SDK'sProtocolcopies the thrown error's.messageverbatim into the JSON-RPC error, and the client-sideMcpErrorconstructor prefixesMCP error <code>:again. The dev proxy has been hiding this withdedupeMcpErrorPrefix, which is why it was easy to miss.Fix
Convert at the JSON-RPC boundary instead of at every throw site:
WireMcpError(src/errors/debug-errors.ts) is anMcpErrorwhose.messageis the bare detail; class,.code,.dataand stack are preserved.mcpErrorDetail()recovers the detail (aDebugError's recordeddetail, otherwise.messagewith exactly one own-code prefix stripped) andtoWireError()converts.tools/call(tool-dispatch.ts) rethrows throughtoWireError; the prompt and resource handlers throwWireMcpErrordirectly.McpError/DebugError, whose prefixed.messageis right in logs, and every existinginstanceof/.code/ message-contains test holds unchanged.Tests
tests/core/unit/server/mcp-wire-errors.test.tsdrives the real handlers through an SDKServer/Clientpair overInMemoryTransportand pins the exact client-side message (one prefix, then the reason) for: tools/call argument validation, an unknown tool, a non-McpErrorhandler rejection wrapped as InternalError, an unknown prompt, and an unknown resource (read + subscribe).mcpErrorDetail/WireMcpError/toWireErrorindebug-errors.test.ts.npm run typecheck:all,npm run lint, and the full server unit directory pass; the test ratchet is unchanged.🤖 Generated with Claude Code
https://claude.ai/code/session_01DP1W22KJbkHZi4ueAtMFPF