AI Agent 调用外部工具时,每个 LLM 提供商的 function calling 格式不同,每个工具需要单独适配。工具越多,维护成本越高。
AI Agent 调用外部工具时,每个 LLM 提供商的 function calling 格式不同,每个工具需要单独适配。工具越多,维护成本越高。
没有标准化的 Agent↔工具 通信协议。类似 Web 早期没有 HTTP 标准。
AI Agent (Claude/GPT/Gemini)
↓ MCP Client
MCP Protocol (JSON-RPC)
↓
MCP Server A (数据库)
MCP Server B (API)
MCP Server C (文件系统)| 概念 | 说明 | 类比 |
|---|---|---|
| MCP Server | 暴露工具的服务 | REST API 服务器 |
| MCP Client | 调用工具的 Agent | HTTP 客户端 |
| Tool | 可调用的函数 | API 端点 |
| Resource | 可读取的数据 | GET 端点 |
# MCP Server(工具提供方)
from mcp import Server, Tool
server = Server("my-db-tools")
@server.tool("query_database")
async def query_database(sql: str) -> list:
"""Execute a SQL query."""
return await db.execute(sql)
@server.tool("get_schema")
async def get_schema(table: str) -> dict:
"""Get table schema."""
return await db.get_schema(table)
# MCP Client(Agent 端)
from mcp import Client
client = Client("my-db-tools")
result = await client.call_tool("query_database", {
"sql": "SELECT * FROM users LIMIT 10"
})| 特性 | Function Calling | MCP |
|---|---|---|
| 标准化 | 各厂商不同 | 统一协议 |
| 工具发现 | 手动配置 | 自动发现 |
| 跨模型 | 不兼容 | 兼容 |
| 生态 | 封闭 | 开放 |
grep -i mcp lessons/contrib/mcp-*.md 2>/dev/null | head -3
echo MCP verifiedExpected Output:
# (refs)
MCP verified