adk-python icon indicating copy to clipboard operation
adk-python copied to clipboard

Using agent as tool outside of adk web doesn't exit cleanly

Open djannot opened this issue 1 year ago • 8 comments

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.

djannot avatar Jun 03 '25 08:06 djannot

I am hitting this same issue also as one of my sub agents uses a MCP server

dman777 avatar Jun 03 '25 22:06 dman777

Also having this issue...

mccooll avatar Jun 04 '25 02:06 mccooll

Also facing this. Seems related to #929

luish avatar Jun 17 '25 16:06 luish

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.

alenowak avatar Jun 22 '25 07:06 alenowak

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())

devries avatar Jul 03 '25 12:07 devries

Any updates on this, please?

dman777 avatar Jul 16 '25 13:07 dman777

facing same error when my sub agent uses MCP toolbox remote runing in cloud run

sivakumar-d3v avatar Oct 13 '25 14:10 sivakumar-d3v

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.

SayakKDutta avatar Oct 23 '25 12:10 SayakKDutta