Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/brave-donkeys-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modelcontextprotocol/core-internal': patch
'@modelcontextprotocol/client': patch
---

`SdkError` now accepts standard `ErrorOptions`, and version-negotiation probe failures surface the underlying network error via `Error.cause` (#2657).
11 changes: 8 additions & 3 deletions packages/client/src/client/probeClassifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,9 +311,14 @@ function classifyNetworkError(error: unknown, context: ProbeClassifierContext):
}
return {
kind: 'error',
error: new SdkError(SdkErrorCode.EraNegotiationFailed, `Version negotiation probe failed: ${describeError(error)}`, {
cause: error
})
error: new SdkError(
SdkErrorCode.EraNegotiationFailed,
`Version negotiation probe failed: ${describeError(error)}`,
// Keep data.cause for existing consumers while also exposing the
// standard Error.cause chain (#2657).
{ cause: error },
{ cause: error }
)
};
}

Expand Down
11 changes: 11 additions & 0 deletions packages/client/test/client/probeClassifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,17 @@ describe('row: network outage → typed connect error (Node)', () => {
const verdict = classify({ kind: 'network-error', error: new TypeError('fetch failed') }, { environment: 'node' });
expect(verdict.kind).toBe('error');
});

test('the underlying network error is reachable via Error.cause (#2657)', () => {
const cause = Object.assign(new Error('fetch failed'), { code: 'ECONNREFUSED' });
const verdict = classify({ kind: 'network-error', error: cause });
expect(verdict.kind).toBe('error');
if (verdict.kind === 'error') {
expect(verdict.error.cause).toBe(cause);
// The legacy data.cause slot stays populated too.
expect(((verdict.error as SdkError).data as { cause?: unknown }).cause).toBe(cause);
}
});
});

describe('row: timeout — transport-aware verdict', () => {
Expand Down
5 changes: 3 additions & 2 deletions packages/core-internal/src/errors/sdkErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ export class SdkError extends Error {
constructor(
public readonly code: SdkErrorCode,
message: string,
public readonly data?: unknown
public readonly data?: unknown,
options?: ErrorOptions
) {
super(message);
super(message, options);
this.name = 'SdkError';
stampErrorBrands(this, new.target);
}
Expand Down
Loading