Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/nullish-handler-rejection.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/core': patch
---

A request handler that rejects with a nullish reason (a bare `reject()` or `throw null`) no longer strands the peer without a reply. `Protocol`'s inbound-request error path indexed `error['code']` directly, which throws a `TypeError` when the rejection reason is `null`/`undefined`; that throw propagated to the outer `.catch`, so no JSON-RPC error response was ever sent and the requester hung until its own timeout. The reason is now coalesced to a safe object before the error code and message are read, so a `-32603` (Internal error) response is always returned.
11 changes: 8 additions & 3 deletions packages/core-internal/src/shared/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1137,14 +1137,19 @@ export abstract class Protocol<ContextT extends BaseContext> {
// the wire code for a handler-thrown error, so per-era
// wire-code policy lives in the codec rather than in any
// handler. Non-integer codes still fall through to −32603.
const thrownCode = Number.isSafeInteger(error['code']) ? (error['code'] as number) : ProtocolErrorCode.InternalError;
// Coalesce the reason first: a handler may reject with a
// nullish value (a bare `reject()` or `throw null`), and
// indexing `error['code']` on it would throw here and strand
// the peer with no error response at all.
const reason = (error ?? {}) as { code?: unknown; message?: string; data?: unknown };
const thrownCode = Number.isSafeInteger(reason.code) ? (reason.code as number) : ProtocolErrorCode.InternalError;
const errorResponse: JSONRPCErrorResponse = {
jsonrpc: '2.0',
id: request.id,
error: {
code: codec.encodeErrorCode(thrownCode),
message: error.message ?? 'Internal error',
...(error['data'] !== undefined && { data: error['data'] })
message: reason.message ?? 'Internal error',
...(reason.data !== undefined && { data: reason.data })
}
};
await capturedTransport?.send(errorResponse);
Expand Down
24 changes: 24 additions & 0 deletions packages/core-internal/test/shared/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,30 @@ describe('protocol tests', () => {
expect((abortReason as SdkError).code).toBe(SdkErrorCode.ConnectionClosed);
});

test('sends an error response when a request handler rejects with a nullish reason', async () => {
await protocol.connect(transport);

// A handler that rejects with `undefined` (a bare `reject()` or
// `throw null` in library code) must still yield a JSON-RPC error
// response; otherwise the error-response encode itself throws while
// indexing the nullish reason and the requester hangs until timeout.
protocol.setRequestHandler('ping', async () => Promise.reject(undefined));

transport.onmessage?.({ jsonrpc: '2.0', id: 1, method: 'ping', params: {} });

await vi.waitFor(() => {
const errorSend = sendSpy.mock.calls.find(
([msg]) => (msg as JSONRPCErrorResponse)?.id === 1 && (msg as JSONRPCErrorResponse)?.error !== undefined
);
expect(errorSend).toBeDefined();
});

const errorSend = sendSpy.mock.calls.find(
([msg]) => (msg as JSONRPCErrorResponse)?.id === 1 && (msg as JSONRPCErrorResponse)?.error !== undefined
)!;
expect((errorSend[0] as JSONRPCErrorResponse).error.message).toBeTruthy();
});

test('should remove abort listener from caller signal when request settles', async () => {
await protocol.connect(transport);

Expand Down
Loading