🔌 OpenClaw MCP 生态系统完全指南

连接一切:从核心概念到无状态化迁移

🌐 什么是 MCP?

MCP(Model Context Protocol)就像AI世界的"USB接口标准"。它让AI Agent能够连接各种外部工具和数据源,就像你用USB连接各种设备一样简单。

💡 MCP 的核心价值

  • 标准化:统一的协议,让Agent和工具无缝连接
  • 扩展性:通过MCP可以连接19,900+服务器
  • 灵活性:支持自定义MCP服务器
  • 生态化:97M+月度SDK下载,376K+ GitHub Stars
19.9K+
MCP 服务器
97M+
月度下载
376K+
GitHub Stars

⏰ MCP 无状态化倒计时

31

天 - 2026-07-28 截止

initialize握手移除 | Session ID移除 | 新Mcp-Method/Mcp-Name Header

🚀 快速开始

1. 连接 MCP 服务器

# 列出可用的 MCP 服务器
openclaw mcp list

# 连接到一个 MCP 服务器
openclaw mcp connect filesystem

# 查看服务器提供的工具
openclaw mcp tools filesystem

2. 使用 MCP 工具

// 在 Skill 中使用 MCP 工具
const { Skill } = require('@openclaw/core');

class MCPEnabledSkill extends Skill {
  async init() {
    // 获取 MCP 客户端
    this.mcp = this.getMCPClient('filesystem');
  }

  async execute(options) {
    // 调用 MCP 工具
    const files = await this.mcp.callTool('list_files', {
      path: options.path
    });
    
    return files;
  }
}

🛠️ 自定义 MCP 服务器

1. 创建简单的 MCP 服务器

const { Server } = require('@modelcontextprotocol/sdk');
const { StdioServerTransport } = require('@modelcontextprotocol/sdk/server/stdio.js');

// 创建服务器
const server = new Server({
  name: "my-custom-mcp",
  version: "1.0.0"
}, {
  capabilities: {
    tools: {}
  }
});

// 定义工具
server.setRequestHandler(ListToolsRequestSchema, async () => {
  return {
    tools: [
      {
        name: "echo",
        description: "回显输入的文本",
        inputSchema: {
          type: "object",
          properties: {
            text: { type: "string" }
          },
          required: ["text"]
        }
      }
    ]
  };
});

// 实现工具
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  if (request.params.name === "echo") {
    return {
      content: [{
        type: "text",
        text: request.params.arguments.text
      }]
    };
  }
  throw new Error("Unknown tool");
});

// 启动服务器
const transport = new StdioServerTransport();
await server.connect(transport);

2. 在 OpenClaw 中注册

# 在 OpenClaw 配置中添加
openclaw config set mcp.servers.my-custom-mcp.command node
openclaw config set mcp.servers.my-custom-mcp.args '["/path/to/server.js"]'

# 重启 OpenClaw
openclaw restart

🔄 MCP 无状态化迁移

⚠️ 重要:2026-07-28 截止

MCP 规范正在迁移到无状态化架构,需要移除:

  • initialize 握手
  • Session ID
  • 状态保持机制

新增:

  • Mcp-Method Header
  • Mcp-Name Header

迁移步骤

✅ 迁移检查清单

  • ✅ 移除 initialize 请求处理
  • ✅ 移除 sessionId 管理
  • ✅ 添加 Mcp-Method Header 支持
  • ✅ 添加 Mcp-Name Header 支持
  • ✅ 测试无状态请求
  • ✅ 更新文档
// 旧代码(有状态)
server.setRequestHandler(InitializeRequestSchema, async (request) => {
  this.sessionId = generateSessionId();
  return { sessionId: this.sessionId };
});

// 新代码(无状态)
server.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
  const method = extra.headers['mcp-method'];
  const name = extra.headers['mcp-name'];
  
  // 每个请求独立处理,不保持状态
  return await processRequest(request, { method, name });
});

💡 最佳实践

✅ MCP 服务器设计原则

  • 单一职责: 每个MCP服务器只做一件事
  • 无状态设计: 每次请求独立,不依赖session
  • 错误处理: 提供清晰的错误信息
  • 文档完善: 详细描述工具的功能和参数

✅ 性能优化

  • 使用连接池管理MCP连接
  • 实现请求缓存(对只读操作)
  • 设置合理的超时时间
  • 监控MCP调用性能

🔗 热门 MCP 服务器推荐

📁 文件系统(Filesystem)

提供文件读写、目录列表等功能。

🌐 网络请求(Fetch)

发送HTTP请求,获取网页内容。

🗄️ 数据库(Database)

连接MySQL、PostgreSQL等数据库。

🤖 AI模型(AI Models)

调用各种AI模型(GPT、Claude等)。