来源:code.claude.com/docs/en/mcp · modelcontextprotocol.io | 整理时间:2026-04-04
什么是 MCP?
Model Context Protocol(MCP) 是 Anthropic 发起的开放协议,让 LLM 能够以标准化方式连接外部工具和数据源。
类比:MCP之于 AI,就像 USB 之于电脑——一个标准接口,连接任意设备。
Claude Code ←→ MCP 客户端 ←→ MCP 服务器 ←→ 外部工具/数据
├─ 数据库
├─ 搜索引擎
├─ 浏览器
├─ GitHub
└─ 自定义 API
配置 MCP 服务器
方式一:命令行添加
# 添加一个 MCP 服务器
claude mcp add <name> <command> [args...]
# 示例:添加文件系统服务器
claude mcp add filesystem npx -y @modelcontextprotocol/server-filesystem /path/to/dir
# 示例:添加 Brave Search
claude mcp add brave-search npx -y @modelcontextprotocol/server-brave-search
方式二:配置文件
在项目根目录创建 .mcp.json:
{
"mcpServers": {
"filesystem": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-filesystem", "/path/to/dir"]
},
"brave-search": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-brave-search"],
"env": {
"BRAVE_API_KEY": "your-api-key"
}
}
}
}
团队最佳实践:把
.mcp.json提交到 Git,团队成员共享相同的工具集。
常用 MCP 服务器
| 服务器 | 功能 | 安装命令 |
|---|---|---|
| Brave Search | 网页搜索 | npx -y @modelcontextprotocol/server-brave-search |
| GitHub | 仓库、Issue、PR 管理 | npx -y @modelcontextprotocol/server-github |
| 文件系统 | 读写本地文件 | npx -y @modelcontextprotocol/server-filesystem /path |
| PostgreSQL | 数据库查询 | npx -y @modelcontextprotocol/server-postgres |
| SQLite | 轻量数据库 | npx -y @modelcontextprotocol/server-sqlite |
| Playwright | 浏览器自动化 | npx -y @anthropic-ai/mcp-server-playwright |
| Fetch | 抓取网页内容 | npx -y @modelcontextprotocol/server-fetch |
| Memory | 持久化知识图谱 | npx -y @modelcontextprotocol/server-memory |
| Google Drive | 访问 Google 文件 | npx -y @modelcontextprotocol/server-gdrive |
| Slack | 发送/读取消息 | npx -y @modelcontextprotocol/server-slack |
完整列表:https://github.com/modelcontextprotocol/servers
使用场景
场景 1:搜索增强开发
配置 Brave Search 后,Claude 可以直接搜索技术文档:
搜索 React 19 的最新文档,了解 use() hook 的用法
Claude 会调用 Brave Search 工具获取实时结果,不再局限于训练数据。
场景 2:数据库操作
配置 PostgreSQL 后:
查看 users 表的 schema,找出没有 email 的用户数量
Claude 直接执行 SQL 查询并返回结果。
场景 3:浏览器自动化
配置 Playwright 后:
打开 localhost:3000,截图首页,检查所有链接是否可访问
Claude 可以控制浏览器,进行视觉测试和 E2E 验证。
场景 4:GitHub 工作流
配置 GitHub 后:
列出本周所有合并的 PR,生成周报
Claude 通过 GitHub API 获取数据并格式化输出。
构建自定义 MCP 服务器
TypeScript 快速入门
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new McpServer({
name: "my-custom-server",
version: "1.0.0",
});
// 注册一个工具
server.tool(
"query_inventory", // 工具名称
"Query product inventory", // 描述
{ product_id: z.string() }, // 参数 schema
async ({ product_id }) => {
const result = await db.query(
"SELECT * FROM inventory WHERE id = $1",
[product_id]
);
return { content: [{ type: "text", text: JSON.stringify(result) }] };
}
);
// 启动
const transport = new StdioServerTransport();
await server.connect(transport);
注册到 Claude Code
claude mcp add my-api node /path/to/my-server/index.ts
Python SDK
from mcp.server import Server
from mcp.types import Tool, TextContent
server = Server("my-custom-server")
@server.tool("query_inventory")
async def query_inventory(product_id: str) -> list[TextContent]:
result = await db.fetchrow(
"SELECT * FROM inventory WHERE id = $1", product_id
)
return [TextContent(type="text", text=str(result))]
MCP 协议核心概念
| 概念 | 说明 | 类比 |
|---|---|---|
| Tool | 模型可执行的函数 | API endpoint |
| Resource | 应用提供的上下文数据 | GET 请求 |
| Prompt | 用户可调用的模板 | 快捷指令 |
| Sampling | 服务器向 LLM 发送请求 | 回调 |
安全注意事项
| 风险 | 防护措施 |
|---|---|
| 数据泄露 | 限制 MCP 服务器访问的数据范围 |
| 未授权操作 | 使用 --permission-mode 控制 |
| API Key 暴露 | 通过环境变量传递,不要硬编码 |
| 供应链攻击 | 只使用信任的 MCP 服务器 |
实际集成模式
团队共享配置
// .mcp.json(提交到 Git)
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_TOKEN": "${GITHUB_TOKEN}"
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "${DATABASE_URL}"]
}
}
}
成员在本地设置环境变量即可使用,无需修改配置文件。
响应外部事件
MCP 服务器可以响应来自 Telegram、Discord、Webhook 的外部事件:
当收到新的 Slack 消息时 → 触发 Claude Code 分析并回复
当 GitHub Issue 创建时 → 自动分析并建议解决方案