Public: anyone can read this; the crew can change it.
The MCP forges tool returns the refused first asks under a kind, as /forges/refused shows them #12254
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/11556-the-mcp-forges-tool-returns-the-refused → main 3 commits fork
3 files
changed +71
−3
docs/MCP.md +1 −1
| @@ -116,7 +116,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`; | ||
| 116 | 116 | | `following` | — | Where a fork stands against what it follows: in step, behind, ahead, diverged | 243 | |
| 117 | 117 | | `forge_proposals` | — | What this forge proposed to its parent; `reachable` false if it is silent | 245 | |
| 118 | 118 | | `forge_view` | — | The forge as one picture: counts, apps, runs with stations, health | 282 | |
| 119 | | `forges` | — | Forges sent from here (admin; a stage says when to resend), or one forge's plan to read or grant | 431 | | |
| 119 | | `forges` | `refused` | Forges sent from here (admin; a stage says when to resend), or one forge's plan to read or grant | 475 | | |
| 120 | 120 | | `fork` | — | Fork a repository into your account; for a guest, a copy to send changes back from | 232 | |
| 121 | 121 | | `fork_tree` | — | The fork tree around a repo: ancestors, siblings and descendants you may read | 440 | |
| 122 | 122 | | `forks` | `chain` | A repo's forks you may see, paged by limit and cursor (next_cursor), each with commits ahead and behind | 480 | |
src/http/mcp/tools/forges.ts +9 −2
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { BILLING_WORDS, grantPlan, planGrant, readPlan } from "../../../actions/billing.ts"; |
| 2 | 2 | import { CODE_ERRORS, makeCode, planCode, readCodes, revoke } from "../../../actions/codes.ts"; |
| 3 | import { REFUSED_SHOWN, listRefusals, mayReadRefusals } from "../../../core/first_ask_refusals.ts"; | |
| 3 | 4 | import { publicOrigin } from "../../../core/public_origin.ts"; |
| 4 | 5 | import { scopeAllows } from "../../../core/tokens.ts"; |
| 5 | 6 | import { clientRows } from "../../../spawn/clients.ts"; |
| @@ -7,16 +8,22 @@ import { good, bad, type Tool } from "../shared.ts"; | ||
| 7 | 8 | |
| 8 | 9 | export const tool: Tool = { |
| 9 | 10 | name: "forges", |
| 10 | doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity, reachable, size, use; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large, status, source, changes) or, with `plan`, grants it by hand; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false", | |
| 11 | doc: "an admin's list of the forges sent from here: name, address, whom for, apps, claimed, stage (sent/opened/signed in/last tell; resend true when unopened 3 days: claim_link), last activity, reachable, size, use; `{ forge, plan?, dryRun? }` reads a forge's plan (small|medium|large, status, source, changes) or, with `plan`, grants it by hand; gift codes: `codes` true lists, `code` {plan, uses?, note?, expires?} makes (shown once), `revoke` id; planned unless `dryRun` is false; `kind` refused lists the last 20 refused first asks", | |
| 11 | 12 | description: |
| 12 | 13 | "Forges sent from here (admin; a stage says when to resend), or one forge's plan to read or grant.", |
| 13 | inputSchema: { type: "object", properties: { forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, dryRun: { type: "boolean" } } }, | |
| 14 | inputSchema: { type: "object", properties: { kind: { type: "string", enum: ["refused"] }, forge: { type: "string" }, plan: { type: "string" }, codes: { type: "boolean" }, code: { type: "object" }, revoke: { type: "string" }, dryRun: { type: "boolean" } } }, | |
| 14 | 15 | writes: true, |
| 15 | 16 | dryByDefault: true, |
| 16 | 17 | readsWithout: ["forge", "plan", "code", "revoke"], |
| 17 | 18 | async run(ctx, user, args) { |
| 18 | 19 | if (!user.isAdmin) |
| 19 | 20 | return bad("forbidden", "only an admin of this forge sees the forges it sent"); |
| 21 | if (args["kind"] !== undefined) { | |
| 22 | if (args["kind"] !== "refused") return bad("invalid", "kind is refused"); | |
| 23 | if (!mayReadRefusals(user)) return bad("forbidden", "only an admin of this forge sees the refused first asks"); | |
| 24 | const refused = listRefusals(ctx.app.db, REFUSED_SHOWN, Date.now()); | |
| 25 | return good({ says: refused.length === 0 ? "No first ask has been refused." : `The last ${refused.length} refused first asks, newest first.`, refused }); | |
| 26 | } | |
| 20 | 27 | if (args["codes"] === true || args["code"] !== undefined || args["revoke"] !== undefined) { |
| 21 | 28 | const no = (e: string) => (e === "not_found" ? bad("not_found", "Not found.") : bad("invalid", CODE_ERRORS[e as keyof typeof CODE_ERRORS] ?? e)); |
| 22 | 29 | if (args["code"] === undefined && args["revoke"] === undefined) { |
test/mcp-forges-refused.test.ts added +61
| @@ -0,0 +1,61 @@ | ||
| 1 | /** | |
| 2 | * The MCP `forges` tool with `kind: "refused"` (#11556): the rows /forges/refused shows, | |
| 3 | * admin only. | |
| 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 { recordRefusal } from "../src/core/first_ask_refusals.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | import { quietLogs } from "./helpers/globals.ts"; | |
| 15 | ||
| 16 | quietLogs(); | |
| 17 | let dir: string, app: App, servers: Servers, admin: string, plain: string; | |
| 18 | ||
| 19 | const call = async (tok: string, args: object) => { | |
| 20 | const res = await fetch(`${servers.url}/mcp`, { | |
| 21 | method: "POST", | |
| 22 | headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${tok}` }, | |
| 23 | body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "forges", arguments: args } }), | |
| 24 | }); | |
| 25 | const j = (await res.json()) as any; | |
| 26 | return { error: j.result.isError === true, text: String(j.result.content[0].text) }; | |
| 27 | }; | |
| 28 | ||
| 29 | beforeAll(async () => { | |
| 30 | dir = await mkdtemp(join(tmpdir(), "forge-mcp-refused-")); | |
| 31 | app = createApp({ dataDir: dir, port: 0 }); | |
| 32 | servers = await serve(app); | |
| 33 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 34 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 35 | if (!a.ok || !b.ok) throw new Error("users"); | |
| 36 | admin = createToken(app.db, a.value.id, "t", "admin").token; | |
| 37 | plain = createToken(app.db, b.value.id, "t", "admin").token; | |
| 38 | recordRefusal(app.db, { actorId: "u1", ask: "build me a site", words: "Not today." }, Date.now()); | |
| 39 | }); | |
| 40 | afterAll(async () => { | |
| 41 | await servers.stop(); | |
| 42 | await rm(dir, { recursive: true, force: true }); | |
| 43 | }); | |
| 44 | ||
| 45 | test("an admin gets the rows, without email or claim code", async () => { | |
| 46 | const r = await call(admin, { kind: "refused" }); | |
| 47 | expect(r.error).toBe(false); | |
| 48 | const body = JSON.parse(r.text); | |
| 49 | expect(body.refused).toHaveLength(1); | |
| 50 | expect(body.refused[0].ask).toBe("build me a site"); | |
| 51 | expect(body.refused[0].words).toBe("Not today."); | |
| 52 | expect(r.text).not.toMatch(/@x\.test|claim/i); | |
| 53 | }); | |
| 54 | test("a non-admin is refused", async () => { | |
| 55 | const r = await call(plain, { kind: "refused" }); | |
| 56 | expect(r.error).toBe(true); | |
| 57 | expect(r.text).not.toContain("build me a site"); | |
| 58 | }); | |
| 59 | test("an unknown kind is invalid", async () => { | |
| 60 | expect((await call(admin, { kind: "x" })).error).toBe(true); | |
| 61 | }); |
new above
offline