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

sub agent cloud not correctly to transfer to root agent and calling the MCP tool

Open AeonMind opened this issue 1 year ago • 7 comments

I think there are two issues:


Issue 1: in a multi-agent setup (with LlmAgent.sub_agents)

your prompt of 3. Then move the control back to the parent agent. may not work well. You could try 3. transfer to parent agent. . Same applies to document_id_agent.

The agent transfer tool is just setting tool_context.actions.transfer_to_agent per LLM's ask.

Check out: https://github.com/google/adk-python/blob/main/src/google/adk/tools/transfer_to_agent_tool.py#L18


Issue 2: tool not called in AgentTool agent.

I think the tools are likely called. For tools called inside AgentTool agent, they are not rendered in UI. AgentTool is a wrapper to encapsulate an agent with structured input/output.

Since your sub-agents set state, which will be propagated back to parent agent's session. You can verify by checking whether the states are set in parent agent.


Feel free to re-open if this doesn't solve your question.

Originally posted by @Jacksunwei in #620


I have a same issue, I create root agent with two sub agent, the sub agent used as an agent not agent tool, each sub agent with same MCP tools, when I ask the first question , the root agent transfer to one of agent and resolved the question, when i ask the second question, I think it's should to transfer to root agent , then transfer to the second agent to resolved the question, but adk just use the first agent call the tool of the second agent, then thrown the tool did not existing.

If i follow up above solution add " when complated the user task, move the control back to the coordinator agent using the 'transfer_control_tool" into prompts, 'transfer_control_tool' was used an MCP tool.

AeonMind avatar May 30 '25 02:05 AeonMind

ValueError: Function search_index is not found in the tools_dict, I upgrade the adk agent to version 1.1.1, this issue also reappear

AeonMind avatar May 30 '25 08:05 AeonMind

The additional agent prompt is insufficient for the agent to transfer you to the root agent and the second sub-agent at once. According to the first Issue you quoted,

The agent transfer tool is just setting tool_context.actions.transfer_to_agent per LLM's ask.

It seems the agent can set the transfer target only once when the user message is inputted. So, as a workaround, you can add a dummy interaction to reset the transfer target.

In your case, "root -> first sub agent -> root -> second sub agent" needs 3 transfers. It needs at least 3 user inputs, which trigger three different LLM processes and transfer targets.

Meanwhile, I guess the tools.transfer_to_agent is unnecessary to be specified in the prompts since the agent is pretty good at triggering the transfer tool with natural commands.

ImJunSang avatar May 30 '25 11:05 ImJunSang

I would like to add more information to clarify this issue。

adk_python version: 1.0.0 & 1.1.1

I create multi agent system. one coordinator agent and two sub agents(gis_command_agent and database_search_agent). each sub agent with one MCP Server(each MCP Server include some of MCP tools)

Coordinator agent: Handle greetings and route the question to sub agent(gis_command_agent or database_search_agent)

root_agent = LlmAgent(
    model=LiteLlm(model=default_model_name),
    name=coordinator_agent_name,
    description=coordinator_agent_description,
    instruction=coordinator_agent_instruction,
    sub_agents=[
        database_search_agent,
        gis_command_agent
    ]
)

gis_command_agent handle the web map operate request.

gis_command_agent = LlmAgent(
  model=LiteLlm(model=default_model_name),
  name=gis_command_agent_name,
  instruction=gis_command_agent_instruction,
  tools=[MCPToolset(
        connection_params=SseServerParams(
            url=gis_command_mcp_url,
            headers={'Accept': 'text/event-stream'},
        ))],
)

the gis_command_mcp server with group of tools , such as "load_layer", "unload_layer", "location_targeting"

database_search_agent handle the data search from database request.

database_search_agent = LlmAgent(
  model=LiteLlm(model=default_model_name),
  name=database_agent_name,
  description=database_agent_description,
  instruction=database_agent_instruction,
  tools=[MCPToolset(
        connection_params=SseServerParams(
            url=database_mcp_url,
            headers={'Accept': 'text/event-stream'},
        ))]
)

the database_mcp server also with groups of tool, such as "load_tables", "loader_fields_by_table_name"

The process is that:

  1. The first question is load a layer, so coordinator agent route the request to gis_command_agent and call mcp server tool to do the request .

  2. The second question is location , then gis_command_agent call the tool "location_targeting", that all ok.

  3. The issue appear when the third question is search data, it's show call "load_tables" tool,

It's prompt that " Error in event_generator: Functionl oad_tables is not found in the tools_dict. "

So i guest that because the agent did not correctly transfer to database_search_agent .

AeonMind avatar Jun 03 '25 09:06 AeonMind

Any one can successfully prompts the subagent to transfer back the control to the parent agent? I've tried many prompt include explicitly mentioning the parent agent name, but no luck. The subagent just complete the task and never call the transfer_to_agent tool.

rickcodetalk avatar Jun 04 '25 16:06 rickcodetalk

Any one can successfully prompts the subagent to transfer back the control to the parent agent? I've tried many prompt include explicitly mentioning the parent agent name, but no luck. The subagent just complete the task and never call the transfer_to_agent tool.

I can successfully transfer to parent agent, but some time prompt below error, not sure where is the 'query' parameter from.

TypeError: transfer_to_agent() got an unexpected keyword argument 'query' Failed to detach context Traceback (most recent call last): File "/usr/local/lib/python3.13/site-packages/opentelemetry/trace/init.py", line 587, in use_span yield span File "/usr/local/lib/python3.13/site-packages/opentelemetry/sdk/trace/init.py", line 1105, in start_as_current_span yield span File "/usr/local/lib/python3.13/site-packages/opentelemetry/trace/init.py", line 452, in start_as_current_span yield span File "/usr/local/lib/python3.13/site-packages/google/adk/flows/llm_flows/base_llm_flow.py", line 537, in _call_llm_async yield llm_response GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/local/lib/python3.13/site-packages/opentelemetry/context/init.py", line 155, in detach _RUNTIME_CONTEXT.detach(token) ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ File "/usr/local/lib/python3.13/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach self._current_context.reset(token) ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^ ValueError: <Token var=<ContextVar name='current_context' default={} at 0x7fdeacf7ae80> at 0x7fde9a0e4680> was created in a different Context

AeonMind avatar Jun 05 '25 07:06 AeonMind

is there any solution to fix this?

pawan-dream11 avatar Jun 26 '25 16:06 pawan-dream11

Hi @AeonMind and thanks to everyone who participated in this thread.

We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

Thanks for your contribution and understanding!

jwiejulie avatar Aug 10 '25 03:08 jwiejulie