modelcontextprotocol / modelcontextprotocol/python-sdk
client's `read_stream_writer` open after SSE disconnection hanging `.receive()`
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 24.3k
- Forks
- 4k
- Ø Merge
- 1 T. 1 Std.
- Gemergte PRs (30 T.)
- 31
Beschreibung
Initial Checks
- I confirm that I'm using the latest version of MCP Python SDK
- I confirm that I searched for my issue in https://github.com/modelcontextprotocol/python-sdk/issues before opening this issue
Description
This issue MAY be related to the following
- https://github.com/modelcontextprotocol/python-sdk/issues/1805
- https://github.com/modelcontextprotocol/python-sdk/issues/1764
- https://github.com/modelcontextprotocol/python-sdk/issues/262
Experiencing deadlocks on streamable_http transport. In order to reproduce the issue the following can be run.
import asyncio
import logging
import threading
import time
from fastmcp import Client, FastMCP
from fastmcp.client.transports import StreamableHttpTransport
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
handlers=[logging.StreamHandler()],
)
logger = logging.getLogger(__name__)
HOST = "127.0.0.1"
PORT = 8765
SERVER_URL = f"http://{HOST}:{PORT}/mcp"
mcp = FastMCP(name="timeout issue")
SSE_TIMEOUT = 0.1
SLEEP = 60
@mcp.tool
def blocking_call() -> str:
time.sleep(SLEEP)
return "42"
def run_server():
mcp.run(transport="streamable-http", host=HOST, port=PORT)
async def test_blocking_sse():
transport = StreamableHttpTransport(SERVER_URL, sse_read_timeout=SSE_TIMEOUT)
async with Client(transport) as client:
tools = await client.list_tools()
print(f"available tools: {[t.name for t in tools]}")
result = await client.call_tool("blocking_call", {})
print(f"blocking result: {result}")
if __name__ == "__main__":
server_thread = threading.Thread(target=run_server, daemon=True)
server_thread.start()
time.sleep(2)
try:
asyncio.run(test_blocking_sse())
finally:
logger.info(f"{time.strftime('%Y-%m-%d %H:%M:%S')} [CLIENT] Shutting down...")
Execution context := locally built fastmcp at this commit 790ea92
Observed Behavior: the client hangs forever after the SSE read timeout fires, looking at the logs:
- tool request is sent, server returns HTTP 200 with [Content-Type: text/event-stream]
- server starts executing the tool (blocking time.sleep(60))
- client's SSE read timeout fires (after ~5 seconds with default
httpxtimeout) - client closes the HTTP connection
- client hangs indefinitely - call_tool() never returns
- the tool eventually completes on the server side, but when the server tries to send the response back, it gets
BrokenResourceErrorbecause the HTTP connection was already closed by the client.
I know noting about nothing, so I can imagine the above example is just an issue on my side, maybe:
ssetimeout should always be greater or equal to the expected timeout of tool calls (now thatsse_read_timeoutis deprecated it'shttpx.Timeoutcounterpart should be >= tool timeout)- long running tool calls MUST send progress updates
but one thing is for sure and that is, the above configuration hangs indefenitely because the session layer never gets to know that the transport layer is dead after the SSE stream is closed
Within my ignorance of many aspects of the implementation, the missing else branch seems to be the root cause.
and the fix looks something like
if last_event_id is not None: # pragma: no branch
...
else:
error_response = JSONRPCError(...)
await ctx.read_stream_writer.send(SessionMessage(JSONRPCMessage(root=error_response)))
consequently raising McpError
Example Code
Python & MCP Python SDK
Execution context for ease of implementation
- `mcp==1.24.0`
- example codebase run against locally built `fastmcp` at [790ea92](https://github.com/jlowin/fastmcp/tree/790ea92eb59256da68c83097321ebde8f8819bcf) --> 1.24.0
- Python: `python-3.12.7-macos-aarch64-none/bin/python3.12`
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne in src/mcp/client/streamable_http.py bei den Zeilen 433-435 und reproduziere den hängenden Aufruf mit dem Python-Beispiel unter Verwendung von StreamableHttpTransport. Verfolge, wie das SSE-Lese-Timeout die Verbindung schließt und wie die Session Nachrichten von read_stream_writer konsumiert. Erledigt ist dies, wenn der Client-Aufruf nach dem Timeout zurückkehrt oder eine Exception auslöst, statt auf unbestimmte Zeit zu hängen.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- api, networking
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100