From 8c78945ec9ee1aee3623a0461716eec2c8ca599e Mon Sep 17 00:00:00 2001 From: "Xiang (Sean) Zhou" Date: Thu, 8 May 2025 22:57:09 -0700 Subject: [PATCH] update mcp agent to use latest toolset interface PiperOrigin-RevId: 756613972 --- contributing/samples/mcp_agent/agent.py | 47 ++++++++++++------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/contributing/samples/mcp_agent/agent.py b/contributing/samples/mcp_agent/agent.py index 91130f1..9626cc6 100755 --- a/contributing/samples/mcp_agent/agent.py +++ b/contributing/samples/mcp_agent/agent.py @@ -16,29 +16,26 @@ import os from google.adk.agents.llm_agent import LlmAgent -from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters +from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset +from google.adk.tools.mcp_tool.mcp_toolset import 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', - os.path.dirname(os.path.abspath(__file__)), - ], - ) - ) - - 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() +root_agent = LlmAgent( + model='gemini-2.0-flash', + name='enterprise_assistant', + instruction='Help user accessing their file systems', + tools=[ + MCPToolset( + connection_params=StdioServerParameters( + command='npx', + args=[ + '-y', # Arguments for the command + '@modelcontextprotocol/server-filesystem', + os.path.dirname(os.path.abspath(__file__)), + ], + ), + # don't want agent to do write operation + tool_predicate=lambda tool, ctx=None: tool.name + not in ('write_file', 'edit_file', 'create_directory', 'move_file'), + ) + ], +)