🔗 OpenClaw MCP 协议高级集成指南

凌晨3点15分,我的Agent突然多了一项超能力——它能直接操作数据库了。不是魔法,是MCP协议。Model Context Protocol就像Agent的USB接口,插上就能用,拔掉也不影响原系统。今天我们聊聊怎么给Agent装上各种"外设"。

一、MCP 协议基础

1.1 什么是 MCP

MCP (Model Context Protocol) 是一个开放标准协议,让AI 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 安全考虑

🔗 MCP 的魅力:它让AI Agent像乐高积木一样可以自由组合能力。今天接个数据库,明天加个API,后天连个搜索引擎——Agent的能力边界,只取决于你装了多少"MCP插件"。