Skip to content

12.1 新增 MCP 伺服器

學習如何新增不同型別的 MCP 伺服器到 Claude Code。

新增遠端 HTTP 伺服器

HTTP 伺服器是連線到遠端 MCP 伺服器的推薦選項。

### 基本语法

    bash


    claude mcp add --transport http <name> <url>

### 示例:连接到 Notion

    bash


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

### 带身份验证的示例

    bash


    # Bearer 令牌
    claude mcp add --transport http secure-api https://api.example.com/mcp \
    --header "Authorization: Bearer your-token"

    # API 密钥
    claude mcp add --transport http api https://api.example.com/mcp \
    --header "X-API-Key: your-api-key"

### 带多个标头的示例

    bash


    claude mcp add --transport http custom-api https://api.example.com/mcp \
      --header "Authorization: Bearer your-token" \
      --header "X-Custom-Header: custom-value"

## 添加远程 SSE 服务器

SSE(Server-Sent Events)傳輸已棄用,請在可用的地方使用 HTTP 伺服器。

### 基本语法

    bash


    claude mcp add --transport sse <name> <url>

### 示例:连接到 Asana

    bash


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

### 带身份验证的示例

    bash


    claude mcp add --transport sse private-api https://api.company.com/sse \
    --header "X-API-Key: your-key-here"

## 添加本地 stdio 服务器

Stdio 伺服器作為本地程序在您的計算機上執行。

### 基本语法

    bash


    claude mcp add --transport stdio <name> <command> [args...]

### 示例:添加 Airtable 服务器

    bash


    claude mcp add --transport stdio airtable --env AIRTABLE_API_KEY=YOUR_KEY \
    -- npx -y airtable-mcp-server

### 示例:添加数据库服务器

    bash


    claude mcp add --transport stdio db -- npx -y @bytebase/dbhub \
      --dsn "postgresql://readonly:pass@prod.db.com:5432/analytics"

### 示例:添加 Python 服务器

    bash


    claude mcp add --transport stdio python-server -- python server.py

### 使用 `--` 参数

--(雙破折號)將 Claude 自己的 CLI 標誌與傳遞給 MCP 伺服器的命令和引數分開。

    bash


    # 运行 npx server
    claude mcp add --transport stdio myserver -- npx server

    # 运行 python server.py --port 8080
    claude mcp add --transport stdio myserver --env KEY=value -- python server.py --port 8080

### Windows 用户注意事项

在本機 Windows(不是 WSL)上,使用 npx 的本地 MCP 伺服器需要 cmd /c 包裝器:

    bash


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

## 配置选项

### 环境变量

使用 --env 標誌設定環境變數:

    bash


    claude mcp add --transport stdio db \
      --env DB_URL=postgresql://user:pass@localhost/db \
      --env DB_TIMEOUT=30 \
      -- npx -y db-server

### 作用域

使用 --scope 標誌指定配置的儲存位置:

    bash


    # 本地范围(默认)
    claude mcp add --transport http github https://api.github.com/mcp/

    # 项目范围
    claude mcp add --transport http github --scope project https://api.github.com/mcp/

    # 用户范围
    claude mcp add --transport http github --scope user https://api.github.com/mcp/

### 超时设置

使用 MCP_TIMEOUT 環境變數配置 MCP 伺服器啟動超時:

    bash


    # 设置 10 秒超时
    MCP_TIMEOUT=10000 claude

### 输出限制

使用 MAX_MCP_OUTPUT_TOKENS 環境變數增加輸出限制:

    bash


    # 设置 50,000 令牌限制
    MAX_MCP_OUTPUT_TOKENS=50000 claude

## 身份验证

### OAuth 2.0

許多基於雲的 MCP 伺服器需要 OAuth 2.0 身份驗證:

    bash


    # 1. 添加服务器
    claude mcp add --transport http github https://api.github.com/mcp/

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

    # 3. 选择需要身份验证的服务器
    # 4. 按照 OAuth 流程完成身份验证

### API 密钥

使用 API 金鑰進行身份驗證:

    bash


    claude mcp add --transport http api https://api.example.com/mcp \
    --header "Authorization: Bearer your-api-key"

### 环境变量

使用環境變數傳遞憑證:

    bash


    claude mcp add --transport stdio db \
      --env API_KEY=your-key \
      -- npx -y db-server

## 验证安装

### 检查服务器状态

    bash


    # 列出所有已配置的服务器
    claude mcp list

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

    # 在 Claude Code 中检查服务器状态
    /mcp

### 测试服务器连接

    bash


    # 测试服务器连接
    测试 MCP 服务器连接

    # 使用服务器工具
    使用 GitHub 工具查看 PR

## 常见问题

### 连接失败


**問題** : 無法連線到 MCP 伺服器

**解决方案** :

### 身份验证错误


**問題** : 身份驗證失敗

**解决方案** :

### 超时错误


**問題** : 伺服器啟動超時

**解决方案** :

### Windows 执行错误


**問題** : Windows 上無法執行 `npx` 命令

**解决方案** :

    bash


    # 使用 cmd /c 包装器
    claude mcp add --transport stdio my-server -- cmd /c npx -y @some/package

## 最佳实践

### 1\. 选择合适的传输方式
  • HTTP : 適合遠端伺服器和雲服務
  • stdio : 適合本地工具和指令碼
  • SSE : 已棄用,使用 HTTP 替代

2. 安全配置

  • 使用強身份驗證
  • 限制許可權範圍
  • 使用環境變數管理憑證
  • 定期更新憑證

3. 效能最佳化

  • 設定適當的超時
  • 配置輸出限制
  • 使用連線池
  • 啟用快取

4. 錯誤處理

  • 捕獲和處理錯誤
  • 提供恢復方案
  • 記錄錯誤日誌
  • 監控伺服器狀態

刪除伺服器

如果不再需要伺服器,可以刪除它:

    bash


    # 删除服务器
    claude mcp remove github

    # 确认删除
    # 服务器已成功删除

## 重置配置

如果需要重置專案範圍的選擇:

    bash


    # 重置项目选择
    claude mcp reset-project-choices

基于 MIT 许可发布 | 永久导航