# MCP 客户端

URL: https://swb.one/zh-Hans/docs/setup/mcp-client

MCP 是应用使用外部工具的标准方式。按本页操作，可以用密钥把应用与 SwB 配对。

## 开始之前 [#开始之前]

使用支持 Streamable HTTP 远程 MCP 的应用。这种方式让应用通过网络调用工具。应用还需要支持自定义请求头，用来携带密钥。你需要一个 SwB 所有者客户端，或 SwB 所在电脑上的终端。

要在应用所在的电脑上启动 SwB，在一个空文件夹中运行 `uvx swb-one up`，然后打开终端中显示的一次性登录链接。更多选项见[安装](/zh-Hans/docs/install)。

Claude、ChatGPT、Grok 和编程智能体各有专门的页面，使用 OAuth 登录。连接这些应用时，按对应的页面操作。

## 复制设置提示词 [#复制设置提示词]

https://swb.one/prompts/mcp-client.md

````
Read the setup guide at https://swb.one/docs/setup/mcp-client.

Help me connect an MCP client that supports Streamable HTTP and custom request headers. Streamable HTTP is how the client sends tool requests to SwB.

For a new local SwB server, follow **This computer** at https://swb.one/prompts/deploy.md.

Use `http://localhost:8870` if the client runs on the SwB computer. Use the port shown at startup. Use my public HTTPS address if the client runs elsewhere.

## Owner client

Use owner access for the first client if it will set up other apps. On the SwB computer, set up a private owner client. Use the local SwB address with `/mcp` at the end. Load `SWB_KEY_OWNER` from the instance `.env` inside that client. Keep the key in its local config with file mode `0600`. Send it in the `Authorization: Bearer <key>` header only to the local address. Keep the key out of commands, logs and chat. Check that the client can call `inbox`. Give the owner client https://swb.one/prompts/coordinator.md for the remaining connections.

## Paired client

Ask me to pick a short name for the app, such as `notes-bot`. From an app with SwB owner access, call `connect(what="mcp-client", choose={"agent":"notes-bot"})` with my chosen name. If I use a terminal instead, ask me to run this command in my SwB instance folder.

```bash
swb connect mcp-client --choose agent=notes-bot
```

Use `uvx swb-one connect mcp-client --choose agent=notes-bot` if I run SwB with uvx. The result gives me a pairing code. It works once within 10 minutes.

A scope is a group of memory entries. The key gets `agent:<name>` and any scope I grant to that agent. It also gets the scope of a project named in the pairing. Read `general` if the key has access to it.

If you can edit the client settings on this computer, add an MCP server named `swb`. Use a hidden prompt if I have the pairing code. Send the code to `POST /connect` and read the response in memory. Use `mcp_url` for Streamable HTTP and `key` for the `Authorization: Bearer <key>` header. Write the key straight to the client config with file mode `0600`. Keep the code and key out of commands, logs and chat. For a local server, use the MCP address shown at startup.

If I must edit the settings myself, give me this command. Ask me to run it in a private folder on the computer that runs the client. For a different server, give me the command with its base address. Ask me to enter the pairing code at the hidden prompt.

```bash
bash -c '
  umask 077
  set -C
  read -rsp "Pairing code: " code || exit
  printf "\n"
  printf "{\"code\":\"%s\"}" "$code" |
    curl -fsS http://localhost:8870/connect -H "Content-Type: application/json" \
      --data-binary @- > .swb-pairing.json
'
```

SwB returns the key once. Ask me to open `.swb-pairing.json` in a private editor.

Ask me to add an MCP server named `swb` in the client settings. Ask me to choose Streamable HTTP. Ask me to use the `mcp_url` from the file. For a local server, ask me to use the MCP address shown at startup. Ask me to set `Authorization: Bearer <key>` with the `key` from the file. Ask me to delete the file after setup.

Have the new app call `inbox`. A successful call confirms the connection, even when the inbox is empty.

For a round trip, use an app with SwB owner access. On the SwB computer, you can use a private MCP session instead. Load `SWB_KEY_OWNER` from the instance `.env` inside the client. Send it only as a bearer token to the local SwB address. Keep the key out of commands, logs and chat.

Call `send(to="agent:notes-bot", task="SwB setup test. Reply pong.")`. Replace `notes-bot` with my chosen name. Ask the new agent to read the message with `inbox`. Ask it to answer with `reply` and the item's `handle`. From the owner session, call `wait` with the `conversation` returned by `send`. Show me the reply.

For the first shared memory check, use a scope both agents can read. Ask the new agent to call `memory_list` with the scope in `scopes`.

