MCP 工具集成 欢迎阅读 模型上下文协议 (MCP) 集成指南!本示例展示了如何通过标准化的 MCP 协议,将您的 ADK 代理与外部数据源和工具连接起来。 您将学到的内容 MCP 基础知识:了解模型上下文协议 ADK ↔ MCP 集成:使用 连接到 MCP 服务器 外部工具访问:利用 MCP 服务器上的工具 服务器通信:处理本地和远程 MCP 服务器 实际应用:结合文件系统和维基百科的实际示例 核心概念:模型上下文协议 模型上下文协议 (MCP) 是一种开放标准,使 AI 代理能够: 一致地访问外部数据源 使用远程服务器上的工具 与各种应用进行通信 在交互过程中保持上下文一致性 MCP 如何与 ADK 配合 MCPToolset 充当桥梁,实现以下功能: 连接到 MCP
欢迎阅读 模型上下文协议 (MCP) 集成指南!本示例展示了如何通过标准化的 MCP 协议,将您的 ADK 代理与外部数据源和工具连接起来。
MCPToolset 连接到 MCP 服务器模型上下文协议 (MCP) 是一种开放标准,使 AI 代理能够:
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ ADK Agent │◄──►│ MCPToolset │◄──►│ MCP Server │ │ │ │ │ │ │ │ Gemini │ │ Bridge │ │ Tools │ └─────────────┘ └─────────────┘ └─────────────┘
MCPToolset 充当桥梁,实现以下功能:
连接到现有的 MCP 服务器:
位置:./filesystem_agent/
Location: ./firecrawl_agent/
4_4_mcp_tools/ ├── README.md # This file - MCP integration guide ├── requirements.txt # MCP dependencies ├── filesystem_agent/ # Filesystem MCP integration │ ├── __init__.py # Package initialization │ ├── agent.py # Main agent implementation │ └── README.md # Filesystem agent guide ├── firecrawl_agent/ # Firecrawl web scraping integration │ ├── __init__.py # Package initialization │ ├── agent.py # Main agent implementation │ └── README.md # Firecrawl agent guide
MCPToolset 处理所有 MCP 协议细节在运行这些示例之前:
安装依赖:
pip install -r requirements.txt
设置环境:
# From the root tutorials directory cp env.example .env # Edit .env and add your Google AI API key
Node.js 用于 MCP 服务器(适用于社区服务器):
# Install Node.js if not already installed # Required for npm/npx based MCP servers
from google.adk.agents import LlmAgent from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, StdioServerParameters # Create MCP toolset for external server toolset = MCPToolset( connection_params=StdioServerParameters( command='npx', args=['-y', '@modelcontextprotocol/server-filesystem', '/path/to/folder'] ) ) # Create agent with MCP tools agent = LlmAgent( model='gemini-3-flash-preview', name='mcp_agent', instruction='Use MCP tools to help users', tools=[toolset] )
选择一个示例进行探索:
按照每个示例目录中的指南操作
使用 ADK Web 运行:
# From the root tutorials directory adk web
# Connect to filesystem MCP server toolset = MCPToolset( connection_params=StdioServerParameters( command='npx', args=['-y', '@modelcontextprotocol/server-filesystem', '/path/to/folder'] ) ) # Ask agent to use filesystem tools # "List files in the current directory" # "Read the contents of sample.txt"
# Connect to Firecrawl MCP server toolset = MCPToolset( connection_params=StdioServerParameters( command='npx', args=['-y', 'firecrawl-mcp'], env={'FIRECRAWL_API_KEY': 'your_api_key'} ) ) # Ask agent to use web scraping tools # "Scrape the homepage of https://example.com" # "Find all blog post URLs on https://blog.example.com" # "Search for recent AI research papers and extract summaries" # "Extract product details from this e-commerce page: [URL]"
# Test MCP server connection npx @modelcontextprotocol/inspector # Check ADK agent logs adk web --debug
完成本教程后:
MCP 工具可实现:
免责声明:
本文档采用基于机器的 AI 翻译服务进行翻译。尽管我们力求准确,但请注意,自动翻译可能存在错误或不准确之处。应以原文语言版本的文档作为权威依据。如需获取关键信息,建议使用专业的人工翻译。对于因使用本翻译而产生的任何误解或误读,我们概不负责。