railyard/apps/rithub-mcp/test/server.test.mjs
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

57 lines
2.4 KiB
JavaScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { createServer } from "../src/server.mjs";
// The Server class keeps its registered handlers private, so the real way
// to exercise createServer's behavior end to end is a real client talking
// over a real transport -- the SDK ships an in-memory transport pair for
// exactly this.
import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
async function connectedClient(api, ctx) {
const server = createServer(api, ctx);
const [clientTransport, serverTransport] = InMemoryTransport.createLinkedPair();
const client = new Client({ name: "test-client", version: "0.0.0" });
await Promise.all([client.connect(clientTransport), server.connect(serverTransport)]);
return { client, server };
}
test("an agent-scoped session (ctx.agentToken) cannot list or call ownerOnly tools", async () => {
const api = { get: async () => ({ login: "bot" }) };
const { client } = await connectedClient(api, { agentToken: true, repo: "alice/demo", scope: "pr" });
try {
const { tools } = await client.listTools();
assert.ok(!tools.some((t) => t.name === "mint_agent_token"));
assert.ok(tools.some((t) => t.name === "list_repos")); // ordinary tools stay visible
const result = await client.callTool({ name: "mint_agent_token", arguments: { owner: "alice", repo: "demo", scope: "full" } });
assert.equal(result.isError, true);
assert.match(result.content[0].text, /requires your own full Rithub token/);
} finally {
await client.close();
}
});
test("a caller's own full token (ctx.agentToken false) can see and call ownerOnly tools", async () => {
const api = {
get: async (path) => {
if (path === "/user") return { login: "alice" };
if (path === "/repos/alice/demo") return { permissions: { push: true } };
throw new Error(`unexpected GET ${path}`);
},
};
const { client } = await connectedClient(api, { agentToken: false, token: "real-token" });
try {
const { tools } = await client.listTools();
assert.ok(tools.some((t) => t.name === "mint_agent_token"));
const result = await client.callTool({ name: "mint_agent_token", arguments: { owner: "alice", repo: "demo", scope: "read" } });
assert.equal(result.isError, undefined);
assert.ok(result.structuredContent.token.startsWith("rtat_"));
} finally {
await client.close();
}
});