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

OAuth for the MCP: the Claude app and Grok connectors can attach, minting the same ro/rw tokens #1405

Closed opened this in answer to
Closed without merging

scout/forge:scout/384-oauth-for-the-mcp-the-claude-app-and-gro → main 8 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +208 −1
docs/MCP.md +8
@@ -101,6 +101,14 @@ page. **Grok**: Connectors → Add custom connector, the same `/mcp` URL;
101101 approve the page it opens. Pick `ro` unless the connector should be able to
102102 `tell` and `plan`.
103103
104 **By voice.** Once attached, "how is the forge?" calls `brief`: three or four
105 short sentences to read aloud — which milestones took merges this week, what
106 waits for you, how many runs are going. It is built from the same numbers as
107 `milestones` and `now`, and an `ro` token may ask. The last wave's headline
108 is spoken once waves exist (`waves/` is not in the forge yet); until then the
109 sentence is left out rather than invented. Grok's connector flow and whether
110 its free tier allows custom connectors are unconfirmed.
111
104112 The token's scope is the whole of its authority: a connector holding `ro`
105113 can call `apps` and is refused `tell`.
106114
src/core/brief.ts added +64
@@ -0,0 +1,64 @@
1 /**
2 * The forge in a few spoken sentences: what moved this week, what waits for
3 * the owner. Plain data in, one short string out — written to be read aloud
4 * (a Grok connector in a car), so no numbers beyond counts, no markup, no
5 * identifiers a voice would stumble on.
6 */
7
8 export type Moved = { readonly name: string; readonly merged: number };
9
10 /** How many milestones are named aloud; the rest are counted. */
11 export const BRIEF_NAMED = 3;
12 /** Longest a name is read; a title is not a paragraph. */
13 const NAME_MAX = 60;
14
15 const plural = (n: number, one: string, many: string) =>
16 `${n} ${n === 1 ? one : many}`;
17
18 const spoken = (name: string) =>
19 (name.length > NAME_MAX ? `${name.slice(0, NAME_MAX).trimEnd()}…` : name)
20 .replace(/[`*_#<>[\]]/g, "")
21 .replace(/\s+/g, " ")
22 .trim();
23
24 /** Milestones that took merges in the last week, most first. */
25 function movedSentence(moved: readonly Moved[]): string {
26 const live = moved.filter((m) => m.merged > 0);
27 if (live.length === 0) return "Nothing merged this week.";
28 const top = [...live].sort((a, b) => b.merged - a.merged);
29 const named = top
30 .slice(0, BRIEF_NAMED)
31 .map((m) => `${spoken(m.name)} with ${plural(m.merged, "change", "changes")}`);
32 const rest = top.length - named.length;
33 const list =
34 named.length === 1
35 ? named[0]!
36 : `${named.slice(0, -1).join(", ")} and ${named[named.length - 1]}`;
37 return `${plural(live.length, "milestone", "milestones")} moved this week: ${list}${
38 rest > 0 ? `, and ${rest} more` : ""
39 }.`;
40 }
41
42 function waitsSentence(waits: number): string {
43 return waits === 0
44 ? "Nothing needs you."
45 : `${plural(waits, "thing needs", "things need")} you.`;
46 }
47
48 /** Three or four sentences; the wave sentence only when a wave is told. */
49 export function briefWords(input: {
50 moved: readonly Moved[];
51 waits: number;
52 activeRuns: number;
53 wave?: string;
54 }): string {
55 const crew =
56 input.activeRuns === 0
57 ? "The crew is idle."
58 : `The crew has ${plural(input.activeRuns, "run", "runs")} going.`;
59 const wave =
60 input.wave === undefined || input.wave.trim() === ""
61 ? []
62 : [`The last wave: ${spoken(input.wave)}.`];
63 return [movedSentence(input.moved), waitsSentence(input.waits), crew, ...wave].join(" ");
64 }
src/http/mcp.ts +34 −1
@@ -82,6 +82,7 @@ import {
8282 OUTSIDERS_NOTE,
8383 } from "../crew/direct.ts";
8484 import { setCrewOutsiders } from "../actions/repos.ts";
85 import { BRIEF_NAMED, briefWords, type Moved } from "../core/brief.ts";
8586 import { mapJson, marksFor } from "../web/pages/map.ts";
8687 import { undoFleetOp } from "../actions/fleet.ts";
8788 import { applyLimb, isLimbAction, previewLimb } from "../actions/limbs.ts";
@@ -89,7 +90,7 @@ import {
8990 milestoneData,
9091 milestonesJson,
9192 } from "../web/pages/milestones.ts";
92 import { appsOf, asksAnswer } from "../web/studio/asks.ts";
93 import { appsOf, asksAnswer, writableAppsOf } from "../web/studio/asks.ts";
9394 import { applyAsksBulk, previewAsksBulk } from "../actions/asks_bulk.ts";
9495 import type { Ctx, Handler } from "./context.ts";
9596 import {
@@ -207,6 +208,9 @@ function issueArg(ctx: Ctx, repo: Repo, args: Args, key: string) {
207208
208209 const isAnswer = (v: Repo | Answer): v is Answer => "ok" in v;
209210
211 /** Apps the spoken brief reads; a voice answer must be fast. */
212 const BRIEF_REPOS = 20;
213
210214 const TOOLS: readonly Tool[] = [
211215 {
212216 name: "apps",
@@ -561,6 +565,35 @@ const TOOLS: readonly Tool[] = [
561565 ]);
562566 },
563567 },
568 {
569 name: "brief",
570 description:
571 "How the forge is, in three or four short spoken sentences for reading aloud: the milestones that took merges this week, what waits for you, and how many runs are going. Built from the same numbers as `milestones` and `now`. Answer with `says` as it is.",
572 inputSchema: { type: "object", properties: {} },
573 writes: false,
574 async run(ctx, user) {
575 const { repos } = writableAppsOf(ctx, user);
outdated

High (correctness): appsOf returns every app the caller can see, which includes strangers' public apps, newest first. With .slice(0, BRIEF_REPOS), the brief can name other people's milestones and leave out the owner's own. Use writableAppsOf(ctx, user) here. Its doc comment marks it as the "your apps" reading.

576 const moved: Moved[] = [];
577 for (const repo of repos.slice(0, BRIEF_REPOS)) {
578 const limbs = milestonesJson(ctx, repo)["limbs"];
579 for (const l of Array.isArray(limbs) ? limbs : []) {
580 const merged = (l.mergedInDays as { merged?: unknown } | undefined)
581 ?.merged;
582 if (typeof merged === "number" && merged > 0 && !l.deferred)
583 moved.push({ name: String(l.name), merged });
584 }
585 }
586 const now = await TOOLS.find((t) => t.name === "now")!.run(ctx, user, {});
587 if (!now.ok) return now;
588 const rows = now.value as { run: string | null }[];
589 const waits = rows.filter((r) => r.run === null).length;
590 const activeRuns = rows.length - waits;
591 return good({
592 says: briefWords({ moved, waits, activeRuns }),
593 named: BRIEF_NAMED,
594 });
595 },
596 },
564597 {
565598 name: "stop",
566599 description:
test/brief.test.ts added +43
@@ -0,0 +1,43 @@
1 /** The spoken brief: three or four short sentences, readable aloud. */
2 import { describe, expect, test } from "bun:test";
3 import { briefWords } from "../src/core/brief.ts";
4
5 const sentences = (s: string) => s.split(/(?<=\.)\s+/);
6
7 describe("briefWords", () => {
8 test("says what moved, what needs the owner, and the crew, in four sentences at most", () => {
9 const says = briefWords({
10 moved: [
11 { name: "Mail", merged: 2 },
12 { name: "The `door`", merged: 5 },
13 { name: "Billing", merged: 1 },
14 { name: "Search", merged: 1 },
15 { name: "Idle", merged: 0 },
16 ],
17 waits: 2,
18 activeRuns: 1,
19 wave: "Sign in with a link",
20 });
21 expect(says).toBe(
22 "4 milestones moved this week: The door with 5 changes, Mail with 2 changes and Billing with 1 change, and 1 more. 2 things need you. The crew has 1 run going. The last wave: Sign in with a link.",
23 );
24 expect(sentences(says).length).toBeLessThanOrEqual(4);
25 });
26
27 test("a quiet forge is three plain sentences and no wave is invented", () => {
28 const says = briefWords({ moved: [], waits: 0, activeRuns: 0 });
29 expect(says).toBe(
30 "Nothing merged this week. Nothing needs you. The crew is idle.",
31 );
32 });
33
34 test("a long name is cut and markup is not read aloud", () => {
35 const says = briefWords({
36 moved: [{ name: "# " + "x".repeat(200), merged: 1 }],
37 waits: 1,
38 activeRuns: 0,
39 });
40 expect(says.length).toBeLessThan(200);
41 expect(says).not.toContain("#");
42 });
43 });
test/mcp.test.ts +59
@@ -12,6 +12,9 @@ import { createApp, type App } from "../src/app.ts";
1212 import { addToCrew, createAgent } from "../src/actions/crew.ts";
1313 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
1414 import { createApp as insertApp } from "../src/core/apps.ts";
15 import { linkIssue } from "../src/actions/dependencies.ts";
16 import { openIssue } from "../src/actions/issues.ts";
17 import { mergePull, openPull } from "../src/actions/pulls.ts";
1518 import { listIssues } from "../src/core/issues.ts";
1619 import { getRepoById, type Repo } from "../src/core/repos.ts";
1720 import { createRun, listRuns } from "../src/core/runs.ts";
@@ -164,6 +167,7 @@ describe("the forge over MCP", () => {
164167 "asks_act",
165168 "seal",
166169 "now",
170 "brief",
167171 "stop",
168172 "health",
169173 "audit",
@@ -307,6 +311,12 @@ describe("the forge over MCP", () => {
307311 .error,
308312 ).toContain("finished");
309313 expect((await tool(rw, "now")).value).toEqual([]);
314
outdated

Only the quiet forge is tested through the MCP, so deleting the milestone loop in brief would still pass. Add one case where a merged change on the caller's milestone is named, and a stranger's public app with merges is not.

315 // The spoken brief: short sentences a voice can read, for a reader too.
316 const brief = await tool(ro, "brief");
317 expect(brief.value.says).toBe(
318 "Nothing merged this week. Nothing needs you. The crew is idle.",
319 );
310320 });
311321
312322 test("say lands on the conversation", async () => {
@@ -524,6 +534,55 @@ describe("the forge over MCP", () => {
524534 ).toContain("forbidden");
525535 });
526536
537 test("the brief names the caller's milestones, never a stranger's public ones", async () => {
538 /** An app of `owner`'s with a pillar whose one change merged. */
539 const moved = async (owner: User, name: string, pillar: string) => {
540 const made = await createRepo(app, owner, { name, visibility: "public" });
541 if (!made.ok) throw new Error(String(made.error));
542 const repo = () => getRepoById(app.db, made.value.id)!;
543 await commitToBranch(app, owner, repo(), {
544 message: "seed",
545 files: [{ path: "README.md", content: `# ${name}\n` }],
546 });
547 const record = insertApp(app.db, {
548 repoId: made.value.id,
549 slug: name,
550 access: "private",
551 });
552 if (!record.ok) throw new Error(record.error);
553 const open = (title: string) => {
554 const o = openIssue(app, owner, repo(), { title });
555 if (!o.ok) throw new Error(String(o.error));
556 return o.value;
557 };
558 const top = open(pillar);
559 const leaf = open(`${name} leaf`);
560 const linked = linkIssue(app, owner, repo(), top, leaf);
561 if (!linked.ok) throw new Error(String(linked.error));
562 await commitToBranch(app, owner, repo(), {
563 branch: "feature/x",
564 base: "main",
565 message: "x",
566 files: [{ path: "x.md", content: "x\n" }],
567 });
568 const pull = await openPull(app, owner, repo(), {
569 title: "Land x",
570 body: `Closes #${leaf.number}`,
571 baseBranch: "main",
572 headBranch: "feature/x",
573 });
574 if (!pull.ok) throw new Error(String(pull.error));
575 const merged = await mergePull(app, owner, repo(), pull.value, "squash");
576 if (!merged.ok) throw new Error(JSON.stringify(merged.error));
577 };
578 // The stranger (an admin, who writes everything) is newer, so sorts ahead.
579 await moved(bob, "mine", "P0 Mine: the caller's own");
580 await moved(alice, "theirs", "P0 Theirs: a stranger's");
581 const says: string = (await tool(bobRw, "brief")).value.says;
582 expect(says).toContain("P0 Mine");
583 expect(says).not.toContain("Theirs");
584 }, 30_000);
585
527586 test("a browser session is not a way in, even signed in", async () => {
528587 const signin = await fetch(`${base}/signin`, {
529588 method: "POST",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs