Skip to content

Commit 83d1fe3

Browse files
Fix StreamableHTTP SSE premature stream closure for server-initiated requests
The SSE writer was closing the stream immediately upon receiving ANY JSONRPCResponse, including responses to server-initiated requests like elicitation. This prevented bidirectional communication patterns from working correctly over StreamableHTTP transport. Changed the SSE writer to only close the stream when receiving the response to the ORIGINAL client request, allowing server-initiated request/response cycles (elicitation, sampling, etc.) to complete before closing the stream. Impact: - Elicitation now works correctly over StreamableHTTP transport - Sampling and other server-initiated requests will also work - No breaking changes to existing functionality Testing: - Manually verified elicitation request/response cycle completes - Server logs show: request sent → response received → tool completes - Existing tests continue to pass
1 parent 48cb8d3 commit 83d1fe3

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/mcp/server/streamable_http.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,16 @@ async def sse_writer():
467467
event_data = self._create_event_data(event_message)
468468
await sse_stream_writer.send(event_data)
469469

470-
# If response, remove from pending streams and close
470+
# Only close when we receive the response to the ORIGINAL request
471+
# Server-initiated requests (like elicitation) will have different IDs
471472
if isinstance(
472473
event_message.message.root,
473474
JSONRPCResponse | JSONRPCError,
474475
):
475-
break
476+
response_id = str(event_message.message.root.id)
477+
if response_id == request_id:
478+
break
479+
# Otherwise, continue streaming - this is a response to a server-initiated request
476480
except Exception:
477481
logger.exception("Error in SSE writer")
478482
finally:

0 commit comments

Comments
 (0)