Open
Description
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
I designed a tool called write, in which ctx.sample
is used to call the LLM, and the client side uses sampling_handler to process the LLM results and return them to sever.
from mcp.client.streamable_http import streamablehttp_client
async with streamablehttp_client(url = "http://0.0.0.0:8888/mcp", timeout=6000, sse_read_timeout=6000) as (read, write, _):
async with ClientSession(
read, write, sampling_callback=sampling_handler, read_timeout_seconds=timedelta(seconds=6000)
) as session:
await session.initialize()
result = await session.call_tool("write", arguments={"id":"123", "title": title, "outline":str(outline), "web_search":True}, read_timeout_seconds=timedelta(seconds=6000))
Even though I set all the timeouts to large values, unexpected connection closed error still occurs.
Error in post_writer:
+ Exception Group Traceback (most recent call last):
| File "/root/miniconda3/envs/zzt/lib/python3.10/runpy.py", line 196, in _run_module_as_main
| return _run_code(code, main_globals, None,
| File "/root/miniconda3/envs/zzt/lib/python3.10/runpy.py", line 86, in _run_code
| exec(code, run_globals)
| File "/workspace/imitate_writing/test/test_model.py", line 203, in <module>
| File "/root/miniconda3/envs/zzt/lib/python3.10/asyncio/runners.py", line 44, in run
| return loop.run_until_complete(main)
| File "/root/miniconda3/envs/zzt/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
| return future.result()
| File "/workspace/imitate_writing/test/test_model.py", line 194, in test
| continue
| File "/root/miniconda3/envs/zzt/lib/python3.10/contextlib.py", line 217, in __aexit__
| await self.gen.athrow(typ, value, traceback)
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/mcp/client/streamable_http.py", line 437, in streamablehttp_client
| async with anyio.create_task_group() as tg:
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/mcp/client/streamable_http.py", line 461, in streamablehttp_client
| yield `(`
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/mcp/shared/session.py", line 218, in __aexit__
| return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| exceptiongroup.ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/workspace/imitate_writing/test/test_model.py", line 160, in run_sse
| result = await session.call_tool("write", arguments={"id":"123", "title": title, "outline":str(outline), "web_search":True})
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/mcp/client/session.py", line 264, in call_tool
| return await self.send_request(
| File "/root/miniconda3/envs/zzt/lib/python3.10/site-packages/mcp/shared/session.py", line 286, in send_request
| raise McpError(response_or_error.error)
| mcp.shared.exceptions.McpError: Connection closed
This error doesn’t occur every time — it happens occasionally when I test with a large amount of data. I’ve tried to locate where the connection is being closed, but I haven’t been able to identify the exact source of the issue.
Example Code
Python & MCP Python SDK
python: 3.10
mcp: 1.9.4
Metadata
Metadata
Assignees
Labels
No labels