Skip to content

Commit 84066d8

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 815d885 commit 84066d8

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
@@ -469,12 +469,16 @@ async def sse_writer():
469469
event_data = self._create_event_data(event_message)
470470
await sse_stream_writer.send(event_data)
471471

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

0 commit comments

Comments
 (0)