modelcontextprotocol / modelcontextprotocol/python-sdk
Response leak in SSE handlers
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.3k
- Forks
- 4k
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 31
Description
I found a resource leak in the streamable HTTP client. When SSE streaming fails with an exception, the HTTP response isn't closed.
Location:
src/mcp/client/streamable_http.py:
_handle_sse_response(line 336)_handle_resumption_request(line 251)
The Issue:
python
async def _handle_sse_response(self, response: httpx.Response, ...):
try:
event_source = EventSource(response)
async for sse in event_source.aiter_sse():
if is_complete:
await response.aclose() # Only closed here
break
except Exception as e:
await ctx.read_stream_writer.send(e)
# response leaked!
If the SSE iteration raises an exception (malformed JSON, network error, etc.), the response is never closed.
Impact:
Connection pool gets exhausted in long-running clients, eventually causing new requests to hang or fail.
Fix:
try:
...
except Exception as e:
...
finally:
await response.aclose()
Both methods need this fix.
Env:
Python SDK version: 1.1.2 (or main branch)
Python: 3.12
Transport: StreamableHTTP
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Read src/mcp/client/streamable_http.py, focusing on _handle_sse_response and _handle_resumption_request. Trace how each method handles SSE iteration and exceptions, then verify that both paths close the HTTP response after failures as well as normal completion.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- networking
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 58/100