Ask me to paste the coordinator prompt from https://swb.one/prompts/coordinator.md into the new app. The client can subscribe to `switchboard.message.created` if it supports MCP events. It can also use `wait` or `inbox` to read messages.

For each owner-only connection, give me its matching `swb connect` command. For example, give me `swb connect slack`.
````

把提示词粘贴到 SwB 所有者客户端，或粘贴到要连接的应用中。

## 本地所有者客户端 [#本地所有者客户端]

在 SwB 所在的电脑上，用一个仅自己使用的客户端设置其他应用。使用启动时显示的本地 `/mcp` 地址。把实例 `.env` 中的 `SWB_KEY_OWNER` 作为 `Authorization: Bearer <key>` 使用。这个密钥拥有完整的所有者权限。把它保存在客户端的本地私有配置中，文件权限设为 `0600`。只把它发给本地的 SwB。

## 配对客户端 [#配对客户端]

如果智能体能在本地编辑设置，让它来配置应用。把密钥保存在应用的私有设置中。设置文件的权限设为 `0600`。

### 为应用命名 [#为应用命名]

给应用起一个简短的名称并告诉智能体，例如 `notes-bot`。SwB 会在这个应用发出的消息上显示该名称。
### 获取配对码 [#获取配对码]

让一个以所有者身份登录的 SwB 智能体带上应用名称调用 `connect_code`。也可以在 SwB 实例文件夹中运行 `uvx swb-one connect mcp-client --choose agent=notes-bot`。把 `notes-bot` 换成你的应用名称。
### 获取密钥 [#获取密钥]

在运行应用的电脑上，用终端进入一个仅自己可访问的文件夹。把 `https://swb.example.com` 换成你的 SwB 地址，保留末尾的 `/connect`。如果是本地 SwB，使用 `http://localhost:8870/connect`，端口以启动时显示的为准。出现提示后，在 10 分钟内输入配对码，输入的内容会隐藏。

```bash
bash -c '
  umask 077
  set -C
  read -rsp "Pairing code: " code || exit
  printf "\n"
  printf "{\"code\":\"%s\"}" "$code" |
    curl -fsS https://swb.example.com/connect -H "Content-Type: application/json" \
      --data-binary @- > .swb-pairing.json
'
```
### 妥善保管密钥 [#妥善保管密钥]

用仅自己可见的编辑器打开 `.swb-pairing.json`。上面的命令以 `0600` 权限保存响应。把其中的 `key` 填入应用设置。SwB 只返回一次密钥。
### 把 SwB 添加到应用 [#把-swb-添加到应用]

在应用的 MCP 设置中添加一个名为 `swb` 的服务器。选择 Streamable HTTP。如果是公网 SwB 服务器，使用文件中的 `mcp_url`。如果是本地 SwB，使用启动时显示的 MCP 地址。
### 添加密钥 [#添加密钥]

在应用的私有设置中添加请求头 `Authorization: Bearer <key>`。保存密钥后，删除 `.swb-pairing.json`。

## 智能体会做什么 [#智能体会做什么]

智能体以所有者登录身份调用 `connect(what="mcp-client")`。SwB 返回 MCP 地址，并询问应用名称。智能体带上 `choose={"agent":"notes-bot"}` 再次调用 `connect`。

SwB 为这个名称生成配对码，并返回 `POST /connect` 步骤。配对码是一次性代码，10 分钟内有效。如果应用已有密钥，SwB 会说明这一点，等你调用 `connect_revoke` 之后才生成新的配对码。

在 SwB 服务器的终端中运行 `swb connect mcp-client --choose agent=notes-bot`，可以得到同样的步骤。

## 凭据 [#凭据]

| 项 | 显示位置 | 格式 | 去向 |
| - | - | - | - |
| 配对码 | `connect` 或 `connect_code` 的返回结果 | `XXXXX-XXXXX`，10 分钟内有效，只能使用一次 | `POST /connect` 请求 |
| 智能体密钥 | `POST /connect` 的响应，只返回一次 | 以 `swb_` 开头 | 应用私有设置中的 `Authorization` 请求头 |

把密钥保存在应用的私有设置中。SwB 只保存密钥的哈希值。

## 检查连接 [#检查连接]

让新智能体调用 `inbox`。让以所有者身份登录的智能体调用 `connect_list`，查看新密钥的最近调用时间。让所有者智能体向 `agent:notes-bot` 发送一条测试消息。让新智能体用 `inbox` 读取，再用 `reply` 回复。

## 排查问题 [#排查问题]

如果 `POST /connect` 失败，检查 SwB 地址。如果配对码已过期或已用过，向所有者要一个新的。如果密钥泄露，用 `connect_revoke` 撤销它。此后 SwB 会拒绝使用这个密钥的调用。
