caelum0x 2a842f24a7 feat(rithub-mcp): agent-scoped tokens + the agent PR loop (wedge #2)
Repo owners can now mint a token limited to ONE repo and a chosen scope
(read / pr / full) instead of handing an agent their full account token --
via one MCP tool call (mint_agent_token) or the plain /agent-tokens
settings page. scopeGuard.mjs enforces the fence on every request before
it reaches the real Rithub API; a pr-scope token can open branches/PRs/
issues/comments but never merge, touch another repo, or mint itself a
broader token (the 5 token-management tools are ownerOnly and refused
outright for an agent-scoped session).

enable_agent_on_repo turns on the second half: labelling any open issue
"agent" makes bin/agent-worker.mjs (a separate pm2 poller) read the issue,
call the Anthropic Messages API for a structured fix, commit it on a new
branch, and open a pull request that closes the issue -- commenting on the
issue when it starts and when it opens the PR. The worker uses the same
scope-guarded client a human-held token would be, so it can't exceed the
fence either.

Verified end to end against production: a throwaway mail.tm user, a real
one-line bug, mint/list/revoke (plus a confirmed-blocked self-escalation
attempt), enable_agent_on_repo, an agent-labelled issue, one real
pollOnce() run against the live Anthropic API that produced a correct
one-line fix, a PR that closed the issue on merge, and the REST
/agent-tokens mint/list routes -- all cleaned up after (repo deleted, user
purged, token deleted, mail.tm inbox deleted). 27/27 unit tests pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-11-19 02:08:50 +03:00

91 lines
4.1 KiB
Markdown

# rithub-mcp
Streamable-HTTP MCP server for Rithub: one endpoint (`https://rithub.dev/mcp`)
any MCP-capable agent (Claude Code, Cursor, Codex) can point at to work on
Rithub repos -- no per-tool glue code.
Auth is a caller-supplied Rithub access token, sent as
`Authorization: Bearer <token>` on every request. This service holds no
credential of its own by default; it forwards the token to the Rithub API
as-is -- except for agent-scoped tokens (below), which it does hold the
backing credential for, encrypted at rest, purely so it can enforce a
narrower fence than the real token would allow on its own.
## Run locally
```
npm install
RITHUB_BASE_URL=https://rithub.dev PORT=3399 node bin/start.mjs
```
`GET /healthz` returns `{"ok":true}`. The MCP endpoint is `POST/GET/DELETE /mcp`.
## Agent-scoped tokens + the agent PR loop (wedge #2)
Any repo owner can mint a token limited to **one repo** and a chosen
**scope** (`read` / `pr` / `full`) instead of handing an agent their full
account token -- via one MCP tool call (`mint_agent_token`) or the plain
settings page at `/agent-tokens` (paste your real token, pick a repo +
scope, get a `rtat_...` token back once). That token can then itself be
used as the `Authorization: Bearer` on `/mcp` -- `src/scopeGuard.mjs`
checks every request against the bound repo + scope before it reaches the
real Rithub API, so a `pr`-scope token can open branches/PRs/issues/
comments but can never merge, touch another repo, or mint itself a
broader token (`ownerOnly` tools are refused outright for an agent-scoped
session -- see `src/server.mjs`).
`enable_agent_on_repo` turns the second half on: labelling any open issue
**"agent"** (the label is auto-created) makes `bin/agent-worker.mjs` -- a
separate pm2 process polling every `AGENT_POLL_INTERVAL_MS` (default
20s) -- read the issue + repo, call the Anthropic Messages API
(`CLAUDE_API_KEY`/`ANTHROPIC_API_KEY`, model `RITHUB_AGENT_MODEL` default
`claude-sonnet-4-6`) for a structured fix, commit it on a new branch, and
open a pull request that `Closes #N`, commenting on the issue when it
starts and when it opens the PR. The worker uses the exact same
scope-guarded client a human-held `pr`-scope token would be restricted
by -- it cannot merge or exceed that fence either. `disable_agent_on_repo`
revokes it. See `src/agentTokens.mjs` (storage + encryption),
`src/agentTokenActions.mjs` (shared mint/list/revoke/enable/disable
logic behind both the MCP tools and the REST settings routes),
`src/claudeFix.mjs` (the model call), `src/agentWorker.mjs` (the poll
loop).
Token records live in `data/agent-tokens.json` (gitignored, `0600`,
AES-256-GCM-encrypted backing tokens; the encryption key is
`RITHUB_MCP_TOKEN_KEY` or an auto-generated `data/token-key.hex`).
Processed-issue state lives in `data/agent-processed.json` so an issue is
never re-processed once it has a PR.
**Verified end to end against production, 2026-11-18:** a throwaway
mail.tm-registered user, a real repo with a real one-line bug, an
`mint_agent_token`/`list_agent_tokens`/`revoke_agent_token` round trip (and
a confirmed-blocked self-escalation attempt), `enable_agent_on_repo`, an
`agent`-labelled issue, one real `pollOnce()` run that called the live
Anthropic API and produced a correct one-line fix, a PR that closed the
issue on merge, and the REST `/agent-tokens` mint/list routes -- all
cleaned up after (repo deleted, user purged, token deleted, mail.tm inbox
deleted). Full trail: `plans/rithub-beat-github-2026-11-18.md`.
## Production
Runs under pm2 (`ecosystem.config.cjs`): `rithub-mcp` (the HTTP server,
bound to `127.0.0.1:3351` only) and `rithub-agent-worker` (the poller,
no HTTP surface). Both use `RITHUB_BASE_URL=http://127.0.0.1:3350` (the
engine's own port) instead of the public `rithub.dev` domain, because
Cloudflare's edge proxy in front of `rithub.dev` hard-times-out slow
requests around ~100s -- `import_from_github` on even a small repo can
exceed that. Caddy (`/etc/caddy/sites-enabled/rithub-dev.caddy`) proxies
`/mcp*` and `/agent-tokens*` on the public domain to the `rithub-mcp`
service.
## Tools
See `src/tools.mjs`. Docs with one-line client config:
`https://rithub.dev/mcp-docs`.
## Tests
```
npm test
```