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 #6884

Parked opened this
Closed without merging

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

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +140 −0
docs/MCP.md +1
@@ -48,6 +48,7 @@ for a tunnel and a forge that sleeps.
4848 | `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 |
4949 | `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`) | |
5050 | `backups_destination` | `{ action, endpoint?, bucket?, region?, accessKey?, secretKey? }`: `set` or `forget` where snapshots go, validated as the Backups page does; answers `{ kind, place, says }`, never a key; a person admin's, with an admin token | yes |
51 | `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 |
5152 | `board_delete` | `{ slug }` (person admins): delete a board by slug; its history keeps it | yes |
5253 | `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 |
5354 | `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 +28
@@ -0,0 +1,28 @@
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: { type: "object" },
9 writes: true,
10 adminOnly: true,
11 async run(ctx, user, args) {
12 const field = (k: string): string | undefined => {
13 const v = args[k];
14 return v === undefined ? "" : typeof v === "string" ? v.trim() : undefined;
15 };
16 const input = {
17 endpoint: field("endpoint"),
18 bucket: field("bucket"),
19 region: field("region"),
20 accessKey: field("accessKey"),
21 secretKey: field("secretKey"),
22 };
23 if (Object.values(input).some((v) => v === undefined)) return bad("invalid", "send the fields as strings");
24 const said = await testDestination(ctx.app, user, input as DestinationInput);
25 if (!said.ok) return bad("not_found", "no such tool");
26 return good({ reached: said.value.startsWith("Reached"), says: said.value });
27 },
28 };
src/http/mcp/tools/index.ts +1
@@ -23,6 +23,7 @@ export { tool as assigned } from "./assigned.ts";
2323 export { tool as attach_look } from "./attach_look.ts";
2424 export { tool as audit } from "./audit.ts";
2525 export { tool as backups_destination } from "./backups_destination.ts";
26 export { tool as backups_test } from "./backups_test.ts";
2627 export { tool as board_delete } from "./board_delete.ts";
2728 export { tool as board_pins } from "./board_pins.ts";
2829 export { tool as board_write } from "./board_write.ts";
test/mcp-backups-test.test.ts added +109
@@ -0,0 +1,109 @@
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" encoding="UTF-8"?><ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/"><Name>my-bucket</Name><Prefix></Prefix><KeyCount>1</KeyCount><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>a.tar</Key><Size>1</Size></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.says).toStartWith("Reached ");
76 expect(out.reached).toBe(true);
77 expect(out.says).toStartWith("Reached ");
78 }, 20_000);
79
80 test("an unreachable one says why, and never echoes a secret", async () => {
81 const r = await test_(admin, fields("http://127.0.0.1:1"));
82 const out = JSON.parse(r.text);
83 expect(out.reached).toBe(false);
84 expect(out.says).toStartWith("Could not list ");
85 expect(r.text).not.toContain(SECRET);
86 expect(r.text).not.toContain("AKIAEXAMPLE");
87 }, 20_000);
88
89 test("a field the page would refuse is refused in the same words", async () => {
90 const r = await test_(admin, { ...fields("ftp://x") });
91 const out = JSON.parse(r.text);
92 expect(out.reached).toBe(false);
93 expect(out.says).toContain("The endpoint must be an http(s) URL");
94 expect(r.text).not.toContain(SECRET);
95 });
96
97 test("a non-admin is refused and does not see the tool", async () => {
98 const r = await test_(plain, fields(`http://127.0.0.1:${fake.port}`));
99 expect(r.error).toBe(true);
100 const listed = await rpc(plain, "tools/list");
101 expect(listed.result.tools.map((t: any) => t.name)).not.toContain("backups_test");
102 });
103
104 test("listed to an admin, not as a read", async () => {
105 const listed = await rpc(admin, "tools/list");
106 const tool = listed.result.tools.find((t: any) => t.name === "backups_test");
107 expect(tool.annotations?.readOnlyHint).not.toBe(true);
108 });
109 });
test/mcp.test.ts +1
@@ -287,6 +287,7 @@ describe("the forge over MCP", () => {
287287 "scorecard",
288288 "flow",
289289 "system",
290 "backups_test",
290291 "telemetry_history",
291292 "contribute",
292293 "limits",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs