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

Importing the right way using MCP tools in a sub agent

Open kanlanc opened this issue 1 year ago • 10 comments

Image

Hey guys,

Im defining a sub browser use agent in the sub agent folder, like the docs mentioned, but im having trouble importing this into the main agent that is going to use this sub agent.

I tried changing the init file but that didnt work either. For reference like this

Image

Importing like this in the main file

Image

but its not working

Any comments, concerns or outright im doing something dumb?

kanlanc avatar Apr 21 '25 01:04 kanlanc

Can you provide more of your project structure and the command you run the main.py file with? @kanlanc

ngothetai avatar Apr 21 '25 02:04 ngothetai

Image

This is the current file structure

And in the main file

Image

kanlanc avatar Apr 21 '25 02:04 kanlanc

Doing it this way, removed the errors

Image

But the console it forever stuck on connecting to browserbase

Image

kanlanc avatar Apr 21 '25 02:04 kanlanc

Is this problem solved yet? I can only use MCP in the root agent.

GaoZhuoXiu avatar May 08 '25 08:05 GaoZhuoXiu

@hangfei @seanzhou1023 @ngothetai Has this been resolved where you can import MCP tools to a sub agent? Im still getting issues when i do this in a sub agent following the exact code in the docs specifically im talking about getting coroutine issues specifically when i call mcp toolset on a sub agent

This is the code from docs that errors out into coroutine is not being awaited issue when I run it in a subagent


# ./adk_agent_samples/mcp_agent/agent.py
from google.adk.agents.llm_agent import LlmAgent
from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters


async def create_agent():
  """Gets tools from MCP Server."""
  tools, exit_stack = await MCPToolset.from_server(
      connection_params=StdioServerParameters(
          command='npx',
          args=["-y",    # Arguments for the command
            "@modelcontextprotocol/server-filesystem",
            # TODO: IMPORTANT! Change the path below to an ABSOLUTE path on your system.
            "/path/to/your/folder",
          ],
      )
  )

  agent = LlmAgent(
      model='gemini-2.0-flash',
      name='enterprise_assistant',
      instruction=(
          'Help user accessing their file systems'
      ),
      tools=tools,
  )
  return agent, exit_stack


root_agent = create_agent()

kanlanc avatar May 13 '25 20:05 kanlanc

I pulled a weird work around for this by using custom agents and inside the async run implementation function where your supposed to write the core code, I called the async function for getting MCP toolset

It looks promising but one thing im unsure about that could lead to issues is where should i close the connection from for a custom sub agent

kanlanc avatar May 13 '25 20:05 kanlanc

I knew it was too good to be true and im running into this error

Image

kanlanc avatar May 13 '25 22:05 kanlanc

Re-opened this issue, as it has the most context on the problem. Closing the other one instead as duplicate. https://github.com/google/adk-python/issues/712

ankursharmas avatar May 15 '25 21:05 ankursharmas

My current work around for this is to call MCP toolset in root agent and pass it down to sub agents for them to use.

It works rn

kanlanc avatar May 15 '25 21:05 kanlanc

we have already changed the way MCP toolset is used. We no longer support the old way. And please install from source or wait for next minor release since 1.0 still has some issues.

example to follow :

https://github.com/google/adk-python/tree/main/contributing/samples/mcp_stdio_server_agent

feel free to reopen if latest codes on main or in next minor release still don't work

seanzhou1023 avatar May 28 '25 00:05 seanzhou1023