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/streamable-http-restart-close.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@modelcontextprotocol/client': patch
---

Allow StreamableHTTPClientTransport to be restarted after close by clearing its aborted controller.
11 changes: 7 additions & 4 deletions packages/client/src/client/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,8 @@ export class StreamableHTTPClientTransport implements Transport {
// `StartSSEOptions`) must honour the per-request abort exactly as the
// original POST did — both as a fetch signal and as a "do not surface
// onerror" gate.
const isIntentionalAbort = (): boolean => this._abortController?.signal.aborted === true || requestSignal?.aborted === true;
const transportSignal = this._abortController?.signal;
const isIntentionalAbort = (): boolean => transportSignal?.aborted === true || requestSignal?.aborted === true;

try {
// Try to open an initial SSE stream with GET to listen for server messages
Expand Down Expand Up @@ -683,9 +684,9 @@ export class StreamableHTTPClientTransport implements Transport {
// Honour BOTH the transport-wide abort and the per-request abort
// (a listen subscription closed during the backoff delay): do not
// resurrect a stream the caller already tore down.
if (this._abortController?.signal.aborted || options.requestSignal?.aborted) return;
if (!this._abortController || this._abortController.signal.aborted || options.requestSignal?.aborted) return;
this._startOrAuthSse(options).catch(error => {
if (this._abortController?.signal.aborted || options.requestSignal?.aborted) return;
if (!this._abortController || this._abortController.signal.aborted || options.requestSignal?.aborted) return;
this.onerror?.(new Error(`Failed to reconnect SSE stream: ${error instanceof Error ? error.message : String(error)}`));
try {
this._scheduleReconnection(options, attemptCount + 1);
Expand Down Expand Up @@ -719,7 +720,8 @@ export class StreamableHTTPClientTransport implements Transport {
// a clean shutdown: no misleading "SSE stream disconnected" onerror,
// and no GET+Last-Event-ID reconnect that would resurrect a stream the
// caller just tore down.
const isIntentionalAbort = (): boolean => this._abortController?.signal.aborted === true || requestSignal?.aborted === true;
const transportSignal = this._abortController?.signal;
const isIntentionalAbort = (): boolean => transportSignal?.aborted === true || requestSignal?.aborted === true;

let lastEventId: string | undefined;
// Track whether we've received a priming event (event with ID)
Expand Down Expand Up @@ -914,6 +916,7 @@ export class StreamableHTTPClientTransport implements Transport {
} finally {
this._cancelReconnection = undefined;
this._abortController?.abort();
this._abortController = undefined;
this.onclose?.();
}
}
Expand Down
12 changes: 12 additions & 0 deletions packages/client/test/client/streamableHttp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,18 @@ describe('StreamableHTTPClientTransport', () => {
await reconnectTransport.close().catch(() => {});
});

it('can be started again after close()', async () => {
await transport.start();
const firstAbortController = transport['_abortController'];

await transport.close();

expect(firstAbortController?.signal.aborted).toBe(true);
expect(transport['_abortController']).toBeUndefined();
await expect(transport.start()).resolves.toBeUndefined();
expect(transport['_abortController']).toBeDefined();
});

it('should terminate session with DELETE request', async () => {
// First, simulate getting a session ID
const message: JSONRPCMessage = {
Expand Down
Loading