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

MCP `subscription_wake`: use a resting subscription again now, as the Subscriptions page's Wake button does, for a forge admin #5445

Merged opened this

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

Merged

scout/forge:scout/5430-mcp-subscription-wake-use-a-resting-subs → main 3 commits fork

  • Merged. scout/5430-mcp-subscription-wake-use-a-resting-subs was merged into main as bd8fbdf.

Review

Sign in to review this change.
5 files changed +107 −0
docs/MCP.md +1
@@ -130,6 +130,7 @@ for a tunnel and a forge that sleeps.
130130 | `ssh_keys` | `{ action?, title?, key?, id? }`: list your own SSH keys (default), add one (`key` is an authorized_keys line, `title` optional) or remove one by `id`; the API's rules and errors, add and remove need an `rw` token | yes |
131131 | `stop` | stop one run by its id from `now` | yes |
132132 | `subscription_budget` | `{ id, usd }`: set an account's whole-life budget in dollars (up to the `subscription.budget_max_usd` limit), or clear it with `usd: null`, as the Subscriptions page does; a locked budget can only be lowered; a forge admin with an admin token | yes |
133 | `subscription_wake` | `{ id }`: use a resting Claude subscription again now, as the Subscriptions page's Wake button does; answers `{ id, says }`; a forge admin, an admin token | yes |
133134 | `system` | the machine the forge runs on, now: CPU, load, memory, disk, network, processes; a forge admin, or an agent granted `telemetry:read` | |
134135 | `take_updates` | a fork takes its parent's updates when git can do it alone; a conflict is refused. On a spawned forge's code repository it opens the one `forge/take-updates` pull and never moves the default branch | yes |
135136 | `telemetry_grant` | `{ agent, on }`: grant an agent `telemetry:read`, or take it back with `false`, as the Telemetry page does; a person admin's, with an admin token; answers `says` | yes |
src/http/mcp/tools/index.ts +1
@@ -112,6 +112,7 @@ export { tool as spend } from "./spend.ts";
112112 export { tool as ssh_keys } from "./ssh_keys.ts";
113113 export { tool as stop } from "./stop.ts";
114114 export { tool as subscription_budget } from "./subscription_budget.ts";
115 export { tool as subscription_wake } from "./subscription_wake.ts";
115116 export { tool as system } from "./system.ts";
116117 export { tool as take_updates } from "./take_updates.ts";
117118 export { tool as teams } from "./teams.ts";
src/http/mcp/tools/subscription_wake.ts added +24
@@ -0,0 +1,24 @@
1 import { getSubscription, wakeSubscription } from "../../../core/subscriptions.ts";
2 import { good, bad, type Tool } from "../shared.ts";
3
4 export const tool: Tool = {
5 name: "subscription_wake",
6 doc: "`{ id }`: use a resting Claude subscription again now, as the Subscriptions page's Wake button does; answers `{ id, says }`; a forge admin, an admin token",
7 description: "Admin: wake a resting subscription.",
8 inputSchema: {
9 type: "object",
10 properties: { id: { type: "string" } },
11 required: ["id"],
12 },
13 writes: true,
14 adminOnly: true,
15 needsAdminScope: true,
16 run(ctx, _user, args) {
17 const id = args["id"];
18 if (typeof id !== "string" || id === "" || id.length > 64)
19 return bad("invalid", "id is the subscription's id");
20 if (!getSubscription(ctx.app.db, id)) return bad("not_found", "no such subscription");
21 wakeSubscription(ctx.app.db, id);
22 return good({ id, says: "In use again." });
23 },
24 };
test/mcp-subscription-wake.test.ts added +80
@@ -0,0 +1,80 @@
1 /** MCP `subscription_wake`: the Subscriptions page's Wake 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, getUserByName } from "../src/core/users.ts";
10 import { addSubscription, getSubscription, restSubscription } from "../src/core/subscriptions.ts";
11 import { serve, type Servers } from "../src/http/server.ts";
12 import { quietLogs } from "./helpers/globals.ts";
13
14 quietLogs();
15
16 const PASS = "a-long-wake-passphrase";
17
18 describe("MCP subscription_wake", () => {
19 let dataDir = "";
20 let app: App;
21 let servers: Servers;
22
23 const id = (name: string) => getUserByName(app.db, name)!.id;
24 const mcp = async (token: string, args: unknown) => {
25 const res = await fetch(`${servers.url}/mcp`, {
26 method: "POST",
27 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` },
28 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "subscription_wake", arguments: args } }),
29 });
30 const body = (await res.json()) as any;
31 if (!body.result) return { error: true, text: String(body.error?.message ?? "") };
32 return { error: body.result.isError === true, text: body.result.content[0].text as string };
33 };
34
35 beforeAll(async () => {
36 dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-wake-"));
37 app = createApp({ dataDir, port: 0 });
38 servers = await serve(app);
39 for (const [username, isAdmin] of [["alice", true], ["bob", false]] as const) {
40 const made = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, password: PASS, isAdmin });
41 if (!made.ok) throw new Error(username);
42 }
43 }, 30_000);
44
45 afterAll(async () => {
46 servers.stop();
47 app.close();
48 await rm(dataDir, { recursive: true, force: true });
49 });
50
51 test("a resting subscription is no longer resting after the call", async () => {
52 const added = addSubscription(app.db, dataDir, { name: "resting-one", token: "sk-ant-oat01-" + "a".repeat(40) });
53 if (!added.ok) throw new Error(added.error);
54 const sub = added.value;
55 restSubscription(app.db, sub.id, new Date(Date.now() + 3600_000).toISOString(), "limit");
56 expect(getSubscription(app.db, sub.id)!.restingUntil).not.toBeNull();
57 const admin = createToken(app.db, id("alice"), "t", "admin").token;
58 const done = await mcp(admin, { id: sub.id });
59 expect(done.error).toBe(false);
60 expect(JSON.parse(done.text)).toEqual({ id: sub.id, says: "In use again." });
61 expect(getSubscription(app.db, sub.id)!.restingUntil).toBeNull();
62 });
63
64 test("an unknown id is refused", async () => {
65 const admin = createToken(app.db, id("alice"), "t", "admin").token;
66 const done = await mcp(admin, { id: "nope" });
67 expect(done.error).toBe(true);
68 expect(done.text).toContain("no such subscription");
69 });
70
71 test("a non-admin is refused", async () => {
72 const added = addSubscription(app.db, dataDir, { name: "resting-two", token: "sk-ant-oat01-" + "b".repeat(40) });
73 if (!added.ok) throw new Error(added.error);
74 restSubscription(app.db, added.value.id, new Date(Date.now() + 3600_000).toISOString(), "limit");
75 const bob = createToken(app.db, id("bob"), "t", "admin").token;
76 const done = await mcp(bob, { id: added.value.id });
77 expect(done.error).toBe(true);
78 expect(getSubscription(app.db, added.value.id)!.restingUntil).not.toBeNull();
79 });
80 });
test/mcp-write-scope.test.ts +1
@@ -24,6 +24,7 @@ const ADMIN_ARGS: Record<string, Record<string, unknown>> = {
2424 org_teams: { org: "acme", action: "create", team: "core", permission: "read" },
2525 agent: { action: "create", username: "robo", displayName: "Robo", model: "m", brief: "b" },
2626 subscription_budget: { id: "u1", usd: 5 },
27 subscription_wake: { id: "u1" },
2728 prompts: { repo: "alice/game", action: "remove", id: "p1" },
2829 telemetry_grant: { agent: "robo", on: true },
2930 hooks: { repo: "alice/game", action: "add", url: "https://example.com/h", secret: "0123456789abcdef0123" },

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs