Skip to content

Client hangs forever #1789

Description

@afarntrog

Initial Checks

Description

Client hangs forever when sse_read_timeout is specified unless read_timeout_seconds in session.call_tool is also populated.

Example Code

importthreadingimporttimeimportasynciofromtypingimportLiteralfromdatetimeimporttimedeltafrommcpimportClientSessionfrommcp.client.streamable_httpimportstreamablehttp_clientfrommcp.typesimportImageContentasMCPImageContentdefstart_comprehensive_mcp_server(transport: Literal["sse", "streamable-http"], port=int):
frommcp.serverimportFastMCPmcp=FastMCP("Comprehensive MCP Server", port=port)
@mcp.tool(description="Tool that will timeout")deftimeout_tool() ->str:
time.sleep(10)
return"This tool has timed out"@mcp.tool(description="Calculator tool which performs calculations")defcalculator(x: int, y: int) ->int:
returnx+ymcp.run(transport=transport)
# Start serverserver_thread=threading.Thread(
target=start_comprehensive_mcp_server, kwargs={"transport": "streamable-http", "port": 8001}, daemon=True
)
server_thread.start()
time.sleep(3)
print("SERVER STARTED", flush=True)
asyncdeftest_raw_mcp():
print("CREATING RAW MCP CONNECTION...", flush=True)
asyncwithstreamablehttp_client(
url="http://127.0.0.1:8001/mcp",
sse_read_timeout=2, # 2 second timeout
) as (read_stream, write_stream, get_session_id):
print("TRANSPORT CONNECTED", flush=True)
asyncwithClientSession(read_stream, write_stream) assession:
print("SESSION CREATED", flush=True)
awaitsession.initialize()
print("SESSION INITIALIZED", flush=True)
print("CALLING TOOL (with 2s timeout)...", flush=True)
try:
result=awaitsession.call_tool(
"timeout_tool", arguments={},
# read_timeout_seconds=timedelta(seconds=5), # This needs to be provided or the client will hang forever
)
print(f"TOOL RESULT: {result}", flush=True)
exceptExceptionase:
print(f"TOOL CALL EXCEPTION: {type(e).__name__}: {e}", flush=True)
print("DONE", flush=True)
print("RUNNING TEST...", flush=True)
asyncio.run(test_raw_mcp())
print("TEST COMPLETE", flush=True)

Python & MCP Python SDK

1.24.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions