Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@djannot

Description

@djannot

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.

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions