Agent MCP Federation
README.md
# Agent MCP Federation
A starter repository for self-hosted MCP servers that let agents hand off tasks and exchange replies. Each operator configures the agents they trust. Peers connect directly over HTTPS and identify their skills through an agent card.
The local agent uses MCP over stdio. Its server uses MCP Streamable HTTP to send a task to a peer. The receiver stores the task in SQLite, and its agent reads the inbox and replies. Use the same `threadId` for follow-up tasks. [MCP transport specification](https://modelcontextprotocol.io/specification/2025-11-25/basic/transports)
```text
Alice agent ── stdio ── Alice MCP ── HTTPS/MCP ── Bob MCP ── stdio ── Bob agent
│
SQLite inbox
```
## Run the two-agent demo
Requires Node.js 24.2+ and npm.
```sh
npm ci
npm run check
npm test
```
The integration test starts two local MCP listeners and two stdio MCP processes. They exchange tasks in both directions in one thread and recover a reply after the receiver listener restarts. The test also checks retries, conflicting IDs, peer isolation and missing credentials. It uses temporary databases and local HTTP.
## Deploy on Cloudflare Workers
Each deployment has an agent card, a peer list and a D1 inbox. The included [Wrangler config](./wrangler.jsonc) uses a placeholder database ID. Copy it to `wrangler.agent.local.jsonc` and set the Worker name, `vars.FEDERATION_CONFIG`, and the D1 name and ID. Keep the D1 binding named `DB`. `FEDERATION_CONFIG` has the same `agent` and `peers` fields as the Node.js config. Peer URLs must use HTTPS.
```sh
npm ci
npx wrangler login
npx wrangler d1 create agent-mcp-inbox
```
Copy the database ID from Wrangler into your local config. Use a separate database per agent. Set the secret names in `secrets.required` to include `OWNER_TOKEN` and the token names referenced by your peer configuration. Put the values in a private JSON file outside the repository. Use a random base64url token for each owner and direction as described below. An owner token grants all six agent tools, including access to the inbox and the ability to send tasks.
```sh
npx wrangler d1 migrations apply DB --remote --config wrangler.agent.local.jsonc
npm run deploy:worker -- --config wrangler.agent.local.jsonc --secrets-file /path/to/private-secrets.json
```
| Endpoint | Credential | Tools |
| --- | --- | --- |
| `/mcp` | Incoming token issued to this peer | `agent_card`, `task_deliver`, `task_result` |
| `/agent/mcp` | `OWNER_TOKEN` | All six agent tools listed below |
Use the Worker's `https://…workers.dev` URL or a custom domain. Both routes accept MCP Streamable HTTP with bearer authentication. Peers send tasks to the configured `/mcp` URL. The operator's agent uses `/agent/mcp` and supplies the owner token through an environment variable.
```sh
codex mcp add federation --url https://YOUR-WORKER.workers.dev/agent/mcp --bearer-token-env-var FEDERATION_OWNER_TOKEN
```
Set `FEDERATION_OWNER_TOKEN` in the environment that starts Codex. Each peer keeps using the same `agent-mcp-federation/1` wire protocol, including peers running the Node.js version.
To verify two live deployments, set `MCP_ALPHA_URL`, `MCP_BETA_URL`, `MCP_ALPHA_OWNER_TOKEN`, and `MCP_BETA_OWNER_TOKEN` and run `npm run smoke:workers`. The script sends synthetic tasks in both directions using a single thread, checks retries and reads each stored reply. Set `MCP_ALPHA_ID` and `MCP_BETA_ID` if the configured agent IDs differ from `alpha` and `beta`.
## Publish on your own host
1. Use this repository as a GitHub template, or clone it to your server. Run `npm ci`.
2. Copy [config.example.json](./config.example.json) to `config.json`. Set a unique local `agent.id`, describe its skills, and add each peer's ID and HTTPS MCP URL.
3. Generate two independent tokens per pair. Alice sends with `ALICE_TO_BOB` and receives with `BOB_TO_ALICE`. Bob sets those names in reverse in his peer configuration. Exchange the values over a private channel. Keep the token values in a private environment file or in your service's secret store.
```sh
node -e "console.log(require('node:crypto').randomBytes(32).toString('base64url'))"
```
4. Run the peer listener. Keep the SQLite file on persistent local storage in a directory writable only by the service account. A relative database path resolves from the config file's directory.
```sh
node --env-file=/etc/agent-mcp/peer.env src/index.ts serve /etc/agent-mcp/config.json
```
5. Point DNS at the host and put the listener behind your HTTPS reverse proxy. This Caddy example serves just the peer endpoint. The default Node listener is on `127.0.0.1:8787`.
```caddyfile
alice.example.net {
@peer path /mcp
reverse_proxy @peer 127.0.0.1:8787
respond 404
}
```
6. Share your endpoint and agent ID with peers. Add their mirrored configuration to authorize the connection. Use `get_peer_card` from your local agent to verify the connection.
## Connect a local MCP client
Configure your client's stdio server to launch Node with the same config and secret environment as the peer listener. The two processes share the inbox file on the same host. A Codex configuration looks like this:
```toml
[mcp_servers.federation]
command = "node"
args = ["--env-file=/etc/agent-mcp/peer.env", "/opt/agent-mcp-federation/src/index.ts", "agent", "/etc/agent-mcp/config.json"]
```
For local development, the equivalents are `npm run serve -- config.json` and `node src/index.ts agent config.json`, with the required token environment variables already set. Use Node directly as the stdio command so stdout contains only MCP messages.
| Local tool | Use |
| --- | --- |
| `list_peers` | List configured peer IDs. |
| `get_peer_card` | Read a peer's declared skills. |
| `send_task` | Send a message to a peer and get its task and thread IDs. |
| `list_inbox` | Read pending work or page through inbound thread history. |
| `reply_task` | Save the reply to an inbound task. |
| `get_task_result` | Poll the receiver for the reply to a sent task. |
**Example handoff.** Ask Alice to look up Bob's skills and send him a short review request. Bob's agent checks `list_inbox`, completes the review within its local permissions, and calls `reply_task` with the sender and task IDs. Alice polls `get_task_result` and uses the returned `threadId` with a new task ID if she needs a follow-up. An agent can consult multiple configured peers by repeating this workflow.
## Interoperate with another implementation
The peer endpoint exposes these tools using MCP `structuredContent`. Authenticate each request with the bearer token the receiving operator configured for the sender.
| Peer tool | Input | Result |
| --- | --- | --- |
| `agent_card` | Empty object | `{protocol, id, name, description, skills}` |
| `task_deliver` | `{taskId, threadId, message}` | `{task, duplicate}` |
| `task_result` | `{taskId}` | `{taskId, threadId, status, reply?}` |
The card's protocol is `agent-mcp-federation/1`. Task and thread IDs are UUIDs. Messages and replies are 1 to 4,000 characters after trimming. A result is `pending` or `replied`, and contains `reply` only when the receiver has answered. An identical resend returns the existing result. Reusing an ID for a different message or thread fails. If using your own client, generate and persist both IDs before sending, so a lost response can be retried safely. Tool errors use MCP `isError` and `structuredContent.error`.
## Operating model
- Start with trusted peers. Each receiver maps a directional token to one peer and scopes task lookup to that sender. The public HTTPS endpoint exposes the three peer tools. Local inbox access and replies are available over stdio.
- Your agent or scheduler chooses when to check the inbox and what to do with messages. Treat peer content as external input and keep approval for side effects with the local operator.
- The listener stores message and reply text in SQLite. Back up the database and set a retention policy appropriate for your data. This starter keeps completed tasks for replay and caps each peer at 100 pending tasks.
- Each installation has a static list of peers. Exchange endpoint URLs directly with other operators. Use `allowLoopbackHttp: true` only for local tests.
[MIT license](./LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues