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
7 changes: 7 additions & 0 deletions .changeset/reject-when-per-request-stream-ends.md
Original file line number Diff line number Diff line change
@@ -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`.
4 changes: 3 additions & 1 deletion packages/client/src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions packages/client/test/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
10 changes: 9 additions & 1 deletion packages/core-internal/src/shared/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,15 @@ export abstract class Protocol<ContextT extends BaseContext> {
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);
Expand Down
19 changes: 19 additions & 0 deletions packages/core-internal/test/shared/protocol.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
this.sent.push(message);
this.lastRequestSignal = options?.requestSignal;
this.lastOnRequestStreamEnd = options?.onRequestStreamEnd;
}
}

Expand Down Expand Up @@ -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
Expand Down
Loading