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

Parked opened this
Closed without merging

torque/forge:torque/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 +115 −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` (#8859) answers home's Open bugs tile and the terminal's bugs screen: open and overdue counts and each bug's repository, number, priority and due time; a forge admin sees every repository, anyone else only public ones.
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 +24
@@ -0,0 +1,24 @@
1 import { openBugs } from "../../../flow/bugs.ts";
2 import { good, type Tool } from "../shared.ts";
3
4 export const tool: Tool = {
5 name: "bugs",
6 doc: "no arguments: open bugs on their clocks (4 h for `high`, 48 h otherwise), as home's Open bugs tile and the terminal's bugs screen: `{ open, overdue, bugs: [{ repo, number, high, createdAt, dueAt, overdue }] }`, oldest due first; a forge admin sees every repository, anyone else only public ones",
7 description: "Open bugs with their clocks: counts and each bug's repository, number, priority and due time.",
8 inputSchema: { type: "object", properties: {} },
9 writes: false,
10 run(ctx, user) {
11 const admin = user.isAdmin === true && user.kind !== "agent";
12 const now = Date.now();
13 const bugs = openBugs(ctx.app.db, admin ? {} : { publicOnly: true })
14 .map((b) => ({ ...b, overdue: Date.parse(b.dueAt) <= now }))
15 .sort((a, b) => Date.parse(a.dueAt) - Date.parse(b.dueAt));
16 const overdue = bugs.filter((b) => b.overdue).length;
17 return good({
18 open: bugs.length,
19 overdue,
20 says: `${bugs.length} open bug${bugs.length === 1 ? "" : "s"}, ${overdue === 0 ? "none" : overdue} past ${overdue === 1 ? "its" : "their"} clock.`,
21 bugs,
22 });
23 },
24 };
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 +87
@@ -0,0 +1,87 @@
1 /** The `bugs` tool answers what the terminal's bugs screen does: an admin sees every repository, anyone else public ones. */
2 import { afterAll, beforeAll, describe, 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 import { promote } from "./helpers/members.ts";
11
12 describe("MCP bugs", () => {
13 let dir: string;
14 let app: App;
15 let servers: Servers;
16 let adminTok: string;
17 let guestTok: string;
18 let id = 1;
19 let n = 0;
20
21 const call = async (token: string) => {
22 const res = await fetch(`${servers.url}/mcp`, {
23 method: "POST",
24 headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` },
25 body: JSON.stringify({ jsonrpc: "2.0", id: id++, method: "tools/call", params: { name: "bugs", arguments: {} } }),
26 });
27 const body: any = await res.json();
28 return JSON.parse(body.result.content[0].text);
29 };
30
31 const bug = (repo: string, hoursOld: number, high: boolean) => {
32 n++;
33 const iid = `i${n}`;
34 const at = new Date(Date.now() - hoursOld * 3_600_000).toISOString();
35 app.db.run(
36 "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)",
37 [iid, repo, n, at],
38 );
39 for (const l of high ? ["bug", "high"] : ["bug"]) app.db.run("INSERT INTO issue_label (issue_id, label_id) VALUES (?1, ?2)", [iid, `${repo}-${l}`]);
40 };
41
42 beforeAll(async () => {
43 dir = await mkdtemp(join(tmpdir(), "forge-mcp-bugs-"));
44 app = createApp({ dataDir: dir, port: 0 });
45 servers = await serve(app);
46 const mk = async (username: string, first = false) => {
47 const u = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst: first });
48 if (!u.ok) throw new Error(u.error);
49 promote(app.db, u.value);
50 return u.value;
51 };
52 const alice = await mk("alice", true);
53 const bob = await mk("bob");
54 adminTok = createToken(app.db, alice.id, "ro", "ro").token;
55 guestTok = createToken(app.db, bob.id, "ro", "ro").token;
56 for (const [name, vis] of [["open", "public"], ["secret", "private"]] as const) {
57 app.db.run("INSERT INTO repo (id, owner_id, name, visibility) VALUES (?1, ?2, ?1, ?3)", [name, alice.id, vis]);
58 for (const l of ["bug", "high"]) app.db.run("INSERT INTO label (id, repo_id, name) VALUES (?1, ?2, ?3)", [`${name}-${l}`, name, l]);
59 }
60 bug("open", 5, true); // past its 4 h clock
61 bug("open", 1, false);
62 bug("secret", 60, false);
63 }, 30_000);
64
65 afterAll(async () => {
66 servers.stop();
67 app.close();
68 await rm(dir, { recursive: true, force: true });
69 });
70
71 test("an admin sees every repository, and an overdue high bug is named", async () => {
72 const got = await call(adminTok);
73 expect(got.open).toBe(3);
74 expect(got.overdue).toBe(2);
75 const high = got.bugs.find((b: any) => b.repo === "alice/open" && b.high);
76 expect(high).toMatchObject({ number: 1, overdue: true });
77 expect(Date.parse(high.dueAt) - Date.parse(high.createdAt)).toBe(4 * 3_600_000);
78 expect(got.bugs[0].repo).toBe("alice/secret");
79 });
80
81 test("a guest does not see a private repository's bug", async () => {
82 const got = await call(guestTok);
83 expect(got.open).toBe(2);
84 expect(got.overdue).toBe(1);
85 expect(JSON.stringify(got)).not.toContain("secret");
86 });
87 });
test/mcp.test.ts +1
@@ -298,6 +298,7 @@ describe("the forge over MCP", () => {
298298 "thought",
299299 "asked",
300300 "assigned",
301 "bugs",
301302 "assign",
302303 "app_members",
303304 "app_notes",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs