Skip to content

MCP(Model Context Protocol)完整参考指南

本文件夹包含 MCP 服务器配置和 Claude Code 使用的全面文档和示例。

目录

  1. 概览
  2. MCP 架构
  3. MCP 生态系统
  4. MCP 安装方式
  5. MCP 设置流程
  6. MCP 工具搜索
  7. 动态工具更新
  8. MCP Apps
  9. MCP Elicitation
  10. 工具描述和指令上限
  11. MCP Prompts 作为斜杠命令
  12. Server 去重
  13. 最近的生命周期修复
  14. MCP 资源通过 @ 提及
  15. MCP 作用域
  16. MCP 配置管理
  17. 可用 MCP Server 表
  18. 实战示例
  19. MCP vs Memory:决策矩阵
  20. 请求/响应模式
  21. 环境变量
  22. Claude 作为 MCP Server
  23. 受管 MCP 配置(企业)
  24. 插件提供的 MCP Servers
  25. Subagent 作用域 MCP
  26. MCP 输出限制
  27. 用代码执行解决上下文膨胀
  28. 最佳实践
  29. 安装说明
  30. 故障排查
  31. 相关概念
  32. 更多资源

概览

MCP(Model Context Protocol)是 Claude 访问外部工具、API 和实时数据源的标准化方式。与 Memory 不同,MCP 提供对变化数据的实时访问。

主要特性:

  • 实时访问外部服务
  • 实时数据同步
  • 可扩展架构
  • 安全认证
  • 基于工具的交互

MCP 架构

    graph TB
    A["Claude"]
    B["MCP Server"]
    C["外部服务"]

    A -->|请求: list_issues| B
    B -->|查询| C
    C -->|数据| B
    B -->|响应| A

    A -->|请求: create_issue| B
    B -->|操作| C
    C -->|结果| B
    B -->|响应| A

    style A fill:#e1f5fe,stroke:#333,color:#333
    style B fill:#f3e5f5,stroke:#333,color:#333
    style C fill:#e8f5e9,stroke:#333,color:#333
  

MCP 生态系统

    graph TB
    A["Claude"] -->|MCP| B["Filesystem<br/>MCP Server"]
    A -->|MCP| C["GitHub<br/>MCP Server"]
    A -->|MCP| D["Database<br/>MCP Server"]
    A -->|MCP| E["Slack<br/>MCP Server"]
    A -->|MCP| F["Google Docs<br/>MCP Server"]

    B -->|文件 I/O| G["本地文件"]
    C -->|API| H["GitHub 仓库"]
    D -->|查询| I["PostgreSQL/MySQL"]
    E -->|消息| J["Slack 工作区"]
    F -->|文档| K["Google Drive"]

    style A fill:#e1f5fe,stroke:#333,color:#333
    style B fill:#f3e5f5,stroke:#333,color:#333
    style C fill:#f3e5f5,stroke:#333,color:#333
    style D fill:#f3e5f5,stroke:#333,color:#333
    style E fill:#f3e5f5,stroke:#333,color:#333
    style F fill:#f3e5f5,stroke:#333,color:#333
    style G fill:#e8f5e9,stroke:#333,color:#333
    style H fill:#e8f5e9,stroke:#333,color:#333
    style I fill:#e8f5e9,stroke:#333,color:#333
    style J fill:#e8f5e9,stroke:#333,color:#333
    style K fill:#e8f5e9,stroke:#333,color:#333
  

MCP 安装方式

Claude Code 支持多种传输协议用于 MCP 服务器连接:

HTTP 传输(推荐)

# 基本 HTTP 连接
claude mcp add --transport http notion https://mcp.notion.com/mcp

# 带认证头的 HTTP
claude mcp add --transport http secure-api https://api.example.com/mcp \
  --header "Authorization: Bearer your-token"

Stdio 传输(本地)

用于本地运行的 MCP 服务器:

# 本地 Node.js 服务器
claude mcp add --transport stdio myserver -- npx @myorg/mcp-server

# 带环境变量
claude mcp add --transport stdio myserver --env KEY=value -- npx server

SSE 传输(已弃用)

Server-Sent Events 传输已被 http 取代,但仍受支持:

claude mcp add --transport sse legacy-server https://example.com/sse

Windows 特定说明

在原生 Windows(非 WSL)上,对 npx 命令使用 cmd /c

claude mcp add --transport stdio my-server -- cmd /c npx -y @some/package

OAuth 2.0 认证

Claude Code 支持对需要 OAuth 的 MCP 服务器进行 OAuth 2.0 认证。当连接到启用 OAuth 的服务器时,Claude Code 会处理整个认证流程:

