Skip to content

fix: drain terminal streamable HTTP responses#2721

Closed
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/drain-sse-response-on-completion
Closed

fix: drain terminal streamable HTTP responses#2721
he-yufeng wants to merge 1 commit into
modelcontextprotocol:mainfrom
he-yufeng:fix/drain-sse-response-on-completion

Conversation

@he-yufeng
Copy link
Copy Markdown

Summary

Fixes #2707.

The streamable HTTP client currently closes SSE responses as soon as it reads the terminal JSON-RPC response or error. With HTTP keepalive, that can leave unread response bytes on the connection and force the next request down a fresh connection path.

This changes the request SSE paths to treat the terminal JSON-RPC message as logical completion, but still drain the response stream to EOF before returning. Cancellation and shutdown paths still close the stream via the existing context managers.

Testing

uv run pytest tests/shared/test_streamable_http.py -k "drains_after_terminal_event" -q
uv run pytest tests/shared/test_streamable_http.py -q
uv run ruff check src/mcp/client/streamable_http.py tests/shared/test_streamable_http.py
uv run ruff format --check src/mcp/client/streamable_http.py tests/shared/test_streamable_http.py
git diff --check

@he-yufeng
Copy link
Copy Markdown
Author

Closing this because #2712 already covers the same streamable HTTP SSE drain path. I missed that existing PR during duplicate search, so keeping this open would only add review noise.

@he-yufeng he-yufeng closed this May 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

streamable_http: early response.aclose() poisons keepalive connection, causes ~260ms latency on every subsequent tool call

1 participant