主题
MCP 模型上下文协议
Model Context Protocol(MCP)是让 Claude 连接外部数据/能力的标准协议。可以让 Claude 直接查数据库、读 GitHub issue、调任意 API。
一句话
MCP = 让 Claude 用真实数据,而不是凭空猜。
适用场景
| 场景 | 用 MCP | 别用 |
|---|---|---|
| 让 Claude 查你的数据库 | ✅ | |
| 让 Claude 看 GitHub issue 上下文 | ✅ | |
| 让 Claude 控制浏览器 | ✅ | |
| 让 Claude 操作 Notion / Linear / Jira | ✅ | |
| 让 Claude 算法实现 | ❌ | 内置工具够了 |
| 简单文件操作 | ❌ | Bash / Edit 工具够 |
工作原理
你: "帮我看看 #234 issue 的最新评论是什么"
↓
Claude 通过 MCP 调 GitHub server
↓
GitHub MCP server: gh api ... → 返回评论
↓
Claude 拿到真实数据回答你配置位置
~/.claude/mcp.json 或 .claude/mcp.json(项目级):
json
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_..."
}
},
"postgres": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://user:pass@localhost/mydb"]
}
}
}常用 MCP 服务器
官方 MCP(Anthropic 维护)
| MCP | 用途 | 包名 |
|---|---|---|
| filesystem | 受限文件系统访问 | @modelcontextprotocol/server-filesystem |
| github | GitHub API(issue/PR/repo) | @modelcontextprotocol/server-github |
| gitlab | GitLab API | @modelcontextprotocol/server-gitlab |
| git | Git 仓库元信息 | @modelcontextprotocol/server-git |
| postgres | PostgreSQL 查询 | @modelcontextprotocol/server-postgres |
| sqlite | SQLite 查询 | @modelcontextprotocol/server-sqlite |
| brave-search | Brave 搜索 | @modelcontextprotocol/server-brave-search |
| slack | Slack 频道/消息 | @modelcontextprotocol/server-slack |
| google-drive | Google Drive 文件 | @modelcontextprotocol/server-google-drive |
| memory | 持久记忆(KG) | @modelcontextprotocol/server-memory |
| fetch | HTTP fetch + markdown 转换 | @modelcontextprotocol/server-fetch |
| puppeteer | 浏览器自动化 | @modelcontextprotocol/server-puppeteer |
社区 MCP(精选)
| MCP | 用途 |
|---|---|
mcp-server-notion | Notion 数据库读写 |
mcp-server-linear | Linear issue 管理 |
mcp-server-jira | Jira 工单 |
mcp-server-figma | Figma 设计稿 |
mcp-server-chrome | Chrome 浏览器控制 |
mcp-server-supabase | Supabase 数据库 |
mcp-server-stripe | Stripe 支付 |
完整目录:MCP Servers GitHub。
实战例子
例 1:连数据库
json
{
"mcpServers": {
"mydb": {
"command": "npx",
"args": [
"-y",
"@modelcontextprotocol/server-postgres",
"postgresql://localhost/myapp"
]
}
}
}然后你可以问 Claude:
"看一下 users 表里上周注册的用户数"
Claude 会:
- 通过 MCP 连数据库
- 自动生成 SQL
- 执行
- 回答你
例 2:GitHub 集成
json
{
"mcpServers": {
"github": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-github"],
"env": {
"GITHUB_PERSONAL_ACCESS_TOKEN": "ghp_xxx"
}
}
}
}可以问:
"看一下我项目里 'good first issue' 标签的 open issue,按时间排序"
例 3:自动化浏览器
json
{
"mcpServers": {
"puppeteer": {
"command": "npx",
"args": ["-y", "@modelcontextprotocol/server-puppeteer"]
}
}
}可以问:
"打开 example.com,截图首页,提取所有 H1"
验证 MCP 加载
/mcp # 列出所有 MCP server 状态启动 claude 时也会输出加载日志:
✅ MCP server "github" loaded (12 tools)
✅ MCP server "postgres" loaded (8 tools)安全提醒
MCP = 给 Claude 真实权限
- 数据库 MCP 让 Claude 能跑任意 SQL(包括 DROP TABLE)
- GitHub MCP 让 Claude 能改 PR、关 issue
- 文件系统 MCP 让 Claude 能读写指定目录
只配你信任 Claude 操作的 server,敏感生产数据库不要直接接 MCP。
最佳实践:
- 数据库:连只读副本 + 限定 user 权限
- GitHub:用细粒度 PAT,只授权特定 repo 的特定权限
- 文件系统:限定到项目目录,不要给
~ - 生产系统:别接
MCP vs Hook vs Skill
| MCP | Hook | Skill | |
|---|---|---|---|
| 目的 | 提供数据 / 能力 | 强制规则 | 复用工作流 |
| 触发 | Claude 决定需要时 | 生命周期事件 | 任务匹配 |
| 输出 | 数据返给 Claude | 拦截 / 修改工具调用 | 指导 Claude |
| 例子 | 查数据库 | 阻止 .env 访问 | 代码审查工作流 |
通常组合使用:
任务:"看上周哪些用户报了 bug"
↓
Skill: bug-investigator 激活
↓
Subagent: 委派给 bug-hunter
↓
MCP: 查 GitHub issue + 查 PostgreSQL events 表
↓
Hook: 不允许 DELETE/UPDATE(只读保护)
↓
最终回答调试 MCP
bash
# 看 MCP server 日志
tail -f ~/.claude/logs/mcp-*.log
# 强制重启 MCP
/mcp restart
# 测单个工具
/mcp call github list_repos自己写 MCP server
参考 MCP SDK。最简模板:
typescript
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
const server = new Server({
name: "my-server",
version: "0.1.0"
}, {
capabilities: { tools: {} }
});
server.setRequestHandler({ method: "tools/list" }, () => ({
tools: [{
name: "hello",
description: "Say hello",
inputSchema: { type: "object", properties: { name: { type: "string" } } }
}]
}));
server.setRequestHandler({ method: "tools/call" }, (req) => ({
content: [{ type: "text", text: `Hello, ${req.params.arguments.name}!` }]
}));
await server.connect(new StdioServerTransport());发布到 npm 后用户就能 npx my-server 装。