# 连接到启用 OAuth 的 MCP 服务器(交互式流程)
claude mcp add --transport http my-service https://my-service.example.com/mcp

# 预配置 OAuth 凭据用于非交互式设置
claude mcp add --transport http my-service https://my-service.example.com/mcp \
  --client-id "your-client-id" \
  --client-secret "your-client-secret" \
  --callback-port 8080
功能 描述
交互式 OAuth 使用 /mcp 触发基于浏览器的 OAuth 流程
预配置 OAuth 客户端 为 Notion、Stripe 等常见服务内置 OAuth 客户端(v2.1.30+)
预配置凭据 --client-id--client-secret--callback-port 标志用于自动设置
令牌存储 令牌安全存储在系统钥匙串中
逐步认证 支持特权操作的逐步认证
发现缓存 OAuth 发现元数据被缓存以实现更快的重连
元数据覆盖 .mcp.json 中的 oauth.authServerMetadataUrl 用于覆盖默认的 OAuth 元数据发现

覆盖 OAuth 元数据发现

如果你的 MCP 服务器在标准 OAuth 元数据端点(/.well-known/oauth-authorization-server)上返回错误,但公开了可用的 OIDC 端点,你可以告诉 Claude Code 从特定 URL 获取 OAuth 元数据。在服务器配置的 oauth 对象中设置 authServerMetadataUrl

{
  "mcpServers": {
    "my-server": {
      "type": "http",
      "url": "https://mcp.example.com/mcp",
      "oauth": {
        "authServerMetadataUrl": "https://auth.example.com/.well-known/openid-configuration"
      }
    }
  }
}

URL 必须使用 https://。此选项需要 Claude Code v2.1.64 或更高版本。

Claude.ai MCP 连接器

在 Claude.ai 账户中配置的 MCP 服务器在 Claude Code 中自动可用。这意味着你通过 Claude.ai 网界面设置的任何 MCP 连接都将无需额外配置即可访问。

Claude.ai MCP 连接器也可在 --print 模式(v2.1.83+)中使用,支持非交互式和脚本化使用。

启动说明(v2.1.117+): 当同时配置了本地和 claude.ai MCP 服务器时,并发连接是默认行为(之前是串行的),在使用多个服务器时减少了启动延迟。

要在 Claude Code 中禁用 Claude.ai MCP 服务器,将 ENABLE_CLAUDEAI_MCP_SERVERS 环境变量设置为 false

ENABLE_CLAUDEAI_MCP_SERVERS=false claude

注意: 此功能仅对使用 Claude.ai 账户登录的用户可用。


MCP 设置流程

    sequenceDiagram
    participant User
    participant Claude as Claude Code
    participant Config as 配置文件
    participant Service as 外部服务

    User->>Claude: 输入 /mcp
    Claude->>Claude: 列出可用的 MCP 服务器
    Claude->>User: 显示选项
    User->>Claude: 选择 GitHub MCP
    Claude->>Config: 更新配置
    Config->>Claude: 激活连接
    Claude->>Service: 测试连接
    Service-->>Claude: 认证成功
    Claude->>User: ✅ MCP 已连接!
  

/mcp 命令

在会话中输入 /mcp 可列出已连接的服务器、触发 OAuth 流程和检查连接状态。

  • v2.1.121 起,MCP 在遇到瞬态错误时会重试初始连接最多 3 次。
  • v2.1.128 起,/mcp 显示每个已连接服务器的工具数量,并直观标记报告 0 个工具的服务器,以便一眼就能发现配置错误的服务器。

MCP 工具搜索

当 MCP 工具描述超过上下文窗口的 10% 时,Claude Code 会自动启用工具搜索,以高效选择正确的工具,而不会让模型上下文不堪重负。

设置 描述
ENABLE_TOOL_SEARCH auto(默认) 当工具描述超过上下文的 10% 时自动启用
ENABLE_TOOL_SEARCH auto:<N> 在自定义阈值 N 个工具时自动启用
ENABLE_TOOL_SEARCH true 无论工具数量多少都始终启用
ENABLE_TOOL_SEARCH false 禁用;所有工具描述都完整发送

注意: 工具搜索需要 Sonnet 4 或更高版本,或 Opus 4 或更高版本。Haiku 模型不支持工具搜索。

按服务器绕过工具搜索(v2.1.121+)

如果某个特定 MCP 服务器的工具在每个回合都需要,可以将其配置标记为 "alwaysLoad": true 以跳过工具搜索延迟并使其工具始终可用:

