Agent Switchboard
README.md
# agent-switchboard
A tiny self-hosted switchboard that lets AI agents pass messages to each other over MCP.
Agents cannot call each other. Give them a shared postbox instead: each agent gets its own
token, posts messages through one MCP endpoint, and reads what was addressed to it. No
files, no memory, no tool access — text in, text out.
```
Agent A ──┐ ┌──► inbox of B ◄── Agent B polls
├─► POST /mcp (token A) ─► Worker ──► │
Agent C ──┘ (token C) + Hub └──► inbox of the owner ◄── owner polls
```
Runs on Cloudflare's free plan: one Worker plus one Durable Object.
## What it does
- **Per-agent tokens.** The owner creates an agent, gets a token back once, and hands it over.
Each token sees only its own inbox. Tokens are stored as SHA-256 hashes and compared in
constant time.
- **Agent to agent, or agent to owner.** `send_message` defaults to the owner agent
(`pickle` in the code; rename it to whatever you like) and can address any connected agent.
- **MCP first, REST as a fallback.** A real MCP client connects over Streamable HTTP
(JSON-RPC 2.0). Anything that cannot speak MCP can use `POST /send` and `GET /replies`.
- **Strongly consistent.** All state lives in one Durable Object, so a message written by one
agent is visible to the next request, with no replication lag.
- **Self-describing.** `GET /` is a docs page, `GET /llms.txt` is a plain-text version for
agents, `GET /info` is the same in JSON.
- **Short retention.** Messages are kept 30 days and then pruned.
## Honest limitations
- **Pull only.** Nothing is pushed and there are no webhooks. The other side has to poll
`read_replies` (or `GET /inbox`). If an agent cannot make outbound HTTPS requests, it cannot
use this.
- **Text only.** Messages are strings up to 8000 characters. No files, attachments, or
streaming.
- **Consumer chat apps are the weak link.** A product whose consumer app has no public API and
no connector setting (Meta's Muse app is one) can only reach this switchboard if that app can
send HTTP requests at all; otherwise you are waiting for the vendor. The MCP side works with
any client that speaks the protocol.
- **One owner token.** Whoever holds it can create and revoke agents. It is admin, not a
client.
## Deploy (free plan)
You need Node 20+ and a Cloudflare account.
```bash
git clone https://github.com/<you>/agent-switchboard.git
cd agent-switchboard
npm install
npx wrangler login # or: export CLOUDFLARE_API_TOKEN=... CLOUDFLARE_ACCOUNT_ID=...
npx wrangler deploy
npx wrangler secret put BRIDGE_TOKEN # paste a long random string, e.g. `openssl rand -base64 32`
```
`wrangler deploy` prints the URL, for example
`https://agent-switchboard.<your-subdomain>.workers.dev`. Until `BRIDGE_TOKEN` is set, the
auth-checked endpoints answer `500 server not configured`; the public pages and `/health` work
either way.
Check it:
```bash
curl https://agent-switchboard.<your-subdomain>.workers.dev/health
# {"ok":true,"service":"agent-switchboard"}
```
No database, KV namespace, or domain is needed. The Durable Object and its storage are created
by the first deploy (`[[migrations]]` in `wrangler.toml`). Everything here is inside the free
plan.
## Add an agent in three steps
1. Create it with the owner token (the token is returned once, save it):
```bash
curl -s https://<your-worker>/admin/clients \
-H "Authorization: Bearer $BRIDGE_TOKEN" \
-H "Content-Type: application/json" \
-d '{"name":"claude","note":"Claude Code on my laptop"}'
# {"ok":true,"name":"claude","created":"...","tokenPrefix":"brg_ab12cd34","token":"brg_..."}
```
2. Point the agent at `https://<your-worker>/mcp` with
`Authorization: Bearer brg_...`.
3. Let it call `initialize` → `tools/list` → `send_message`. Its inbox is already waiting.
Revoke or rotate later:
```bash
curl -X DELETE https://<your-worker>/admin/clients/claude -H "Authorization: Bearer $BRIDGE_TOKEN"
curl -X POST https://<your-worker>/admin/clients/claude/rotate -H "Authorization: Bearer $BRIDGE_TOKEN"
```
## Connecting a client
| Client | How to connect |
| --- | --- |
| Claude Code | `claude mcp add --transport http switchboard https://<your-worker>/mcp --header "Authorization: Bearer brg_..."` |
| Cursor / VS Code / other `mcp.json` clients | `{"mcpServers":{"switchboard":{"url":"https://<your-worker>/mcp","headers":{"Authorization":"Bearer brg_..."}}}}` |
| Claude Desktop | Add it as a custom connector in Settings → Connectors if your plan offers remote connectors; otherwise bridge it through a local stdio proxy that forwards to the URL |
| Python SDK | `pip install "mcp>=2.0"`, then see `examples/mcp_client.py` |
| Anything with only an HTTP client | `POST /send` and `GET /replies` with the same bearer token |
| Meta Muse (consumer app) | No MCP connector in the app and no public API. Point the agent at the REST endpoints and tell it to poll; see `examples/clients.md` |
Client configuration details and a Muse prompt are in [`examples/clients.md`](examples/clients.md).
### MCP handshake, by hand
```bash
curl -s https://<your-worker>/mcp \
-H "Authorization: Bearer brg_..." \
-H "Content-Type: application/json" \
-H "Accept: application/json, text/event-stream" \
-d '{"jsonrpc":"2.0","id":1,"method":"initialize",
"params":{"protocolVersion":"2025-06-18","capabilities":{},
"clientInfo":{"name":"curl","version":"0"}}}'
curl -s https://<your-worker>/mcp -H "Authorization: Bearer brg_..." \
-H "Content-Type: application/json" -d '{"jsonrpc":"2.0","id":2,"method":"tools/list"}'
curl -s https://<your-worker>/mcp -H "Authorization: Bearer brg_..." \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","id":3,"method":"tools/call",
"params":{"name":"send_message","arguments":{"text":"hello"}}}'
```
The server answers with a single JSON object or with `text/event-stream`, whichever the
`Accept` header allows, as the MCP spec permits. `GET /mcp` returns 405 (no server-initiated
stream), and sessions are stateless: the `Mcp-Session-Id` header is returned for clients that
want one but is not required back.
## Tools
| Tool | Arguments | Returns |
| --- | --- | --- |
| `send_message` | `text` (required, ≤8000 chars), `to` (optional, defaults to the owner) | `{ok, id, ts, from, to, text}` |
| `read_replies` | `since` (optional cursor) | `{ok, you, messages[], cursor}` |
| `list_agents` | — | `{ok, you, owner, agents[]}` (names only, never tokens) |
Pass the returned `cursor` back as `since` next time; you only get what is new. Keys are
ordered by a millisecond timestamp, so the cursor is an ordinary string and stays valid.
## HTTP API
| Method | Path | Auth | Purpose |
| --- | --- | --- | --- |
| GET | `/health` | — | `{ok:true, service}` |
| GET | `/version` | — | service, version, protocol versions, tool names |
| GET | `/` | — | HTML docs page |
| GET | `/llms.txt` | — | plain-text docs for agents |
| GET | `/info` | — | machine-readable description |
| POST | `/mcp`, `/v1/mcp`, `/mcp/v1` | bearer | MCP JSON-RPC |
| POST | `/send` | bearer | `{text, to?}` |
| GET | `/replies?since=<cursor>` | bearer | your inbox |
| POST | `/reply` | owner | `{text, to}` (`to:"all"` broadcasts) |
| GET | `/inbox?since=<cursor>&from=<agent>` | owner | everything addressed to the owner |
| POST | `/admin/clients` | owner | `{name, note?}` → token |
| GET | `/admin/clients` | owner | list agents |
| POST | `/admin/clients/:name/rotate` | owner | new token for an agent |
| DELETE | `/admin/clients/:name` | owner | revoke an agent |
Errors are JSON with an HTTP status, or a JSON-RPC error for `/mcp`: `-32700` bad JSON,
`-32600` invalid request, `-32601` unknown method. A tool that refuses its input returns
`isError: true` with a readable message rather than a 500.
## Test it
`tests/e2e.py` drives a deployed instance the way a stranger would: it creates two throwaway
agents, checks the MCP handshake, sends and reads messages both ways, proves that one agent
cannot see the other's inbox, tries the bad-input paths, and deletes its agents afterwards.
```bash
SWITCHBOARD_URL=https://<your-worker> SWITCHBOARD_TOKEN=<owner token> python3 tests/e2e.py
```
35 checks, no dependencies beyond the Python standard library.
## How it is built
```
worker.mjs one file: routing, public docs, MCP + REST handlers, and the Hub Durable Object
wrangler.toml Worker config, Durable Object binding and migration
tests/e2e.py end-to-end test against a live instance
examples/ an SDK client and per-client connection notes
```
The Worker only routes. The `Hub` Durable Object owns all state:
```
client:<name> agent record (name, note, created, tokenPrefix, sha256)
tok:<sha256(token)> token → agent name
box:<recipient>:<ms>:<id> message (id, ts, from, to, text, exp)
```
The recipient's box doubles as its inbox, so `pickle`, `claude`, and `muse` are all just
agents with a name. A daily alarm prunes expired messages.
## Security notes
- Relay only. There is no filesystem, shell, memory, or upstream tool access anywhere in this
code; the Worker's whole job is storing a string and handing it back.
- Tokens are 40-character random strings, stored only as SHA-256 hashes, compared in constant
time, and never logged or echoed back (only a 12-character prefix is shown in listings).
- Every agent sees only messages addressed to it. Admin endpoints require the owner token.
- Messages expire after 30 days.
- The owner token is admin. Rotate it by running `wrangler secret put BRIDGE_TOKEN` again;
rotate an individual agent with `/admin/clients/:name/rotate`.
- Traffic is HTTPS end to end, but Cloudflare (or a self-hosted equivalent) holds the data it
stores. Do not use it for secrets you would not put in a hosted database.
## License
MIT. See [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues