@@ -68,7 +68,7 @@ export class StreamableHTTPServerTransport implements Transport {
68
68
private _requestResponseMap : Map < RequestId , JSONRPCMessage > = new Map ( ) ;
69
69
private _initialized : boolean = false ;
70
70
private _enableJsonResponse : boolean = false ;
71
- private _sseStreamKey = "standalone-sse" ;
71
+ private _standaloneSSE : ServerResponse | undefined ;
72
72
73
73
74
74
sessionId ?: string | undefined ;
@@ -148,9 +148,8 @@ export class StreamableHTTPServerTransport implements Transport {
148
148
// Resumability will be supported in the future
149
149
150
150
// Check if there's already an active standalone SSE stream for this session
151
- const existingStream = this . _responseMapping . get ( this . _sseStreamKey ) ;
152
151
153
- if ( existingStream !== undefined ) {
152
+ if ( this . _standaloneSSE !== undefined ) {
154
153
// Only one GET SSE stream is allowed per session
155
154
res . writeHead ( 409 ) . end ( JSON . stringify ( {
156
155
jsonrpc : "2.0" ,
@@ -166,14 +165,12 @@ export class StreamableHTTPServerTransport implements Transport {
166
165
// otherwise the client will just wait for the first message
167
166
res . writeHead ( 200 , headers ) . flushHeaders ( ) ;
168
167
169
- // Store the response for this request so we can use it for standalone server notifications
170
- // This response doesn't have an associated request ID, so we'll use a special string to track it
171
- this . _responseMapping . set ( this . _sseStreamKey , res ) ;
168
+ // Assing the response to the standalone SSE stream
169
+ this . _standaloneSSE = res ;
172
170
173
171
// Set up close handler for client disconnects
174
172
res . on ( "close" , ( ) => {
175
- // Clean up resources associated with this connection
176
- this . _responseMapping . delete ( this . _sseStreamKey ) ;
173
+ this . _standaloneSSE = undefined ;
177
174
} ) ;
178
175
}
179
176
@@ -461,14 +458,13 @@ export class StreamableHTTPServerTransport implements Transport {
461
458
throw new Error ( "Cannot send a response on a standalone SSE stream unless resuming a previous client request" ) ;
462
459
}
463
460
464
- const standaloneStream = this . _responseMapping . get ( this . _sseStreamKey ) ;
465
- if ( standaloneStream === undefined ) {
461
+ if ( this . _standaloneSSE === undefined ) {
466
462
// The spec says the server MAY send messages on the stream, so it's ok to discard if no stream
467
463
return ;
468
464
}
469
465
470
466
// Send the message to the standalone SSE stream
471
- standaloneStream . write ( `event: message\ndata: ${ JSON . stringify ( message ) } \n\n` ) ;
467
+ this . _standaloneSSE . write ( `event: message\ndata: ${ JSON . stringify ( message ) } \n\n` ) ;
472
468
return ;
473
469
}
474
470
0 commit comments