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

MCP `bugs` lists open bugs with their clocks, as home's tile and the terminal do #8878

Parked opened this
Closed without merging

scout/forge:scout/8859-mcp-bugs-lists-open-bugs-with-their-cloc → main 3 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
5 files changed +110 −0
docs/MCP.md +2
@@ -29,6 +29,8 @@ for a tunnel and a forge that sleeps.
2929 The complete list, with schemas, is MCP `tools/list` called with a token.
3030 `/llms.txt`, under "## MCP tools", lists only the tools open without a token (one line each, from its `doc`) and counts the rest, pointing on to `tools/list`; signed in, it lists every tool.
3131
32 `bugs` lists open bugs with their clocks, as home's tile and the terminal's bugs screen do: an admin sees every repository, anyone else public ones only (#8859).
33
3234 Sending a failed webhook delivery again (`redeliver`, #8075) is the terminal's `r` on a hook's Deliveries screen only; the `hooks` tool has no action for it yet.
3335
3436 ### Adding a tool
src/http/mcp/tools/bugs.ts added +29
@@ -0,0 +1,29 @@
1 import { openBugs } from "../../../flow/bugs.ts";
2 import { good, type Tool } from "../shared.ts";
3
4 const MAX_LISTED = 100;
5
6 export const tool: Tool = {
7 name: "bugs",
8 doc: "open bugs on their clock, as home's tile and the terminal's bugs screen: `{ open, overdue, bugs: [{ repo, number, priority, dueAt, overdue }] }`, soonest due first; an admin sees every repository, anyone else public ones only",
9 description: "Open bugs with their clocks: counts, and each bug's due time.",
10 inputSchema: { type: "object", properties: {} },
11 writes: false,
12 run(ctx, user) {
13 const all = user.isAdmin === true && user.kind !== "agent";
14 const now = Date.now();
15 const bugs = openBugs(ctx.app.db, all ? {} : { publicOnly: true })
16 .map((b) => ({ repo: b.repo, number: b.number, priority: b.high ? "high" : "low", dueAt: b.dueAt, overdue: Date.parse(b.dueAt) <= now }))
17 .sort((a, b) => Date.parse(a.dueAt) - Date.parse(b.dueAt));
18 const overdue = bugs.filter((b) => b.overdue).length;
19 const first = bugs.find((b) => b.overdue);
20 return good({
21 says: first
22 ? `${overdue} of ${bugs.length} open bugs past their clock; longest waiting: ${first.repo}#${first.number} (${first.priority}).`
23 : `${bugs.length} open bugs, none past their clock.`,
24 open: bugs.length,
25 overdue,
26 bugs: bugs.slice(0, MAX_LISTED),
27 });
28 },
29 };
src/http/mcp/tools/index.ts +1
@@ -34,6 +34,7 @@ export { tool as board_pins } from "./board_pins.ts";
3434 export { tool as board_write } from "./board_write.ts";
3535 export { tool as boards } from "./boards.ts";
3636 export { tool as branches } from "./branches.ts";
37 export { tool as bugs } from "./bugs.ts";
3738 export { tool as capacity } from "./capacity.ts";
3839 export { tool as claim_link } from "./claim_link.ts";
3940 export { tool as close } from "./close.ts";
test/mcp-bugs.test.ts added +77
@@ -0,0 +1,77 @@
1 /** `bugs` lists open bugs with their clocks; a guest sees public repositories only. */
2 import { afterAll, beforeAll, expect, test } from "bun:test";
3 import { mkdtemp, rm } from "node:fs/promises";
4 import { tmpdir } from "node:os";
5 import { join } from "node:path";
6 import { createApp, type App } from "../src/app.ts";
7 import { createToken } from "../src/core/tokens.ts";
8 import { createUser } from "../src/core/users.ts";
9 import { serve, type Servers } from "../src/http/server.ts";
10
11 let dir: string;
12 let app: App;
13 let servers: Servers;
14 let adminTok: string;
15 let guestTok: string;
16 let n = 0;
17
18 const call = async (token: string): Promise<any> => {
19 const res = await fetch(`${servers.url}/mcp`, {
20 method: "POST",
21 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` },
22 body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "bugs", arguments: {} } }),
23 });
24 const body: any = await res.json();
25 return JSON.parse(body.result.content[0].text);
26 };
27
28 function bug(repo: string, hoursOld: number, high: boolean): void {
29 n++;
30 const created = new Date(Date.now() - hoursOld * 3_600_000).toISOString();
31 app.db.run("INSERT INTO issue (id, repo_id, number, kind, title, author_id, state, created_at) VALUES (?1, ?2, ?3, 'issue', 't', (SELECT id FROM user LIMIT 1), 'open', ?4)", [`i${n}`, repo, n, created]);
32 app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [`i${n}`, `${repo}-bug`]);
33 if (high) app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [`i${n}`, `${repo}-high`]);
34 }
35
36 beforeAll(async () => {
37 dir = await mkdtemp(join(tmpdir(), "forge-mcp-bugs-"));
38 app = createApp({ dataDir: dir, port: 0 });
39 servers = await serve(app);
40 const mk = async (username: string, first: boolean) => {
41 const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
42 if (!u.ok) throw new Error(u.error);
43 return u.value;
44 };
45 const alice = await mk("alice", true);
46 const carol = await mk("carol", false);
47 adminTok = createToken(app.db, alice.id, "ro", "ro").token;
48 guestTok = createToken(app.db, carol.id, "ro", "ro").token;
49 for (const [id, vis] of [["pub", "public"], ["priv", "private"]] as const) {
50 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES (?1, ?2, ?1, ?3)", [id, alice.id, vis]);
51 for (const l of ["bug", "high"]) app.db.run("INSERT INTO label (id, repo_id, name) VALUES (?1, ?2, ?3)", [`${id}-${l}`, id, l]);
52 }
53 bug("pub", 5, true); // overdue high
54 bug("pub", 1, false);
55 bug("priv", 10, true); // overdue, private
56 }, 30_000);
57
58 afterAll(async () => {
59 servers.stop();
60 app.close();
61 await rm(dir, { recursive: true, force: true });
62 });
63
64 test("an admin sees every bug, and the overdue high one is named", async () => {
65 const r = await call(adminTok);
66 expect(r).toMatchObject({ open: 3, overdue: 2 });
67 expect(r.bugs[0]).toMatchObject({ repo: "alice/priv", priority: "high", overdue: true });
68 expect(r.says).toContain("alice/priv#3");
69 });
70
71 test("a guest sees public repositories only", async () => {
72 const r = await call(guestTok);
73 expect(r).toMatchObject({ open: 2, overdue: 1 });
74 expect(r.bugs.map((b: any) => b.repo)).toEqual(["alice/pub", "alice/pub"]);
75 expect(JSON.stringify(r)).not.toContain("priv");
76 expect(r.says).toContain("alice/pub#1");
77 });
test/mcp.test.ts +1
@@ -273,6 +273,7 @@ describe("the forge over MCP", () => {
273273 "events",
274274 "reports",
275275 "branches",
276 "bugs",
276277 "commits",
277278 "waves",
278279 "probe",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs