modelcontextprotocol / modelcontextprotocol/python-sdk

Response leak in SSE handlers

Open
#1,450 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs repro P2 ready for work
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.