MCP(Model Context Protocol)是 Anthropic 在 2024 年 11 月发布的开放标准,解决了一个根本问题:AI Agent 如何以统一的方式连接外部工具和数据源。
发布不到 18 个月,OpenAI、Microsoft、Google 已全部采用,成为行业事实标准。理解 MCP,是理解未来 AI 应用架构的基础。
解决什么问题
MCP 之前:每个集成都是定制的
Claude ──── 自定义代码 ──── GitHub API
Claude ──── 自定义代码 ──── Slack API
Claude ──── 自定义代码 ──── Google Drive API
Claude ──── 自定义代码 ──── 数据库
每对"AI + 工具"的组合都需要从头写集成代码。有 10 个工具就要维护 10 套不同的代码。
MCP 之后:一个协议,通用连接
Claude ──── MCP 协议 ──── GitHub MCP Server
──── Slack MCP Server
──── Google Drive MCP Server
──── 数据库 MCP Server
实现一次 MCP 客户端,解锁整个 MCP 生态(社区已有数千个 MCP Server)。
MCP 的核心概念
三个角色
MCP Host(宿主):运行 AI 模型的应用,比如 Claude Code、Claude Desktop、自己写的 Agent
MCP Client(客户端):Host 内部维护与 MCP Server 连接的组件
MCP Server(服务器):暴露工具、数据、提示词的服务,可以是本地进程或远程服务
三种能力
Tools(工具):AI 可以调用的函数
github.createIssue(title, body, labels)
slack.sendMessage(channel, text)
database.query(sql)
Resources(资源):AI 可以读取的数据(文件、数据库记录、API 响应)
file://project/src/main.ts
database://users/123
api://github/repos/owner/repo
Prompts(提示词模板):可复用的提示词,由 Server 提供
/code-review: 按照团队规范审查代码
/explain-error: 解释这个错误并给出修复建议
传输方式:Stdio vs HTTP
Stdio(本地进程):
- MCP Server 作为子进程在本地运行
- Host 通过 stdin/stdout 与 Server 通信
- 适用:本地工具(文件系统、本地数据库、Shell 命令)
- 安全:进程隔离,不经过网络
HTTP + SSE(远程服务):
- MCP Server 作为 HTTP 服务运行(本地或远程)
- 使用 Server-Sent Events 推送结果
- 适用:Web API、云服务、跨机器访问
- 灵活:可以远程部署,多个 Client 共享
在 Claude Code 里使用 MCP
查看已连接的 MCP Server
claude mcp list添加 MCP Server(三种方式)
方式 1:本地 Stdio Server
# 添加文件系统 MCP Server
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem /path/to/project方式 2:通过配置文件(推荐,项目共享)
在项目根目录创建 .claude/mcp.json:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/project"]
},
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "${GITHUB_TOKEN}"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "${DATABASE_URL}"]
}
}
}方式 3:远程 HTTP Server
claude mcp add remote-server --url https://mcp.example.com/sse快速上手:安装最常用的 MCP Server
GitHub(最常用)
# 安装
npm install -g @modelcontextprotocol/server-github
# 配置(需要 GitHub Personal Access Token)
claude mcp add github -- npx -y @modelcontextprotocol/server-github
# 设置环境变量
export GITHUB_PERSONAL_ACCESS_TOKEN=ghp_xxx之后可以对 Claude Code 说:
查看 openclaw/openclaw 仓库最近的 Issues
给 PR #234 添加评论
Filesystem(读写本地文件)
claude mcp add filesystem -- npx -y @modelcontextprotocol/server-filesystem ~/projects让 Claude Code 直接访问指定目录的文件,不需要手动 paste 文件内容。
PostgreSQL
claude mcp add postgres -- npx -y @modelcontextprotocol/server-postgres postgresql://localhost/mydbClaude Code 可以直接查询数据库:
查看 users 表的 Schema
执行这个查询:SELECT * FROM orders WHERE status = 'pending' LIMIT 10
Brave Search(网络搜索)
claude mcp add brave-search -- npx -y @modelcontextprotocol/server-brave-search
# 需要 Brave Search API Key
export BRAVE_API_KEY=xxx自己构建 MCP Server:5 步入门
用 TypeScript SDK 构建
Step 1:安装依赖
npm init -y
npm install @modelcontextprotocol/sdk zod
npm install -D typescript @types/nodeStep 2:创建 Server
// src/index.ts
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: "my-custom-server",
version: "1.0.0",
});Step 3:注册工具
// 注册一个"查询公司内部 API"的工具
server.tool(
"get-employee-info",
"根据员工 ID 查询员工信息",
{
employeeId: z.string().describe("员工 ID"),
},
async ({ employeeId }) => {
// 调用你的内部 API
const response = await fetch(
`https://internal-api.company.com/employees/${employeeId}`,
{ headers: { Authorization: `Bearer ${process.env.INTERNAL_API_TOKEN}` } }
);
const data = await response.json();
return {
content: [
{
type: "text",
text: JSON.stringify(data, null, 2),
},
],
};
}
);Step 4:注册资源(可选)
// 暴露内部文档作为资源
server.resource(
"company-handbook",
"company://handbook",
async (uri) => {
const content = await fs.readFile('./docs/handbook.md', 'utf-8');
return {
contents: [{ uri: uri.href, text: content }],
};
}
);Step 5:启动 Server
// 连接 Stdio 传输(本地使用)
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("MCP Server 已启动"); // 用 stderr 输出,不干扰 stdoutStep 6:在 Claude Code 里添加
# 编译
npx tsc
# 添加到 Claude Code
claude mcp add my-company-server -- node /path/to/dist/index.js生产级 MCP:Anthropic 发现的关键优化
Anthropic 工程博客揭示:当连接大量 MCP Server 时,传统的"全部加载"方式会导致 Token 爆炸。
解决方法:把工具暴露为文件系统路径,Agent 按需加载:
servers/
├── google-drive/getDocument.ts
├── salesforce/updateRecord.ts
└── slack/sendMessage.ts
实测从 150,000 Token 降到 2,000 Token(减少 98.7%)。
详见:[MCP 代码执行模式深度解析]
常用 MCP Server 资源
官方维护:
@modelcontextprotocol/server-filesystem:文件系统@modelcontextprotocol/server-github:GitHub@modelcontextprotocol/server-postgres:PostgreSQL@modelcontextprotocol/server-brave-search:网络搜索
社区热门:
- Notion MCP:访问 Notion 工作区
- Linear MCP:Linear 项目管理
- Obsidian MCP:Obsidian 笔记库
- Stripe MCP:Stripe 支付数据
查找更多:
来源:modelcontextprotocol.io 官方文档 | hypernestlabs.com MCP 完整指南 | Anthropic 工程博客 | 整理:ClaudeEagle