> ## Documentation Index
> Fetch the complete documentation index at: https://docs.trae.cn/llms.txt
> Use this file to discover all available pages before exploring further.

TRAE CLI 支持通过模型上下文协议（Model Context Protocol，MCP）来集成第三方能力。
## MCP 传输协议 {#f3e2effb}
TRAE CLI 支持的 MCP 传输协议为 stdio、SSE 和 Streamable HTTP。
## 添加 MCP Server {#aa65de5b}
你可以使用 `traecli config edit` 命令来编辑全局配置文件 trae_cli.yaml，在其中添加 MCP Server 配置。
需要为各个类型的 MCP Server 配置的字段如下：

* **stdio**
   <!-- @cols-width: 136,116,100,352 -->
   | | | | | \
   |**字段** |**类型** |**是否必填** |**描述** |
   |---|---|---|---|
   | | | | | \
   |name |String |是 |该 MCP Server 的名称，是其唯一标识。 |
   | | | | | \
   |type |String |是 |该 MCP Server 的传输类型，此处为固定值 `stdio`。 |
   | | | | | \
   |command |String |是 |可执行文件或脚本的绝对路径。 |
   | | | | | \
   |args |List<String> |否 |传递给命令的参数列表。 |
   | | | | | \
   |env |Map |否 |需要注入到进程中的环境变量（键值对）。 |
   | | | | | \
   |timeout |String |否 |进程启动或响应的超时时间，单位为秒（s）。 |
   | | | | | \
   |disabled_tools |List<String> |否 |需要禁用的工具或匹配模式（支持 `*` 通配符）。 |

* **SSE**
   <!-- @cols-width: 136,116,100,352 -->
   | | | | | \
   |**字段** |**类型** |**是否必填** |**描述** |
   |---|---|---|---|
   | | | | | \
   |name |String |是 |该 MCP Server 的名称，是其唯一标识。 |
   | | | | | \
   |type |String |是 |该 MCP Server 的传输类型，此处为固定值 `sse`。 |
   | | | | | \
   |url |String |是 |远程服务器的完整 URL 地址。 |
   | | | | | \
   |headers |Map |否 |请求中需要附加的 HTTP 请求头。 |
   | | | | | \
   |args |List<String> |否 |传递给命令的参数列表。 |
   | | | | | \
   |env |Map |否 |需要注入到进程中的环境变量（键值对）。 |
   | | | | | \
   |timeout |String |否 |进程启动或响应的超时时间，单位为秒（s）。 |
   | | | | | \
   |disabled_tools |List<String> |否 |需要禁用的工具或匹配模式（支持 `*` 通配符）。 |

* **Streamable HTTP**
   <!-- @cols-width: 136,116,100,352 -->
   | | | | | \
   |**字段** |**类型** |**是否必填** |**描述** |
   |---|---|---|---|
   | | | | | \
   |name |String |是 |该 MCP Server 的名称，是其唯一标识。 |
   | | | | | \
   |type |String |是 |该 MCP Server 的传输类型，此处为固定值 `http`。 |
   | | | | | \
   |url |String |是 |远程服务器的完整 URL 地址。 |
   | | | | | \
   |headers |Map |否 |请求中需要附加的 HTTP 请求头。 |
   | | | | | \
   |args |List<String> |否 |传递给命令的参数列表。 |
   | | | | | \
   |env |Map |否 |需要注入到进程中的环境变量（键值对）。 |
   | | | | | \
   |timeout |String |否 |进程启动或响应的超时时间，单位为秒（s）。 |
   | | | | | \
   |disabled_tools |List<String> |否 |需要禁用的工具或匹配模式（支持 `*` 通配符）。 |


以下为三类 MCP Server 的配置示例:
```YAML
mcp_servers:
  # 示例 1: 通过本地命令启动 (stdio)
  - name: "my-local-tool"
    type: stdio
    command: "/path/to/your/tool"
    args: ["--arg1", "--arg2"]
    timeout: 100s
    env:
      ENV_VAR_NAME: "some_value"

  # 示例 2: 通过 SSE 连接
  - name: "my-sse-tool"
    type: sse
    url: "http://example.com/mcp-server"
    headers:
      key: value
    disabled_tools:
      - "*pattern*"
      - exact_match

  # 示例 3: 通过 Streamable HTTP 连接
  - name: "my-streamable-http-tool"
    type: http
    url: "http://example.com/streamable-mcp-server"
    headers:
      key: value
```

## 管理 MCP Server {#196570da}
通过 `/mcp` 命令查看 MCP Server 的具体情况，包括 MCP Server 初始化失败的错误信息。
对于支持浏览器 OAuth 的 MCP Server，可以对其进行 Authenticate、Re-authenticate、Clear authentication 等操作。
## MCP Server 鉴权 {#922654a2}
### stdio {#930f1285}
对于 stdio 类型的 MCP Server，你可以通过命令行参数或环境变量来传递鉴权信息。

