Skip to content

Classify a server's subscribe/unsubscribe rejection as server_error (#3095) - #3109

Merged
cliffhall merged 2 commits into
v2/mainfrom
v2/fix/3095-subscribe-protocol-error
Dec 1, 2026
Merged

cliffhall merged 2 commits into
v2/mainfrom
v2/fix/3095-subscribe-protocol-error

Conversation

@cliffhall

Copy link
Copy Markdown
Member

Closes #3095

Problem

InspectorClient.subscribeToResource() / unsubscribeFromResource() wrapped every failure in a plain Error("Failed to subscribe to resource: …", { cause }). The CLI's classifyError reads a ProtocolError only at the top level (deliberately, per #3086), so a server's JSON-RPC rejection of resources/subscribe or resources/unsubscribe fell to the usage catch-all: {"code":"error"}, exit 1.

Fix

Both catch sites now throw through a new subscriptionFailure(prefix, error) in core/mcp/subscriptionAck.ts:

  • A ProtocolError is rebuilt as a ProtocolError: same code and data, built through ProtocolError.fromError so a branded subclass (ResourceNotFoundError, …) survives. It keeps the same Failed to subscribe to resource: … message, with the original as cause.
  • Anything else (a closed stream, a local fault) keeps the plain wrapper. That also keeps a buried ConnectionClosed from reading as a server exit (the Classify server JSON-RPC errors, failed stdio starts and bad URL schemes (#3070) #3086 shape).

I rebuilt the error rather than rethrowing it unwrapped (the other option the issue names). That way the TUI, the web toast and the CLI envelope keep the Failed to subscribe… wording users already see. Only the classification changes.

Server answers -32601/-32602 to resources/subscribe Before After
envelope code error server_error
exit code 1 (usage) 5
jsonRpcCode / jsonRpcData absent from the server's reply

Tests

  • clients/cli/__tests__/failure-classes.test.ts: the regression test the issue asks for. A real in-process server declares subscriptions and answers resources/subscribe with -32601 and with -32602. The test drives InspectorClient.subscribeToResource() against it, then runs the rejection through classifyError, and gets server_error, exit 5, with the server's jsonRpcCode/jsonRpcData. The one-shot CLI has no resources/subscribe method (it is a stream method, which mcpdo serves), so the test drives the client call mcpdo makes. It fails on v2/main (exit 1) and passes here.
  • clients/web/src/test/core/mcp/subscriptionAck.test.ts: unit tests for subscriptionFailure. A ProtocolError stays one, with its code, data, prefixed message and cause; a branded subclass stays branded; a non-protocol failure (incl. ConnectionClosed) stays a plain wrapper.
  • The existing wrap tests in inspectorClient-coverage-backfill.test.ts pass unchanged.

npm run local:gate green.

🤖 Generated with Claude Code

cliffhall and others added 2 commits November 30, 2026 22:42
…3095)

InspectorClient wrapped every resources/subscribe and resources/unsubscribe
failure in a plain Error, hiding the SDK's ProtocolError from classifyError,
which reads one only at the top level. A server that refused a subscription
therefore exited 1 (usage) with {"code":"error"}.

The wrap now goes through subscriptionFailure(): a ProtocolError is rebuilt as
a ProtocolError (same code and data, via fromError so branded subclasses
survive) carrying the same "Failed to subscribe to resource: …" message and the
original as cause. Every other failure keeps the plain wrapper, so a buried
ConnectionClosed still does not read as a server exit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: cliffhall <cliff@futurescale.com>

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.

🟢 Approval recommended

No active review issues were identified for the subscription error-handling changes.

No open issues 🎉

What changed in this PR

Fixes #3095 by preserving server JSON-RPC errors through InspectorClient’s subscription failure handling, allowing CLI classification as server_error with exit code 5.

Changes:

  • Adds a shared error wrapper that preserves protocol error codes, data, subclasses, and causes while retaining user-facing message prefixes.
  • Uses the wrapper for both subscribe and unsubscribe failures.
  • Adds unit coverage and real-server regression tests for subscription rejections.
File Description
core/​mcp/​subscriptionAck.ts Adds protocol-aware subscription failure wrapping.
core/​mcp/​inspectorClient.ts Applies the wrapper to subscribe and unsubscribe failures.
clients/​web/​src/​test/​core/​mcp/​subscriptionAck.test.ts Tests protocol error preservation and ordinary error wrapping.
clients/​cli/​__tests__/​failure-classes.test.ts Verifies server subscription rejections classify as server_error, exit 5.

Review effort: Balanced

@cliffhall

Copy link
Copy Markdown
Member Author

Copilot review closed: round 1 was clean (no inline comments, no headline findings, no suppressed comments), so no further rounds.

@cliffhall
cliffhall merged commit 4286469 into v2/main Dec 1, 2026
12 checks passed
@cliffhall
cliffhall deleted the v2/fix/3095-subscribe-protocol-error branch December 1, 2026 04:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Issues and PRs for v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI/mcpdo: a server's JSON-RPC rejection of resources/subscribe or unsubscribe still exits 1 (wrapped in a plain Error) — follow-up to #3070

2 participants