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

MCP `backups_test`: check that the snapshot destination is reachable, as the Backups page's Test button does, for a forge admin #5780

Parked opened this
Closed without merging

scout/forge:scout/5474-mcp-backups-test-check-that-the-snapshot → main 4 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +148 −0
docs/MCP.md +1
@@ -45,6 +45,7 @@ for a tunnel and a forge that sleeps.
4545 | `assigned` | `{ limit? }`: the open issues and pulls assigned to you in every repository you can still read (repository, number, title, kind, state), as `GET /api/v1/user/assigned` | |
4646 | `attach_look` | `{ app, pull, width, says, image, dryRun? }`: attach a look to an open pull: a PNG or JPEG (at most 5 MB, base64 in `image`), `width` phone or desktop, one line in `says`; planned first (`dryRun` is true unless `false`) | yes |
4747 | `audit` | who can do what, per app, read from its contract.json in git: yes, own or no per door, and whether outsiders may direct its crew (`crewOutsiders`) | |
48 | `backups_test` | `{ endpoint?, bucket?, region?, accessKey?, secretKey? }`: list the snapshot bucket as the Backups page's Test button does (blank fields test the destination in effect); a forge admin only; answers `{ reached, says }`, never a key | yes |
4849 | `board_delete` | `{ slug }` (person admins): delete a board by slug; its history keeps it | yes |
4950 | `board_pins` | `{ op?, slug?, title?, by? }` (person admins): without `op`, the panels pinned to your home (`slug`, `title`); `pin` or `unpin` a panel by board `slug` and panel `title`, or `move` it up or down with `by` (-1 or 1) | yes |
5051 | `board_write` | `{ slug, board }` (person admins): write a whole board under a slug, made or replaced: `{ title, range, refresh, panels: [{ title, chart, query, w, h }] }` | yes |
src/http/mcp/tools/backups_test.ts added +37
@@ -0,0 +1,37 @@
1 import { testDestination, type DestinationInput } from "../../../actions/destination.ts";
2 import { good, bad, type Tool } from "../shared.ts";
3
4 export const tool: Tool = {
5 name: "backups_test",
6 doc: "`{ endpoint?, bucket?, region?, accessKey?, secretKey? }`: list the snapshot bucket as the Backups page's Test button does (blank fields test the destination in effect); a forge admin only; answers `{ reached, says }`, never a key",
7 description: "Test the snapshot destination.",
8 inputSchema: {
9 type: "object",
10 properties: {
11 endpoint: {},
12 bucket: {},
13 region: {},
14 accessKey: {},
15 secretKey: {},
16 },
17 },
18 writes: true,
19 adminOnly: true,
20 async run(ctx, user, args) {
21 const field = (k: string): string | undefined => {
22 const v = args[k];
23 return v === undefined ? "" : typeof v === "string" ? v.trim() : undefined;
24 };
25 const input = {
26 endpoint: field("endpoint"),
27 bucket: field("bucket"),
28 region: field("region"),
29 accessKey: field("accessKey"),
30 secretKey: field("secretKey"),
31 };
32 if (Object.values(input).some((v) => v === undefined)) return bad("invalid", "send the fields as strings");
33 const said = await testDestination(ctx.app, user, input as DestinationInput);
34 if (!said.ok) return bad("not_found", "no such tool");
35 return good({ reached: said.value.startsWith("Reached"), says: said.value });
36 },
37 };
src/http/mcp/tools/index.ts +1
@@ -21,6 +21,7 @@ export { tool as assign } from "./assign.ts";
2121 export { tool as assigned } from "./assigned.ts";
2222 export { tool as attach_look } from "./attach_look.ts";
2323 export { tool as audit } from "./audit.ts";
24 export { tool as backups_test } from "./backups_test.ts";
2425 export { tool as board_delete } from "./board_delete.ts";
2526 export { tool as board_pins } from "./board_pins.ts";
2627 export { tool as board_write } from "./board_write.ts";
test/mcp-backups-test.test.ts added +108
@@ -0,0 +1,108 @@
1 /** MCP `backups_test`: the Backups page's Test button, from a harness. */
2
3 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
4 import { mkdtemp, rm } from "node:fs/promises";
5 import { tmpdir } from "node:os";
6 import { join } from "node:path";
7 import { createApp, type App } from "../src/app.ts";
8 import { createToken } from "../src/core/tokens.ts";
9 import { createUser } from "../src/core/users.ts";
10 import { serve, type Servers } from "../src/http/server.ts";
11 import { quietLogs } from "./helpers/globals.ts";
12
13 quietLogs();
14
15 const PASS = "a-long-backups-passphrase";
16 const SECRET = "sekrit-key-1234";
17
18 describe("MCP backups_test", () => {
19 let dataDir = "";
20 let app: App;
21 let servers: Servers;
22 let fake: ReturnType<typeof Bun.serve>;
23 let admin = "";
24 let plain = "";
25
26 const rpc = async (token: string, method: string, params?: unknown) => {
27 const res = await fetch(`${servers.url}/mcp`, {
28 method: "POST",
29 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` },
30 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
31 });
32 return (await res.json()) as any;
33 };
34 const test_ = async (token: string, args: unknown) => {
35 const body = await rpc(token, "tools/call", { name: "backups_test", arguments: args });
36 if (body.error) return { error: true, text: body.error.message as string };
37 return { error: body.result.isError === true, text: body.result.content[0].text as string };
38 };
39
40 beforeAll(async () => {
41 dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-backups-"));
42 app = createApp({ dataDir, port: 0 });
43 servers = await serve(app);
44 fake = Bun.serve({
45 port: 0,
46 hostname: "127.0.0.1",
47 fetch: () =>
48 new Response(
49 '<?xml version="1.0"?><ListBucketResult><IsTruncated>false</IsTruncated><Contents><Key>a.tar</Key></Contents></ListBucketResult>',
50 { headers: { "content-type": "application/xml" } },
51 ),
52 });
53 const made: Record<string, any> = {};
54 for (const [username, isAdmin] of [["alice", true], ["bob", false]] as const) {
55 made[username] = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, password: PASS, isAdmin });
56 if (!made[username].ok) throw new Error(username);
57 }
58 admin = createToken(app.db, made["alice"].value.id, "t", "admin").token;
59 plain = createToken(app.db, made["bob"].value.id, "t", "admin").token;
60 }, 30_000);
61
62 afterAll(async () => {
63 fake.stop(true);
64 servers.stop();
65 app.close();
66 await rm(dataDir, { recursive: true, force: true });
67 });
68
69 const fields = (endpoint: string) => ({ endpoint, bucket: "my-bucket", accessKey: "AKIAEXAMPLE", secretKey: SECRET });
70
71 test("a reachable local destination is reached, and says so", async () => {
72 const r = await test_(admin, fields(`http://127.0.0.1:${fake.port}`));
73 expect(r.error).toBe(false);
74 const out = JSON.parse(r.text);
75 expect(out.reached).toBe(true);
76 expect(out.says).toStartWith("Reached ");
77 }, 20_000);
78
79 test("an unreachable one says why, and never echoes a secret", async () => {
80 const r = await test_(admin, fields("http://127.0.0.1:1"));
81 const out = JSON.parse(r.text);
82 expect(out.reached).toBe(false);
83 expect(out.says).toStartWith("Could not list ");
84 expect(r.text).not.toContain(SECRET);
85 expect(r.text).not.toContain("AKIAEXAMPLE");
86 }, 20_000);
87
88 test("a field the page would refuse is refused in the same words", async () => {
89 const r = await test_(admin, { ...fields("ftp://x") });
90 const out = JSON.parse(r.text);
91 expect(out.reached).toBe(false);
92 expect(out.says).toContain("The endpoint must be an http(s) URL");
93 expect(r.text).not.toContain(SECRET);
94 });
95
96 test("a non-admin is refused and does not see the tool", async () => {
97 const r = await test_(plain, fields(`http://127.0.0.1:${fake.port}`));
98 expect(r.error).toBe(true);
99 const listed = await rpc(plain, "tools/list");
100 expect(listed.result.tools.map((t: any) => t.name)).not.toContain("backups_test");
101 });
102
103 test("listed to an admin, not as a read", async () => {
104 const listed = await rpc(admin, "tools/list");
105 const tool = listed.result.tools.find((t: any) => t.name === "backups_test");
106 expect(tool.annotations?.readOnlyHint).not.toBe(true);
107 });
108 });
test/mcp.test.ts +1
@@ -280,6 +280,7 @@ describe("the forge over MCP", () => {
280280 "scorecard",
281281 "flow",
282282 "system",
283 "backups_test",
283284 "telemetry_history",
284285 "contribute",
285286 "limits",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs