Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
small refactor for send logic
  • Loading branch information
ihrpr committed Apr 9, 2025
commit 6a8fa5ff2b1a0d8eaf94aa04c7342fc4bb65749c
8 changes: 2 additions & 6 deletions src/server/streamableHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,9 @@ export class StreamableHTTPServerTransport implements Transport {

async send(message: JSONRPCMessage, options?: { relatedRequestId?: RequestId }): Promise<void> {
let requestId = options?.relatedRequestId;
let shouldCloseConnection = false;
if ('result' in message || 'error' in message) {
// If the message is a response, use the request ID from the message
requestId = message.id;
// This is a response to the original request, we can close the stream
// after sending all related responses
shouldCloseConnection = true;
}
if (requestId === undefined) {
throw new Error("No request ID provided for the message");
Expand All @@ -380,8 +376,8 @@ export class StreamableHTTPServerTransport implements Transport {
sseResponse.write(
`event: message\ndata: ${JSON.stringify(message)}\n\n`,
);

if (shouldCloseConnection) {
// After all JSON-RPC responses have been sent, the server SHOULD close the SSE stream.
if ('result' in message || 'error' in message) {
this._sseResponseMapping.delete(requestId);
// Only close the connection if it's not needed by other requests
const canCloseConnection = ![...this._sseResponseMapping.entries()].some(([id, res]) => res === sseResponse && id !== requestId);
Expand Down