
2024 年底 Anthropic 开源 MCP 协议时,很多人还没意识到这意味着什么。一年半后,MCP 已成为 AI Agent 生态的"USB 接口"——任何工具只需实现一次 MCP Server,就能被 Claude、Cursor、OpenClaw 等所有 Agent 框架调用。
MCP(Model Context Protocol)是 Anthropic 发布的一个开放协议,用来标准化 AI 模型与外部工具之间的通信。类比:
在 MCP 出现之前,每接入一个工具都要写适配代码——DeepSeek 的 function calling 格式跟 OpenAI 不一样,数据库工具跟文件系统工具各写一套。MCP 解决了这个问题。
┌──────────────┐ JSON-RPC 2.0 ┌──────────────┐
│ MCP Client │ ◄──────────────────────► │ MCP Server │
│ (AI Agent) │ stdio / HTTP SSE │ (工具/数据) │
└──────────────┘ └──────────────┘
│
┌────────┴────────┐
│ Resources │ ← 数据(文件、DB、API)
│ Tools │ ← 可执行操作
│ Prompts │ ← 预置提示模板
└─────────────────┘概念 | 说明 | 举例 |
|---|---|---|
Resources | Agent 可读取的数据 | 文件内容、数据库查询结果、API 返回 |
Tools | Agent 可调用的操作 | 创建文件、发送消息、执行命令 |
Prompts | 预定义的提示模板 | "帮我审查这段代码的安全性" |
Transports | 通信方式 | stdio(本地进程)、HTTP SSE(远程服务) |
用 @modelcontextprotocol/sdk 十分钟写出一个天气查询工具:
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const server = new McpServer({
name: "weather-server",
version: "1.0.0",
});
// 注册一个 Tool
server.tool(
"get_weather",
"获取指定城市的实时天气",
{ city: z.string().describe("城市名称,如 Beijing") },
async ({ city }) => {
const temp = Math.round(Math.random() * 20 + 10);
return {
content: [{ type: "text", text: `${city} 当前温度 ${temp}°C,晴` }],
};
}
);
const transport = new StdioServerTransport();
await server.connect(transport);这个 Server 启动后,任何支持 MCP 的 Client 都能自动发现并调用 get_weather。
以 OpenClaw 为例,在 openclaw.json 的 mcpServers 里加一条:
{
"mcpServers": {
"weather": {
"command": "node",
"args": ["/path/to/weather-server/index.js"]
}
}
}重启后 Agent 自动拥有天气查询能力。不需要改一行 Agent 代码。
本地 stdio 适合个人工具,HTTP SSE 适合团队共享:
import express from "express";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { SSEServerTransport } from "@modelcontextprotocol/sdk/server/sse.js";
const app = express();
const server = new McpServer({ name: "team-tools", version: "1.0.0" });
server.tool("deploy_to_server", "部署项目到服务器",
{ branch: z.string() },
async ({ branch }) => {
// 执行部署逻辑
return { content: [{ type: "text", text: `已部署 ${branch} 分支 ` }] };
}
);
app.post("/mcp", async (req, res) => {
const transport = new SSEServerTransport("/mcp", res);
await server.connect(transport);
});
app.listen(3100);团队其他成员只需在 Client 配置里写上 url: "http://your-server:3100/mcp",立即可用。
经过一年半的快速发展,MCP 生态已经相当成熟:
类别 | 代表项目 |
|---|---|
AI 编程 | Cursor、Windsurf、AtomCode |
Agent 框架 | OpenClaw、LangChain、CrewAI |
数据库 | MCP Server for PostgreSQL / MySQL / SQLite |
文件系统 | filesystem MCP Server(官方的) |
云服务 | Cloudflare、Supabase、Vercel MCP |
搜索引擎 | Brave Search、Tavily MCP |
浏览器 | Playwright MCP(浏览器自动化) |
通信 | Slack、Discord、飞书 MCP |
MCP 正在做 AI Agent 生态当年 HTTP 对 Web 做的事——标准化连接。不管你是:
MCP 都是你现在就应该了解并开始用的协议。别人还在手写适配代码的时候,你已经用标准化接口跑通了整个工具链。
本文为作者原创,未经授权禁止转载、洗稿、搬运。如需引用请保留原文链接。