Skip to content

Commit 4433044

Browse files
committed
separate field on the class for standaloneSSE
1 parent 2c2cf5b commit 4433044

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

Diff for: src/server/streamableHttp.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class StreamableHTTPServerTransport implements Transport {
6868
private _requestResponseMap: Map<RequestId, JSONRPCMessage> = new Map();
6969
private _initialized: boolean = false;
7070
private _enableJsonResponse: boolean = false;
71-
private _sseStreamKey = "standalone-sse";
71+
private _standaloneSSE: ServerResponse | undefined;
7272

7373

7474
sessionId?: string | undefined;
@@ -148,9 +148,8 @@ export class StreamableHTTPServerTransport implements Transport {
148148
// Resumability will be supported in the future
149149

150150
// Check if there's already an active standalone SSE stream for this session
151-
const existingStream = this._responseMapping.get(this._sseStreamKey);
152151

153-
if (existingStream !== undefined) {
152+
if (this._standaloneSSE !== undefined) {
154153
// Only one GET SSE stream is allowed per session
155154
res.writeHead(409).end(JSON.stringify({
156155
jsonrpc: "2.0",
@@ -166,14 +165,12 @@ export class StreamableHTTPServerTransport implements Transport {
166165
// otherwise the client will just wait for the first message
167166
res.writeHead(200, headers).flushHeaders();
168167

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;
172170

173171
// Set up close handler for client disconnects
174172
res.on("close", () => {
175-
// Clean up resources associated with this connection
176-
this._responseMapping.delete(this._sseStreamKey);
173+
this._standaloneSSE = undefined;
177174
});
178175
}
179176

@@ -461,14 +458,13 @@ export class StreamableHTTPServerTransport implements Transport {
461458
throw new Error("Cannot send a response on a standalone SSE stream unless resuming a previous client request");
462459
}
463460

464-
const standaloneStream = this._responseMapping.get(this._sseStreamKey);
465-
if (standaloneStream === undefined) {
461+
if (this._standaloneSSE === undefined) {
466462
// The spec says the server MAY send messages on the stream, so it's ok to discard if no stream
467463
return;
468464
}
469465

470466
// 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`);
472468
return;
473469
}
474470

0 commit comments

Comments
 (0)