Skip to content

fix: prevent double-prefixed McpError message when reconstructing from wire - #2802

Closed
KooshaPari wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
KooshaPari:fix-2786-mcp-error-double-prefix
Closed

fix: prevent double-prefixed McpError message when reconstructing from wire#2802
KooshaPari wants to merge 1 commit into
modelcontextprotocol:v1.xfrom
KooshaPari:fix-2786-mcp-error-double-prefix

Conversation

@KooshaPari

Copy link
Copy Markdown

Fixes #2786

Problem

When a request handler throws McpError, the client receives a double-prefixed message:

server threw:   MCP error -32601: Unknown tool: nope
client received: MCP error -32601: MCP error -32601: Unknown tool: nope

This happens because:

  1. McpError's constructor calls super(MCP error ${code}: ${message}), so .message already carries the prefix
  2. The server serializes the error as message: error.message, sending the prefixed string over the wire
  3. McpError.fromError() (and one new McpError() call) reconstructs from the wire message, which adds the prefix again

Changes

  • src/types.ts: Added McpError._stripErrorPrefix() to strip the "MCP error NNNN: " prefix before passing the message to the constructor in fromError(). This ensures deserialized errors get exactly one prefix.
  • src/shared/protocol.ts: Changed the queued-response error path to use McpError.fromError() instead of new McpError(), so both deserialization paths benefit from the prefix stripping.
  • test/shared/protocol.test.ts: Added 3 tests verifying that fromError does not double-prefix, correctly handles messages without prefix, and handles negative error codes.

Testing

  • All 3 new tests pass
  • Existing test suite unaffected

…m wire (fixes #2786)

McpError's constructor prefixes messages with 'MCP error NNNN: ', and the
server serializes this prefixed message as error.message. When the client
reconstructs the error via fromError, the already-prefixed message gets
the prefix added again, resulting in messages like:
  'MCP error -32601: MCP error -32601: Unknown tool: nope'

Fixed by stripping the prefix in fromError before passing to the
constructor, so deserialized errors get exactly one prefix. Also changed
the queued-response path in protocol.ts to use fromError consistently
instead of new McpError.
Copilot AI lite review requested due to automatic review settings September 13, 2026 04:36
@KooshaPari
KooshaPari requested a review from a team as a code owner September 13, 2026 04:36
@changeset-bot

changeset-bot Bot commented Sep 13, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: cce7939

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@pkg-pr-new

pkg-pr-new Bot commented Sep 13, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@modelcontextprotocol/sdk@2802

commit: cce7939

@KooshaPari

Copy link
Copy Markdown
Author

Closing per author request. Will re-evaluate repo selection.

@KooshaPari KooshaPari closed this Sep 13, 2026
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.

2 participants