# Agent tool

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

An agent tool runs one or more agents. MCP lets an agent use outside tools. A local session is an open agent conversation on a paired device.

## Copy the setup prompt [#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.
````

Paste the prompt into a SwB owner agent. It calls `connect(what="agent-tool", choose={"agent":"example-agent"})` to get the settings. Start the name with a lowercase letter or digit. Use up to 40 lowercase letters, digits or hyphens. SwB uses the name to label the agent.

Use the pairing code to [get the agent key](/docs/setup/mcp-client#get-the-key). This activates the agent name for local delivery. Keep the key in private settings.

<Tabs items={['MCP', 'Local session']}>
<Tab value="MCP">
Add SwB to your tool with the returned settings and the agent key.

Send a test message to the agent name. Ask the agent to read it with `inbox`. Ask the agent to answer with `reply`. The agent can use the memory you share with it.
</Tab>
<Tab value="Local session">
Use a [driver manifest](#driver-manifest) if the tool has commands to list sessions and send work. Use [status hooks](#status-hooks) if the tool can run a command when its state changes.

Enter the one-time code when you pair your device. Ask your owner agent to call `targets(block="local")`. Use the full session address from the result to send a test message. Read the answer with `result`.
</Tab>
</Tabs>

## Personal agents [#personal-agents]

MCP Events sends a notice when a message arrives. A callback is the address that receives these notices.

Call `events/list` to see the available events. Use `events/subscribe` for `switchboard.message.created`. Set `mailbox_id` to the paired agent name.

Use webhook delivery with a public HTTPS callback address. 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` and the event's `message_id`. Answer with `reply` and the conversation handle.

Use a [local webhook](/docs/companion#local-webhook) for a receiver on your device or private network.

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

Use the paired SwB key to authorize the route. Set `key_env` to its `SWB_KEY_<NAME>` variable. Use a separate receiver secret for signing or bearer authentication.

Use one delivery method per mailbox.

### Keep your chat connections [#existing-channels]

Tell the setup agent which chat apps already reach your agent.

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 your agent with SwB over MCP as a separate connection. Use MCP Events or a local webhook for notices about new messages. To connect Telegram directly to SwB, [set up a separate bot](/docs/setup/telegram) with a new token.

## Driver manifest [#driver-manifest]

A driver manifest is a JSON file that tells the companion which commands to run. Save it as `~/.config/swb/ade/<name>.json`. If you set `XDG_CONFIG_HOME`, use its `swb/ade` folder.

Use Linux or macOS for a custom driver. The account that runs the companion must own the folder and file. Replace `example-agent` with your agent name. Use mode `0700` for the folder. Use mode `0600` for the file. Apply these permissions to existing manifests too.

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

Set `name` to your agent name. Set `available` to a command that succeeds when your tool is ready. Set `list` to a command that returns sessions as JSON. Use `sessions` for the path to the session array. Use an empty string for a top-level array. Map `id`, `name`, `dir` and `state` to the session fields. Use `states` to map tool states to SwB states.

Each command uses an `argv` array. The first entry is the executable. Each later entry is one argument. Use `{session}` in `send` to select the open session. To pass the text as an argument, put `{text}` immediately after the literal `--` argument. To pass the text on standard input, set `"stdin": "text"`.

Use `check` to confirm that the session can receive new work. Set `send.reply` to read an answer from the send command. For a separate command, set `read` to that command. Set `read.reply` to the answer field. Use `wait` if the tool has a command to wait for the answer. See the [bundled examples](https://github.com/ZhehanZhang/SwB/tree/release/1.0/src/switchboard/companion/caps/ade_presets).

[Pair the device](/docs/companion#pair-a-device) with the driver feature.

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

## Status hooks [#status-hooks]

A hook runs a command when the tool reports a change. [Pair the device](/docs/companion#pair-a-device) with the hooks feature.

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

Use your paired agent name in `swb hook <name>`. Pass one JSON object on standard input. Keep the same session ID for each event in that session.

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

Use `start`, `working`, `blocked`, `idle`, `done` or `ended` as the event. Put the answer in `text` on the `done` event.

Run `swb hook <name> --wakes <session>` to read work for that session. Each line contains a JSON object with `text` and `conversation`. Pass the text to the open session through the tool's own interface. SwB returns the answer to the sender's conversation.