* 通过命令行参数传递鉴权信息：
   ```YAML
   mcp_servers:
     - name: Context4Code
       type: stdio
       command: npx
       args: ["mcp", "serve"]
       env: {}
   ```

* 通过环境变量传递鉴权信息：
   ```YAML
   mcp_servers:
     - name: "GitHub CLI"
       type: "stdio"
       command: "gh"
       args: ["api", "mcp"]
       env:
         - key: "key1"
           value: "value1"
   ```


### SSE {#173fa4e8}
对于 SSE 类型的 MCP Server，你可以通过浏览器 OAuth 来完成鉴权。
如果使用的 MCP Server 支持 RFC 8414（用于动态发现）及 RFC 7591（用于动态注册），则可以将 MCP Tool 配置到 trae_cli.yaml 中，然后通过 `/mcp` 命令触发浏览器鉴权流程。
<!-- @cols-width: 103,671 -->
| | | \
|**概念** |**描述** |
|---|---|
| | | \
|RFC 8414 |支持客户端动态发现授权服务器的配置信息和能力。 |\
| |客户端会检查 MCP Server 是否支持 RFC 8414 协议，服务端是否暴露 `schema://host + /.well-known/oauth-authorization-server` 地址并返回 `registration_endpoint` 字段。 |
| | | \
|RFC 7591 |支持动态获取 client id 和 client secret 等信息，无需提前到管理后台注册应用。 |

示例配置如下：
```YAML
mcp_servers:
  - name: "Asana"
    url: "https://mcp.asana.com/sse"
    type: "sse"
  - name: "Sentry"
    url: "https://mcp.sentry.dev/mcp"
    type: "http"
```

配置完成后，启动 TRAE CLI，然后通过 `/mcp` 命令进入 MCP Server 详情页，选择 "Authenticate" 即可触发浏览器 OAuth 流程。

::::cols
@col 69
![Image=1460x482](https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/831c925f0564449684982404bc315868~tplv-goo7wpa0wc-image.image)


@col 30
![Image=731x556](https://p9-arcosite.byteimg.com/tos-cn-i-goo7wpa0wc/1ef1c6ae27404d039fe3b9a7d6e40075~tplv-goo7wpa0wc-image.image)

::::

OAuth 优先级相关说明：
对于某些同时支持 OAuth RFC 8414 和其他鉴权方式的 MCP Server，若你想使用其他鉴权方式，则直接配置 MCP Server 所需的 token 即可，例如上述的 `url` 字段或 header 鉴权方式。
TRAE CLI 会优先使用普通鉴权方式来尝试连接，失败后再尝试 OAuth RFC 8414。 
### HTTP {#2c20a75e}

* 通过在 URL 中直接嵌入鉴权参数来进行身份验证，适用于简单的 API 密钥鉴权场景：
   ```YAML
   mcp_servers:
     - name: "Weather API"
       url: "https://api.weather.com/mcp?api_key=your_weather_api_key&format=json"
       type: "http"
     - name: "Database Connector"
       url: "https://db-service.example.com/mcp?token=your_db_token&database=production"
       type: "sse"
   ```

* 通过 HTTP 请求头传递鉴权信息：
   ```YAML
   mcp_servers:
     - name: "github"
       url: "https://api.githubcopilot.com/mcp/"
       type: "http"
       headers:
         Authorization: "Bearer your_access_token_here"
         X-MCP-Toolsets: "users,orgs"
   ```

   不同 MCP Server 支持配置的请求头字段不同。以上 GitHub MCP Server 中的请求头字段说明如下：
   <!-- @cols-width: 146,693 -->
   | | | \
   |**字段** |**描述** |
   |---|---|
   | | | \
   |Authorization |通过后台生成的[鉴权令牌](https://github.com/settings/personal-access-tokens/new)，控制可见权限。 |
   | | | \
   |X-MCP-Toolsets |由于 GitHub MCP Server 默认提供的工具过多（将近 100 个），使用时会严重消耗上下文，因此可以通过修改 `url` 字段或增加 `X-MCP-Toolsets` 字段来控制所需的[工具集合](https://github.com/github/github-mcp-server/blob/main/docs/remote-server.md)。 |


## TRAE CLI 是否可以运行 TRAE IDE 中的 MCP Server？ {#fafdd298}
TRAE CLI 兼容 TRAE IDE 中的项目级 MCP Server。
TRAE IDE 存储项目级 MCP Server 的目录是 `.trae/mcp.json`。TRAE CLI 可以读取和运行该目录下的 MCP Server。

