通过子代理管理 MCP(Multi-Channel Publisher)¶
您已经了解了如何使用相关方法将您的代理程序连接到外部资源。 直接集成 MCP 工具 以及如何通过某种方式将您的代理服务提供给外部客户。 Agent-Exposed MCP Server然而,随着您的 ADK(Android Development Kit)的多智能体功能的不断增强,您可能需要一种方法来处理那些复杂、需要多步骤推理的任务,同时避免让单个模型的上下文窗口负担过重(即避免因处理过多信息而导致模型性能下降)。
立即开始使用吧!¶
import os
from google.adk.agents.llm_agent import LlmAgent
from google.adk.tools.agent_tool import AgentTool
from google.adk.tools.mcp_tool import McpToolset
from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams
from mcp import StdioServerParameters
# Step 1: Initialize your MCP connection
mcp_connection = StdioConnectionParams(
server_params=StdioServerParameters(
command='npx',
args=['-y', '@modelcontextprotocol/server-postgres', 'postgres://user:pass@localhost:5432/db'],
)
)
# Step 2: Create the child Sub-Agent and attach the McpToolset
database_sub_agent = LlmAgent(
name="database_sub_agent",
model="gemini-flash-latest",
description="Delegates tasks to a database specialist...",
instruction="You are a SQL expert...",
tools=[McpToolset(connection_params=mcp_connection, tool_filter=['query_db', 'list_tables'])]
)
database_tool = AgentTool(agent=database_sub_agent)
# Step 3: Provide the AgentTool to your primary root agent
root_agent = LlmAgent(
name="primary_orchestrator",
model="gemini-pro-latest",
instruction="You are the main assistant. You have access to specialized agents. Delegate data retrieval tasks to your database tool.",
tools=[database_tool]
)
架构角色与工具的适配¶
在 ADK 框架中, AgentTool “wraps an” 的意思是“将……包裹起来”或“用……覆盖”。例如:
- “This function wraps the input data in a protective layer before processing it.”(这个函数在处理数据之前,会先将数据包裹在一个保护层中。)
- “The software uses a special material to wrap the wires to prevent damage.”(该软件使用特殊材料来包裹电线,以防止损坏。) LlmAgent 将其作为原生工具提供给上级协调器(parent orchestrator)使用。子代理(child agent)通过该工具来获取所需的资源或服务。 McpToolset该系统通过某种方式连接到底层的 MCP 服务器。 list_tools该工具会将外部 MCP 工具的定义转换为与 ADK 兼容的形式。 BaseTool 实例(instances)以及代理(proxies)会处理所有的执行请求(execution calls)。call_tool)以异步的方式执行。
工具范围界定与认知过滤(Tool Scoping and Cognitive Filtering)tool_filter)¶
在分配某个值时…… McpToolset 你可以将这些工具仅提供给特定的子代理(sub-agent),并通过相应的设置来限制该子代理能够使用的具体工具种类。 tool_filter 参数:
* **认知专注力(Cognitive Focus):** 仅暴露与子代理的职责相关的具体操作,例如 `read_file`、`list_directory`。 安全性与沙箱环境: 防止未经授权的人员访问那些可能被 MCP 服务器暴露出来的危险工具,从而将程序的执行环境与不可预测的输入数据隔离开来。
连接传输方式(Connection Transport Modes)¶
子代理的 McpToolset 必须根据部署架构,选择以下两种主要连接模式中的一种进行配置:
* 本地子进程(Local Subprocess)StdioConnectionParams): 生成一个本地的可执行文件或进程(例如,通过某种方式)。 npx 或者 python通过标准输入/输出进行通信。
最适合独立运行的单容器环境。
* 远程网络(Remote Network)StreamableHTTPConnectionParams / SseConnectionParams):
通过 HTTP/SSE 协议进行连接,并使用相应的头部信息(例如……)。 X-Goog-Api-Key 或者 Authorization最适合用于可扩展的、多租户的,或独立管理的 MCP(Media Center Platform)后端系统。
定义及生命周期规则¶
- 用于生产环境的同步实例化(Synchronous Instantiation for Production): 在部署多代理系统时(无论是使用 Cloud Run、GKE 还是 Agent Engine),子代理及其相关组件都需要被正确配置和管理。
McpToolset必须同步实例化(即:这些对象必须同时被创建或初始化)。agent.py而不是在异步工厂函数(async factory function)的内部。 - 会话的持久化与恢复:
McpToolset支持序列化; 通过(某种方式或机制)实现序列化。__getstate__和__setstate__虽然会话状态会在应用程序的整个生命周期内被保留下来,但当代理进程重新启动时,MCP 插件使用的套接字(socket)及标准输入/输出(stdio)连接会被动态地重新初始化。 - “清理管理”(Cleanup Management): 当在户外跑步时……
adk web在自定义运行时环境中,需要通过显式的方式(即明确地执行相关操作)来完成系统的卸载或资源释放。await toolset.close()或者await runner.close()确保后台服务器的子进程以及网络连接能够被正确地终止(即:不会留下任何未处理的资源或错误状态)。