04 - 现有 ACI 工具实现
4.1 Model Context Protocol (MCP) 架构分析
4.1.1 MCP 概述与定位
Model Context Protocol (MCP) 是由 Anthropic 于 2024 年 11 月发布的开放协议,旨在标准化 AI Agent 与外部工具、数据源的交互方式。MCP 是 ACI 范式的首个标准化实现。
flowchart TB
subgraph "MCP Host"
H1[Claude Desktop]
H2[Cursor IDE]
H3[Custom Client]
end
subgraph "MCP Protocol"
P1[Tool Discovery]
P2[Context Management]
P3[Capability Negotiation]
end
subgraph "MCP Servers"
S1[File System Server]
S2[Database Server]
S3[Browser Server]
S4[Custom Server]
end
H1 --> P1
H2 --> P2
H3 --> P3
P1 --> S1
P2 --> S2
P3 --> S3
P1 --> S4
核心数据:
- 发布 3 个月内 GitHub stars 超过 8,000
- 官方和社区服务器超过 1,000 个
- 支持宿主包括 Claude Desktop、Cursor、Zed、Sourcegraph 等
4.1.2 MCP 架构设计
MCP 采用客户端-服务器架构,基于 JSON-RPC 2.0 协议:
┌─────────────────┐ ┌─────────────────┐
│ MCP Client │ <-----> │ MCP Server │
│ (Host) │ stdio │ (Tool) │
│ │ or │ │
│ - Claude Desktop│ SSE │ - File System │
│ - Cursor │ │ - Database │
│ - IDE Plugin │ │ - Browser │
└─────────────────┘ └─────────────────┘
传输层:
- stdio:本地进程通信(推荐,安全可靠)
- SSE:Server-Sent Events(远程服务)
4.1.3 MCP 协议流程
1. 初始化握手:
// Client -> Server
{
"jsonrpc": "2.0",
"method": "initialize",
"params": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {},
"resources": {}
},
"clientInfo": {
"name": "claude-desktop",
"version": "1.0.0"
}
},
"id": 0
}
// Server -> Client
{
"jsonrpc": "2.0",
"result": {
"protocolVersion": "2024-11-05",
"capabilities": {
"tools": {
"listChanged": true
}
},
"serverInfo": {
"name": "filesystem-server",
"version": "1.0.0"
}
},
"id": 0
}
2. 工具发现:
// Client -> Server
{
"jsonrpc": "2.0",
"method": "tools/list",
"id": 1
}
// Server -> Client
{
"jsonrpc": "2.0",
"result": {
"tools": [
{
"name": "read_file",
"description": "读取文件内容。支持相对路径和绝对路径。",
"inputSchema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "文件路径"
}
},
"required": ["path"]
}
},
{
"name": "list_directory",
"description": "列出目录内容",
"inputSchema": {
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "目录路径"
}
},
"required": ["path"]
}
}
]
},
"id": 1
}
3. 工具调用:
// Client -> Server
{
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"name": "read_file",
"arguments": {
"path": "/Users/alice/project/README.md"
}
},
"id": 2
}
// Server -> Client
{
"jsonrpc": "2.0",
"result": {
"content": [
{
"type": "text",
"text": "# Project README\n\nThis is a sample project..."
}
],
"isError": false
},
"id": 2
}
4.1.4 MCP 的优势与局限
优势:
- ✅ 标准化协议,跨 Host 兼容
- ✅ 本地运行,数据安全
- ✅ 丰富的社区生态
- ✅ 支持动态工具发现
局限:
- ⚠️ 相对年轻,工具链待完善
- ⚠️ 性能依赖 Host 的 LLM 能力
- ⚠️ 调试和观测工具不足
- ⚠️ 生产环境稳定性待验证
4.2 Claude Code 工具系统设计
4.2.1 Claude Code 概述
Claude Code 是 Anthropic 于 2024 年推出的 AI 编程助手,内置了一套完整的 ACI 工具系统,支持代码编辑、文件操作、命令执行等开发任务。
核心特性:
- 40+ 内置工具:覆盖开发全流程
- Bash 命令执行:支持任意 shell 命令
- 文件编辑:精确的代码级操作
- 代码搜索:基于语义的代码查找
4.2.2 Claude Code 工具分类
mindmap
root((Claude Code<br/>工具系统))
文件操作
读取 read
编辑 edit
创建 create
搜索 grep
代码分析
符号查找 lsp
诊断检查 diagnostics
代码搜索 search
命令执行
Bash 执行
长时间任务
工作流
Todo 管理
任务委托
工具示例:
| 工具 | 用途 | 示例 |
|---|---|---|
Read | 读取文件 | Read file_path="src/main.ts" |
Edit | 编辑文件 | Edit file_path="src/main.ts" oldString="..." newString="..." |
Bash | 执行命令 | Bash command="npm test" |
Grep | 代码搜索 | Grep pattern="TODO" path="src" |
LSP | 代码导航 | LSP file_path="src/main.ts" line=10 character=5 |
4.2.3 Claude Code 的 ACI 设计特点
1. 工具描述优化:
// Claude Code 工具定义示例
{
name: "Bash",
description: `执行 bash 命令。
使用指南:
- 优先使用 glob、grep 等专用工具而非 ls/find
- 需要编辑时,先读取相关文件
- 危险操作(rm, git push)需特别小心
- 长时间任务使用 timeout 参数`,
parameters: {
command: {
type: "string",
description: "要执行的 bash 命令"
},
timeout: {
type: "number",
description: "超时时间(毫秒),默认 120000"
}
}
}
特点:
- 详细的自然语言描述
- 包含使用指南和最佳实践
- 警告潜在风险
2. 双向交互模式:
User: "帮我修复这个 bug"
Claude: "我来帮您分析。首先让我查看相关文件..."
[Claude 使用 Read 工具读取文件]
Claude: "我发现问题在第 42 行。需要修改这个函数。"
[Claude 使用 Edit 工具修改代码]
Claude: "修改完成。现在运行测试验证..."
[Claude 使用 Bash 运行测试]
Claude: "测试通过!已修复 bug。"
3. 渐进式确认:
对于高风险操作,Claude Code 会请求确认:
Claude: "我需要执行 'rm -rf node_modules',确认吗?"
User: "确认"
Claude: [执行操作]
4.2.4 Claude Code vs MCP
| 维度 | Claude Code | MCP |
|---|---|---|
| 定位 | 内置工具集 | 开放协议 |
| 工具来源 | Anthropic 官方 | 社区 + 自建 |
| 扩展性 | 有限 | 极高 |
| 使用场景 | 编程助手 | 通用 Agent 平台 |
| 部署方式 | 云服务 | 本地/远程 |
| 数据隐私 | 需信任 Anthropic | 完全可控 |
关系:Claude Code 可以集成 MCP 服务器,未来可能支持完整的 MCP 协议。
4.3 其他 ACI 协议实现
4.3.1 OpenAI Functions
OpenAI Functions 是 OpenAI 在 2023 年 6 月推出的函数调用机制,是 ACI 的早期实现。
使用示例:
import openai
functions = [
{
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市名称,如'北京'、'Shanghai'"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["location"]
}
}
]
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[{"role": "user", "content": "北京今天天气如何?"}],
functions=functions,
function_call="auto"
)
# LLM 自动判断需要调用 get_weather
if response.choices[0].finish_reason == "function_call":
function_call = response.choices[0].message.function_call
# 执行函数...
特点:
- ✅ 原生集成在 GPT-4/GPT-3.5 中
- ✅ 开发者生态最成熟
- ✅ 支持并行函数调用
- ⚠️ 仅支持 OpenAI 模型
- ⚠️ 需要开发者自行管理工具执行
采用数据:根据 OpenAI 2024 报告,72% 的生产级应用使用 Functions API。
4.3.2 LangChain Tools
LangChain Tools 是 LangChain 框架的工具抽象层,提供了丰富的预置工具和灵活的自定义能力。
架构设计:
from langchain.tools import BaseTool, Tool
from langchain.agents import initialize_agent, AgentType
# 定义工具
class CalculatorTool(BaseTool):
name = "calculator"
description = "执行数学计算,如'2 + 2'或'sqrt(16)'"
def _run(self, query: str):
return eval(query) # 简化示例
# 内置工具
from langchain_community.tools import WikipediaQueryRun
from langchain_community.utilities import WikipediaAPIWrapper
wikipedia = WikipediaQueryRun(api_wrapper=WikipediaAPIWrapper())
tools = [
CalculatorTool(),
wikipedia
]
# 创建 Agent
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)
agent.run("计算圆的面积,半径为5,同时查找圆周率的历史")
工具生态(部分):
| 工具类别 | 代表工具 | 数量 |
|---|---|---|
| 搜索 | Google Search, Bing, DuckDuckGo | 10+ |
| 数据库 | SQLDatabase, MongoDB | 5+ |
| 文档处理 | PDF, CSV, Notion | 15+ |
| 开发工具 | GitHub, Jira, GitLab | 20+ |
| 云服务 | AWS, GCP, Azure | 10+ |
| 通讯 | Slack, Discord, Email | 8+ |
社区数据:
- GitHub Stars: 95,000+
- PyPI 月下载量: 3,000,000+
- 社区贡献工具: 500+
4.3.3 LlamaIndex Tools
LlamaIndex 专注于数据检索和知识库场景,其工具系统针对 RAG(检索增强生成)优化。
核心特性:
- 数据连接器:150+ 数据源(PDF、SQL、API、SaaS)
- 索引优化:向量索引、树形索引、列表索引
- 查询引擎:支持复杂查询分解
示例:
from llama_index.core.tools import QueryEngineTool, ToolMetadata
from llama_index.core.agent import ReActAgent
# 创建查询引擎工具
query_engine_tools = [
QueryEngineTool(
query_engine=sql_engine,
metadata=ToolMetadata(
name="sql_query",
description="查询销售数据库,支持自然语言查询"
)
),
QueryEngineTool(
query_engine=doc_engine,
metadata=ToolMetadata(
name="doc_search",
description="搜索产品文档"
)
)
]
# 创建 Agent
agent = ReActAgent.from_tools(
query_engine_tools,
llm=llm,
verbose=True
)
response = agent.chat("Q3 销售额最高的产品是什么?它有什么特性?")
定位差异:
- LangChain:通用 Agent 框架,工具种类丰富
- LlamaIndex:数据/知识库场景,检索能力强
- MCP:标准化协议,跨平台兼容
- OpenAI Functions:原生集成,简单易用
4.4 实际代码示例与集成模式
4.4.1 MCP Server 开发示例
创建一个简单的天气查询 MCP Server:
# weather_server.py
from mcp.server import Server
from mcp.types import TextContent, Tool
import httpx
server = Server("weather-server")
@server.list_tools()
async def list_tools() -> list[Tool]:
return [
Tool(
name="get_weather",
description="获取指定城市的当前天气",
inputSchema={
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
)
]
@server.call_tool()
async def call_tool(name: str, arguments: dict):
if name == "get_weather":
city = arguments["city"]
# 调用天气 API
async with httpx.AsyncClient() as client:
response = await client.get(
f"https://api.weather.com/v1/current?city={city}"
)
data = response.json()
return [
TextContent(
type="text",
text=f"{city}当前天气:{data['temperature']}°C,{data['condition']}"
)
]
if __name__ == "__main__":
server.run(transport="stdio")
配置使用:
// claude_desktop_config.json
{
"mcpServers": {
"weather": {
"command": "python",
"args": ["/path/to/weather_server.py"]
}
}
}
4.4.2 多工具编排示例
使用 LangChain 编排多个工具完成复杂任务:
from langchain import OpenAI, LLMMathChain, SerpAPIWrapper
from langchain.agents import initialize_agent, Tool, AgentType
# 定义工具
search = SerpAPIWrapper()
llm_math = LLMMathChain(llm=OpenAI(temperature=0))
tools = [
Tool(
name="Search",
func=search.run,
description="用于搜索当前事件或实时信息"
),
Tool(
name="Calculator",
func=llm_math.run,
description="用于数学计算"
)
]
# 初始化 Agent
agent = initialize_agent(
tools,
OpenAI(temperature=0),
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
# 执行复杂查询
agent.run("特斯拉当前股价是多少?如果我买10股需要多少钱?")
# Agent 思考过程:
# 1. 需要知道特斯拉股价 -> 使用 Search
# 2. 计算10股价格 -> 使用 Calculator
4.4.3 工具选择策略对比
| 策略 | 实现方式 | 适用场景 |
|---|---|---|
| Zero-Shot | 直接根据描述选择 | 工具少(<10)、描述清晰 |
| Few-Shot | 提供示例 | 工具多、需要区分相似工具 |
| Self-Ask | 分解子问题 | 复杂任务需要多步推理 |
| ReAct | 推理+行动交替 | 需要探索的开放性问题 |
| Plan-and-Execute | 先规划再执行 | 确定性多步任务 |
4.5 本章小结
现有 ACI 工具实现呈现多元化发展:
- MCP 正在成为开放标准,生态快速成长
- Claude Code 代表企业级内置工具系统
- OpenAI Functions 拥有最广泛的开发者基础
- LangChain/LlamaIndex 提供丰富的框架和生态
选型建议:
- 追求标准化和可移植性 → MCP
- 快速原型开发 → OpenAI Functions
- 复杂 Agent 工作流 → LangChain
- 知识库/RAG 场景 → LlamaIndex
下一章将分析 ACI 采用的风险与挑战,并提供实施建议。
本章引用:
- MCP Specification. https://modelcontextprotocol.io/specification
- Anthropic. “Claude Code Documentation.” 2024.
- OpenAI. “Function Calling API Reference.” 2024.
- LangChain Documentation. https://python.langchain.com
- LlamaIndex Documentation. https://docs.llamaindex.ai