@@ -217,7 +217,6 @@ export class StreamableHTTPServerTransport implements Transport {
217217
218218 // Assign the response to the standalone SSE stream
219219 this . _streamMapping . set ( this . _standaloneSseStreamId , res ) ;
220-
221220 // Set up close handler for client disconnects
222221 res . on ( "close" , ( ) => {
223222 this . _streamMapping . delete ( this . _standaloneSseStreamId ) ;
@@ -345,8 +344,10 @@ export class StreamableHTTPServerTransport implements Transport {
345344 const isInitializationRequest = messages . some (
346345 msg => 'method' in msg && msg . method === 'initialize'
347346 ) ;
347+ const mcpSessionId = req . headers [ "mcp-session-id" ] as string | undefined ;
348348 if ( isInitializationRequest ) {
349- if ( this . _initialized ) {
349+ // if generateSessionId is not set, the server does not support session management
350+ if ( this . _initialized && this . sessionId !== undefined && mcpSessionId !== this . sessionId ) {
350351 res . writeHead ( 400 ) . end ( JSON . stringify ( {
351352 jsonrpc : "2.0" ,
352353 error : {
@@ -368,7 +369,7 @@ export class StreamableHTTPServerTransport implements Transport {
368369 } ) ) ;
369370 return ;
370371 }
371- this . sessionId = this . sessionIdGenerator ( ) ;
372+ this . sessionId = mcpSessionId ?? this . sessionIdGenerator ( ) ;
372373 this . _initialized = true ;
373374
374375 }
@@ -419,17 +420,8 @@ export class StreamableHTTPServerTransport implements Transport {
419420 this . _requestToStreamMapping . set ( message . id , streamId ) ;
420421 }
421422 }
422-
423423 // Set up close handler for client disconnects
424424 res . on ( "close" , ( ) => {
425- // find a stream ID for this response
426- // Remove all entries that reference this response
427- for ( const [ id , stream ] of this . _requestToStreamMapping . entries ( ) ) {
428- if ( streamId === stream ) {
429- this . _requestToStreamMapping . delete ( id ) ;
430- this . _requestResponseMap . delete ( id ) ;
431- }
432- }
433425 this . _streamMapping . delete ( streamId ) ;
434426 } ) ;
435427
@@ -577,7 +569,7 @@ export class StreamableHTTPServerTransport implements Transport {
577569 // Get the response for this request
578570 const streamId = this . _requestToStreamMapping . get ( requestId ) ;
579571 const response = this . _streamMapping . get ( streamId ! ) ;
580- if ( ! streamId || ! response ) {
572+ if ( ! streamId ) {
581573 throw new Error ( `No connection established for request ID: ${ String ( requestId ) } ` ) ;
582574 }
583575
@@ -588,9 +580,10 @@ export class StreamableHTTPServerTransport implements Transport {
588580 if ( this . _eventStore ) {
589581 eventId = await this . _eventStore . storeEvent ( streamId , message ) ;
590582 }
591-
592- // Write the event to the response stream
593- this . writeSSEEvent ( response , message , eventId ) ;
583+ if ( response ) {
584+ // Write the event to the response stream
585+ this . writeSSEEvent ( response , message , eventId ) ;
586+ }
594587 }
595588
596589 if ( isJSONRPCResponse ( message ) ) {
@@ -603,6 +596,9 @@ export class StreamableHTTPServerTransport implements Transport {
603596 const allResponsesReady = relatedIds . every ( id => this . _requestResponseMap . has ( id ) ) ;
604597
605598 if ( allResponsesReady ) {
599+ if ( ! response ) {
600+ throw new Error ( `No connection established for request ID: ${ String ( requestId ) } ` ) ;
601+ }
606602 if ( this . _enableJsonResponse ) {
607603 // All responses ready, send as JSON
608604 const headers : Record < string , string > = {
0 commit comments