Files
ai-agent/docs/mcp.md

175 lines
10 KiB
Markdown

# The hub as an MCP server
*Since 2026-11-03.* An AI app — Claude.ai (web, phone, desktop), ChatGPT, Le
Chat, Cursor, VS Code — plugs ai-agent in as a **custom connector** and drives
it with the `ai-agent` CLI's commands as tools: start a conversation, continue
one, wait for its answer, read and search them, run an agent (and, through
the plugins' tools, schedule work).
**Address**: `https://ai-agent.gabvdl.xyz/mcp` — the guest host, the one
the internet reaches (the apps call it from their own servers, not from the
browser).
## Decisions (Gabriel, 2026-11-03 form)
| question | answer |
|---|---|
| who can connect | the owner, and the guests he switches *AI apps* on for |
| endpoint | `ai-agent.gabvdl.xyz/mcp` (already public) |
| the owner approves | both: on the admin host (Authelia) or from the Pixel, by number matching (since 2026-11-15) |
| tool groups | read, run, schedule, agents (not worktrees, not What's new) — on 2026-11-28 cron jobs and `schedule` went: the Cron jobs and Scheduled conversations plugins' tools |
| read-only vs full | picked per connection, on the consent page |
| personal tokens | no — every app goes through OAuth + an approval |
| stdio `ai-agent mcp` | no |
| tokens | 1 h access, 90-day rolling refresh, revocable |
| "via Claude.ai" pill | yes |
## Flow
```
app ──POST /mcp──────────────────────────────► 401 + WWW-Authenticate (resource_metadata)
app ──GET /.well-known/oauth-protected-resource/mcp, …/oauth-authorization-server
app ──POST /oauth/register (RFC 7591) ───────► client_id (+ secret if confidential)
browser ──GET /oauth/authorize?…&code_challenge (S256)
└─► parked request ─► 302 /connect/<id> (PWA page, outside the shell)
· owner on ai-agent.lab (Authelia): Allow (full / read) ─► code
· owner elsewhere: picks full / read, "Approve on my phone" ─► the page shows a
two-digit number, the push offers three: the one on the page allows, any
other refuses (the page polls /oauth/consent/<id>) ─► code
· guest signed in on ai-agent.gabvdl.xyz, AI apps on: "Allow as Dom" ─► code
└─► 302 back to the app's redirect URI with code + state + iss
app ──POST /oauth/token (code + verifier) ───► access (1 h) + refresh (90 d, rolled)
app ──POST /mcp Authorization: Bearer … ────► initialize · tools/list · tools/call
```
The tools run inside the backend against its own API over loopback, as the
connection's caller: the owner, or — via the `X-AIA-MCP-User` downgrade — the
guest, limited to their CLI's routes.
## Tools
| group | tools |
|---|---|
| read | `list_conversations` · `get_conversation` · `search_conversations` · `past_conversations` · `inbox` · `browse` (projects, skills, agents, models, workers, usage, budget) · `wait_for_conversation` |
| run | `start_conversation` · `send_message` · `fork_conversation` · `stop_conversation` · `rename_conversation` · `archive_conversation` |
| agents | `run_agent` (cron jobs: the Cron jobs plugin's `plugin_cron-jobs_*` tools since 2026-11-28) |
| plugins | `plugin_<id>_<command>` — every command a live plugin's `plugin.json` marks `"mcp": true` (in group `read` when its action is `readOnly`, else `run`), e.g. `plugin_plans_ls`, `plugin_plans_show`, `plugin_memories_ls`, `plugin_memories_show`, `plugin_cron-jobs_ls` / `_run` / `_pause` …; Send later is `plugin_scheduled-conversations_add` (`in` / `at`, new or `to` an existing one) · `_ls` · `_show` · `_cancel` · `_start` (the `schedule` group went with its tools on 2026-11-28) |
A guest gets the conversation tools, `inbox`, `browse` (projects, models,
budget) and the plugin tools of the plugins granted to them. A read-only
connection gets the read tools — the read-only plugin tools too, called by
GET. Plugin tools are listed from `GET /api/plugins/commands` on each
`tools/list` (the hub answers as the connection's caller); a hub that can't
list them leaves the core tools. Plans, memories and goals were core tools
until 2026-11-26 (`browse plans|goals|memories`, `read_document`,
`work_on_goal`); plans and memories are now their plugins' tools, goals has
none. Docs: `docs/plugins.md` → "Commands".
## Connecting an app
- **Claude.ai**: Settings → Connectors → *Add custom connector* → paste the
address → Add → Connect. Its callback is
`https://claude.ai/api/mcp/auth_callback` (claude.com later — both fine).
- **ChatGPT**: Settings → Apps & Connectors → Advanced → *Developer mode*, then
Create → the address, authentication OAuth.
- **Others** (Le Chat, Cursor, VS Code, Claude Code `claude mcp add --transport
http ai-agent https://ai-agent.gabvdl.xyz/mcp`): a remote HTTP MCP server
with OAuth. Claude Code on a worker: see "On a worker" below.
## On a worker (Claude Code on the Orus MacBook)
*Since 2026-11-18* (Gabriel's form: this replaces the `ai-agent` CLI through
the worker's outbox, 2026-11-16 → 11-18). A Claude Code on one of the owner's
workers drives the hub with these tools through a connection **bound to that
worker**:
| what | answer |
|---|---|
| the CLI path | removed for every worker — the CLI keeps only `past` there (an outbox `lookup`) |
| what the connection sees | bound to the worker: its profile's conversations, runs on that machine only |
| setup | one-time, by hand on the Mac |
**Setup on the Mac** (once):
1. In a terminal: `claude mcp add --transport http --scope user ai-agent
https://ai-agent.gabvdl.xyz/mcp` — user scope, so the runs the hub
launches there get it too (same `~/.claude.json`).
2. In `claude`: `/mcp` → *ai-agent* → *Authenticate*. The browser opens the
consent page: pick **Read only**, *Approve on my phone*, tap the number the
page shows (or approve on `ai-agent.lab`).
3. On the hub: Settings → AI apps → the new *Claude Code* connection → **Acts
as** → the worker (Orus Macbook), then switch *Read-only* off for full
access. Approving read-only first means it never holds full owner access,
not even for the minute before it is bound.
**What binding does** (`grant.worker`, PATCH `/api/mcp/connections/{id}`
`{worker}`, `""` unbinds; owner connections only, never to a guest's
computer). Every tool call carries `X-AIA-As-Worker: <id>` and
`X-AIA-As-Worker-Mode: run|read` (from the connection's scope), and the gate
answers it as `workers/bound.py` `worker_caller` — the owner, narrowed:
- **tools**: `WORKER_TOOLS` (`cli/mcp_tools.py`) — the conversation tools,
`wait`, `past`, `browse` (models, workers, usage, budget). No `inbox`,
agents, cron or plugin tools, no browse of projects / skills (the lab's
own files). **No scheduling** (since 2026-11-28, Gabriel's call — Full
only): Send later is the scheduled-conversations plugin's tools, and a
bound connection has none (its scopes hold no `plugins.use`); the plugin
also refuses a call that names a worker. Schedule from the owner's own CLI
or a full, unbound connection. Only `READ_ROUTES` (+ `RUN_ROUTES` for a full connection) answer.
- **reads**: the worker's own profile's conversations only
(`Caller.profile` → `users/access.py` `visible` / `filter_cards`).
- **writes**: on that worker only. `start_conversation` is pinned to it
(another `worker` is a 403, `newProject` too); send / fork / stop / rename
/ archive take only conversations that ran there
(`access.ensure_runs_here`), and a `parent` it can't see is dropped.
- A bound connection whose worker is unpaired gets nothing (403), never the
owner's full access.
The Mac now **calls the hub** — `ai-agent.gabvdl.xyz`, the public host, over
OAuth — where a worker otherwise never does. That is the trade Gabriel picked;
the binding is what keeps a work laptop's Claude Code out of the Personal
profile.
## Limits and safety
- Registration is open (it grants nothing): ≤ 200 apps and ≤ 10 a day per
address, unapproved ones pruned after a day; redirect URIs https, loopback
http or an app's own scheme — plain ASCII, a lower-case host, no userinfo,
no backslash or whitespace, and they must survive a parse/unparse round
trip (`https://evil.example\@claude.ai/cb` is claude.ai to Python and
evil.example to a browser).
- ≤ 20 requests waiting at once and ≤ 3 per address, each for 15 min.
Approval pushes: ≤ 2 per address and ≤ 6 overall per 10 min, ≤ 20 a day;
past that the page says to approve on the admin host.
- **The phone approves by number matching** (2026-11-15). Before, the push
had *Allow / Read only / Deny*, and whoever had opened the consent page
collected the code once Allow was tapped: anyone on the internet could
register an app named "Claude" with the real claude.ai callback, raise the
push and wait for a reflex tap. Now the page that asks for the push shows a
number (in the answer to that request only — the poll never repeats it; the
tab keeps it in `sessionStorage`), the push offers three, and only that
number approves, with the access picked on the page. Any other answer
refuses and closes the request. The page always says "only allow this if
you just started it from the app".
- A decision takes the request out of the store in the same step and hands
the code to the caller that decided — it is never parked where a poll on the
request's id could pick it up.
- A refusal (or a request refused before consent: no PKCE, wrong resource…)
is only sent back to the usual apps' callbacks (`KNOWN_REDIRECT_HOSTS`);
for any other address the page says so and redirects nowhere.
- The owner's approve call needs Authelia *and* a same-origin request.
- Tokens: a rolled refresh token is remembered. Presented again after its
60 s grace, it ends the connection if anything minted after it was ever
used (two holders); if nothing was (the app never got the answer), it is
served again and the unused pair dies. Revoking a refresh token
(`/oauth/revoke`) ends its connection; ≤ 12 token records per connection.
Deleting a guest ends their connections. A connection with no user is
refused, never the owner's.
- `/oauth/register` takes ≤ 64 KB, `/oauth/token` and `/oauth/revoke`
≤ 16 KB, `/mcp` ≤ 2 MB.
- Revoking (Settings → AI apps → Disconnect, or a guest's own page) drops the
connection's tokens at once; switching a guest's *AI apps* off cuts theirs on
the next call.
- Traefik: `ai-agent-mcp` router, no compress / secure-headers, 10 req/s per IP
(burst 100).