Public: anyone can read this; the crew can change it.
MCP `pins`: list, pin and unpin the caller's pinned repositories, with the same rules as the API #7012
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/4798-mcp-pins-list-pin-and-unpin-the-caller-s → main 15 commits fork 4 threads · 4 unresolved
5 files
changed +191
−0
docs/MCP.md +1
| @@ -112,6 +112,7 @@ for a tunnel and a forge that sleeps. | ||
| 112 | 112 | | `outsiders` | `{ repo, on?, dryRun? }`: whether people who cannot write may direct the crew; with `on`, planned first (`dryRun` is true unless `false`); set only by the app's admin with an admin token, never an agent | yes | |
| 113 | 113 | | `parent_unlink` | no input: stop signing in through the parent forge, as Settings does; refused when it is the account's last way in, and "Nothing to unlink." when nothing is linked; an admin token | yes | |
| 114 | 114 | | `person` | `{ username, limit?, cursor? }`: a person's public profile and the repositories you may see, as `GET /api/v1/users/:username` and its `/repos`; a private one you cannot read is never listed | | |
| 115 | | `pins` | `{ action?, repo?, limit?, cursor? }`: `list` (the default) the caller's pinned repositories, paged by `owner/name` as `GET /api/v1/user/pins` is (`next_cursor`), leaving out a pin whose repository the caller can no longer read (the API's list still names it); `pin` or `unpin` `repo`, as `PUT`/`DELETE /api/v1/user/pins/:owner/:name` do. A repository the caller cannot see is not found | yes | | |
| 115 | 116 | | `plan` | `{ app, issues }`: open up to 12 linked issues, all or none (see below) | yes | |
| 116 | 117 | | `preview` | `{ url }`: what the forge pill says of an app host's URL (production or a pull's preview): `{ app, environment, pull: { number, title } \| null, check: "green" \| "red" \| "running" \| null, firstFailure, logUrl, working: [names] }`; `firstFailure` is the check's first failing test or its summary, `logUrl` is set only for someone who may read the log (a writer), `working` names the agents with a run going; a URL that is not one of this forge's app hosts is `invalid`, an app you cannot read is `not_found` and a pull you cannot see is `no_such_pull`, as `repo` and `pull` say | | |
| 117 | 118 | | `probe` | knock on a running app's doors as nobody, a user and an admin; the ones that disobey its contract | | |
src/http/mcp/tools/index.ts +1
| @@ -86,6 +86,7 @@ export { tool as origins } from "./origins.ts"; | ||
| 86 | 86 | export { tool as outsiders } from "./outsiders.ts"; |
| 87 | 87 | export { tool as parent_unlink } from "./parent_unlink.ts"; |
| 88 | 88 | export { tool as person } from "./person.ts"; |
| 89 | export { tool as pins } from "./pins.ts"; | |
| 89 | 90 | export { tool as plan } from "./plan.ts"; |
| 90 | 91 | export { tool as preview } from "./preview.ts"; |
| 91 | 92 | export { tool as probe } from "./probe.ts"; |
src/http/mcp/tools/pins.ts added +55
| @@ -0,0 +1,55 @@ | ||
| 1 | import { pageLimit } from "../../../core/issues.ts"; | |
| 2 | import { listPinned, pinRepo, unpinRepo } from "../../../actions/origins.ts"; | |
| 3 | import { canRead, getReposByIds } from "../../../core/repos.ts"; | |
| 4 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 5 | ||
| 6 | export const tool: Tool = { | |
| 7 | name: "pins", | |
| 8 | doc: "`{ action?, repo?, limit?, cursor? }`: `list` (the default) the caller's pinned repositories, paged by `owner/name` as `GET /api/v1/user/pins` is (`next_cursor`), leaving out a pin whose repository the caller can no longer read (the API's list still names it); `pin` or `unpin` `repo`, as `PUT`/`DELETE /api/v1/user/pins/:owner/:name` do. A repository the caller cannot see is not found", | |
| 9 | description: "Your pinned repos: list, pin or unpin `repo`.", | |
| 10 | inputSchema: { | |
| 11 | type: "object", | |
| 12 | properties: { | |
| 13 | action: { type: "string", enum: ["list", "pin", "unpin"] }, | |
| 14 | repo: REPO_ARG, | |
| 15 | limit: { type: "integer" }, | |
| 16 | cursor: { type: "string" }, | |
| 17 | }, | |
| 18 | }, | |
| 19 | writes: true, | |
| 20 | readsWithout: "repo", | |
| 21 | run(ctx, user, args) { | |
| 22 | const action = args["action"] ?? "list"; | |
| 23 | if (action !== "list" && action !== "pin" && action !== "unpin") | |
outdated
Unbounded list. The API pages this with |
||
| 24 | return bad("invalid", "action is list, pin or unpin"); | |
| 25 | if (action === "list") { | |
| 26 | const { limit, cursor } = args; | |
| 27 | if (limit !== undefined && (typeof limit !== "number" || !Number.isFinite(limit))) | |
| 28 | return bad("invalid", "`limit` is a number"); | |
| 29 | if (cursor !== undefined && typeof cursor !== "string") | |
| 30 | return bad("invalid", "`cursor` is the one a previous answer gave"); | |
| 31 | const size = pageLimit(limit as number | undefined); | |
| 32 | const pins = listPinned(ctx.app, user); | |
| 33 | const repos = getReposByIds(ctx.app.db, pins.map((p) => p.repoId)); | |
| 34 | const all = pins | |
| 35 | .flatMap((pin) => { | |
| 36 | const repo = repos.get(pin.repoId); | |
| 37 | if (!repo || !canRead(ctx.app.db, repo, user)) return []; | |
Good: this filters out pins whose repository the caller can no longer read. |
||
| 38 | return [{ repo: `${repo.ownerName}/${repo.name}`, pinnedAt: pin.createdAt }]; | |
| 39 | }) | |
| 40 | .sort((a, b) => (a.repo < b.repo ? -1 : a.repo > b.repo ? 1 : 0)); | |
| 41 | const after = cursor ? all.filter((p) => p.repo > cursor) : all; | |
| 42 | const page = after.slice(0, size); | |
| 43 | return good({ | |
| 44 | pins: page, | |
| 45 | next_cursor: after.length > size ? page[page.length - 1]!.repo : null, | |
| 46 | }); | |
| 47 | } | |
| 48 | const repo = repoArg(ctx, user, args); | |
| 49 | if (isAnswer(repo)) return repo; | |
| 50 | const done = action === "pin" ? pinRepo(ctx.app, user, repo) : unpinRepo(ctx.app, user, repo); | |
| 51 | return done.ok | |
| 52 | ? good({ repo: `${repo.ownerName}/${repo.name}`, pinned: action === "pin" }) | |
| 53 | : bad(done.error, action === "pin" ? "that could not be pinned" : "that repository is not pinned"); | |
| 54 | }, | |
| 55 | }; |
test/mcp-pins.test.ts added +133
| @@ -0,0 +1,133 @@ | ||
| 1 | /** | |
| 2 | * The `pins` tool is /api/v1/user/pins over MCP: the caller's own pins, the | |
| 3 | * same visibility rule, the same scope gate. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 6 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 7 | import { tmpdir } from "node:os"; | |
| 8 | import { join } from "node:path"; | |
| 9 | import { createApp, type App } from "../src/app.ts"; | |
| 10 | import { createRepo } from "../src/actions/repos.ts"; | |
| 11 | import { createToken } from "../src/core/tokens.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 14 | ||
| 15 | describe("MCP pins", () => { | |
| 16 | let dataDir: string; | |
| 17 | let app: App; | |
| 18 | let servers: Servers; | |
| 19 | let aliceRw: string; | |
| 20 | let bobRw: string; | |
| 21 | let bobRo: string; | |
| 22 | let id = 1; | |
| 23 | ||
| 24 | const tool = async (token: string, args: unknown) => { | |
| 25 | const res = await fetch(`${servers.url}/mcp`, { | |
| 26 | method: "POST", | |
| 27 | headers: { | |
| 28 | "content-type": "application/json", | |
| 29 | accept: "application/json, text/event-stream", | |
| 30 | authorization: `Bearer ${token}`, | |
| 31 | }, | |
| 32 | body: JSON.stringify({ | |
| 33 | jsonrpc: "2.0", | |
| 34 | id: id++, | |
| 35 | method: "tools/call", | |
| 36 | params: { name: "pins", arguments: args }, | |
| 37 | }), | |
| 38 | }); | |
| 39 | const body: any = await res.json(); | |
| 40 | const text: string = body.result.content[0].text; | |
| 41 | return body.result.isError === true ? { error: text } : { value: JSON.parse(text) }; | |
| 42 | }; | |
| 43 | const listed = async (token: string) => | |
| 44 | (await tool(token, {})).value.pins.map((p: any) => p.repo); | |
| 45 | ||
| 46 | beforeAll(async () => { | |
| 47 | dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-pins-")); | |
| 48 | app = createApp({ dataDir, port: 0 }); | |
| 49 | servers = await serve(app); | |
| 50 | const mk = async (username: string, first = false) => { | |
| 51 | const u = await createUser(app.db, { | |
| 52 | origin: "seed", | |
| 53 | trusted: true, | |
| 54 | username, | |
| 55 | email: `${username}@forge.local`, | |
| 56 | password: "a-good-passphrase", | |
| 57 | adminIfFirst: first, | |
| 58 | }); | |
| 59 | if (!u.ok) throw new Error(u.error); | |
| 60 | return u.value; | |
| 61 | }; | |
| 62 | const alice = await mk("alice", true); | |
| 63 | const bob = await mk("bob"); | |
| 64 | aliceRw = createToken(app.db, alice.id, "rw", "rw").token; | |
| 65 | bobRw = createToken(app.db, bob.id, "rw", "rw").token; | |
| 66 | bobRo = createToken(app.db, bob.id, "ro", "ro").token; | |
| 67 | for (const [name, visibility] of [["open", "public"], ["secret", "private"], ["later", "public"]] as const) { | |
| 68 | const made = await createRepo(app, alice, { name, visibility }); | |
| 69 | if (!made.ok) throw new Error(String(made.error)); | |
| 70 | } | |
| 71 | }, 30_000); | |
| 72 | ||
| 73 | afterAll(async () => { | |
| 74 | servers.stop(); | |
| 75 | app.close(); | |
| 76 | await rm(dataDir, { recursive: true, force: true }); | |
| 77 | }); | |
| 78 | ||
| 79 | test("a person pins, lists and unpins; the list is theirs alone", async () => { | |
| 80 | expect(await listed(bobRw)).toEqual([]); | |
| 81 | expect((await tool(bobRw, { action: "pin", repo: "alice/open" })).value).toEqual({ | |
| 82 | repo: "alice/open", | |
| 83 | pinned: true, | |
| 84 | }); | |
| 85 | expect(await listed(bobRw)).toEqual(["alice/open"]); | |
| 86 | expect(await listed(aliceRw)).toEqual([]); | |
| 87 | expect((await tool(bobRw, { action: "unpin", repo: "alice/open" })).value.pinned).toBe(false); | |
| 88 | expect(await listed(bobRw)).toEqual([]); | |
| 89 | expect((await tool(bobRw, { action: "unpin", repo: "alice/open" })).error).toContain("not_found"); | |
| 90 | }); | |
| 91 | ||
| 92 | test("a private repository the caller cannot see is not found", async () => { | |
| 93 | expect((await tool(bobRw, { action: "pin", repo: "alice/secret" })).error).toContain("not_found"); | |
| 94 | expect((await tool(bobRw, { action: "pin", repo: "alice/nope" })).error).toContain("not_found"); | |
| 95 | expect(await listed(bobRw)).toEqual([]); | |
| 96 | expect((await tool(aliceRw, { action: "pin", repo: "alice/secret" })).value.pinned).toBe(true); | |
| 97 | }); | |
| 98 | ||
| 99 | test("a pin on a repository that later went private is no longer listed", async () => { | |
| 100 | await tool(bobRw, { action: "pin", repo: "alice/later" }); | |
| 101 | expect(await listed(bobRw)).toEqual(["alice/later"]); | |
| 102 | app.db.run("UPDATE repo SET visibility = 'private' WHERE name = 'later'"); | |
| 103 | expect(await listed(bobRw)).toEqual([]); | |
| 104 | expect(await listed(aliceRw)).not.toContain("alice/later"); | |
| 105 | await tool(aliceRw, { action: "pin", repo: "alice/later" }); | |
| 106 | expect(await listed(aliceRw)).toContain("alice/later"); | |
| 107 | }); | |
| 108 | ||
| 109 | test("list pages by owner/name with limit and cursor", async () => { | |
| 110 | for (const n of ["pa", "pb", "pc"]) { | |
| 111 | app.db.run( | |
| 112 | "INSERT INTO repo (id, owner_id, name, visibility, default_branch) SELECT 'pg-' || ?1, owner_id, ?1, 'public', 'main' FROM repo WHERE name = 'open'", | |
| 113 | [n], | |
| 114 | ); | |
| 115 | await tool(bobRw, { action: "pin", repo: `alice/${n}` }); | |
| 116 | } | |
| 117 | const first = (await tool(bobRw, { limit: 2 })).value; | |
| 118 | expect(first.pins.map((p: any) => p.repo)).toEqual(["alice/pa", "alice/pb"]); | |
| 119 | expect(first.next_cursor).toBe("alice/pb"); | |
| 120 | const second = (await tool(bobRw, { limit: 2, cursor: first.next_cursor })).value; | |
| 121 | expect(second.pins.map((p: any) => p.repo)).toContain("alice/pc"); | |
| 122 | expect(second.next_cursor).toBeNull(); | |
| 123 | expect((await tool(bobRw, { limit: "x" })).error).toContain("invalid"); | |
| 124 | for (const n of ["pa", "pb", "pc"]) await tool(bobRw, { action: "unpin", repo: `alice/${n}` }); | |
| 125 | }); | |
| 126 | ||
| 127 | test("a read-only token lists but is refused pin and unpin; a bad action is invalid", async () => { | |
| 128 | expect(await listed(bobRo)).toEqual([]); | |
| 129 | expect((await tool(bobRo, { action: "pin", repo: "alice/open" })).error).toContain("forbidden"); | |
| 130 | expect((await tool(bobRo, { action: "unpin", repo: "alice/open" })).error).toContain("forbidden"); | |
| 131 | expect((await tool(bobRw, { action: "nuke", repo: "alice/open" })).error).toContain("invalid"); | |
| 132 | }); | |
| 133 | }); |
test/mcp.test.ts +1
| @@ -311,6 +311,7 @@ describe("the forge over MCP", () => { | ||
| 311 | 311 | "boards", |
| 312 | 312 | "board_write", |
| 313 | 313 | "board_pins", |
| 314 | "pins", | |
| 314 | 315 | "board_delete", |
| 315 | 316 | "report", |
| 316 | 317 | ])); |
new above
offline
Low: whether this call counts as a read is decided by whether
repois present, not byaction. So{action:"list", repo:"a/b"}from a read-only token is refused as a write, and{action:"pin"}with no repo gets through the scope gate as a read (it then fails atrepoArg, so nothing is written).readsWhen: (a) => (a.action ?? "list") === "list"would say what is meant. This can be a follow-up.