MCP 协议简介
Model Context Protocol (MCP) 是一个开放协议,用于连接 AI 模型与外部工具和数据源。OpenClaw 作为 MCP Client,可以连接多个 MCP Server,让 AI 助手获得访问数据库、文件系统、API 等外部资源的能力。
查看 MCP 连接
openclaw mcp list
MCP Servers:
Name Transport Status Tools
──────────────────────────────────────────────
filesystem stdio ● Online 5
database sse ● Online 3
github stdio ● Online 8
slack-tools stdio ○ Offline 0
添加 MCP Server
Stdio 传输方式
{
"mcp": {
"servers": {
"filesystem": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/allowed/dir"],
"env": {}
}
}
}
}
SSE 传输方式
{
"mcp": {
"servers": {
"remote-tools": {
"transport": "sse",
"url": "https://mcp-server.example.com/sse",
"headers": {
"Authorization": "Bearer {{MCP_SERVER_TOKEN}}"
}
}
}
}
}
常用 MCP Server 配置
文件系统
{
"mcp": {
"servers": {
"filesystem": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/home/user/documents"]
}
}
}
}
PostgreSQL 数据库
{
"mcp": {
"servers": {
"postgres": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres"],
"env": {
"POSTGRES_URL": "{{DATABASE_URL}}"
}
}
}
}
}
GitHub
{
"mcp": {
"servers": {
"github": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "{{GITHUB_TOKEN}}"
}
}
}
}
}
Web 搜索
{
"mcp": {
"servers": {
"brave-search": {
"transport": "stdio",
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": "{{BRAVE_API_KEY}}"
}
}
}
}
}
为频道分配 MCP 工具
{
"channels": {
"telegram-main": {
"mcpServers": ["filesystem", "brave-search"],
"tools": ["mcp:filesystem:read_file", "mcp:brave-search:search"]
}
}
}
查看 MCP Server 提供的工具
openclaw mcp tools filesystem
Tools from 'filesystem':
read_file Read contents of a file
write_file Write content to a file
list_directory List files in a directory
search_files Search for files by name
get_file_info Get file metadata
MCP Server 健康检查
openclaw mcp health
MCP Server Health:
filesystem ✓ Healthy Tools: 5 Latency: 5ms
database ✓ Healthy Tools: 3 Latency: 15ms
github ✓ Healthy Tools: 8 Latency: 120ms
slack-tools ✗ Offline Connection refused
安全配置
限制 MCP 工具的使用权限:
{
"mcp": {
"security": {
"requireApproval": ["write_file", "delete_file", "execute_query"],
"blockedTools": ["drop_table"],
"maxCallsPerMinute": 30,
"timeout": 10000
}
}
}
requireApproval 列表中的工具在执行前会请求用户确认。
开发自定义 MCP Server
创建一个简单的 MCP Server:
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({
name: "my-tools",
version: "1.0.0"
}, {
capabilities: { tools: {} }
});
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "get_time",
description: "获取当前时间",
inputSchema: { type: "object", properties: {} }
}]
}));
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "get_time") {
return {
content: [{ type: "text", text: new Date().toISOString() }]
};
}
});
const transport = new StdioServerTransport();
await server.connect(transport);
在 OpenClaw 中注册:
{
"mcp": {
"servers": {
"my-tools": {
"transport": "stdio",
"command": "node",
"args": ["./my-mcp-server.js"]
}
}
}
}
调试 MCP 连接
openclaw mcp test filesystem
openclaw mcp logs filesystem --last 20
总结
MCP 协议为 OpenClaw 提供了标准化的工具扩展机制。通过连接各种 MCP Server,AI 助手可以访问文件系统、数据库、外部 API 等丰富的外部资源,真正实现「AI + 工具」的强大组合。