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

MCP release_keeper answers the crew's run token on the forge's own repository, the same body as GET /api/v1/keeper #15455

Merged opened this

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

Merged

scout/forge:scout/15453-mcp-release-keeper-answers-the-crew-s-ru → main 0 commits fork

  • Merged. scout/15453-mcp-release-keeper-answers-the-crew-s-ru was merged into main as 9fa4a90.

Review

Sign in to review this change.
3 files changed +56 −3
src/http/mcp.ts +4
@@ -50,6 +50,7 @@ import { claimDone, completeWrite, dropWrite, recordWrite } from "../core/conver
5050 import { type Args, alreadyDone, notAdminTool, visible, plansOnly, str, REPO_ARG, lean, fenceAnswer, heldWrite, askedFirst, readThrough, type ToolShape } from "./mcp/shared.ts";
5151 import { CONVERSATION_BRIEF } from "../crew/conversation_brief.ts";
5252 import { TOOLS } from "./mcp/tools.ts";
53 import { runMcpRefusal } from "./run_gate.ts";
5354 import { NO_TOOL_HINT, READ_ONLY_REFUSAL, hintOf, hintOfText, noToolNamed, withNextStep } from "./mcp/hints.ts";
5455 import { DEFAULT_TOOLS, GUEST_TOOLS, wantsAll } from "./mcp/catalog.ts";
5556 import { roleOf, type Role } from "../core/roles.ts";
@@ -736,6 +737,9 @@ export function mcpRoutes(router: Router<Handler>): void {
736737 const fault = rpcFault(value, undefined);
737738 if (fault !== undefined) return fault;
738739 const message = asRpcObject(value);
740 // A run token calls `release_keeper` here and nothing else (`http/run_gate.ts`, #15453).
741 const runRefusal = runMcpRefusal(ctx.auth, message);
742 if (runRefusal !== undefined) return runRefusal;
739743 const reported = openToolCall(ctx, message, true);
740744 if (reported !== undefined) return json(await reported);
741745 const answer = await rpcOne(ctx, user, message);
src/http/run_gate.ts +23 −3
@@ -8,8 +8,9 @@
88 * `explore` and the OpenAPI address, whose handlers answer from those two repositories alone. Every
99 * other request answers `not_found`, the same as a repository the crew member cannot see: other
1010 * repositories (the crew member's own, the owner's private ones), the account, tokens, secrets,
11 * admin, MCP and the web pages. Allow-listed, so a route added tomorrow is closed to these tokens
12 * until someone writes it in here.
11 * admin, MCP (but `release_keeper` for a crew run on the forge's own repository) and the web pages.
12 * Allow-listed, so a route added tomorrow is closed to these tokens until someone writes it in
13 * here.
1314 *
1415 * Inside its own repositories the routes that reach out of it are refused too:
1516 * forking or copying it elsewhere, proposing to the parent, syncing from an
@@ -99,6 +100,21 @@ export function runRepos(app: App, auth: Auth): Repo[] | undefined {
99100 .filter((r): r is Repo => r !== undefined);
100101 }
101102
103 /** The one MCP tool a run token may call, and only on the forge's own repository (#15453). */
104 export const RUN_MCP_TOOL = "release_keeper";
105
106 /**
107 * What `/mcp` answers a run token's message, after `runGate` let a crew run on the forge's own
108 * repository through: undefined for a `tools/call` of `RUN_MCP_TOOL`, the gate's `not_found` for
109 * any other method or tool. Undefined for every other credential.
110 */
111 export function runMcpRefusal(auth: Auth, message: Record<string, unknown>): Response | undefined {
112 if (auth.run === undefined) return undefined;
113 const params = message["params"];
114 const name = typeof params === "object" && params !== null ? (params as Record<string, unknown>)["name"] : undefined;
115 return message["method"] === "tools/call" && name === RUN_MCP_TOOL ? undefined : hidden();
116 }
117
102118 /** The refusal for this request, or undefined when it may proceed. */
103119 export function runGate(
104120 app: App,
@@ -119,8 +135,12 @@ export function runGate(
119135 if ((method === "GET" || method === "HEAD") && FORGE_READS.has(path)) return undefined;
120136 // A crew run on the forge's own repository reads why releases stopped (#15449): the keeper's
121137 // answer, which its handler still gives only to `mayRead`. An app preview's token never does.
122 if ((method === "GET" || method === "HEAD") && path === "/api/v1/keeper" && scope.app !== true && forgeRepoOf(app)?.id === repo.id)
138 const crewOnForge = scope.app !== true && forgeRepoOf(app)?.id === repo.id;
139 if ((method === "GET" || method === "HEAD") && path === "/api/v1/keeper" && crewOnForge)
123140 return undefined;
141 // The same answer over MCP (#15453): the door opens to the body, where `runMcpRefusal` lets
142 // through one `tools/call` of `release_keeper` and nothing else.
143 if (method === "POST" && path === "/mcp" && crewOnForge) return undefined;
124144
125145 for (const own of [repo, fork]) {
126146 const api = under(path, "/api/v1/repos", own);
test/keeper-crew-reads.test.ts +29
@@ -27,6 +27,13 @@ describe("GET /api/v1/keeper with the crew's run token", () => {
2727 let preview: string;
2828 const get = (token?: string) =>
2929 fetch(`${servers.url}/api/v1/keeper`, token ? { headers: { authorization: `Bearer ${token}` } } : {});
30 const mcp = (body: unknown, token?: string) =>
31 fetch(`${servers.url}/mcp`, {
32 method: "POST",
33 headers: { "content-type": "application/json", ...(token ? { authorization: `Bearer ${token}` } : {}) },
34 body: JSON.stringify(body),
35 });
36 const call = (name: string, id = 1) => ({ jsonrpc: "2.0", id, method: "tools/call", params: { name, arguments: {} } });
3037 const until = new Date(Date.now() + 3_600_000).toISOString();
3138
3239 beforeAll(async () => {
@@ -73,4 +80,26 @@ describe("GET /api/v1/keeper with the crew's run token", () => {
7380 test("a run on another repository and an app preview's token are not let through", async () => {
7481 for (const token of [elsewhere, preview]) expect((await get(token)).status).toBe(404);
7582 });
83
84 test("over MCP, the crew's token gets the same body release_keeper as GET /api/v1/keeper", async () => {
85 const res = await mcp(call("release_keeper"), crew);
86 expect(res.status).toBe(200);
87 const rpc: any = await res.json();
88 expect(rpc.result.isError).toBeUndefined();
89 const over: any = JSON.parse(rpc.result.content[0].text);
90 const http: any = await (await get(crew)).json();
91 expect(over).toEqual(http);
92 expect(over.heartbeat).toMatchObject({ outcome: "skipped" });
93 });
94
95 test("over MCP, the crew's token reaches no other tool or method", async () => {
96 for (const body of [call("whoami"), call("read_file"), { jsonrpc: "2.0", id: 2, method: "tools/list", params: {} }, { jsonrpc: "2.0", id: 3, method: "initialize", params: {} }])
97 expect((await mcp(body, crew)).status).toBe(404);
98 });
99
100 test("over MCP, anonymous, a run elsewhere and an app preview stay refused", async () => {
101 const anon = await mcp(call("release_keeper"));
102 expect(anon.status).toBe(401);
103 for (const token of [elsewhere, preview]) expect((await mcp(call("release_keeper"), token)).status).toBe(404);
104 });
76105 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs