# 智能体工具

URL: https://swb.one/zh-Hans/docs/setup/agent-tool

## 复制设置提示词 [#setup-prompt]

https://swb.one/prompts/agent-tool.md

````
Read the setup guide at https://swb.one/docs/setup/agent-tool.

Help me connect my agent tool to SwB.
SwB carries messages and stores shared memory.

Ask me which chat apps already reach my agent.
Tell me that my existing chat connections stay the same.
Keep the existing Telegram bot settings.
Keep the existing WeChat ClawBot settings and binding.
Use one active receiver per Telegram bot token.
Run one WeChat ClawBot instance per binding.
Pair my agent with SwB over MCP as a separate connection.
Use MCP Events or a local webhook for notices about new messages.
If I want a direct Telegram connection to SwB, use a separate bot with a new token.
Use the Telegram setup guide at https://swb.one/docs/setup/telegram.

Ask me to choose a name for the agent.
Use up to 40 lowercase letters, digits and dashes.
Start with a letter or digit.
Use my chosen name in place of `my-tool`.
Use a SwB client with owner access for setup.
Call `connect(what="agent-tool", choose={"agent":"my-tool"})`.
Alternatively, run this command in my SwB folder.

```sh
swb connect agent-tool --choose agent=my-tool
```

Keep the existing key if the client already has one.
If SwB returns a pairing code, redeem it in a private terminal within ten minutes.
Use `POST /connect`.
Send `{"code":"<pairing code>"}` in the request body.
Send a `User-Agent` header that names the app on each HTTP request to SwB.
Read the response in memory.
Add a server named `swb` in the client settings.
Use `mcp_url` as the Streamable HTTP address.
Streamable HTTP lets the app call SwB tools over the network.
Use the returned `key` in the `Authorization: Bearer <key>` header.
Store the key in the app's private settings.
Keep credentials private.

## Messages

Use MCP to read messages, send replies and share memory.
Use `wait` or `inbox` to read messages from an open conversation.

## Personal agents

MCP Events sends a notice when a message arrives.
Call `events/list` to see the events SwB offers.
Use `events/subscribe` for `switchboard.message.created` with `mailbox_id` set to my agent name.
Use webhook delivery with a public HTTPS callback address.
A callback is the address that receives notices.
Use a secret with the prefix `whsec_` and a base64 value that decodes to 24 to 64 bytes.
Use the same secret to verify Standard Webhooks signatures at the callback.
Read the message with `get_message`.
Answer with `reply` and the conversation handle.

For a local webhook, pair the companion with the webhook feature.
A webhook receives messages at a web address.
Save the webhook configuration in `~/.config/swb/webhook/my-tool.json`.
Use my chosen agent name in the file name and the `name` field.
Set the required `key_env` field to a `SWB_KEY_<NAME>` variable.
Save my agent's paired SwB key in that variable on the companion device.
The companion uses this key to register the delivery route with SwB.
Use a loopback address or an address on a private or shared network.
A loopback address reaches the same device.
A shared address is in the `100.64.0.0/10` range.
Use a separate receiver secret to sign messages or send a bearer token.
Give the receiver and the companion the same receiver secret.
On the companion device, save it in the variable named in `secret_env`.
For `standard-webhooks`, use the same secret format as for MCP Events.
For a receiver that expects a bearer token, set `auth` to `bearer`.
For bearer tokens, use an HTTP loopback address or an HTTPS address.

```sh
swb companion connect https://swb.example.com --cap webhook
```

```json
{"name":"my-tool","url":"http://127.0.0.1:8644/webhooks/swb","auth":"standard-webhooks","key_env":"SWB_KEY_MY_TOOL","secret_env":"SWB_WEBHOOK_MY_TOOL"}
```

Use the handle from the webhook to answer with `reply`.
Call `inbox` when the agent starts.
The mailbox keeps messages if webhook delivery fails.
Use one delivery method per mailbox.

## Status hooks

Use a status hook if the tool can run a command when a session changes.
Use the same agent name in the command and in MCP.
Pair the device with a companion if I want to use status hooks or a driver.
A companion is a SwB program that connects this device to my SwB server.
Install the `swb` command with `pipx install swb.one`.
Use `companion_code` from the owner client.
Enter the code at the companion's hidden prompt.
Replace the example address with my SwB address.
Add one option for each feature I use.

```sh
swb companion connect https://swb.example.com --cap hooks --cap ade
```

Send one JSON object to `swb hook <name>` on standard input.
Use the session ID from the tool.
Use the folder and label from the session.
Set `event` to `start`, `working`, `blocked`, `idle`, `done` or `ended`.
Include `text` for the answer on a `done` event.

```json
{"event":"done","session":"session-1","name":"My session","dir":"/path/to/project","text":"Test complete"}
```

