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
5 changes: 5 additions & 0 deletions .changeset/resumption-token-seeded.md
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 5 additions & 1 deletion packages/client/src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions packages/client/test/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading