Skip to content

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
githubGitHub API(issue/PR/repo)@modelcontextprotocol/server-github
gitlabGitLab API@modelcontextprotocol/server-gitlab
gitGit 仓库元信息@modelcontextprotocol/server-git
postgresPostgreSQL 查询@modelcontextprotocol/server-postgres
sqliteSQLite 查询@modelcontextprotocol/server-sqlite
brave-searchBrave 搜索@modelcontextprotocol/server-brave-search
slackSlack 频道/消息@modelcontextprotocol/server-slack
google-driveGoogle Drive 文件@modelcontextprotocol/server-google-drive
memory持久记忆(KG)@modelcontextprotocol/server-memory
fetchHTTP fetch + markdown 转换@modelcontextprotocol/server-fetch
puppeteer浏览器自动化@modelcontextprotocol/server-puppeteer

社区 MCP(精选)

MCP用途
mcp-server-notionNotion 数据库读写
mcp-server-linearLinear issue 管理
mcp-server-jiraJira 工单
mcp-server-figmaFigma 设计稿
mcp-server-chromeChrome 浏览器控制
mcp-server-supabaseSupabase 数据库
mcp-server-stripeStripe 支付

完整目录:MCP Servers GitHub

实战例子

例 1:连数据库

json
{
  "mcpServers": {
    "mydb": {
      "command": "npx",
      "args": [
        "-y",
        "@modelcontextprotocol/server-postgres",
        "postgresql://localhost/myapp"
      ]
    }
  }
}

然后你可以问 Claude:

"看一下 users 表里上周注册的用户数"

Claude 会:

  1. 通过 MCP 连数据库
  2. 自动生成 SQL
  3. 执行
  4. 回答你

例 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。

最佳实践:

  1. 数据库:连只读副本 + 限定 user 权限
  2. GitHub:用细粒度 PAT,只授权特定 repo 的特定权限
  3. 文件系统:限定到项目目录,不要给 ~
  4. 生产系统别接

MCP vs Hook vs Skill

MCPHookSkill
目的提供数据 / 能力强制规则复用工作流
触发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 装。

资源

内容采用 CC BY-NC-ND 4.0 协议 · 禁止商业使用 / 禁止改编 / 禁止训练 AI 模型