{
  "mcpServers": {
    "always-on-tool": {
      "command": "node",
      "args": ["./tools/always.js"],
      "alwaysLoad": true
    }
  }
}

谨慎使用——每个始终加载的工具都会消耗可用于工具搜索以显示更相关工具的上下文。


动态工具更新

Claude Code 支持 MCP list_changed 通知。当 MCP 服务器动态添加、移除或修改其可用工具时,Claude Code 会接收更新并自动调整其工具列表——无需重新连接或重启。


MCP Apps

MCP Apps 是第一个官方 MCP 扩展,使 MCP 工具调用能够返回直接在聊天界面中渲染的交互式 UI 组件。MCP 服务器可以提供丰富的仪表板、表单、数据可视化和多步骤工作流——全部内联显示,无需离开对话。


MCP Elicitation

MCP 服务器可以通过交互式对话框向用户请求结构化输入(v2.1.49+)。这允许 MCP 服务器在工作流中请求额外信息——例如,提示确认、从选项列表中选择或填写必填字段——为 MCP 服务器交互添加交互性。


工具描述和指令上限

从 v2.1.84 开始,Claude Code 对每个 MCP 服务器的工具描述和指令实施 2 KB 上限。这防止单个服务器因过于冗长的工具定义而消耗过多上下文,减少上下文膨胀并保持交互效率。


MCP Prompts 作为斜杠命令

MCP 服务器可以暴露在 Claude Code 中显示为斜杠命令的 prompts。Prompts 使用以下命名约定访问:

/mcp__<server>__<prompt>

例如,如果名为 github 的服务器暴露了一个名为 review 的 prompt,你可以将其调用为 /mcp__github__review


Server 去重

当相同的 MCP 服务器在多个作用域(本地、项目、用户)中定义时,本地配置优先。这允许你使用本地自定义覆盖项目级或用户级 MCP 设置,而不会产生冲突。


最近的生命周期修复

v2.1.136 中修复了两个长期存在的 MCP 生命周期 bug——如果你运行多服务器设置,值得升级:

  • MCP 服务器在 /clear 后保持不变:通过 .mcp.json、插件或 claude.ai 连接器配置的服务器在 VS Code、JetBrains 或 Agent SDK 中执行 /clear 后不再消失。早期版本会静默丢弃它们并需要重启。
  • OAuth refresh-token 并发刷新修复:多服务器 OAuth 设置在多个服务器同时刷新时不再丢失 refresh tokens。这消除了影响具有多个 OAuth 保护的 MCP 服务器设置的 “每天早上必须重新认证” 模式。

MCP 资源通过 @ 提及

你可以在提示中使用 @ 提及语法直接引用 MCP 资源:

@server-name:protocol://resource/path

例如,要引用特定的数据库资源:

@database:postgres://mydb/users

这允许 Claude 获取并内联包含 MCP 资源内容作为对话上下文的一部分。


MCP 作用域

MCP 配置可以存储在不同的作用域中,具有不同的共享级别:

