🔌 OpenClaw MCP协议集成完全指南

用Model Context Protocol打通AI Agent的任督二脉

世界上有一种协议叫MCP,它就像AI世界的USB接口。 在它出现之前,每个AI工具都是一座孤岛,各自为政。MCP来了,它们终于能坐下来好好说话了。

📋 MCP是什么?

MCP(Model Context Protocol)是Anthropic提出的开放协议,让AI模型能标准化地访问外部工具和数据源。类比:

核心优势:一个MCP Server可以被任何支持MCP的AI Client调用,无需重复集成。

🏗️ MCP架构

组件角色示例
MCP Client发起请求OpenClaw、Claude Desktop、Cursor
MCP Server提供服务GitHub Server、数据库Server、文件系统Server
MCP Protocol通信标准JSON-RPC 2.0 over stdio/SSE
# MCP通信流程
┌─────────────┐                    ┌─────────────┐
│ MCP Client  │  ←─ JSON-RPC ──→  │ MCP Server  │
│ (OpenClaw)  │     over stdio     │ (GitHub)    │
└─────────────┘                    └─────────────┘

# 1. Client初始化连接
# 2. Server返回工具列表和能力声明
# 3. Client根据任务调用工具
# 4. Server返回执行结果

🚀 OpenClaw中配置MCP

方法1:Gateway配置

# openclaw.yaml - Gateway MCP配置
mcpServers:
  # GitHub MCP Server
  github:
    command: npx
    args: ["-y", "@anthropic/mcp-server-github"]
    env:
      GITHUB_TOKEN: ${GITHUB_TOKEN}

  # 文件系统 MCP Server
  filesystem:
    command: npx
    args: ["-y", "@anthropic/mcp-server-filesystem", "/workspace"]

  # 数据库 MCP Server
  postgres:
    command: npx
    args: ["-y", "@anthropic/mcp-server-postgres", "postgresql://..."]

  # Puppeteer MCP Server(浏览器)
  puppeteer:
    command: npx
    args: ["-y", "@anthropic/mcp-server-puppeteer"]

方法2:动态配置

# 使用OpenClaw CLI动态添加MCP Server
openclaw mcp add github --command "npx" --args "@anthropic/mcp-server-github"
openclaw mcp add filesystem --command "npx" --args "@anthropic/mcp-server-filesystem /data"

# 查看已配置的MCP Server
openclaw mcp list

# 测试MCP Server连接
openclaw mcp test github

🔧 常用MCP Server推荐

Server功能安装命令
GitHub仓库、PR、Issues、Discussionsnpx @anthropic/mcp-server-github
Filesystem文件读写、目录管理npx @anthropic/mcp-server-filesystem
PostgreSQL数据库查询npx @anthropic/mcp-server-postgres
Puppeteer浏览器自动化npx @anthropic/mcp-server-puppeteer
Brave Search网络搜索npx @anthropic/mcp-server-brave-search
Slack消息收发npx @anthropic/mcp-server-slack
Google Drive文档管理npx @anthropic/mcp-server-gdrive
Sentry错误监控npx @anthropic/mcp-server-sentry

💡 实战:GitHub MCP深度使用

# 配置
mcpServers:
  github:
    command: npx
    args: ["-y", "@anthropic/mcp-server-github"]
    env:
      GITHUB_TOKEN: ghp_xxxxxxxxxx

# Agent可直接使用的GitHub工具
# 无需编写任何代码,AI自动调用:

# 搜索仓库
→ "搜索GitHub上关于OpenClaw Skills的仓库,按star排序"

# 管理Issues
→ "创建一个issue,标题是'浏览器自动化偶发超时',标签加bug和triage"

# PR Review
→ "Review PR #42,检查是否有安全问题和性能瓶颈"

# Discussions运营
→ "列出anthropics/openclaw仓库过去24小时的新讨论,按活跃度排序"
🔥 进阶玩法
  • 组合多个MCP Server:GitHub + Slack = 自动通知PR状态
  • MCP + Cron = 定时巡检GitHub Issues
  • MCP + SubAgent = 多个Agent并行处理不同仓库

🌐 MCP vs A2A vs 直接集成

三种集成方式对比

特性MCPA2A直接集成
适用场景工具调用Agent间通信特定平台
实时性
复杂度
标准化开放协议开放协议私有API
生态丰富度快速增长起步中平台绑定

🛡️ 安全最佳实践

# 安全配置示例
mcpServers:
  github:
    command: docker
    args: ["run", "--rm", "-i", "gh-mcp-server"]
    env:
      GITHUB_TOKEN: ${GITHUB_TOKEN}  # 从环境变量读取
    # 可选:限制可访问的工具
    allowedTools:
      - search_repositories
      - create_issue
      - list_pull_requests
    # 禁止危险操作
    disabledTools:
      - delete_repository
      - push_files
⚠️ 常见坑点