From 3e50bd014ed5efa4ce1042306600132fc21f4833 Mon Sep 17 00:00:00 2001 From: anupamgit <1099942+anupamgit@users.noreply.github.com> Date: Wed, 18 Nov 2026 20:42:30 -1000 Subject: [PATCH] fix(client): fail promptly when a response stream closes without a reply A modern Streamable HTTP request could wait until its read timeout after the server closed the response stream without replying. Reject with ConnectionClosed when the stream ends, including when a request resumed with a saved token gets HTTP 405. Validation: all 126 tests in the transport and protocol test files passed. Lint and type checks passed for client and core-internal. --- .../reject-when-per-request-stream-ends.md | 7 +++++++ packages/client/src/client/streamableHttp.ts | 4 +++- .../client/test/client/streamableHttp.test.ts | 19 +++++++++++++++++++ packages/core-internal/src/shared/protocol.ts | 10 +++++++++- .../test/shared/protocol.test.ts | 19 +++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 .changeset/reject-when-per-request-stream-ends.md diff --git a/.changeset/reject-when-per-request-stream-ends.md b/.changeset/reject-when-per-request-stream-ends.md new file mode 100644 index 0000000000..755a5066ac --- /dev/null +++ b/.changeset/reject-when-per-request-stream-ends.md @@ -0,0 +1,7 @@ +--- +'@modelcontextprotocol/core-internal': patch +'@modelcontextprotocol/client': patch +'@modelcontextprotocol/server': patch +--- + +Reject in-flight outbound JSON-RPC on modern Streamable HTTP when the per-request SSE stream ends without a response, using `ConnectionClosed` instead of waiting until the read timeout. Wire `TransportSendOptions.onRequestStreamEnd` through `Protocol`, and forward it on the client transport's explicit resumption GET path when `send()` is called with a saved `resumptionToken`. \ No newline at end of file diff --git a/packages/client/src/client/streamableHttp.ts b/packages/client/src/client/streamableHttp.ts index 091674e101..7653f2982e 100644 --- a/packages/client/src/client/streamableHttp.ts +++ b/packages/client/src/client/streamableHttp.ts @@ -1013,7 +1013,9 @@ export class StreamableHTTPClientTransport implements Transport { this._startOrAuthSse({ resumptionToken, replayMessageId: isJSONRPCRequest(message) ? message.id : undefined, - requestSignal: options?.requestSignal + requestSignal: options?.requestSignal, + onresumptiontoken, + onRequestStreamEnd: options?.onRequestStreamEnd }).catch(error => this.onerror?.(error)); return; } diff --git a/packages/client/test/client/streamableHttp.test.ts b/packages/client/test/client/streamableHttp.test.ts index 17fd2df276..d30d724bde 100644 --- a/packages/client/test/client/streamableHttp.test.ts +++ b/packages/client/test/client/streamableHttp.test.ts @@ -1843,6 +1843,25 @@ describe('StreamableHTTPClientTransport', () => { expect(onStreamEnd).not.toHaveBeenCalled(); }); + it('onRequestStreamEnd fires when send() starts on an explicit resumption token and GET returns 405', async () => { + transport = new StreamableHTTPClientTransport(new URL('http://localhost:1234/mcp')); + const fetchMock = globalThis.fetch as Mock; + fetchMock.mockResolvedValueOnce({ + ok: false, + status: 405, + headers: new Headers() + }); + const onStreamEnd = vi.fn(); + await transport.start(); + await transport.send( + { jsonrpc: '2.0', method: 'tools/call', id: 1, params: {} }, + { resumptionToken: 'saved-event-id', requestSignal: new AbortController().signal, onRequestStreamEnd: onStreamEnd } + ); + await vi.waitFor(() => expect(onStreamEnd).toHaveBeenCalledTimes(1)); + expect(fetchMock).toHaveBeenCalledTimes(1); + expect(fetchMock.mock.calls[0]![1]?.method).toBe('GET'); + }); + it('onRequestStreamEnd fires when a primed POST→GET resume hits 405 (non-resumable terminal)', async () => { // R1 regression: against a server that stamps SSE event ids on the // listen POST stream but returns 405 on the GET resume, diff --git a/packages/core-internal/src/shared/protocol.ts b/packages/core-internal/src/shared/protocol.ts index a5b2a44979..fd2fd4d4d9 100644 --- a/packages/core-internal/src/shared/protocol.ts +++ b/packages/core-internal/src/shared/protocol.ts @@ -1749,7 +1749,15 @@ export abstract class Protocol { resumptionToken, onresumptiontoken, headers, - requestSignal: requestAbort?.signal + requestSignal: requestAbort?.signal, + ...(streamCloseCancels && { + onRequestStreamEnd: () => { + cancel( + new SdkError(SdkErrorCode.ConnectionClosed, 'Request stream closed before a response was received'), + 'cancelled' + ); + } + }) }) ).catch(error => { this._progressHandlers.delete(messageId); diff --git a/packages/core-internal/test/shared/protocol.test.ts b/packages/core-internal/test/shared/protocol.test.ts index 2fb0f64813..3c31574bd0 100644 --- a/packages/core-internal/test/shared/protocol.test.ts +++ b/packages/core-internal/test/shared/protocol.test.ts @@ -857,9 +857,11 @@ describe('protocol tests', () => { readonly hasPerRequestStream = true; sent: JSONRPCMessage[] = []; lastRequestSignal: AbortSignal | undefined; + lastOnRequestStreamEnd: (() => void) | undefined; override async send(message: JSONRPCMessage, options?: TransportSendOptions): Promise { this.sent.push(message); this.lastRequestSignal = options?.requestSignal; + this.lastOnRequestStreamEnd = options?.onRequestStreamEnd; } } @@ -947,6 +949,23 @@ describe('protocol tests', () => { expect(cancelledSent(tx.sent)).toHaveLength(0); }); + test('modern era + per-request-stream transport: remote stream end rejects with ConnectionClosed', async () => { + const tx = new PerRequestStreamTransport(); + const proto = createTestProtocol(); + await proto.connect(tx); + setNegotiatedProtocolVersion(proto, '2026-07-28'); + + const pending = testRequest(proto, { method: 'example', params: {} }, z.object({}), { timeout: 60_000 }); + + expect(tx.lastOnRequestStreamEnd).toBeTypeOf('function'); + tx.lastOnRequestStreamEnd?.(); + + await expect(pending).rejects.toMatchObject({ + code: SdkErrorCode.ConnectionClosed + }); + expect(cancelledSent(tx.sent)).toHaveLength(0); + }); + // "A client MUST NOT attempt to cancel its `initialize` request." The // handshake is exempt from the POST path above on every transport: an // abort or timeout rejects the caller locally and sends nothing. Both