通过 MCP 连接工具

通过 MCP 将 Claude Code 连接到工具

了解如何使用 Model Context Protocol 将 Claude Code 连接到您的工具。

使用 MCP 可以做什么

连接 MCP servers 后,您可以要求 Claude Code:

  • 从问题跟踪器实现功能:”添加 JIRA 问题 ENG-4521 中描述的功能,并在 GitHub 上创建 PR。”
  • 分析监控数据:”检查 Sentry 和 Statsig 以检查 ENG-4521 中描述的功能的使用情况。”
  • 查询数据库:”根据我们的 PostgreSQL 数据库,找到 10 个使用功能 ENG-4521 的随机用户的电子邮件。”
  • 集成设计:”根据在 Slack 中发布的新 Figma 设计更新我们的标准电子邮件模板”
  • 自动化工作流:”创建 Gmail 草稿,邀请这 10 个用户参加关于新功能的反馈会议。”

安装 MCP servers

选项 1:添加远程 HTTP server

1
2
3
4
5
6
7
8
9
# 基本语法
claude mcp add --transport http <name> <url>

# 真实示例:连接到 Notion
claude mcp add --transport http notion https://mcp.notion.com/mcp

# 带有 Bearer token 的示例
claude mcp add --transport http secure-api https://api.example.com/mcp \
--header "Authorization: Bearer your-token"

选项 2:添加远程 SSE server

1
2
3
4
5
# 基本语法
claude mcp add --transport sse <name> <url>

# 真实示例:连接到 Asana
claude mcp add --transport sse asana https://mcp.asana.com/sse

选项 3:添加本地 stdio server

1
2
3
4
5
6
# 基本语法
claude mcp add [options] <name> -- <command> [args...]

# 真实示例:添加 Airtable server
claude mcp add --transport stdio --env AIRTABLE_API_KEY=YOUR_KEY airtable \
-- npx -y airtable-mcp-server

重要:选项顺序

所有选项(--transport--env--scope--header)必须在 server 名称之前。然后 -- (双破折号) 将 server 名称与传递给 MCP server 的命令和参数分开。

管理您的 servers

1
2
3
4
5
6
7
8
9
10
11
# 列出所有配置的 servers
claude mcp list

# 获取特定 server 的详细信息
claude mcp get github

# 删除 server
claude mcp remove github

# (在 Claude Code 中) 检查 server 状态
/mcp

MCP 安装范围

本地范围

1
2
3
4
5
# 添加本地范围的 server (默认)
claude mcp add --transport http stripe https://mcp.stripe.com

# 显式指定本地范围
claude mcp add --transport http stripe --scope local https://mcp.stripe.com

项目范围

1
2
# 添加项目范围的 server
claude mcp add --transport http paypal --scope project https://mcp.paypal.com/mcp

生成的 .mcp.json 文件:

1
2
3
4
5
6
7
8
9
{
"mcpServers": {
"shared-server": {
"command": "/path/to/server",
"args": [],
"env": {}
}
}
}

用户范围

1
2
# 添加用户 server
claude mcp add --transport http hubspot --scope user https://mcp.hubspot.com/anthropic

选择正确的范围

  • 本地范围:个人 servers、实验配置或特定于一个项目的敏感凭据
  • 项目范围:团队共享的 servers、项目特定的工具或协作所需的服务
  • 用户范围:跨多个项目需要的个人实用程序、开发工具或经常使用的服务

实际示例

连接到 Sentry 监控错误

1
2
3
4
5
6
7
8
9
10
# 1. 添加 Sentry MCP server
claude mcp add --transport http sentry https://mcp.sentry.dev/mcp

# 2. 使用 /mcp 对您的 Sentry 帐户进行身份验证
/mcp

# 3. 调试生产问题
"What are the most common errors in the last 24 hours?"
"Show me the stack trace for error ID abc123"
"Which deployment introduced these new errors?"

连接到 GitHub 进行代码审查

1
2
3
4
5
6
7
8
9
10
# 1. 添加 GitHub MCP server
claude mcp add --transport http github https://api.githubcopilot.com/mcp/

