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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15618-the-same-export-is-an-action-on-an-exist → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/15618-the-same-export-is-an-action-on-an-exist was merged into main as 23c76c0.

Review

Sign in to review this change.
5 files changed +77 −2
docs/MCP.md +1
@@ -101,6 +101,7 @@ a failure names the bytes over and the longest descriptions.
101101
102102 - A new door is an `action` or `kind` on the tool that already owns the
103103 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).
104105 - A new tool's description is one sentence.
105106 - A `dryRun` argument is bare `{ type: "boolean" }`: the server's
106107 `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";
22 import { emailVerified } from "../../../core/verify.ts";
33 import { updateProfile } from "../../../actions/users.ts";
44 import { publicUser } from "../../respond.ts";
5 import { exportAnswer } from "../export_answer.ts";
56 import { good, bad, type Tool } from "../shared.ts";
67
78 export const tool: Tool = {
outdated

Ceiling is invented here; reuse the MCP layer's ceiling or document its origin.

89 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",
1011 description: "Read or change your profile.",
1112 inputSchema: {
1213 type: "object",
@@ -14,11 +15,15 @@ export const tool: Tool = {
1415 displayName: { type: "string" },
1516 email: { type: "string" },
1617 commitEmail: { type: "string" },
18 action: { enum: ["export"] },
1719 },
1820 },
1921 writes: true,
2022 readsWithout: ["displayName", "email", "commitEmail"],
23 readsWhen: (args) => args["action"] === "export",
2124 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);
2227 const { displayName, email, commitEmail } = args;
2328 if (displayName === undefined && email === undefined && commitEmail === undefined)
2429 return good({ ...publicUser(user), emailVerified: emailVerified(ctx.app.db, user.id) });
test/mcp-profile.test.ts +35
@@ -85,4 +85,39 @@ describe("MCP profile", () => {
8585 expect(res.error).toContain("admin-scope");
8686 expect((await tool(admin, {})).value.displayName).toBe(before);
8787 });
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 });
88123 });
test/refusals-lead-somewhere.test.ts +1 −1
@@ -36,7 +36,7 @@ const TERMINAL: Readonly<Record<string, string>> = {
3636 "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.",
3737 "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.",
3838 "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.",
4040 "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).",
4141 "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.",
4242 "src/http/respond.ts\tYour token's scope or your role on this resource": "The same hint, by code.",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs