Skip to content

fix(core): forward cause from the SdkError data slot to Error.cause - #2683

Open
xiangnuans wants to merge 1 commit into
modelcontextprotocol:mainfrom
xiangnuans:fix/2657-sdkerror-cause
Open

fix(core): forward cause from the SdkError data slot to Error.cause#2683
xiangnuans wants to merge 1 commit into
modelcontextprotocol:mainfrom
xiangnuans:fix/2657-sdkerror-cause

Conversation

@xiangnuans

Copy link
Copy Markdown

Closes #2657

Problem

classifyNetworkError (and other call sites) construct an SdkError with the underlying error in the data slot as { cause }:

new SdkError(SdkErrorCode.EraNegotiationFailed, `Version negotiation probe failed: ${describeError(error)}`, { cause: error })

But SdkError's third parameter is data, not ErrorOptions, and the constructor called super(message) with no options — so Error.cause stayed undefined and the { cause } object just landed on this.data. Anything walking the standard .cause chain (pino's default err serializer, Sentry's exception linking) stops at the SdkError, so the root network error is lost:

ENOTFOUND / ECONNREFUSED / ETIMEDOUT  →  all render as just "fetch failed"

Fix

Per the issue's preferred option 1 (safer, since the mismatch is silent at every call site), SdkError now forwards cause to super() when its data carries one, so the chain is unbroken:

SdkError: Version negotiation probe failed: fetch failed
  └─ TypeError: fetch failed
       └─ Error: getaddrinfo ENOTFOUND does-not-resolve.invalid
  • The full data object is still retained on this.data (backwards compatible — .data.cause keeps working).
  • SdkHttpError's { status, statusText } payload has no cause, so it is unaffected.
  • Covers any other call site that hands { cause } to the data slot.

Tests

Added packages/core-internal/test/errors/sdkErrorCause.test.ts:

  • cause from the data slot reaches Error.cause (fails on main without the fix)
  • the full object is still retained on .data
  • no cause / no data.cause stays undefined
  • an SdkHttpError status payload is not treated as a cause

pnpm --filter @modelcontextprotocol/core-internal test (1452 passing), typecheck, and lint all clean.

`classifyNetworkError` (and other call sites) pass an underlying error to
`SdkError` as `{ cause }` in the `data` slot, but the constructor called
`super(message)` with no options — so `Error.cause` stayed `undefined` and
the standard cause chain broke. Loggers/trackers that walk `.cause` (pino's
default serializer, Sentry) stopped at the `SdkError` and lost the root
network error, so `ENOTFOUND` / `ECONNREFUSED` / `ETIMEDOUT` all rendered
identically as "fetch failed".

Forward `cause` to `super()` when `data` carries one, while still retaining
the full object on `this.data`. This covers every call site that hands
`{ cause }` to the data slot (the issue's preferred fix), and leaves
`SdkHttpError`'s status/statusText payload untouched.

Closes modelcontextprotocol#2657
@xiangnuans
xiangnuans requested a review from a team as a code owner August 18, 2026 03:24
@changeset-bot

changeset-bot Bot commented Aug 18, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 18ea77a

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

@pkg-pr-new

pkg-pr-new Bot commented Aug 18, 2026

Copy link
Copy Markdown

Open in StackBlitz

@modelcontextprotocol/client

npm i https://pkg.pr.new/@modelcontextprotocol/client@2683

@modelcontextprotocol/codemod

npm i https://pkg.pr.new/@modelcontextprotocol/codemod@2683

@modelcontextprotocol/core

npm i https://pkg.pr.new/@modelcontextprotocol/core@2683

@modelcontextprotocol/server

npm i https://pkg.pr.new/@modelcontextprotocol/server@2683

@modelcontextprotocol/server-legacy

npm i https://pkg.pr.new/@modelcontextprotocol/server-legacy@2683

@modelcontextprotocol/express

npm i https://pkg.pr.new/@modelcontextprotocol/express@2683

@modelcontextprotocol/fastify

npm i https://pkg.pr.new/@modelcontextprotocol/fastify@2683

@modelcontextprotocol/hono

npm i https://pkg.pr.new/@modelcontextprotocol/hono@2683

@modelcontextprotocol/node

npm i https://pkg.pr.new/@modelcontextprotocol/node@2683

commit: 18ea77a

@claude claude Bot added the v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes label Aug 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v2 Ideas, requests and plans for v2 of the SDK which will incorporate major changes and fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2] classifyNetworkError passes { cause } into SdkError's data slot, so the underlying network error never reaches Error.cause

1 participant