If a plugin can deliver messages to the session, read wakes from `swb hook <name> --wakes <session>`.
A wake is a message for an open session.
Read one JSON object per line.
Use the tool's supported interface to deliver the `text` to the session.

## CLI session driver

Use a driver if the tool has commands to list and message open sessions.
A driver is a file that maps SwB operations to commands from the tool.
Use Linux or macOS for a custom driver.
Save the driver as a JSON file in `~/.config/swb/ade/`.
Use `$XDG_CONFIG_HOME/swb/ade/` if that environment variable is set.
The account that runs the companion must own the folder and file.
Set `name` to the same agent name.
Set `available`, `list` and `send` to the tool's commands.
Use `argv` to list each command and its arguments separately.
Use JSON output for the session list and reply.
Map the session ID, label, folder and state from the list response.
In `send`, use `{session}` as a separate argument for the session ID.
Use `"stdin": "text"` or put `{text}` immediately after a separate `--` argument.
Use `check` to confirm a session is idle before sending.
Use `wait` and `read` when the tool returns its reply from a separate command.
Use only the tool's documented commands.

Use this example for the structure.
Replace the example commands and JSON fields with those of my tool.

```json
{
  "name": "my-tool",
  "available": {"argv": ["my-tool", "status", "--json"]},
  "list": {
    "argv": ["my-tool", "sessions", "--json"],
    "sessions": "sessions",
    "id": "id",
    "name": "label",
    "dir": "folder",
    "state": "status",
    "states": {"ready": "idle", "running": "working", "approval": "blocked"}
  },
  "check": {
    "argv": ["my-tool", "sessions", "show", "{session}", "--json"],
    "expect": {"id": "{session}", "status": "ready"}
  },
  "send": {
    "argv": ["my-tool", "sessions", "send", "{session}", "--json", "--", "{text}"],
    "reply": "answer"
  }
}
```

Replace `my-tool` with my agent name in the file name.
Set these permissions for new and existing manifests before you call `targets`.
The folder needs mode `0700`. The file needs mode `0600`.

```sh
chmod 00700 "${XDG_CONFIG_HOME:-$HOME/.config}/swb/ade"
chmod 0600 "${XDG_CONFIG_HOME:-$HOME/.config}/swb/ade/my-tool.json"
```

Call `targets(block="local")` from the owner client.
Send a test message to the session address returned by `targets`.
Read the answer with `result` and the conversation from `send`.

## Check the connection

Call `inbox` from the paired client.
Call `swb_guide(trigger="setup")` from the same client.
From the owner client, use `send` to send a test message to `agent:my-tool`.
Replace `my-tool` with my chosen name.
Read it with `inbox` from the paired client.
Answer with `reply` and the item's handle.
Read the answer from the owner client with `result` and the conversation from `send`.

Call `memory_summary` from the paired client.
Read the tool's shared notes, such as `AGENTS.md`.
Select facts to share.
Keep credentials and private details on the device.
Use `memory_sync` with `source="agents-md"`.
Give each fact a stable `source_key`.
Use the project slug for project memory.
````

把提示词粘贴到以 SwB 所有者身份登录的智能体中。

MCP 是应用使用外部工具的方式。选一个简短的智能体名称。让以所有者身份登录的智能体按下面的示例调用 `connect`。

```python
connect(what="agent-tool", choose={"agent":"example-agent"})
```

名称最多 40 个字符，可用小写英文字母、数字和连字符。开头用小写英文字母或数字。

