diff --git a/.changeset/resumption-token-seeded.md b/.changeset/resumption-token-seeded.md new file mode 100644 index 0000000000..88221d9850 --- /dev/null +++ b/.changeset/resumption-token-seeded.md @@ -0,0 +1,5 @@ +--- +'@modelcontextprotocol/client': patch +--- + +`StreamableHTTPClientTransport` keeps the resumption cursor it was opened with. When a stream resumed with a resumption token disconnects before any event carrying an id arrives, the reconnect now sends the same `Last-Event-ID` instead of omitting it, so the server resumes the stream instead of treating it as a new one and the events sent in the meantime are still replayed. diff --git a/packages/client/src/client/streamableHttp.ts b/packages/client/src/client/streamableHttp.ts index 091674e101..524473797c 100644 --- a/packages/client/src/client/streamableHttp.ts +++ b/packages/client/src/client/streamableHttp.ts @@ -777,7 +777,11 @@ export class StreamableHTTPClientTransport implements Transport { // caller just tore down. const isIntentionalAbort = (): boolean => this._abortController?.signal.aborted === true || requestSignal?.aborted === true; - let lastEventId: string | undefined; + // Seed the cursor from the token this stream was opened with. A resumed stream that ends + // before any id-bearing event must reconnect with the same `Last-Event-ID`: dropping it + // makes the server treat the reconnect as a brand-new stream, so nothing is replayed. + // Re-sending the same cursor is idempotent — the server replays from where it left off. + let lastEventId: string | undefined = options.resumptionToken; // Track whether we've received a priming event (event with ID) // Per spec, server SHOULD send a priming event with ID before closing let hasPrimingEvent = false; diff --git a/packages/client/test/client/streamableHttp.test.ts b/packages/client/test/client/streamableHttp.test.ts index 17fd2df276..edd48066d9 100644 --- a/packages/client/test/client/streamableHttp.test.ts +++ b/packages/client/test/client/streamableHttp.test.ts @@ -2625,6 +2625,53 @@ describe('StreamableHTTPClientTransport', () => { const secondCallHeaders = fetchMock.mock.calls[1]![1]?.headers; expect(secondCallHeaders?.get('last-event-id')).toBe('evt-1'); }); + + it('should keep the resumption token when a resumed stream ends before any id-bearing event', async () => { + transport = new StreamableHTTPClientTransport(new URL('http://localhost:1234/mcp'), { + reconnectionOptions: { + initialReconnectionDelay: 10, + maxReconnectionDelay: 1000, + reconnectionDelayGrowFactor: 1, + maxRetries: 1 + } + }); + + // A resumed stream that never sends an id: a comment line, then a graceful close. + const encoder = new TextEncoder(); + const stream = new ReadableStream({ + start(controller) { + controller.enqueue(encoder.encode(': resuming\n\n')); + controller.close(); + } + }); + + const fetchMock = globalThis.fetch as Mock; + fetchMock.mockResolvedValueOnce({ + ok: true, + status: 200, + headers: new Headers({ 'content-type': 'text/event-stream' }), + body: stream + }); + fetchMock.mockResolvedValueOnce({ + ok: true, + status: 200, + headers: new Headers({ 'content-type': 'text/event-stream' }), + body: new ReadableStream() + }); + + await transport.start(); + await transport['_startOrAuthSse']({ resumptionToken: 'evt-1' }); + + await vi.advanceTimersByTimeAsync(50); + await vi.advanceTimersByTimeAsync(150); + + expect(fetchMock).toHaveBeenCalledTimes(2); + const firstCallHeaders = fetchMock.mock.calls[0]![1]?.headers; + expect(firstCallHeaders?.get('last-event-id')).toBe('evt-1'); + // The reconnect resumes from the same cursor: replay is idempotent, dropping it is not. + const secondCallHeaders = fetchMock.mock.calls[1]![1]?.headers; + expect(secondCallHeaders?.get('last-event-id')).toBe('evt-1'); + }); }); describe('Reconnection Logic with maxRetries 0', () => {