ADK MCP代理
两个ADK代理以代码模式演示MCP模式。
快速开始
uv sync
cp .env.example .env
# Run all examples
uv run python example.py
./test.sh direct # Test direct agent
./test.sh code # Test code mode agent
# Or run tests directly (includes verbose warnings)
uv run python test_direct_agent.py
uv run python test_code_mode_agent.py两名特工
1.直接代理人
传统的MCP工具调用-代理直接调用工具。
from adk_mcp_agent.direct_agent import direct_agent
from google.adk.runners import Runner
runner = Runner(agent=direct_agent)
response = runner.run("Calculate 5 + 3")2.代码模式代理
Cloudflare代码模式模式代理生成使用工具执行的Python代码。
from adk_mcp_agent.code_mode_agent import code_mode_agent
from google.adk.runners import Runner
runner = Runner(agent=code_mode_agent)
response = runner.run("Calculate sum of squares from 1 to 10")MCP服务器示例
配置于 mcp_config.py:
- 计算器 (自定义)-使用FastMCP的算术运算
- 文件系统 (预构建)-/tmp中的文件操作
- pdf (预构建)-PDF文本提取
添加自定义MCP服务器
- 在中创建服务器
servers/your_server/mcp_server.py:
from fastmcp import FastMCP
mcp = FastMCP(name="your_server")
@mcp.tool
async def your_tool(param: str) -> dict:
return {"result": param}
if __name__ == "__main__":
mcp.run()- 增添
mcp_config.py:
"your_server": {
"command": "python",
"args": ["-m", "adk_mcp_agent.servers.your_server.mcp_server"],
"env": {"PYTHONPATH": AGENT_DIR}
}已知问题
架构兼容性问题
问题
一些预构建的MCP服务器使用 元组验证 在他们的JSON模式中,这是有效的JSON模式,但大多数LLM提供商不支持:
"items": [{"type": "number"}, {"type": "number"}] // 2-element tuple这会导致Google ADK/Gemini崩溃:
AttributeError: 'list' object has no attribute 'items'跨提供商兼容性
元组验证在很大程度上不受支持:
- 谷歌双子座:架构转换器中的运行时崩溃
