async def test_connect_to_server():
async with ClientSessionGroup() as clinet_session_group:
for server in ["stream_law", "sse_kb"]:
server_param = server_config_group.get_server_param(server)
print(f"connect to server: {server_param}")
print("----------------------------------------")
await clinet_session_group.connect_to_server(server_param)
await asyncio.sleep(3)
print("*****************list tools********************")
for name, tool in clinet_session_group.tools.items():
print(f"tool_name: {name}")
print("----------------------------------------")
await asyncio.sleep(3)
print("*****************disconnect********************")
for session in clinet_session_group.sessions:
print(f"disconnect session: {session}")
await clinet_session_group.disconnect_from_server(session)
await asyncio.sleep(2)
connect to server: url='http://x.x.x.x:8080/mcp' headers={} timeout=datetime.timedelta(seconds=30) sse_read_timeout=datetime.timedelta(seconds=300) terminate_on_close=True
----------------------------------------
connect to server: url='http://x.x.x.x:9000/sse/' headers={} timeout=30.0 sse_read_timeout=300.0
----------------------------------------
*****************list tools********************
tool_name: search_ft
----------------------------------------
tool_name: search_fg
----------------------------------------
tool_name: search_law_xl
----------------------------------------
tool_name: writ_search
----------------------------------------
tool_name: law_search
----------------------------------------
tool_name: paper_search
----------------------------------------
tool_name: web_search
----------------------------------------
*****************disconnect********************
disconnect session: <mcp.client.session.ClientSession object at 0x0000022AAAF1C050>
+ Exception Group Traceback (most recent call last):
| File "C:\project\python\work\mcp\mcp-client\main.py", line 107, in <module>
| asyncio.run(test_connect_to_server())
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 195, in run
| return runner.run(main)
| ^^^^^^^^^^^^^^^^
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
| return self._loop.run_until_complete(task)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 691, in run_until_complete
| return future.result()
| ^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\main.py", line 31, in test_connect_to_server
| async with ClientSessionGroup() as clinet_session_group:
| ^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\mcp\client\session_group.py", line 149, in __aexit__
| async with anyio.create_task_group() as tg:
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Exception Group Traceback (most recent call last):
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| ExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\mcp\client\sse.py", line 154, in sse_client
| yield read_stream, write_stream
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 737, in __aexit__
| cb_suppress = await cb(*exc_details)
| ^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\mcp\shared\session.py", line 220, in __aexit__
| return await self._task_group.__aexit__(exc_type, exc_val, exc_tb)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 783, in __aexit__
| return self.cancel_scope.__exit__(exc_type, exc_val, exc_tb)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 457, in __exit__
| raise RuntimeError(
| RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
+------------------------------------
|
| During handling of the above exception, another exception occurred:
|
| Traceback (most recent call last):
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 696, in aclose
| await self.__aexit__(None, None, None)
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 754, in __aexit__
| raise exc_details[1]
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 737, in __aexit__
| cb_suppress = await cb(*exc_details)
| ^^^^^^^^^^^^^^^^^^^^^^
| File "C:\Users\gaojun\AppData\Local\Programs\Python\Python312\Lib\contextlib.py", line 231, in __aexit__
| await self.gen.athrow(value)
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\mcp\client\sse.py", line 53, in sse_client
| async with anyio.create_task_group() as tg:
| ^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 778, in __aexit__
| if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| File "C:\project\python\work\mcp\mcp-client\.venv\Lib\site-packages\anyio\_backends\_asyncio.py", line 457, in __exit__
| raise RuntimeError(
| RuntimeError: Attempted to exit cancel scope in a different task than it was entered in
+------------------------------------
进程已结束,退出代码为 1
async def test_connect_to_server():
async with ClientSessionGroup() as clinet_session_group:
for server in ["stream_law"]:
server_param = server_config_group.get_server_param(server)
print(f"connect to server: {server_param}")
print("----------------------------------------")
await clinet_session_group.connect_to_server(server_param)
await asyncio.sleep(3)
print("*****************list tools********************")
for name, tool in clinet_session_group.tools.items():
print(f"tool_name: {name}")
print("----------------------------------------")
await asyncio.sleep(3)
print("*****************disconnect********************")
for session in clinet_session_group.sessions:
print(f"disconnect session: {session}")
await clinet_session_group.disconnect_from_server(session)
await asyncio.sleep(2)
connect to server: url='http://x.x.x.x:8080/mcp' headers={} timeout=datetime.timedelta(seconds=30) sse_read_timeout=datetime.timedelta(seconds=300) terminate_on_close=True
----------------------------------------
*****************list tools********************
tool_name: search_ft
----------------------------------------
tool_name: search_fg
----------------------------------------
tool_name: search_law_xl
----------------------------------------
*****************disconnect********************
disconnect session: <mcp.client.session.ClientSession object at 0x000001F6ABF8E180>
进程已结束,退出代码为 0
Describe the bug
When adding multiple servers using the Client Session Group, an error occurs when closing the connection
No problem when adding only one server
code
logs
·
Only one server is OK
code
logs