Model Context Protocol — AI 领域的 "USB-C"
MCP(Model Context Protocol)是 Anthropic 在 2024 年 11 月发布的开源协议,旨在为 AI 模型提供连接外部数据源和工具的标准接口。就像 USB-C 统一了设备充电接口一样,MCP 正在统一 AI 与外部世界的连接方式。
统一的协议规范,一次实现到处可用
用户授权机制,AI 不能越权访问资源
100+ 开源 MCP Server,持续增长中
MIT 协议开源,任何人都可以使用和贡献
MCP 通信架构
发起请求的一方,如 Claude Desktop、Cursor、Claude Code 等
提供服务的一方,连接具体资源如文件系统、数据库等
AI 可以调用的函数,如"读取文件"、"执行搜索"
AI 可以访问的数据,如文件内容、数据库记录
预定义的提示词模板,简化常见任务
Server 向 Client 推送的实时更新
读写本地文件系统,支持文件搜索、创建、编辑、删除等操作。
与 GitHub 交互,管理仓库、Issue、PR,进行代码搜索等。
控制浏览器,执行网页自动化、截图、爬取等任务。
查询和操作 SQLite 数据库,执行 SQL 语句。
使用 Brave 搜索引擎进行网络搜索。
读取和发送 Gmail 邮件,管理邮箱。
Claude Desktop 的 MCP 配置文件(macOS: ~/Library/Application Support/Claude/claude_desktop_config.json):
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-filesystem",
"/Users/yourname/Documents"
]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "your_token_here"
}
},
"sqlite": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-sqlite",
"--db-path",
"/path/to/your/database.db"
]
}
}
}
Anthropic 官方桌面应用,原生支持 MCP
AI 代码编辑器,支持 MCP 连接外部工具
命令行 AI 助手,深度集成 MCP
VS Code 插件、Zed 等持续增加中
使用 TypeScript 开发简单的 MCP Server:
import { Server } from "@modelcontextprotocol/sdk/server";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio";
const server = new Server({
name: "my-mcp-server",
version: "1.0.0"
}, {
capabilities: {
tools: {}
}
});
// 注册工具
server.setRequestHandler("tools/list", async () => ({
tools: [{
name: "hello",
description: "Say hello to someone",
inputSchema: {
type: "object",
properties: {
name: { type: "string", description: "Name to greet" }
},
required: ["name"]
}
}]
}));
// 处理工具调用
server.setRequestHandler("tools/call", async (request) => {
if (request.params.name === "hello") {
const name = request.params.arguments.name;
return { content: [{ type: "text", text: `Hello, ${name}!` }] };
}
});
// 启动服务器
const transport = new StdioServerTransport();
server.connect(transport);