收到配对码后，换取[智能体密钥](/zh-Hans/docs/setup/mcp-client#获取密钥)。这样 SwB 就能给同名的本地会话发送消息。

<Tabs items={['MCP', '本地会话']}>
<Tab value="MCP">
把 SwB 返回的设置添加到应用。将密钥保存在应用的私有设置中。SwB 用你选的名称显示这个智能体。

从另一个 SwB 应用发送一条测试消息。在已连接的应用中调用 `inbox`。用收到的 `handle` 调用 `reply` 回复。

应用也可以通过[收件箱事件流](/zh-Hans/docs/connect#收件箱事件流)接收消息。
</Tab>
<Tab value="本地会话">
如果应用提供列出会话和发送消息的命令，使用[驱动](#driver-manifest)。如果应用能在状态改变时运行命令，使用[状态钩子](#status-hooks)。

按[配对设备](/zh-Hans/docs/companion#配对设备)中的说明操作。

让以所有者身份登录的智能体调用 `targets(block="local")`。向返回的完整地址 `local:session:<companion>:<harness>:<session>` 发送测试消息。用 `result` 查看回复。
</Tab>
</Tabs>

## 个人智能体 [#personal-agents]

MCP Events 会在新消息到达时发出通知。回调地址用来接收通知。

通过 `events/list` 查看可用事件。用 `events/subscribe` 订阅 `switchboard.message.created`。将 `mailbox_id` 设为配对时选的智能体名称。

使用 Webhook 投递时，请使用公网 HTTPS 回调地址。密钥由 `whsec_` 前缀和 base64 值组成。base64 部分解码后应为 24 至 64 字节。接收端使用同一密钥验证 Standard Webhooks 签名。

用通知中的 `message_id` 调用 `get_message` 读取消息。用对话标识调用 `reply` 回复。

应用在同一设备或专用网络上接收消息时，使用 [Companion 的本地 Webhook](/zh-Hans/docs/companion#local-webhook)。

```sh
swb companion connect https://swb.example.com --cap webhook
```

用配对所得的 SwB 密钥授权投递路由。在 `key_env` 中填写存放这把密钥的 `SWB_KEY_<NAME>` 变量名。用单独的接收端密钥签名或用作 Bearer 令牌。

每个邮箱选择一种投递方式。

### 保留现有聊天连接 [#existing-channels]

告诉负责设置的智能体，哪些聊天应用已接入你的智能体。

保留现有 Telegram 机器人的设置。保留现有微信 ClawBot 的设置和绑定。每个 Telegram 机器人令牌只由一个程序接收消息。每个绑定只运行一个微信 ClawBot 实例。

通过 MCP 将智能体单独配对到 SwB。用 MCP Events 或本地 Webhook 接收新消息通知。若要将 Telegram 直接接入 SwB，请用新令牌[设置一个独立的机器人](/zh-Hans/docs/setup/telegram)。

## 驱动配置文件 [#driver-manifest]

把驱动设置保存为 JSON 文件。保存目录是 `$XDG_CONFIG_HOME/swb/ade/`。默认目录是 `~/.config/swb/ade/`。

在 Linux 或 macOS 上使用自定义驱动。目录和文件必须归运行 Companion 的账户所有。把 `example-agent` 换成你的智能体名称。目录权限设为 `0700`。文件权限设为 `0600`。已有配置也使用这些权限。

```sh
chmod 00700 "${XDG_CONFIG_HOME:-$HOME/.config}/swb/ade"
chmod 0600 "${XDG_CONFIG_HOME:-$HOME/.config}/swb/ade/example-agent.json"
```

| 字段 | 用途 |
| - | - |
| `name` | SwB 使用的智能体名称 |
| `available` | 检查工具是否可用的命令 |
| `list` | 列出会话的命令，以及名称和状态等字段在输出中的位置 |
| `check` | 发送前检查会话状态的命令 |
| `send` | 把消息交给会话的命令 |
| `wait`, `read` | 等待会话处理完成和读取回复的命令 |

用 `sessions` 指定数组的位置。如果输出本身就是数组，填写空字符串。用 `id`、`name`、`dir` 和 `state` 指定各个字段的位置。在 `states` 中把工具的状态对应到 SwB 的状态。

在 `argv` 数组中填写命令及其参数。在 `send` 中把 `{session}` 作为单独的参数填写。把 `{text}` 放在参数 `--` 紧接的位置，或设置 `"stdin": "text"`，通过标准输入传入消息。

下面是 Paseo 的发送命令示例。

```json
{
  "send": {
    "argv": ["paseo", "send", "{session}", "--no-wait", "--json", "--", "{text}"],
    "expect": {
      "agentId": "{session}",
      "status": "sent"
    }
  }
}
```

从发送命令的输出中读取回复时，使用 `send.reply`。用单独的命令读取回复时，使用 `read.reply`。

参阅[内置驱动示例](https://github.com/ZhehanZhang/SwB/tree/release/1.0/src/switchboard/companion/caps/ade_presets)。

在使用驱动的设备上启动 Companion。把示例地址换成你的 SwB 地址。

```sh
swb companion connect https://swb.example.com --cap ade
```

## 状态钩子 [#status-hooks]

钩子是在会话状态改变时运行的命令。用 `--cap hooks` 启动 Companion。在应用中运行 `swb hook <name>`，通过标准输入传入一个 JSON 事件。`<name>` 是 SwB 使用的智能体名称。

```sh
swb companion connect https://swb.example.com --cap hooks
```

同一会话的所有事件都使用相同的 `session`。

```json
{"event":"start","session":"example-session","name":"Example session","dir":"/path/to/workspace"}
```

`event` 可取 `start`、`working`、`blocked`、`idle`、`done` 或 `ended`。发送 `done` 事件时，把回答填入 `text`。

对每个打开的会话运行 `swb hook <name> --wakes <session>`。此命令为每条消息输出一行 JSON，其中包含 `text` 和 `conversation`。用应用的 API 把文本交给会话。在消息流打开前，消息留在智能体的邮箱中。
