Public: anyone can read this; the crew can change it.

MCP `invites`: list the outstanding invites and revoke one, beside `mint_invite` #4885

Parked opened this
Closed without merging

scout/forge:scout/4793-mcp-invites-list-the-outstanding-invites → main 2 commits fork 1 thread · 1 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +132 −1
docs/MCP.md +1
@@ -71,6 +71,7 @@ for a tunnel and a forge that sleeps.
7171 | `make_it_yours` | `{ repo, name?, visibility?, dryRun? }`: a fork of an app you can see, as your own app, following the original; code and history copied, data and secrets not; private unless `visibility` is public (a public original only); at most 5 copies an account a day; plans unless `dryRun` is false | yes |
7272 | `origin_apps` | `{ action, id?, host?, kind?, clientId?, clientSecret? }`: `list` your OAuth apps for connecting origins, `add` one (a `host`, or `kind` github), or `remove` one by `id`; the API's rules and errors, an admin-scope token; a secret is never read back, only `hasSecret` | yes |
7373 | `front_page` | `{ pinned?, hidden?, weights?, dryRun? }`: the front page as a stranger sees it, ranked, with numbers; a site admin sets what is pinned (first public one featured), hidden, and what counts (weights of forks, ships, use, fresh, 0 to 10) | yes |
74 | `invites` | `{ action, id? }`: `list` the outstanding invites (state and expiry, never a link) or `revoke` one by `id`, as the `/api/v1/admin/invites` routes do; a person admin with an admin token | yes |
7475 | `map` | an app's plan: ready now, waiting on what, the critical path, where the crew is, parked twice and why (`app`); draws the `ui://forge/map` card | |
7576 | `milestones` | `{ app, issue? }`: how far each thing asked for is (and, with `issue`, whether you watch it: `watching`), from the dependency tree: the plan's limbs with their weights, or one milestone opened (who is building, what is blocked and on what, what merged in 7 days, an estimate, and for an owner `spend`: dollars, changes landed ever, the costliest three issues) | |
7677 | `limb` | `{ app, issue, action, reason?, pillar?, operation?, dryRun? }`: defer, resume or close a whole limb as not doing, or undo an operation; plans unless `dryRun` is false, and applying takes an admin token | yes |
src/http/mcp.ts +31 −1
@@ -97,7 +97,7 @@ import {
9797 } from "../web/studio/cards.ts";
9898 import { verifyAccount } from "../actions/accounts.ts";
9999 import { setAccountCeilings, setAccountGrant } from "../actions/model_grant.ts";
100 import { inviteLink, mint as mintInviteLink } from "../actions/invites.ts";
100 import { inviteLink, list as listInviteLinks, mint as mintInviteLink, revoke as revokeInviteLink } from "../actions/invites.ts";
101101 import { suspendAccount, unsuspendAccount } from "../actions/accounts.ts";
102102 import { setRole } from "../actions/roles.ts";
103103 import { ABILITIES, roleOf, rowOf } from "../core/roles.ts";
@@ -1612,6 +1612,36 @@ const TOOLS: readonly Tool[] = [
16121612 return good({ ...made.value.invite, link: inviteLink(origin, made.value.token) });
16131613 },
16141614 },
1615 {
1616 name: "invites",
1617 description: "List open invites, or revoke one by `id`. Person admins only.",
1618 inputSchema: {
1619 type: "object",
1620 properties: {
1621 action: { type: "string", enum: ["list", "revoke"] },
1622 id: { type: "string" },
1623 },
1624 required: ["action"],
1625 },
1626 writes: true,
1627 run(ctx, user, args) {
1628 if (!mayBoards(user)) return bad("forbidden", "invites are a forge admin's");
1629 if (!scopeAllows(ctx.auth.scope ?? "ro", "admin"))
1630 return bad("forbidden", "invites need an admin token");
1631 const action = args["action"];
1632 if (action === "list") {
1633 const listed = listInviteLinks(ctx.app, user);
1634 return listed.ok ? good({ items: listed.value }) : bad("not_found", "no such invite");
1635 }
1636 if (action === "revoke") {
1637 const id = args["id"];
1638 if (typeof id !== "string" || id === "") return bad("invalid", "revoke needs an invite id");
1639 const done = revokeInviteLink(ctx.app, user, id);
1640 return done.ok ? good(done.value) : bad("not_found", "no such open invite");
1641 }
1642 return bad("invalid", 'action is "list" or "revoke"');
1643 },
1644 },
16151645 {
16161646 name: "account_role",
16171647 description:
test/mcp-invites.test.ts added +99
@@ -0,0 +1,99 @@
1 /**
2 * The `invites` tool is the admin invites API over MCP: the same actions, so
3 * the same rules. A listing never carries a link or a token.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { createApp, type App } from "../src/app.ts";
10 import { createToken } from "../src/core/tokens.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { setSignup } from "../src/settings/signup.ts";
13 import { setLimit } from "../src/settings/limits.ts";
14 import { seedAdmin } from "./helpers/admin.ts";
15 import { promote } from "./helpers/members.ts";
16 import { createUser } from "../src/core/users.ts";
17 import { quietLogs } from "./helpers/globals.ts";
18
19 quietLogs();
20
21 let dir: string;
22 let app: App;
23 let servers: Servers;
24 let admin = "";
25 let ro = "";
26 let bob = "";
27 let id = 1;
28
29 const tool = async (token: string, name: string, args: unknown = {}) => {
30 const res = await fetch(`${servers.url}/mcp`, {
31 method: "POST",
32 headers: {
33 "content-type": "application/json",
34 accept: "application/json, text/event-stream",
35 authorization: `Bearer ${token}`,
36 },
37 body: JSON.stringify({ jsonrpc: "2.0", id: id++, method: "tools/call", params: { name, arguments: args } }),
38 });
39 const body = (await res.json()) as any;
40 const text: string = body.result.content[0].text;
41 return { text, error: body.result.isError === true, value: body.result.isError ? undefined : JSON.parse(text) };
42 };
43
44 beforeAll(async () => {
45 dir = await mkdtemp(join(tmpdir(), "forge-mcp-invites-"));
46 app = createApp({ dataDir: dir, port: 0 });
47 servers = await serve(app);
48 const a = await seedAdmin(app, { username: "olive", email: "[email protected]", password: "owner-passphrase" });
49 if (!a.ok) throw new Error(a.error);
50 setSignup(app.db, "invite", a.value.id);
51 setLimit(app.db, "signup.per_source", 1000, a.value.id);
52 const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" });
53 if (!b.ok) throw new Error(b.error);
54 promote(app.db, b.value);
55 admin = createToken(app.db, a.value.id, "admin", "admin").token;
56 ro = createToken(app.db, a.value.id, "ro", "ro").token;
57 bob = createToken(app.db, b.value.id, "bob", "admin").token;
58 }, 30_000);
59
60 afterAll(async () => {
61 servers.stop();
62 app.close();
63 await rm(dir, { recursive: true, force: true });
64 });
65
66 const signup = (username: string, invite: string) =>
67 fetch(`${servers.url}/api/v1/users`, {
68 method: "POST",
69 headers: { "content-type": "application/json" },
70 body: JSON.stringify({ username, email: `${username}@f.dev`, password: "an-invited-passphrase", invite }),
71 });
72
73 test("an admin mints, lists and revokes; a revoked link signs no one up", async () => {
74 const kept = (await tool(admin, "mint_invite", { days: 3 })).value;
75 const gone = (await tool(admin, "mint_invite")).value;
76 const goneToken = gone.link.split("invite=")[1];
77
78 const listed = await tool(admin, "invites", { action: "list" });
79 const ids = listed.value.items.map((i: { id: string }) => i.id);
80 expect(ids).toContain(kept.id);
81 expect(ids).toContain(gone.id);
82 expect(listed.text).not.toContain("link");
83 expect(listed.text).not.toContain(goneToken);
84 expect(listed.text).not.toContain(kept.link.split("invite=")[1]);
85
86 const done = await tool(admin, "invites", { action: "revoke", id: gone.id });
87 expect(done.value.state).toBe("revoked");
88 expect((await tool(admin, "invites", { action: "revoke", id: gone.id })).error).toBe(true);
89 expect((await tool(admin, "invites", { action: "revoke" })).text).toContain("invalid");
90 expect((await tool(admin, "invites", { action: "nope" })).text).toContain("invalid");
91
92 expect((await signup("late", goneToken)).status).toBeGreaterThanOrEqual(400);
93 expect((await signup("early", kept.link.split("invite=")[1])).status).toBeLessThan(300);
94 });
95
96 test("a non-admin and a lesser token are refused", async () => {
97 expect((await tool(bob, "invites", { action: "list" })).text).toContain("forbidden");
98 expect((await tool(ro, "invites", { action: "list" })).error).toBe(true);
99 });
test/mcp.test.ts +1
@@ -201,6 +201,7 @@ describe("the forge over MCP", () => {
201201 "model_grant",
202202 "whoami_can",
203203 "mint_invite",
204 "invites",
204205 "account_role",
205206 "account_suspend",
206207 "model_ceiling",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs