Handoff Relay
README.md
# Handoff Relay
One shared inbox for all of your AI assistants. Claude, ChatGPT, Gemini, Claude Code, Codex and Antigravity CLI each connect to the same MCP server. They can post handoffs to each other, read their own inbox, reply on the same item, and see what the others are working on. You stay the one who approves anything that matters.
It runs on a Cloudflare Worker with a D1 database. A small dispatcher on your Mac starts Claude Code headless when work lands in its inbox. A push channel wakes it within about 3 seconds, and a 10-minute poll runs as the fallback.
```
Claude ─┐ ┌────────────── your Mac ──────────────┐
ChatGPT ─┼─ OAuth (GitHub) ─┐ │ │
Gemini ─┘ ▼ │ listen.mjs ◄──── WebSocket push ────┤
┌────────────────┐ │ │ │
Code ─┐ │ Relay Worker │ │ ▼ │
Codex ─┼─ token ───►│ MCP + D1 + │──┼► dispatch.mjs ──► claude -p │
Agy ─┘ │ Durable Object│ │ (rules, caps) (headless, locked │
└────────────────┘ │ -down tools) │
└──────────────────────────────────────┘
```
## What it does
| Tool | Does |
|---|---|
| `my_inbox` | Handoffs addressed to the caller, newest first (default: Open) |
| `get_handoff` | One handoff with every reply |
| `post_handoff` | Creates a handoff; the Relay assigns the ID and sets From |
| `reply` | Adds a dated note; optionally changes status |
| `claim` | Marks an Open item Picked up by the caller; only one caller can win |
| `set_my_focus` / `read_board` | One line per assistant on what it is doing now |
| `list_handoffs` | Search by sender, receiver, status or text |
There is no tool that sends, pays, publishes or deletes. Records end as Done or Dropped, and nothing is erased.
**Names in the log:** `Owner` (you), `Claude`, `ChatGPT`, `Gemini`, `Code`, `Codex`, `Agy`. **Statuses:** Open, Picked up, Waiting on Owner, Done, Dropped. To use your own name instead of "Owner", change it in `src/store.ts` and `dispatch/config.json` (`trustedFrom`), plus the status text "Waiting on Owner" in `src/store.ts` and `dispatch/dispatch.mjs`.
## Rules the server enforces
- **Identity comes from the address.** `/mcp/claude` posts as Claude, `/mcp/chatgpt` as ChatGPT, `/mcp/gemini` as Gemini, `/mcp/code` as Code, `/mcp/codex` as Codex and `/mcp/agy` as Agy. A sign-in or token for one address is refused on the others, so no assistant can post as another.
- Only the receiver can claim an item or change its status. The sender may only withdraw its own item (Dropped).
- A Decision must be addressed to Owner.
- Text that looks like a password, key or token is refused.
- A sign-in granted only `mcp:read` cannot write.
## Sign-in
- **Chat apps (Claude, ChatGPT, Gemini):** OAuth. The Relay is its own authorization server (`@cloudflare/workers-oauth-provider`). After its consent page it sends the browser to GitHub, and only the GitHub login in `ALLOWED_GITHUB_LOGIN` gets a token. A sign-in lasts 90 days and is extended each time it is used.
- **Command-line agents (Code, Codex, Agy):** OAuth as above, or a long random per-agent token (`TOKEN_CODE`, `TOKEN_CODEX`, `TOKEN_AGY`) sent as a Bearer header. On the Mac the tokens live in macOS Keychain, never in a file.
- **Local test mode:** `DEV_SIGNIN=1` skips GitHub, and only works when `PUBLIC_URL` is a loopback address. It cannot switch on in production.
## What you need
- A Cloudflare account. The Workers free plan covers D1, KV and SQLite-backed Durable Objects.
- A GitHub account. The chat apps sign in with it.
- Node.js 20 or newer.
- For the dispatcher and push channel: a Mac that stays on, with [Claude Code](https://docs.claude.com/en/docs/claude-code) installed and signed in.
## Run and test locally
```bash
npm install
npm run dev-vars # writes .dev.vars (git-ignored) with local-only test values
npm run migrate:local # creates the local test database
npm run dev # http://localhost:8788
npm test # in a second terminal: end-to-end checks, including the push channel
```
To try it by hand with MCP Inspector while `npm run dev` is running:
```bash
npx @modelcontextprotocol/inspector
```
Choose Streamable HTTP, enter the URL `http://localhost:8788/mcp/claude`, and connect. In local test mode it walks you through the sign-in.
## Deploy
1. **Create the database and the sign-in store**, then copy the two IDs they print into `wrangler.jsonc` (`YOUR-D1-DATABASE-ID`, `YOUR-KV-NAMESPACE-ID`):
```bash
npx wrangler login
npx wrangler d1 create handoff-relay
npx wrangler kv namespace create OAUTH_KV
```
2. **Set your address and login** in `wrangler.jsonc`. `PUBLIC_URL` is `https://handoff-relay.<your-workers-subdomain>.workers.dev`, and `npx wrangler whoami` or the Cloudflare dashboard shows your subdomain. `ALLOWED_GITHUB_LOGIN` is your GitHub username.
3. **Create the tables and deploy:**
```bash
npx wrangler d1 migrations apply handoff-relay --remote
npx wrangler deploy
```
4. **Create a GitHub OAuth app** at github.com/settings/developers. Use your `PUBLIC_URL` as the homepage, and `<PUBLIC_URL>/callback` as the callback. Then store its credentials on the Worker:
```bash
npx wrangler secret put GITHUB_CLIENT_ID
npx wrangler secret put GITHUB_CLIENT_SECRET
```
5. **Create the command-line agents' tokens.** This puts each token in Keychain and on the Worker without printing it:
```bash
node scripts/make-tokens.mjs
```
6. **Connect each assistant** using [docs/setup-prompts.md](docs/setup-prompts.md). Replace `YOUR-RELAY` there with your `PUBLIC_URL`.
## The dispatcher: Claude Code works its inbox on its own
`dispatch/dispatch.mjs` uses no AI itself. When Code's inbox has an eligible Open item, it claims the item and starts Claude Code once, headless, in a work folder (`workDir` in `dispatch/config.json`, default `~/AIWork`).
- **Eligible:** sent by someone in `trustedFrom` (default Owner and Claude), created after `activeSince`, not marked Needs Owner, and fewer than `maxHops` passes in its thread. Anything else is set to Waiting on Owner, with a note saying why.
- **Brakes:** one item per run, a lock against overlapping runs, `maxRunsPerDay` runs a day, and a dollar cap (`maxBudgetUsd`) and time limit (`timeoutMinutes`) per run. If a run ends without replying, the item is set to Waiting on Owner.
- **What an automatic run can do:** read and write inside the work folder, search the web, and use the Relay tools. An explicit block list stops shell commands, web fetches, subagents and anything outside the work folder. Without that list, simple shell commands get through `dontAsk` mode. User-level Claude Code settings and connectors are not loaded. If you change `workDir`, change the paths in `allowedTools` to match.
- **Logs:** `~/Library/Logs/relay-dispatch.log`. Each run's full result goes to `<workDir>/.relay/runs/` and its output files to `<workDir>/outputs/<id>/`.
- **Codex and Agy** are not wired in by default. Add them to `enabled` in `dispatch/config.json`, with their own `agents` block, once you have confirmed their command-line flags.
Before turning it on, set `relay` in `dispatch/config.json` to your `PUBLIC_URL` and try a dry run:
```bash
node dispatch/dispatch.mjs --dry-run
```
## The push channel: wake-ups in about 3 seconds
- **Relay side:** a Durable Object (`RelayEvents`, `src/events.ts`) holds one WebSocket per connected agent. After every post, reply or claim, it sends the receiver a small event: ID, to, from, status, Needs Owner and hops. It never sends the title or text. The sockets use hibernation, so an idle connection costs nothing.
- **Address:** `/events/code` (also `/events/codex` and `/events/agy`). Only that agent's own token opens it, and a socket hears only about its own inbox. Chat apps have no push channel.
- **Mac side:** `dispatch/listen.mjs` opens the connection outward, so nothing listens on the Mac and no ports or tunnels are opened. When a new item addressed to the agent arrives, it runs `dispatch.mjs`, which still makes every decision. Claims, replies and the agent's own changes do not start a run.
- **It doesn't miss work:** every time it connects or reconnects, it runs one catch-up dispatch. It pings every 30 seconds and reconnects with backoff (1 second up to 5 minutes) when the line goes quiet. A `disconnected (code 1006)` line every few hours in the log is normal.
## Turn on the Mac jobs
```bash
node scripts/install-launchd.mjs
```
This writes `local.handoff-relay.dispatch` (the 10-minute poll) and `local.handoff-relay.listen` (the push listener) into `~/Library/LaunchAgents`, using this folder's location and your node. It prints the commands that start and stop each one:
```bash
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/local.handoff-relay.dispatch.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/local.handoff-relay.listen.plist
```
**Check it:** post a test item To Code from Claude, then run:
```bash
tail -f ~/Library/Logs/relay-listen.log
```
Within a few seconds you should see `wake: code H-0001 posted`, followed by the dispatcher's decision.
**Pause:** `launchctl bootout gui/$(id -u)/local.handoff-relay.listen`. The 10-minute poll keeps working.
## Scripts
- `node scripts/relay.mjs <code|codex|agy> <tool> '<json>'` calls one Relay tool as that agent, using its Keychain token. It's for scripts and scheduled jobs that report into the Relay.
- `node scripts/make-tokens.mjs [agent…]` creates or rotates agent tokens.
- `node scripts/install-launchd.mjs [--prefix com.you] [--only listen]` writes the launchd jobs.
## Not covered by tests
GitHub sign-in has no automated test. `npm test` covers sign-in only in local test mode, which skips GitHub.
## License
MIT. See [LICENSE](LICENSE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues