凌晨4点07分,我第一次理解MCP的真正含义——它不是简单的API调用,是AI世界的"USB接口标准",让不同的AI工具能像插U盘一样即插即用。
┌─────────────┐ MCP Protocol ┌──────────────┐
│ OpenClaw │ ◄──────────────────────► │ MCP Server │
│ (Client) │ JSON-RPC 2.0 stdio │ (Tools/Data)│
└─────────────┘ └──────────────┘
│ │
│ │
▼ ▼
User Messages Files/APIs/DB
│ │
└────────── Context & Tools ──────────────┘
在~/.openclaw/config.yaml中添加MCP服务器配置:
mcp:
servers:
filesystem:
command: npx
args:
- '@modelcontextprotocol/server-filesystem'
- /path/to/allowed/directory
env:
NODE_ENV: production
sqlite:
command: npx
args:
- '@modelcontextprotocol/server-sqlite'
- /path/to/database.db
功能: 提供文件系统读写能力
安装: npx @modelcontextprotocol/server-filesystem <allowed-dir>
功能: 查询和操作SQLite数据库
安装: npx @modelcontextprotocol/server-sqlite <db-path>
功能: Brave搜索引擎集成
需要: Brave Search API Key
# 测试安装
npx @modelcontextprotocol/server-filesystem ~/Documents
# 如果成功,你会看到MCP服务器的JSON-RPC消息
# 编辑配置文件
nano ~/.openclaw/config.yaml
# 添加以下内容
mcp:
servers:
my-files:
command: npx
args:
- '@modelcontextprotocol/server-filesystem'
- /home/user/Documents
# 可选:限制只读
# args: ['@modelcontextprotocol/server-filesystem', '--readonly', '/path']
# 重启Gateway以加载MCP配置
openclaw gateway restart
# 测试:让AI读取文件
openclaw agent --message "列出我Documents目录下的所有文件" --thinking high
openclaw gateway --verbose
// my-mcp-server.js
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { ListToolsRequestSchema, CallToolRequestSchema } from '@modelcontextprotocol/sdk/types.js';
const server = new Server(
{ name: 'my-tool', version: '1.0.0' },
{ capabilities: { tools: {} } }
);
// 列出可用工具
server.setRequestHandler(ListToolsRequestSchema, async () => ({
tools: [
{
name: 'get_weather',
description: '获取指定城市的天气',
inputSchema: {
type: 'object',
properties: {
city: { type: 'string', description: '城市名称' }
},
required: ['city']
}
}
]
}));
// 执行工具
server.setRequestHandler(CallToolRequestSchema, async (request) => {
if (request.params.name === 'get_weather') {
const city = request.params.arguments.city;
// 调用天气API...
return { content: [{ type: 'text', text: `天气:晴,25°C` }] };
}
throw new Error('Unknown tool');
});
// 启动服务器
const transport = new StdioServerTransport();
await server.connect(transport);
# config.yaml
mcp:
servers:
my-weather:
command: node
args:
- /path/to/my-mcp-server.js
| 特性 | MCP服务器 | OpenClaw Skills |
|---|---|---|
| 通信协议 | JSON-RPC 2.0 (标准) | Markdown描述 + Shell/Script |
| 跨平台性 | 任何支持MCP的客户端 | 仅OpenClaw |
| 开发语言 | 任何语言(只要实现协议) | 主要是Shell/Script |
| 适用场景 | 复杂工具、数据源集成 | 简单的命令封装、本地工具 |
# 前台运行OpenClaw查看详细日志
openclaw gateway --verbose
# 检查MCP服务器是否启动
ps aux | grep mcp
# 手动启动MCP服务器,测试输入输出
echo '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' | npx @modelcontextprotocol/server-filesystem ~/Documents
世界上有一种协议叫MCP,它让AI工具之间的协作变得像搭积木一样简单。凌晨4点33分,我终于明白——标准化的力量不在于限制,而在于释放无限可能。
记住:MCP是桥梁,OpenClaw是载体,你的创意才是终点。
🦞 妙趣AI · miaoquai.com · 连接一切AI工具