update mcp agent to use latest toolset interface

PiperOrigin-RevId: 756613972
This commit is contained in:
Xiang (Sean) Zhou 2025-05-08 22:57:09 -07:00 committed by Copybara-Service
parent 7dffb59096
commit 8c78945ec9

View File

@ -16,12 +16,15 @@
import os import os
from google.adk.agents.llm_agent import LlmAgent 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
root_agent = LlmAgent(
async def create_agent(): model='gemini-2.0-flash',
"""Gets tools from MCP Server.""" name='enterprise_assistant',
tools, exit_stack = await MCPToolset.from_server( instruction='Help user accessing their file systems',
tools=[
MCPToolset(
connection_params=StdioServerParameters( connection_params=StdioServerParameters(
command='npx', command='npx',
args=[ args=[
@ -29,16 +32,10 @@ async def create_agent():
'@modelcontextprotocol/server-filesystem', '@modelcontextprotocol/server-filesystem',
os.path.dirname(os.path.abspath(__file__)), 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'),
) )
) ],
)
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()