Public: anyone can read this; the crew can change it.
Access 4c: the grants view as an MCP tool and a terminal screen #18130
Merged 8h 50m ago, live in production
scout/forge:scout/18106-access-4c-the-grants-view-as-an-mcp-tool → main 0 commits fork
3 files
changed +88
−5
docs/features/who-can.md +1 −1
| @@ -7,7 +7,7 @@ row: Who may do what | ||
| 7 | 7 | One read over every table that holds a grant, in one shape: who, what they may do (an action or a role), on what, who granted it and when, and the page where it is changed. A table that does not record who granted it, or when, says "not recorded". It reads `member`, `team_member`, `app_member`, `game_grant`, `model_grant`, `subscription_grant`, `secret_grant`, `egress_grant`, `device_grant`, `child_reach_grant`, `origin_read_grant`, `pull_run_grant`, `telemetry_grant`, `client_cloud_member`, `crew_team_member` and `share` (`src/core/grants_view.ts`, the one reader). It changes no access: nothing here grants or revokes. |
| 8 | 8 | |
| 9 | 9 | - **Web:** Settings → Who may do what (`/settings/who-can`). A person sees their role, what the role alone lets them do, and every grant they hold. A forge admin sees every grant, and can look up a person (all they can reach, with their role) or a resource (everyone who reaches it; an app is `owner/name`, and what that app itself is granted is listed too). Each row links to where it is changed. |
| 10 | - **MCP:** the `access` tool with `action: "grants"` and optional `person` or `resource` (a forge admin's). | |
| 10 | - **MCP:** `whoami_can` with `kind: "grants"`, for anyone: your role and the grants you hold, or every grant for a forge admin (`{ scope, standing, grants }`). A forge admin's `access` tool with `action: "grants"` takes `person` or `resource` too. | |
| 11 | 11 | - **Terminal:** `forge tui`, the palette entry "Who may do what": your own, or as an admin `p` a person, `r` a resource, `c` clear. |
| 12 | 12 | - Rules (`src/actions/grants_view.ts`): anyone else asking for another person's or a resource's answers `not_found`. |
| 13 | 13 | - A new table named `*_grant` or `*_member` that the view does not read fails `test/grants-view.test.ts`: add its entry to `MAPPINGS` in `src/core/grants_view.ts`, with the names and the page to change it. |
src/http/mcp/tools/whoami_can.ts +11 −4
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import { ABILITIES, roleOf, rowOf } from "../../../core/roles.ts"; |
| 2 | import { whoMayDoWhat } from "../../../actions/grants_view.ts"; | |
| 2 | 3 | import { LAST_WAY_IN, removeOwnPasskey } from "../../../actions/passkeys.ts"; |
| 3 | 4 | import { listPasskeys } from "../../../core/passkeys.ts"; |
| 4 | 5 | import { scopeAllows } from "../../../core/tokens.ts"; |
| @@ -9,13 +10,13 @@ import { good, bad, type Tool, repoArg, isAnswer } from "../shared.ts"; | ||
| 9 | 10 | |
| 10 | 11 | export const tool: Tool = { |
| 11 | 12 | name: "whoami_can", |
| 12 | doc: "`{ kind?, remove?, repo? }`: who you are (`username`, and `via` when your own Talk agent asks) and your role, with that role's row of the roles table, each ability yes or no; `repo` (owner/name) adds what you may do there (`read`, `write`, `admin`); `actions` lists what you may do forge-wide, by the names GET /api/v1/may takes; `kind: \"passkeys\"` lists your own passkeys (`{ items }`: id, name, created, lastUsed), people only; `remove: id` with `kind: \"passkeys\"` removes one of them, as Settings → Passkeys does (an admin-scope token, never an agent)", | |
| 13 | doc: "`{ kind?, remove?, repo? }`: who you are (`username`, and `via` when your own Talk agent asks) and your role, with that role's row of the roles table, each ability yes or no; `repo` (owner/name) adds what you may do there (`read`, `write`, `admin`); `actions` lists what you may do forge-wide, by the names GET /api/v1/may takes; `kind: \"passkeys\"` lists your own passkeys (`{ items }`: id, name, created, lastUsed), people only; `kind: \"grants\"`: who may do what (`{ scope, standing, grants }`): every grant for a forge admin, else your own; `remove: id` with `kind: \"passkeys\"` removes one of them, as Settings → Passkeys does (an admin-scope token, never an agent)", | |
| 13 | 14 | description: |
| 14 | "What you can do: your role and each action, yes or no.", | |
| 15 | "What you may do: your role, each action yes or no.", | |
| 15 | 16 | inputSchema: { |
| 16 | 17 | type: "object", |
| 17 | 18 | properties: { |
| 18 | kind: { type: "string", enum: ["role", "passkeys"] }, | |
| 19 | kind: { type: "string", enum: ["role", "passkeys", "grants"] }, | |
| 19 | 20 | remove: { type: "string" }, |
| 20 | 21 | repo: { type: "string", maxLength: 201 }, |
| 21 | 22 | }, |
| @@ -52,7 +53,13 @@ export const tool: Tool = { | ||
| 52 | 53 | })), |
| 53 | 54 | }); |
| 54 | 55 | } |
| 55 | if (kind !== "role") return bad("invalid", "kind is one of: role, passkeys"); | |
| 56 | if (kind === "grants") { | |
| 57 | // The page's own action (#18050), unfiltered: everything for a forge admin who is a person, | |
| 58 | // the caller's own for anyone else, an agent included (#18106). | |
| 59 | const asked = whoMayDoWhat(ctx.app, user); | |
| 60 | return asked.ok ? good(asked.value) : bad("not_found", "sign in to see who may do what"); | |
| 61 | } | |
| 62 | if (kind !== "role") return bad("invalid", "kind is one of: role, passkeys, grants"); | |
| 56 | 63 | const role = roleOf(ctx.app.db, user); |
| 57 | 64 | // Asked in a repository, what this person may do there, by the same checks every door uses. |
| 58 | 65 | let here: { repo: string; read: boolean; write: boolean; admin: boolean } | undefined; |
test/who-may.test.ts added +76
| @@ -0,0 +1,76 @@ | ||
| 1 | /** | |
| 2 | * Who may do what (#18106): MCP `whoami_can` with `kind: "grants"`, open to anyone, reads the | |
| 3 | * page's own action (`actions/grants_view.ts`), as the page and terminal do | |
| 4 | * (`test/who-can.test.ts`). A forge admin sees every grant; a plain member only the rows whose | |
| 5 | * principal is their own account. | |
| 6 | */ | |
| 7 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 8 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp, type App as Forge } from "../src/app.ts"; | |
| 12 | import { setMember } from "../src/core/roles.ts"; | |
| 13 | import { createUser, getUserByName, type User } from "../src/core/users.ts"; | |
| 14 | import type { Ctx } from "../src/http/context.ts"; | |
| 15 | import { tool } from "../src/http/mcp/tools/whoami_can.ts"; | |
| 16 | import { quietLogs } from "./helpers/globals.ts"; | |
| 17 | ||
| 18 | quietLogs(); | |
| 19 | ||
| 20 | interface Listed { | |
| 21 | scope: string; | |
| 22 | standing: { name: string; role: string }; | |
| 23 | grants: { principal: { id: string }; who: string; action: string }[]; | |
| 24 | } | |
| 25 | ||
| 26 | describe("who may do what", () => { | |
| 27 | let dataDir: string; | |
| 28 | let forge: Forge; | |
| 29 | const user = (name: string): User => getUserByName(forge.db, name)!; | |
| 30 | ||
| 31 | async function listed(name: string): Promise<Listed> { | |
| 32 | const who = user(name); | |
| 33 | const url = new URL("http://forge.test/mcp"); | |
| 34 | const ctx: Ctx = { app: forge, request: new Request(url.href), url, params: {}, auth: { user: who, scope: "ro", via: "token" }, theme: undefined }; | |
| 35 | const answer = await tool.run(ctx, who, { kind: "grants" }); | |
| 36 | if (!answer.ok) throw new Error(JSON.stringify(answer)); | |
| 37 | return answer.value as Listed; | |
| 38 | } | |
| 39 | ||
| 40 | beforeAll(async () => { | |
| 41 | dataDir = await mkdtemp(join(tmpdir(), "forge-who-may-")); | |
| 42 | forge = createApp({ dataDir, port: 0 }); | |
| 43 | for (const [username, adminIfFirst] of [["alice", true], ["bob", false], ["carol", false]] as const) { | |
| 44 | const made = await createUser(forge.db, { | |
| 45 | origin: "seed", | |
| 46 | trusted: true, | |
| 47 | username, | |
| 48 | email: `${username}@forge.local`, | |
| 49 | password: "a-good-passphrase", | |
| 50 | adminIfFirst, | |
| 51 | }); | |
| 52 | if (!made.ok) throw new Error(made.error); | |
| 53 | } | |
| 54 | setMember(forge.db, user("bob").id, true, user("alice").id, new Date()); | |
| 55 | setMember(forge.db, user("carol").id, true, user("alice").id, new Date()); | |
| 56 | }); | |
| 57 | afterAll(async () => { | |
| 58 | forge.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("an admin lists every grant; a plain member lists only their own", async () => { | |
| 63 | const admin = await listed("alice"); | |
| 64 | expect(admin.scope).toBe("all"); | |
| 65 | const held = new Set(admin.grants.map((g) => g.principal.id)); | |
| 66 | expect(held.has(user("bob").id)).toBe(true); | |
| 67 | expect(held.has(user("carol").id)).toBe(true); | |
| 68 | ||
| 69 | const bob = await listed("bob"); | |
| 70 | expect(bob.scope).toBe("self"); | |
| 71 | expect(bob.standing.name).toBe("bob"); | |
| 72 | expect(bob.grants.length).toBeGreaterThan(0); | |
| 73 | expect(bob.grants.every((g) => g.principal.id === user("bob").id)).toBe(true); | |
| 74 | expect(bob.grants.some((g) => g.who === "bob" && g.action === "member")).toBe(true); | |
| 75 | }); | |
| 76 | }); |
new above
offline