ep08-mcp-in-claude-code

Claude Code 101 系列教程 · 第 8 课

Claude (Anthropic)

2026-05-20

MCP in Claude Code
第 8 课 · MCP in Claude Code

MCP in Claude Code

Claude Code 101 系列教程 · 第 8 课 · 来自 Claude 官方频道


什么是 MCP?

MCP(Model Context Protocol) 是一个开源标准,让 Claude Code 能够连接外部工具和数据源。

没有 MCP,你需要手动把其他工具的数据复制粘贴到聊天中。有了 MCP,Claude Code 可以直接读取和操作这些系统:

核心思路: 如果你发现自己总是在某个工具和 Claude Code 之间复制粘贴,那就是该加一个 MCP Server 的时候了。


三种添加 MCP Server 的方式

1. HTTP Server(推荐)

适合云端服务,最广泛支持的传输方式:

claude mcp add --transport http notion https://mcp.notion.com/mcp

2. SSE Server

旧版远程协议,逐步被 HTTP 取代:

claude mcp add --transport sse asana https://mcp.asana.com/sse

3. Stdio Server(本地进程)

适合需要直接系统访问或自定义脚本的场景:

claude mcp add --transport stdio db \
  -- npx -y @bytebase/dbhub \
  --dsn "postgresql://readonly:pass@localhost:5432/mydb"

三种作用域(Scope)

作用域 加载范围 团队共享 存储位置
Local(默认) 仅当前项目 ~/.claude.json
Project 仅当前项目 是(通过版本控制) 项目根目录 .mcp.json
User 你的所有项目 ~/.claude.json

添加时指定作用域

# 项目级(提交到版本控制,团队共享)
claude mcp add --transport http github --scope project \
  https://api.githubcopilot.com/mcp/

# 用户级(跨项目可用,个人私有)
claude mcp add --transport http hubspot --scope user \
  https://mcp.hubspot.com/anthropic

优先级

Local > Project > User > Plugin > Claude.ai Connectors


管理 MCP Server

claude mcp list              # 列出所有已配置的 server
claude mcp get github        # 查看某个 server 的详情
claude mcp remove github     # 移除 server

在 Claude Code 内部运行 /mcp 可以查看所有 server 的连接状态和工具数量。


实战示例

连接 GitHub

claude mcp add --transport http github \
  https://api.githubcopilot.com/mcp/ \
  --header "Authorization: Bearer YOUR_GITHUB_PAT"

连接后你可以直接说:

Review PR #456 and suggest improvements
Create a new issue for the bug we just found

连接 Sentry(错误监控)

claude mcp add --transport http sentry https://mcp.sentry.dev/mcp

然后:

What are the most common errors in the last 24 hours?
Show me the stack trace for error ID abc123

连接数据库

claude mcp add --transport stdio db \
  -- npx -y @bytebase/dbhub \
  --dsn "postgresql://user:pass@host:5432/analytics"

然后:

What's our total revenue this month?
Show me the schema for the orders table

Tool Search — 上下文友好

MCP Server 默认采用 Tool Search(工具搜索) 机制:

这意味着你可以添加很多 MCP Server,而不必担心上下文窗口被占满。


从 Claude Desktop 导入

如果你已经在 Claude Desktop 中配置了 MCP Server,可以一键导入:

claude mcp add-from-claude-desktop

总结


本教程基于 Claude 官方视频 MCP in Claude Code 整理,属于 Claude Code 101 系列第 8 课。