Using agent as tool outside of adk web doesn't exit cleanly
Describe the bug I have a program which I run directly (outside of adk web) and works properly. But as soon as I add an other as a tool to this agent, it doesn't exit cleanly.
State after agent run: {}
Agent finished. Stack exited cleanly.
Warning: Error during MCP session cleanup: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope
Warning: Error during MCP session cleanup: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope
Warning: Error during MCP session cleanup: Attempted to exit a cancel scope that isn't the current tasks's current cancel scope
an error occurred during closing of asynchronous generator <async_generator object stdio_client at 0x112874040>
asyncgen: <async_generator object stdio_client at 0x112874040>
+ Exception Group Traceback (most recent call last):
| File "/Users/denisjannot/Documents/ai/adk/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 772, in __aexit__
| raise BaseExceptionGroup(
| BaseExceptionGroup: unhandled errors in a TaskGroup (1 sub-exception)
+-+---------------- 1 ----------------
| Traceback (most recent call last):
| File "/Users/denisjannot/Documents/ai/adk/venv/lib/python3.12/site-packages/mcp/client/stdio/__init__.py", line 177, in stdio_client
| yield read_stream, write_stream
| GeneratorExit
+------------------------------------
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/denisjannot/Documents/ai/adk/venv/lib/python3.12/site-packages/mcp/client/stdio/__init__.py", line 170, in stdio_client
async with (
File "/Users/denisjannot/Documents/ai/adk/venv/lib/python3.12/site-packages/anyio/_backends/_asyncio.py", line 778, in __aexit__
if self.cancel_scope.__exit__(type(exc), exc, exc.__traceback__):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/denisjannot/Documents/ai/adk/venv/lib/python3.12/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
To Reproduce
Add an agent as a tool using:
sub_agent = create_sub_agent()
agent_as_tool = AgentTool(agent=sub_agent)
Note that the sub agent has a MCP tool.
I am hitting this same issue also as one of my sub agents uses a MCP server
Also having this issue...
Also facing this. Seems related to #929
I'm facing this issue as well. Another workaround that works for me is to install the 'nest_asyncio' python package. Then add the following to the beginning of the application:
import nest_asyncio
nest_asyncio.apply()
The 'nest_asyncio' package allows to use nested calls of asyncio.run and loop.run_until_complete.
I posted this in another thread, but another way to mitigate this, if you are instantiating the runner, is to await the close method of the runner before exiting your async context. For example:
import asyncio
from google.adk.runners import Runner
async def main():
# Set up session service and session if necessary
runner = Runner(app_name="test_app", agent=root_agent, session_service=session_service)
# Do all your fun AI stuff
await runner.close()
if __name__=='__main__':
asyncio.run(main())
Any updates on this, please?
facing same error when my sub agent uses MCP toolbox remote runing in cloud run
I’m encountering the same issue when using multiple subagents in a sequential workflow, where each subagent connects to a different MCP tool, and each tool is hosted on a separate server.