作用域 位置 描述 与谁共享 需要批准
本地(默认) ~/.claude.json(项目路径下) 对当前用户私有,仅当前项目(旧版本中称为 project 仅你
项目 .mcp.json 签入 git 仓库 团队成员 是(首次使用)
用户 ~/.claude.json 在所有项目中可用(旧版本中称为 global 仅你

使用项目作用域

.mcp.json 中存储项目特定的 MCP 配置:

{
  "mcpServers": {
    "github": {
      "type": "http",
      "url": "https://api.github.com/mcp"
    }
  }
}

团队成员首次使用项目 MCP 时会看到批准提示。


MCP 配置管理

添加 MCP 服务器

# 添加基于 HTTP 的服务器
claude mcp add --transport http github https://api.github.com/mcp

# 添加本地 stdio 服务器
claude mcp add --transport stdio database -- npx @company/db-server

# 列出所有 MCP 服务器
claude mcp list

# 获取特定服务器的详细信息
claude mcp get github

# 移除 MCP 服务器
claude mcp remove github

# 重置项目特定的批准选择
claude mcp reset-project-choices

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

可用 MCP Server 表

MCP Server 用途 常用工具 认证 实时
Filesystem 文件操作 read、write、delete 操作系统权限 ✅ 是
GitHub 仓库管理 list_prs、create_issue、push OAuth ✅ 是
Slack 团队通信 send_message、list_channels Token ✅ 是
Database SQL 查询 query、insert、update 凭据 ✅ 是
Google Docs 文档访问 read、write、share OAuth ✅ 是
Asana 项目管理 create_task、update_status API Key ✅ 是
Stripe 支付数据 list_charges、create_invoice API Key ✅ 是
Memory 持久记忆 store、retrieve、delete 本地 ❌ 否

实战示例

示例 1:GitHub MCP 配置

文件: .mcp.json(项目根目录)

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}

可用的 GitHub MCP 工具:

Pull Request 管理

  • list_prs - 列出仓库中的所有 PR
  • get_pr - 获取 PR 详情(包括 diff)
  • create_pr - 创建新 PR
  • update_pr - 更新 PR 描述/标题
  • merge_pr - 将 PR 合并到主分支
  • review_pr - 添加审查评论

示例请求:

/mcp__github__get_pr 456

# 返回:
标题:添加深色模式支持
作者:@alice
描述:使用 CSS 变量实现深色主题
状态:OPEN
审查者:@bob, @charlie

Issue 管理

  • list_issues - 列出所有 issue
  • get_issue - 获取 issue 详情
  • create_issue - 创建新 issue
  • close_issue - 关闭 issue
  • add_comment - 向 issue 添加评论

仓库信息

  • get_repo_info - 仓库详情
  • list_files - 文件树结构
  • get_file_content - 读取文件内容
  • search_code - 在代码库中搜索

Commit 操作

  • list_commits - 提交历史
  • get_commit - 特定提交详情
  • create_commit - 创建新提交

设置

export GITHUB_TOKEN="your_github_token"
# 或使用 CLI 直接添加:
claude mcp add --transport stdio github -- npx @modelcontextprotocol/server-github

配置中的环境变量展开

MCP 配置支持带默认值的环境变量展开。${VAR}${VAR:-default} 语法适用于以下字段:commandargsenvurlheaders

{
  "mcpServers": {
    "api-server": {
      "type": "http",
      "url": "${API_BASE_URL:-https://api.example.com}/mcp",
      "headers": {
        "Authorization": "Bearer ${API_KEY}",
        "X-Custom-Header": "${CUSTOM_HEADER:-default-value}"
      }
    },
    "local-server": {
      "command": "${MCP_BIN_PATH:-npx}",
      "args": ["${MCP_PACKAGE:-@company/mcp-server}"],
      "env": {
        "DB_URL": "${DATABASE_URL:-postgresql://localhost/dev}"
      }
    }
  }
}

变量在运行时展开:

  • ${VAR} - 使用环境变量,如果未设置则报错
  • ${VAR:-default} - 使用环境变量,如果未设置则回退到默认值

示例 2:数据库 MCP 设置

配置:

{
  "mcpServers": {
    "database": {
      "command": "npx",
      "args": ["@modelcontextprotocol/server-database"],
      "env": {
        "DATABASE_URL": "postgresql://user:pass@localhost/mydb"
      }
    }
  }
}

示例用法:

用户:获取所有订单超过 10 个的用户

Claude:我将查询你的数据库以获取该信息。

# 使用 MCP 数据库工具:
SELECT u.*, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id
HAVING COUNT(o.id) > 10
ORDER BY order_count DESC;

# 结果:
- Alice:15 个订单
- Bob:12 个订单
- Charlie:11 个订单

设置

export DATABASE_URL="postgresql://user:pass@localhost/mydb"
# 或使用 CLI 直接添加:
claude mcp add --transport stdio database -- npx @modelcontextprotocol/server-database

示例 3:多 MCP 工作流

场景:每日报告生成

# 使用多个 MCP 的每日报告工作流

## 设置
1. GitHub MCP - 获取 PR 指标
2. Database MCP - 查询销售数据
3. Slack MCP - 发布报告
4. Filesystem MCP - 保存报告

## 工作流

### 步骤 1:获取 GitHub 数据
/mcp__github__list_prs completed:true last:7days

输出:
- 总 PR 数:42
- 平均合并时间:2.3 小时
- 审查周转时间:1.1 小时

### 步骤 2:查询数据库
SELECT COUNT(*) as sales, SUM(amount) as revenue
FROM orders
WHERE created_at > NOW() - INTERVAL '1 day'

输出:
- 销售量:247
- 收入:$12,450

### 步骤 3:生成报告
将数据组合成 HTML 报告

### 步骤 4:保存到文件系统
将 report.html 写入 /reports/

### 步骤 5:发布到 Slack
将摘要发送到 #daily-reports 频道

最终输出:
✅ 报告已生成并发布
📊 本周合并了 47 个 PR
💰 每日销售额 $12,450

设置

export GITHUB_TOKEN="your_github_token"
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
export SLACK_TOKEN="your_slack_token"
# 通过 CLI 添加每个 MCP 服务器或在 .mcp.json 中配置它们

示例 4:文件系统 MCP 操作

配置:

{
  "mcpServers": {
    "filesystem": {
      "command": "npx",
      "args": ["@modelcontextprotocol/server-filesystem", "/home/user/projects"]
    }
  }
}

可用操作:

操作 命令 用途
列出文件 ls ~/projects 显示目录内容
读取文件 cat src/main.ts 读取文件内容
写入文件 create docs/api.md 创建新文件
编辑文件 edit src/app.ts 修改文件
搜索 grep "async function" 在文件中搜索
删除 rm old-file.js 删除文件

设置

# 使用 CLI 直接添加:
claude mcp add --transport stdio filesystem -- npx @modelcontextprotocol/server-filesystem /home/user/projects

MCP vs Memory:决策矩阵

    graph TD
    A["需要外部数据?"]
    A -->|否| B["使用 Memory"]
    A -->|是| C["它经常变化吗?"]
    C -->|否/很少| B
    C -->|是/经常| D["使用 MCP"]

    B -->|存储| E["偏好<br/>上下文<br/>历史"]
    D -->|访问| F["实时 API<br/>数据库<br/>服务"]

    style A fill:#fff3e0,stroke:#333,color:#333
    style B fill:#e1f5fe,stroke:#333,color:#333
    style C fill:#fff3e0,stroke:#333,color:#333
    style D fill:#f3e5f5,stroke:#333,color:#333
    style E fill:#e8f5e9,stroke:#333,color:#333
    style F fill:#e8f5e9,stroke:#333,color:#333
  

请求/响应模式

    sequenceDiagram
    participant App as Claude
    participant MCP as MCP Server
    participant DB as 数据库

    App->>MCP: 请求:"SELECT * FROM users WHERE id=1"
    MCP->>DB: 执行查询
    DB-->>MCP: 结果集
    MCP-->>App: 返回解析后的数据
    App->>App: 处理结果
    App->>App: 继续任务

    Note over MCP,DB: 实时访问<br/>无缓存
  

环境变量

将敏感凭据存储在环境变量中:

# ~/.bashrc 或 ~/.zshrc
export GITHUB_TOKEN="ghp_xxxxxxxxxxxxx"
export DATABASE_URL="postgresql://user:pass@localhost/mydb"
export SLACK_TOKEN="xoxb-xxxxxxxxxxxxx"

然后在 MCP 配置中引用它们:

{
  "env": {
    "GITHUB_TOKEN": "${GITHUB_TOKEN}"
  }
}

Claude 作为 MCP Server(claude mcp serve

Claude Code 本身可以作为其他应用程序的 MCP 服务器。这使外部工具、编辑器和自动化系统能够通过标准 MCP 协议利用 Claude 的功能。

# 在 stdio 上启动 Claude Code 作为 MCP 服务器
claude mcp serve

其他应用程序可以像连接任何基于 stdio 的 MCP 服务器一样连接到此服务器。例如,要将 Claude Code 作为另一个 Claude Code 实例中的 MCP 服务器:

claude mcp add --transport stdio claude-agent -- claude mcp serve

这对于构建一个 Claude 实例编排另一个的多 agent 工作流非常有用。


受管 MCP 配置(企业)

对于企业部署,IT 管理员可以通过 managed-mcp.json 配置文件强制执行 MCP 服务器策略。此文件提供对组织范围内允许或阻止哪些 MCP 服务器的独占控制。

位置:

  • macOS:/Library/Application Support/ClaudeCode/managed-mcp.json
  • Linux:~/.config/ClaudeCode/managed-mcp.json
  • Windows:%APPDATA%\ClaudeCode\managed-mcp.json

功能:

  • allowedMcpServers – 允许的服务器白名单
  • deniedMcpServers – 禁止的服务器黑名单
  • 支持按服务器名称、命令和 URL 模式匹配
  • 组织范围的 MCP 策略在用户配置之前强制执行
  • 防止未经授权的服务器连接

示例配置:

{
  "allowedMcpServers": [
    {
      "serverName": "github",
      "serverUrl": "https://api.github.com/mcp"
    },
    {
      "serverName": "company-internal",
      "serverCommand": "company-mcp-server"
    }
  ],
  "deniedMcpServers": [
    {
      "serverName": "untrusted-*"
    },
    {
      "serverUrl": "http://*"
    }
  ]
}

注意:allowedMcpServersdeniedMcpServers 都匹配某个服务器时,拒绝规则优先。


插件提供的 MCP Servers

插件可以打包自己的 MCP 服务器,在安装插件时自动可用。插件提供的 MCP 服务器可以通过两种方式定义:

  1. 独立 .mcp.json – 在插件根目录放置 .mcp.json 文件
  2. 内联在 plugin.json – 直接在插件清单中定义 MCP 服务器

使用 ${CLAUDE_PLUGIN_ROOT} 变量引用相对于插件安装目录的路径:

{
  "mcpServers": {
    "plugin-tools": {
      "command": "node",
      "args": ["${CLAUDE_PLUGIN_ROOT}/dist/mcp-server.js"],
      "env": {
        "CONFIG_PATH": "${CLAUDE_PLUGIN_ROOT}/config.json"
      }
    }
  }
}

Subagent 作用域 MCP

MCP 服务器可以使用 mcpServers: 键在 agent frontmatter 中内联定义,将其作用域限定到特定 subagent 而非整个项目。当 agent 需要访问工作流中其他 agent 不需要的特定 MCP 服务器时,这非常有用。

---
mcpServers:
  my-tool:
    type: http
    url: https://my-tool.example.com/mcp
---

你是可以访问 my-tool 进行专业操作的 agent。

Subagent 作用域的 MCP 服务器仅在该 agent 的执行上下文中可用,不与父级或同级 agent 共享。


MCP 输出限制

Claude Code 对 MCP 工具输出实施限制以防止上下文溢出:

限制 阈值 行为
警告 10,000 tokens 显示输出较大的警告
默认最大值 25,000 tokens 超过此限制的输出会被截断
磁盘持久化 50,000 字符 超过 50K 字符的工具结果会持久化到磁盘

最大输出限制可通过 MAX_MCP_OUTPUT_TOKENS 环境变量配置:

# 将最大输出增加到 50,000 tokens
export MAX_MCP_OUTPUT_TOKENS=50000

用代码执行解决上下文膨胀

随着 MCP 采用规模的扩大,连接到数十个服务器、拥有数百或数千个工具会带来重大挑战:上下文膨胀。这可以说是大规模 MCP 的最大问题,Anthropic 的工程团队提出了一个优雅的解决方案——使用代码执行而不是直接工具调用。

来源使用 MCP 的代码执行:构建更高效的 Agents — Anthropic 工程博客

问题:两个 token 浪费源

1. 工具定义使上下文窗口过载

大多数 MCP 客户端会预先加载所有工具定义。当连接到数千个工具时,模型在读取用户请求之前必须处理数十万个 tokens。

2. 中间结果消耗额外 tokens

每个中间工具结果都会通过模型的上下文。考虑将会议记录从 Google Drive 转移到 Salesforce——完整记录会两次流经上下文:一次是读取时,一次是写入目标时。2 小时的会议记录可能意味着 50,000+ 个额外 tokens。

    graph LR
    A["模型"] -->|"工具调用: getDocument"| B["MCP Server"]
    B -->|"完整记录 (50K tokens)"| A
    A -->|"工具调用: updateRecord<br/>(重新发送完整记录)"| B
    B -->|"确认"| A

    style A fill:#ffcdd2,stroke:#333,color:#333
    style B fill:#f3e5f5,stroke:#333,color:#333
  

解决方案:MCP 工具作为代码 API

agent 不再通过上下文窗口传递工具定义和结果,而是编写代码将 MCP 工具作为 API 调用。代码在沙盒执行环境中运行,只有最终结果返回给模型。

    graph LR
    A["模型"] -->|"编写代码"| B["代码执行<br/>环境"]
    B -->|"直接调用工具"| C["MCP Servers"]
    C -->|"数据留在<br/>执行环境中"| B
    B -->|"仅最终结果<br/>(最小化 tokens)"| A

    style A fill:#c8e6c9,stroke:#333,color:#333
    style B fill:#e1f5fe,stroke:#333,color:#333
    style C fill:#f3e5f5,stroke:#333,color:#333
  

工作方式

MCP 工具以类型化函数的文件树形式呈现:

servers/
├── google-drive/
│   ├── getDocument.ts
│   └── index.ts
├── salesforce/
│   ├── updateRecord.ts
│   └── index.ts
└── ...

每个工具文件包含一个类型化包装器:

// ./servers/google-drive/getDocument.ts
import { callMCPTool } from "../../../client.js";

interface GetDocumentInput {
  documentId: string;
}

interface GetDocumentResponse {
  content: string;
}

export async function getDocument(
  input: GetDocumentInput
): Promise<GetDocumentResponse> {
  return callMCPTool<GetDocumentResponse>(
    'google_drive__get_document', input
  );
}

然后 agent 编写代码来编排工具:

import * as gdrive from './servers/google-drive';
import * as salesforce from './servers/salesforce';

// 数据直接在工具之间流动——永不通过模型
const transcript = (
  await gdrive.getDocument({ documentId: 'abc123' })
).content;

await salesforce.updateRecord({
  objectType: 'SalesMeeting',
  recordId: '00Q5f000001abcXYZ',
  data: { Notes: transcript }
});

结果:Token 使用量从约 150,000 降至约 2,000——减少 98.7%。

主要优势

优势 描述
渐进式发现 Agent 浏览文件系统以仅加载其需要的工具定义,而非预先加载所有工具
上下文高效的结果 数据在执行环境中进行过滤/转换后再返回给模型
强大的控制流 循环、条件和错误处理在代码中运行,无需通过模型往返
隐私保护 中间数据(PII、敏感记录)留在执行环境中;永不进入模型上下文
状态持久化 Agent 可以将中间结果保存到文件并构建可重用的技能函数

示例:过滤大数据集

// 不使用代码执行——所有 10,000 行都流经上下文
// 工具调用:gdrive.getSheet(sheetId: 'abc123')
//   -> 在上下文中返回 10,000 行

// 使用代码执行——在执行环境中过滤
const allRows = await gdrive.getSheet({ sheetId: 'abc123' });
const pendingOrders = allRows.filter(
  row => row["Status"] === 'pending'
);
console.log(`Found ${pendingOrders.length} pending orders`);
console.log(pendingOrders.slice(0, 5)); // 仅 5 行到达模型

示例:循环而不做往返

// 轮询部署通知——完全在代码中运行
let found = false;
while (!found) {
  const messages = await slack.getChannelHistory({
    channel: 'C123456'
  });
  found = messages.some(
    m => m.text.includes('deployment complete')
  );
  if (!found) await new Promise(r => setTimeout(r, 5000));
}
console.log('Deployment notification received');

需要考虑的权衡

代码执行引入了自身的复杂性。运行 agent 生成的代码需要:

  • 一个安全的沙盒执行环境,具有适当的资源限制
  • 对执行代码的监控和日志记录
  • 与直接工具调用相比的额外基础设施开销

应该权衡这些优势——降低的 token 成本、更低的延迟、更好的工具组合——与这些实现成本。对于只有几个 MCP 服务器的 agent,直接工具调用可能更简单。对于大规模 agent(数十个服务器、数百个工具),代码执行是一个重大改进。

MCPorter:MCP 工具组合运行时

MCPorter 是一个 TypeScript 运行时和 CLI 工具包,使调用 MCP 服务器变得实用且无需样板代码——并通过选择性工具暴露和类型化包装器帮助减少上下文膨胀。

解决的问题: MCPorter 不是预先加载所有 MCP 服务器的所有工具定义,而是让你按需发现、检查和调用特定工具——保持上下文精简。

主要功能:

功能 描述
零配置发现 从 Cursor、Claude、Codex 或本地配置自动发现 MCP 服务器
类型化工具客户端 mcporter emit-ts 生成 .d.ts 接口和可直接运行的包装器
可组合 API createServerProxy() 将工具暴露为 camelCase 方法,带 .text().json().markdown() 辅助方法
CLI 生成 mcporter generate-cli 将任何 MCP 服务器转换为独立 CLI,支持 --include-tools / --exclude-tools 过滤
参数隐藏 可选参数默认隐藏,减少 schema 冗长性

安装:

npx mcporter list          # 无需安装——即时发现服务器
pnpm add mcporter          # 添加到项目
brew install steipete/tap/mcporter  # macOS 通过 Homebrew

示例——在 TypeScript 中组合工具:

import { createRuntime, createServerProxy } from "mcporter";

const runtime = await createRuntime();
const gdrive = createServerProxy(runtime, "google-drive");
const salesforce = createServerProxy(runtime, "salesforce");

// 数据在工具之间流动,而不通过模型上下文
const doc = await gdrive.getDocument({ documentId: "abc123" });
await salesforce.updateRecord({
  objectType: "SalesMeeting",
  recordId: "00Q5f000001abcXYZ",
  data: { Notes: doc.text() }
});

示例——CLI 工具调用:

# 直接调用特定工具
npx mcporter call linear.create_comment issueId:ENG-123 body:'Looks good!'

# 列出可用的服务器和工具
npx mcporter list

MCPorter 补充了上述代码执行方法,通过提供将 MCP 工具作为类型化 API 调用的运行时基础设施——使将中间数据保留在模型上下文之外变得简单直接。


最佳实践

安全注意事项

应该做的 ✅

  • 对所有凭据使用环境变量
  • 定期轮换 tokens 和 API keys(建议每月)
  • 尽可能使用只读 tokens
  • 将 MCP 服务器访问范围限制到最小必需
  • 监控 MCP 服务器使用情况和访问日志
  • 在可用时对外部服务使用 OAuth
  • 对 MCP 请求实施速率限制
  • 在生产使用前测试 MCP 连接
  • 记录所有活动的 MCP 连接
  • 保持 MCP 服务器包更新

不要做的 ❌

  • 不要在配置文件中硬编码凭据
  • 不要将 tokens 或 secrets 提交到 git
  • 不要在团队聊天或电子邮件中共享 tokens
  • 不要将个人 tokens 用于团队项目
  • 不要授予权限
  • 不要忽略认证错误
  • 不要公开暴露 MCP 端点
  • 不要以 root/admin 权限运行 MCP 服务器
  • 不要在日志中缓存敏感数据
  • 不要禁用认证机制

配置最佳实践

  1. 版本控制:将 .mcp.json 保留在 git 中,但对 secrets 使用环境变量
  2. 最小权限:为每个 MCP 服务器授予所需的最小权限
  3. 隔离:尽可能在单独的进程中运行不同的 MCP 服务器
  4. 监控:记录所有 MCP 请求和错误以进行审计跟踪
  5. 测试:在部署到生产之前测试所有 MCP 配置

性能提示

  • 在应用程序级别缓存频繁访问的数据
  • 使用特定的 MCP 查询以减少数据传输
  • 监控 MCP 操作的响应时间
  • 考虑对外部 API 进行速率限制
  • 执行多个操作时使用批处理

安装说明

前置条件

  • 已安装 Node.js 和 npm
  • 已安装 Claude Code CLI
  • 外部服务的 API tokens/凭据

分步设置

  1. 使用 CLI 添加你的第一个 MCP 服务器(示例:GitHub):
claude mcp add --transport stdio github -- npx @modelcontextprotocol/server-github

或在项目根目录创建 .mcp.json 文件:

{
  "mcpServers": {
    "github": {
      "command": "npx",
      "args": ["@modelcontextprotocol/server-github"],
      "env": {
        "GITHUB_TOKEN": "${GITHUB_TOKEN}"
      }
    }
  }
}
  1. 设置环境变量:
export GITHUB_TOKEN="your_github_personal_access_token"
  1. 测试连接:
claude /mcp
  1. 使用 MCP 工具:
/mcp__github__list_prs
/mcp__github__create_issue "Title" "Description"

特定服务的安装

GitHub MCP:

npm install -g @modelcontextprotocol/server-github

Database MCP:

npm install -g @modelcontextprotocol/server-database

Filesystem MCP:

npm install -g @modelcontextprotocol/server-filesystem

Slack MCP:

npm install -g @modelcontextprotocol/server-slack

故障排查

找不到 MCP Server

# 验证 MCP 服务器是否已安装
npm list -g @modelcontextprotocol/server-github

# 如果缺失则安装
npm install -g @modelcontextprotocol/server-github

认证失败

# 验证环境变量是否已设置
echo $GITHUB_TOKEN

# 如果需要则重新导出
export GITHUB_TOKEN="your_token"

# 验证 token 是否具有正确的权限
# 在 https://github.com/settings/tokens 检查 GitHub token 范围

连接超时

  • 检查网络连接:ping api.github.com
  • 验证 API 端点可访问
  • 检查 API 上的速率限制
  • 尝试在配置中增加超时
  • 检查防火墙或代理问题

MCP Server 崩溃

  • 检查 MCP 服务器日志:~/.claude/logs/
  • 验证所有环境变量已设置
  • 确保正确的文件权限
  • 尝试重新安装 MCP 服务器包
  • 检查相同端口上的冲突进程

相关概念

Memory vs MCP

  • Memory:存储持久、不变的数据(偏好、上下文、历史)
  • MCP:访问实时、变化的数据(API、数据库、实时服务)

何时使用每个

  • 使用 Memory:用户偏好、对话历史、学习的上下文
  • 使用 MCP:当前 GitHub issues、实时数据库查询、实时数据

与其他 Claude 功能的集成

  • 将 MCP 与 Memory 结合以获得丰富的上下文
  • 在提示中使用 MCP 工具以获得更好的推理
  • 利用多个 MCP 进行复杂工作流

更多资源


最后更新:2026年5月9日 Claude Code 版本:2.1.138 来源