凌晨3点15分,我的Agent突然多了一项超能力——它能直接操作数据库了。不是魔法,是MCP协议。Model Context Protocol就像Agent的USB接口,插上就能用,拔掉也不影响原系统。今天我们聊聊怎么给Agent装上各种"外设"。
一、MCP 协议基础
1.1 什么是 MCP
MCP (Model Context Protocol) 是一个开放标准协议,让AI Agent能够标准化地连接外部工具和数据源。它定义了:
- 工具发现:Agent自动发现可用工具
- 工具调用:标准化调用格式
- 资源访问:标准化数据访问接口
- 提示模板:可复用的提示模式
1.2 MCP 架构
┌──────────────┐ MCP ┌──────────────┐
│ AI Agent │ ◄──────────► │ MCP Server │
│ (OpenClaw) │ Protocol │ │
└──────────────┘ │ Tools │
│ Resources │
│ Prompts │
└──────┬───────┘
│
┌──────┴───────┐
│ 外部服务 │
│ - 数据库 │
│ - API │
│ - 文件系统 │
└──────────────┘
二、OpenClaw 中的 MCP 配置
2.1 基础配置
# config.yaml 中的 MCP 配置
mcp:
enabled: true
servers:
# 文件系统 MCP
filesystem:
command: "npx"
args: ["-y", "@anthropic/mcp-filesystem", "/var/www"]
enabled: true
# 数据库 MCP
postgres:
command: "npx"
args: ["-y", "@anthropic/mcp-postgres", "postgresql://..."]
enabled: true
# Git MCP
git:
command: "npx"
args: ["-y", "@anthropic/mcp-git", "--repo", "/root/repo"]
enabled: true
2.2 企业微信 MCP Server
OpenClaw 支持通过HTTP调用企业微信MCP Server:
# 列出企业微信MCP所有工具
wecom_mcp({
action: "list",
category: "contact"
})
# 调用具体工具
wecom_mcp({
action: "call",
category: "contact",
method: "getContact",
args: '{"userId": "zhangsan"}'
})
三、创建自定义 MCP 连接器
3.1 MCP Server 开发模板
// mcp-server-template.js
import { McpServer } from "@anthropic/mcp-sdk";
const server = new McpServer({
name: "my-custom-tools",
version: "1.0.0"
});
// 注册工具
server.tool("search_knowledge",
"搜索知识库",
{ query: { type: "string", description: "搜索关键词" } },
async ({ query }) => {
const results = await myKnowledgeBase.search(query);
return {
content: [{
type: "text",
text: JSON.stringify(results, null, 2)
}]
};
}
);
// 注册资源
server.resource("docs",
"docs://readme",
async (uri) => ({
contents: [{
uri: uri.href,
text: "项目文档内容..."
}]
})
);
// 启动服务器
server.run();
3.2 配置自定义 MCP Server
# 在 OpenClaw 中注册自定义 MCP
mcp:
servers:
my-tools:
command: "node"
args: ["/path/to/mcp-server-template.js"]
env:
API_KEY: "${MY_TOOL_API_KEY}"
enabled: true
四、MCP 工具编排
4.1 多 MCP 服务器协同
OpenClaw 可以同时连接多个 MCP 服务器,Agent会自动选择最合适的工具:
# 同时配置多个 MCP 服务器
mcp:
servers:
# 企业微信 - 通讯录管理
wecom-contact:
category: "contact"
enabled: true
# 企业微信 - 文档操作
wecom-doc:
category: "doc"
enabled: true
# 自定义知识库
knowledge:
command: "node"
args: ["./mcp-knowledge-server.js"]
enabled: true
4.2 工具链编排模式
# 典型的多MCP工具链
# 步骤1: 从企业微信获取联系人信息
wecom_mcp({ action: "call", category: "contact", method: "getContact", args: '{}' })
# 步骤2: 查询知识库获取相关信息
wecom_mcp({ action: "call", category: "doc", method: "searchDoc", args: '{"query": "项目进度"}' })
# 步骤3: 使用内置工具生成回复
message({ action: "send", channel: "feishu", target: "chat-id", message: "回复内容" })
五、MCP 调试与管理
5.1 检查 MCP 状态
# 列出所有可用的 MCP 工具
wecom_mcp({ action: "list", category: "contact" })
# 测试 MCP 连接
wecom_mcp({ action: "call", category: "contact", method: "ping", args: '{}' })
5.2 常见问题排查
MCP 故障排查清单:
- ✅ MCP Server 进程是否正常运行
- ✅ 环境变量(API Key等)是否正确
- ✅ 网络连接是否通畅
- ✅ 工具名称和参数是否匹配
- ✅ MCP Server 版本是否兼容
六、MCP 安全考虑
- 最小权限:每个 MCP Server 只授予必要权限
- 输入验证:验证所有 MCP 工具调用的参数
- 审计日志:记录所有 MCP 工具调用
- 网络隔离:MCP Server 运行在独立网络区域
- 凭证管理:MCP 使用的 API Key 通过环境变量管理
🔗 MCP 的魅力:它让AI Agent像乐高积木一样可以自由组合能力。今天接个数据库,明天加个API,后天连个搜索引擎——Agent的能力边界,只取决于你装了多少"MCP插件"。