世界上有一种服务器,它不提供网页,不提供API,只提供工具。就像凌晨2点58分,你突然需要一个"发邮件"的工具,MCP Server说:"我有,拿去用吧。"
—— 妙趣AI · MCP Server哲学
MCP Server(Model Context Protocol Server)是基于MCP协议(Anthropic提出)的工具服务器,它为AI Agent提供标准化的工具接口。Agent通过MCP协议连接Server,查询可用工具列表,并调用这些工具。
MCP Server的作用:
MCP协议定义了Agent和Server之间的通信方式:
# MCP 核心方法 1. tools/list - Agent查询Server有哪些工具 2. tools/call - Agent调用某个工具 3. resources/list - Agent查询可用资源(可选) 4. resources/read - Agent读取某个资源(可选)
| 组件 | 说明 |
|---|---|
| 工具注册表 | Server维护的工具列表(名称、描述、参数schema) |
| 工具执行器 | 收到调用请求后,执行对应工具并返回结果 |
| 协议处理器 | 处理MCP协议消息(JSON-RPC格式) |
| 传输层 | stdio、HTTP、WebSocket等通信方式 |
MCP Server中的工具定义:
{
"name": "github_create_issue",
"description": "Create a new issue in a GitHub repository",
"inputSchema": {
"type": "object",
"properties": {
"repo": { "type": "string", "description": "Repository name" },
"title": { "type": "string", "description": "Issue title" },
"body": { "type": "string", "description": "Issue body" }
},
"required": ["repo", "title"]
}
}
OpenClaw可以通过MCP协议接入外部工具:
# 1. 启动MCP Server(例如GitHub MCP Server)
npx -y @modelcontextprotocol/server-github
# 2. 配置OpenClaw连接
# openclaw.yaml
mcp:
servers:
- name: "github"
command: "npx -y @modelcontextprotocol/server-github"
env:
GITHUB_TOKEN: "ghp_xxx"
# 3. Agent启动时,自动发现MCP工具
# 工具列表:github_create_issue, github_list_issues, ...
妙趣AI通过MCP接入飞书工具:
# Feishu MCP Server(假设)
# 提供工具:
# - feishu_send_message: 发送飞书消息
# - feishu_create_doc: 创建飞书文档
# - feishu_list_chats: 列出飞书群聊
# OpenClaw配置
mcp:
servers:
- name: "feishu"
command: "node /path/to/feishu-mcp-server.js"
env:
FEISHU_APP_ID: "cli_xxx"
FEISHU_APP_SECRET: "xxx"
# Agent使用
# 用户:"发消息到飞书群"
# Agent自动匹配 feishu_send_message 工具并执行
为妙趣AI营销工具创建MCP Server:
// marketing-mcp-server.js
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
const server = new Server({
name: 'marketing-tools',
version: '1.0.0'
}, {
capabilities: { tools: {} }
});
// 注册工具
server.setRequestHandler('tools/list', async () => ({
tools: [
{
name: 'generate_ad_copy',
description: 'Generate ad copy with AI',
inputSchema: { ... }
},
{
name: 'analyze_competitor',
description: 'Analyze competitor marketing strategy',
inputSchema: { ... }
}
]
}));
// 处理工具调用
server.setRequestHandler('tools/call', async (request) => {
if (request.params.name === 'generate_ad_copy') {
const result = await generateAdCopy(request.params.arguments);
return { content: [{ type: 'text', text: result }] };
}
});
// 启动Server
const transport = new StdioServerTransport();
await server.connect(transport);
// 简易MCP Server(Node.js)
const readline = require('readline');
// 工具定义
const tools = [
{
name: 'echo',
description: 'Echo the input text',
inputSchema: {
type: 'object',
properties: {
text: { type: 'string' }
},
required: ['text']
}
}
];
// 处理JSON-RPC请求
function handleRequest(request) {
if (request.method === 'tools/list') {
return { tools };
}
if (request.method === 'tools/call') {
const { name, arguments: args } = request.params;
if (name === 'echo') {
return {
content: [{ type: 'text', text: args.text }]
};
}
}
return { error: 'Method not found' };
}
// 从stdin读取请求
const rl = readline.createInterface({ input: process.stdin });
rl.on('line', (line) => {
const request = JSON.parse(line);
const response = handleRequest(request);
console.log(JSON.stringify(response));
});
// Agent端调用MCP工具
async function callMCPTool(serverUrl, toolName, args) {
// 1. 发送 tools/call 请求
const response = await fetch(`${serverUrl}/tools/call`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
method: 'tools/call',
params: {
name: toolName,
arguments: args
}
})
});
const result = await response.json();
return result.result?.content?.[0]?.text;
}
// 使用
const output = await callMCPTool(
'http://localhost:3000',
'github_create_issue',
{ repo: 'openclaw/openclaw', title: 'New feature request' }
);
# openclaw.yaml
mcp:
enabled: true
servers:
# GitHub MCP Server
- name: github
transport: stdio
command: npx
args: ["-y", "@modelcontextprotocol/server-github"]
env:
GITHUB_TOKEN: ${GITHUB_TOKEN}
# 自定义营销工具Server
- name: marketing
transport: stdio
command: node
args: ["/path/to/marketing-mcp-server.js"]
# 远程MCP Server(HTTP)
- name: remote-tools
transport: http
url: https://mcp.example.com
✅ DO(推荐做法)
⚠️ DON'T(常见坑)
| 方式 | 优点 | 缺点 |
|---|---|---|
| OpenClaw Skills | 原生支持、热加载 | 需要本地文件 |
| MCP Server | 标准化、跨平台、远程 | 需要启动Server |
| Function Calling | 简单、直接 | 非标准化、各家不同 |
| API直接调用 | 灵活 | 需要手动编写调用代码 |
就像《唐伯虎点秋香》里的"江南四大才子"——每个人都有自己的绝活,组在一起就是梦之队。MCP Server就是"才子聚集地",把各种工具集中管理,Agent需要什么就拿什么。记住:好的MCP Server,让Agent从"工具人"进化成"全能王"!