Public: anyone can read this; the crew can change it.
The same export is an action on an existing MCP tool, with the same words as the page #15716
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15618-the-same-export-is-an-action-on-an-exist → main 0 commits fork 3 threads · 3 unresolved
5 files
changed +77
−2
docs/MCP.md +1
| @@ -101,6 +101,7 @@ a failure names the bytes over and the longest descriptions. | ||
| 101 | 101 | |
| 102 | 102 | - A new door is an `action` or `kind` on the tool that already owns the |
| 103 | 103 | resource before it is a new tool. |
| 104 | Example: `profile` with `action: "export"` answers `GET /api/v1/user/export` (admin scope; a size summary and the link past 16,000 bytes). | |
| 104 | 105 | - A new tool's description is one sentence. |
| 105 | 106 | - A `dryRun` argument is bare `{ type: "boolean" }`: the server's |
| 106 | 107 | `instructions` say once that a tool with `dryRun` plans unless it is `false`. |
src/http/mcp/export_answer.ts added +34
| @@ -0,0 +1,34 @@ | ||
| 1 | import { scopeAllows } from "../../core/tokens.ts"; | |
| 2 | import { accountExportFor, EXPORT_PATH } from "../../core/account_export.ts"; | |
| 3 | import { good, bad, type Answer, type Tool } from "./shared.ts"; | |
| 4 | ||
| 5 | /** | |
| 6 | * The most export JSON one tool answer carries. The MCP layer has no ceiling of its own for a | |
| 7 | * tool's output; this one is chosen so the answer stays a few screens for the model reading it. | |
| 8 | * Past it the answer is a size summary and the link to the full document. | |
| 9 | */ | |
| 10 | export const EXPORT_ANSWER_MAX = 16_000; | |
| 11 | ||
| 12 | type Run = Tool["run"]; | |
| 13 | ||
| 14 | /** | |
| 15 | * `profile` with `action: "export"`: the signed-in person's own export, by an admin-scope token. | |
| 16 | */ | |
| 17 | export function exportAnswer(ctx: Parameters<Run>[0], user: Parameters<Run>[1]): Answer { | |
| 18 | if (user.kind !== "user" || !scopeAllows(ctx.auth.scope ?? "ro", "admin")) | |
| 19 | return bad("forbidden", "the export is a person's own, and needs an admin-scope token"); | |
| 20 | const doc = accountExportFor(ctx.app, user, ctx.url.origin); | |
| 21 | if (doc === undefined) return bad("not_found", "no such account"); | |
| 22 | const bytes = Buffer.byteLength(JSON.stringify(doc)); | |
| 23 | if (bytes <= EXPORT_ANSWER_MAX) return good(doc); | |
| 24 | const counts: Record<string, number> = {}; | |
| 25 | for (const [key, value] of Object.entries(doc)) if (Array.isArray(value)) counts[key] = value.length; | |
| 26 | return good({ | |
| 27 | about: doc.about, | |
| 28 | exportedAt: doc.exportedAt, | |
| 29 | bytes, | |
| 30 | counts, | |
| 31 | link: `${ctx.url.origin}${EXPORT_PATH}`, | |
| 32 | says: "too large to send here: open the link signed in, or run `ssh <forge> export`", | |
| 33 | }); | |
| 34 | } |
src/http/mcp/tools/profile.ts +6 −1
| @@ -2,11 +2,12 @@ import { scopeAllows } from "../../../core/tokens.ts"; | ||
| 2 | 2 | import { emailVerified } from "../../../core/verify.ts"; |
| 3 | 3 | import { updateProfile } from "../../../actions/users.ts"; |
| 4 | 4 | import { publicUser } from "../../respond.ts"; |
| 5 | import { exportAnswer } from "../export_answer.ts"; | |
| 5 | 6 | import { good, bad, type Tool } from "../shared.ts"; |
| 6 | 7 | |
| 7 | 8 | export const tool: Tool = { |
| 8 | 9 | name: "profile", |
| 9 | doc: "`{ displayName?, email?, commitEmail? }`: your own profile (`publicUser` and `emailVerified`, never the address); with an argument it changes it by the rules of `PATCH /api/v1/user`, and needs admin scope: only a site admin can mint such a token, so most people sign in to `/mcp` with their password", | |
| 10 | doc: "`{ displayName?, email?, commitEmail?, action? }`: your own profile (`publicUser` and `emailVerified`, never the address); with an argument it changes it by the rules of `PATCH /api/v1/user`, and needs admin scope: only a site admin can mint such a token, so most people sign in to `/mcp` with their password. `action: \"export\"` answers the document of `GET /api/v1/user/export` (a size summary and the link past 16,000 bytes, a bound this tool sets for itself: the MCP layer has none), for a person's account with an admin-scope token", | |
| 10 | 11 | description: "Read or change your profile.", |
| 11 | 12 | inputSchema: { |
| 12 | 13 | type: "object", |
| @@ -14,11 +15,15 @@ export const tool: Tool = { | ||
| 14 | 15 | displayName: { type: "string" }, |
| 15 | 16 | email: { type: "string" }, |
| 16 | 17 | commitEmail: { type: "string" }, |
| 18 | action: { enum: ["export"] }, | |
| 17 | 19 | }, |
| 18 | 20 | }, |
| 19 | 21 | writes: true, |
| 20 | 22 | readsWithout: ["displayName", "email", "commitEmail"], |
| 23 | readsWhen: (args) => args["action"] === "export", | |
| 21 | 24 | run(ctx, user, args) { |
| 25 | if (args["action"] !== undefined && args["action"] !== "export") return bad("invalid", "action is export"); | |
| 26 | if (args["action"] === "export") return exportAnswer(ctx, user); | |
| 22 | 27 | const { displayName, email, commitEmail } = args; |
| 23 | 28 | if (displayName === undefined && email === undefined && commitEmail === undefined) |
| 24 | 29 | return good({ ...publicUser(user), emailVerified: emailVerified(ctx.app.db, user.id) }); |
test/mcp-profile.test.ts +35
| @@ -85,4 +85,39 @@ describe("MCP profile", () => { | ||
| 85 | 85 | expect(res.error).toContain("admin-scope"); |
| 86 | 86 | expect((await tool(admin, {})).value.displayName).toBe(before); |
| 87 | 87 | }); |
| 88 | ||
| 89 | test("action export answers the document of GET /api/v1/user/export, and refuses a token below admin scope", async () => { | |
| 90 | const api: any = await (await fetch(`${servers.url}/api/v1/user/export`, { headers: { authorization: `Bearer ${admin}` } })).json(); | |
| 91 | const got = await tool(admin, { action: "export" }); | |
| 92 | expect(got.value.profile).toEqual(api.profile); | |
| 93 | expect(Object.keys(got.value)).toEqual(Object.keys(api)); | |
| 94 | // The same person on a token that may only read or write: refused with its next step. | |
| 95 | for (const token of [ro, rw]) { | |
| 96 | const refused = await tool(token, { action: "export" }); | |
| 97 | expect(refused.error).toContain("forbidden"); | |
| 98 | expect(refused.error).toContain("Next:"); | |
| 99 | } | |
| 100 | // Another person: on a read-only token the refusal is by scope, with its next step. | |
| 101 | const b = await createUser(app.db, { origin: "seed", trusted: true, username: "bob", email: "[email protected]", password: "a-good-passphrase" }); | |
| 102 | if (!b.ok) throw new Error(b.error); | |
| 103 | const refusedBob = await tool(createToken(app.db, b.value.id, "ro", "ro").token, { action: "export" }); | |
| 104 | expect(refusedBob.error).toContain("forbidden"); | |
outdated
Comment does not match the code: this is a ro token, refused for scope. Make Bob an admin token and assert his own export, or reword. |
||
| 105 | expect(refusedBob.error).toContain("Next:"); | |
| 106 | expect(JSON.stringify(refusedBob)).not.toContain("alice"); | |
| 107 | // On an admin-scope token Bob gets his own export, with nothing of alice's. | |
| 108 | const own = await tool(createToken(app.db, b.value.id, "adm", "admin").token, { action: "export" }); | |
| 109 | expect(own.value.profile.username).toBe("bob"); | |
| 110 | expect(JSON.stringify(own.value)).not.toContain("alice"); | |
| 111 | expect((await tool(admin, { action: "nope" })).error).toContain("invalid"); | |
| 112 | }); | |
| 113 | ||
| 114 | test("an export over the answer ceiling is a size summary and the link", async () => { | |
| 115 | const me: any = await (await fetch(`${servers.url}/api/v1/user`, { headers: { authorization: `Bearer ${admin}` } })).json(); | |
| 116 | for (let i = 0; i < 120; i++) createToken(app.db, me.id, `t${i}-${"x".repeat(150)}`, "ro"); | |
| 117 | const big = await tool(admin, { action: "export" }); | |
| 118 | expect(big.value.bytes).toBeGreaterThan(16_000); | |
| 119 | expect(big.value.link).toBe(`${servers.url}/api/v1/user/export`); | |
| 120 | expect(big.value.counts.tokens).toBeGreaterThan(100); | |
| 121 | expect(big.text!.length).toBeLessThan(2_000); | |
| 122 | }); | |
| 88 | 123 | }); |
test/refusals-lead-somewhere.test.ts +1 −1
| @@ -36,7 +36,7 @@ const TERMINAL: Readonly<Record<string, string>> = { | ||
| 36 | 36 | "src/web/pages/origin-reach.ts\tAsk the host again to allow ${a.host}": "The label of the ask itself, on the button the helper draws.", |
| 37 | 37 | "src/web/pages/passkeys.ts\tThis link has been used or has expired.": "The reader is signed out and locked out: the link came from an admin outside the forge, and there is no account to ask as.", |
| 38 | 38 | "src/actions/account_access.ts\tThat link has been used or has expired.": "The same link, for the API and the terminal: no account to ask as.", |
| 39 | "src/http/mcp/tools/profile.ts\t`{ displayName?, email?, commitEmail? }`: your own": "A tool's description of what its scope is, not a refusal.", | |
| 39 | "src/http/mcp/tools/profile.ts\t`{ displayName?, email?, commitEmail?, action? }`: your own": "A tool's description of what its scope is, not a refusal.", | |
| 40 | 40 | "src/http/api.ts\tthe origin refused: this connection cannot write": "The owner is of a repository on another host; the way through it names is the reader's own (connect again with a token that can write).", |
| 41 | 41 | "src/http/respond.ts\tUse a token or account with the scope or role": "The hint on every 403, which does not know what was refused; a refusal with something to ask for carries `ask` itself.", |
| 42 | 42 | "src/http/respond.ts\tYour token's scope or your role on this resource": "The same hint, by code.", |
new above
offline
Ceiling is invented here; reuse the MCP layer's ceiling or document its origin.