# 2. 在 Claude Code 中进行身份验证
/mcp

# 3. 现在您可以要求 Claude 使用 GitHub
"Review PR #456 and suggest improvements"
"Create a new issue for the bug we just found"
"Show me all open PRs assigned to me"

查询 PostgreSQL 数据库

1
2
3
4
5
6
7
8
# 1. 使用您的连接字符串添加数据库 server
claude mcp add --transport stdio db -- npx -y @bytebase/dbhub \
--dsn "postgresql://readonly:[email protected]:5432/analytics"

# 2. 自然地查询您的数据库
"What's our total revenue this month?"
"Show me the schema for the orders table"
"Find customers who haven't made a purchase in 90 days"

使用远程 MCP servers 进行身份验证

OAuth 2.0 身份验证

  1. 添加需要身份验证的 server:

    1
    claude mcp add --transport http sentry https://mcp.sentry.dev/mcp
  2. 在 Claude Code 中使用 /mcp 命令:

    1
    /mcp
  3. 按照浏览器中的步骤登录。

使用预配置的 OAuth 凭据

1
2
3
claude mcp add --transport http \
--client-id your-client-id --client-secret --callback-port 8080 \
my-server https://mcp.example.com/mcp

从 JSON 配置添加 MCP servers

1
2
3
4
5
# 从 JSON 添加 HTTP server
claude mcp add-json weather-api '{"type":"http","url":"https://api.weather.com/mcp","headers":{"Authorization":"Bearer token"}}'

# 从 JSON 添加 stdio server
claude mcp add-json local-weather '{"type":"stdio","command":"/path/to/weather-cli","args":["--api-key","abc123"],"env":{"CACHE_DIR":"/tmp"}}'

从 Claude Desktop 导入 MCP servers

1
2
# 从 Claude Desktop 导入 servers
claude mcp add-from-claude-desktop

使用 MCP 资源

引用 MCP 资源

使用格式 @server:protocol://resource/path 引用资源:

1
2
3
4
5
> Can you analyze @github:issue://123 and suggest a fix?

> Please review the API documentation at @docs:file://api/authentication

> Compare @postgres:schema://users with @docs:file://database/user-model

使用 MCP Tool Search 进行扩展

当您的 MCP 工具描述会消耗超过 10% 的上下文窗口时,Claude Code 会自动启用 Tool Search。

使用 ENABLE_TOOL_SEARCH 环境变量:

行为
auto 当 MCP 工具超过 10% 的上下文时激活 (默认)
auto:<N> 在自定义阈值激活,其中 <N> 是百分比
true 始终启用
false 禁用,所有 MCP 工具预先加载
1
2
3
4
5
# 使用自定义 5% 阈值
ENABLE_TOOL_SEARCH=auto:5 claude

# 完全禁用 tool search
ENABLE_TOOL_SEARCH=false claude

使用 MCP 提示作为命令

MCP servers 可以公开在 Claude Code 中作为命令可用的提示:

1
2
3
4
5
> /mcp__github__list_prs

> /mcp__github__pr_review 456

> /mcp__jira__create_issue "Bug in login flow" high

托管 MCP 配置

选项 1:使用 managed-mcp.json 的独占控制

部署 managed-mcp.json 文件到系统目录:

  • macOS:/Library/Application Support/ClaudeCode/managed-mcp.json
  • Linux/WSL:/etc/claude-code/managed-mcp.json
  • Windows:C:\Program Files\ClaudeCode\managed-mcp.json

选项 2:使用允许列表和拒绝列表

1
2
3
4
5
6
7
8
9
10
{
"allowedMcpServers": [
{ "serverName": "github" },
{ "serverName": "sentry" },
{ "serverUrl": "https://mcp.company.com/*" }
],
"deniedMcpServers": [
{ "serverName": "dangerous-server" }
]
}

本文档来自 Claude Code 官方文档

© 2026 Generative AI Discovery All Rights Reserved.
Theme by hiero