Public: anyone can read this; the crew can change it.
The MCP list tools refuse a bad limit with one code and the maximum their schema promises #14423
Check · Review · Land · Live done — Landed and live in production.
mason/forge:mason/14417-the-mcp-list-tools-refuse-a-bad-limit-wi → main 4 commits fork
8 files
changed +113
−18
docs/MCP.md +1 −1
| @@ -188,7 +188,7 @@ bytes in `tools/list`. After changing a tool run `bun scripts/mcp-table.ts`; | ||
| 188 | 188 | | `proposal_tokens` | — | A child's propose-only token: `mint` (`child`, `origin`), `list`, `revoke` (`id`) | 396 | |
| 189 | 189 | | `propose_to_parent_forge` | — | Offer this forge's landed change to its parent as a pull on `child/<name>/…` | 221 | |
| 190 | 190 | | `pull` | — | A pull: title, state, head, base; open: `mergeable`, `conflicts`, `checkStale`, `checkedAgainst`, `check` | 406 | |
| 191 | | `pulls` | — | A repository's pulls, newest first; no `repo`: all open pulls you may read, oldest first | 454 | | |
| 191 | | `pulls` | — | A repository's pulls, newest first; no `repo`: all open pulls you may read, oldest first | 492 | | |
| 192 | 192 | | `queue_gates` | — | Recent merge-queue gates: pulls, verdicts, failing tests | 277 | |
| 193 | 193 | | `quotas` | — | Enforced per-account quotas (never an admin): repositories, apps, storage, previews, runs, checks | 342 | |
| 194 | 194 | | `read_file` | — | No shell? Read a file or list a directory at a ref (default branch); over 5 MB answers tooLarge | 317 | |
src/contract/page_query.ts +9
| @@ -17,6 +17,15 @@ export function parseLimit(raw: string | null, max: number, fallback: number): n | ||
| 17 | 17 | return n >= 1 && n <= max ? n : undefined; |
| 18 | 18 | } |
| 19 | 19 | |
| 20 | /** | |
| 21 | * The same rule for a limit that arrived as a JSON value (an MCP argument): a number is whole, | |
| 22 | * never a string. | |
| 23 | */ | |
| 24 | export function parseLimitValue(raw: unknown, max: number, fallback: number): number | undefined { | |
| 25 | if (raw === undefined) return fallback; | |
| 26 | return typeof raw === "number" && Number.isInteger(raw) && raw >= 1 && raw <= max ? raw : undefined; | |
| 27 | } | |
| 28 | ||
| 20 | 29 | /** |
| 21 | 30 | * Parse a raw `cursor` against the shape the route issues: null when absent, |
| 22 | 31 | * the string when it matches `shape`, undefined when it does not. |
src/http/mcp/tools/commits.ts +3 −4
| @@ -3,7 +3,7 @@ import { diffCommit } from "../../../git/diff.ts"; | ||
| 3 | 3 | import { repoDir } from "../../../git/repos.ts"; |
| 4 | 4 | import { isSafeRef } from "../../../git/guard.ts"; |
| 5 | 5 | import { checkOfCommit } from "../../../actions/runs.ts"; |
| 6 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 6 | import { good, bad, limitArg, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 7 | 7 | |
| 8 | 8 | export const tool: Tool = { |
| 9 | 9 | name: "commits", |
| @@ -45,9 +45,8 @@ export const tool: Tool = { | ||
| 45 | 45 | const ref = args["ref"] ?? repo.defaultBranch; |
| 46 | 46 | if (typeof ref !== "string" || ref.length > 255 || !isSafeRef(ref)) |
| 47 | 47 | return bad("invalid", "`ref` is a branch, tag or commit"); |
| 48 | const limit = args["limit"] ?? 30; | |
| 49 | if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > 100) | |
| 50 | return bad("invalid", "`limit` is a whole number from 1 to 100"); | |
| 48 | const limit = limitArg(args, 100, 30); | |
| 49 | if (typeof limit !== "number") return limit; | |
| 51 | 50 | const log = await commitLog(repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), ref, { limit }); |
| 52 | 51 | return good({ commits: log.ok ? log.value : [] }); |
| 53 | 52 | }, |
src/http/mcp/tools/issues.ts +3 −4
| @@ -8,7 +8,7 @@ import { markReaders } from "../../../crew/direct.ts"; | ||
| 8 | 8 | import type { Database } from "bun:sqlite"; |
| 9 | 9 | import type { User } from "../../../core/users.ts"; |
| 10 | 10 | import { ISSUES_ROWS } from "../../../web/studio/cards.ts"; |
| 11 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 11 | import { good, bad, limitArg, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 12 | 12 | |
| 13 | 13 | export const tool: Tool = { |
| 14 | 14 | name: "issues", |
| @@ -33,9 +33,8 @@ export const tool: Tool = { | ||
| 33 | 33 | const state = args["state"] ?? "open"; |
| 34 | 34 | if (state !== "open" && state !== "closed" && state !== "all") |
| 35 | 35 | return bad("invalid", "`state` is open, closed or all"); |
| 36 | const limit = args["limit"] ?? LIST_LIMIT; | |
| 37 | if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > LIST_LIMIT_MAX) | |
| 38 | return bad("invalid_limit", `limit is a whole number from 1 to ${LIST_LIMIT_MAX}`); | |
| 36 | const limit = limitArg(args, LIST_LIMIT_MAX, LIST_LIMIT); | |
| 37 | if (typeof limit !== "number") return limit; | |
| 39 | 38 | let cursor: number | undefined; |
| 40 | 39 | if (args["cursor"] !== undefined) { |
| 41 | 40 | const raw = args["cursor"]; |
src/http/mcp/tools/pulls.ts +4 −5
| @@ -9,7 +9,7 @@ import type { Ctx } from "../../context.ts"; | ||
| 9 | 9 | import type { User } from "../../../core/users.ts"; |
| 10 | 10 | import { whyPulls } from "../../../flow/why.ts"; |
| 11 | 11 | import { pageRows } from "../page.ts"; |
| 12 | import { good, bad, type Args, type Answer, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 12 | import { good, bad, limitArg, type Args, type Answer, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 13 | 13 | |
| 14 | 14 | export const tool: Tool = { |
| 15 | 15 | name: "pulls", |
| @@ -21,7 +21,7 @@ export const tool: Tool = { | ||
| 21 | 21 | properties: { |
| 22 | 22 | repo: REPO_ARG, |
| 23 | 23 | state: { type: "string", enum: ["open", "closed"] }, |
| 24 | limit: { type: "integer", minimum: 1, maximum: 200 }, | |
| 24 | limit: { type: "integer", minimum: 1, maximum: 50, description: "with repo; 200 without" }, | |
| 25 | 25 | cursor: { type: "string" }, |
| 26 | 26 | }, |
| 27 | 27 | }, |
| @@ -33,9 +33,8 @@ export const tool: Tool = { | ||
| 33 | 33 | const state = args["state"] ?? "open"; |
| 34 | 34 | if (state !== "open" && state !== "closed") |
| 35 | 35 | return bad("invalid", "`state` is open or closed"); |
| 36 | const limit = args["limit"] ?? 20; | |
| 37 | if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > 50) | |
| 38 | return bad("invalid", "`limit` is a whole number from 1 to 50"); | |
| 36 | const limit = limitArg(args, 50, 20); | |
| 37 | if (typeof limit !== "number") return limit; | |
| 39 | 38 | const db = ctx.app.db; |
| 40 | 39 | const pulls = listPulls(db, repo.id, { state, limit, ...sealedScope(db, repo, user) }); |
| 41 | 40 | const reviews = reviewSummariesFor(db, pulls); |
src/http/mcp/tools/queue_gates.ts +3 −4
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | import { doctorRuns } from "../../../checks/doctor.ts"; |
| 2 | 2 | import { recentGates } from "../../../checks/gate_result.ts"; |
| 3 | import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 3 | import { good, limitArg, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"; | |
| 4 | 4 | |
| 5 | 5 | export const tool: Tool = { |
| 6 | 6 | name: "queue_gates", |
| @@ -18,9 +18,8 @@ export const tool: Tool = { | ||
| 18 | 18 | run(ctx, user, args) { |
| 19 | 19 | const repo = repoArg(ctx, user, args); |
| 20 | 20 | if (isAnswer(repo)) return repo; |
| 21 | const limit = args["limit"] ?? 20; | |
| 22 | if (typeof limit !== "number" || !Number.isInteger(limit) || limit < 1 || limit > 100) | |
| 23 | return bad("invalid", "`limit` is a whole number from 1 to 100"); | |
| 21 | const limit = limitArg(args, 100, 20); | |
| 22 | if (typeof limit !== "number") return limit; | |
| 24 | 23 | const gates = recentGates(ctx.app.db, repo.id, limit); |
| 25 | 24 | const red = gates.filter((g) => g.state === "red").length; |
| 26 | 25 | return good({ says: `${gates.length} gate(s), ${red} red.`, gates, doctor: doctorRuns(ctx.app.db, repo.id, limit) }); |
test/mcp-limit.test.ts added +81
| @@ -0,0 +1,81 @@ | ||
| 1 | /** | |
| 2 | * MCP list tools read `limit` as the HTTP routes do (#14417): the schema's maximum passes, anything | |
| 3 | * else is `invalid_limit`. | |
| 4 | */ | |
| 5 | import { afterAll, beforeAll, 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/core/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 | import { TOOLS } from "../src/http/mcp/tools.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 18 | ||
| 19 | let dataDir: string; | |
| 20 | let app: App; | |
| 21 | let servers: Servers; | |
| 22 | let token: string; | |
| 23 | ||
| 24 | async function call(name: string, args: unknown): Promise<{ error?: string; ok: boolean }> { | |
| 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({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }), | |
| 33 | }); | |
| 34 | const body = (await res.json()) as any; | |
| 35 | const text: string = body.result.content[0].text; | |
| 36 | return body.result.isError === true ? { ok: false, error: text } : { ok: true }; | |
| 37 | } | |
| 38 | ||
| 39 | beforeAll(async () => { | |
| 40 | dataDir = await mkdtemp(join(tmpdir(), "forge-limit-mcp-")); | |
| 41 | app = createApp({ dataDir, port: 0 }); | |
| 42 | servers = await serve(app); | |
| 43 | const u = await createUser(app.db, { | |
| 44 | origin: "seed", | |
| 45 | trusted: true, | |
| 46 | username: "ada", | |
| 47 | email: "[email protected]", | |
| 48 | password: "a-good-passphrase", | |
| 49 | }); | |
| 50 | if (!u.ok) throw new Error("user"); | |
| 51 | const r = await createRepo(app.db, app.reposRoot, { ownerId: u.value.id, name: "svc", visibility: "public" }); | |
| 52 | if (!r.ok) throw new Error("repo"); | |
| 53 | token = createToken(app.db, u.value.id, "ro", "ro").token; | |
| 54 | }, 30_000); | |
| 55 | ||
| 56 | afterAll(async () => { | |
| 57 | servers.stop(); | |
| 58 | app.db.close(); | |
| 59 | await rm(dataDir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | const maxOf = (name: string): number => { | |
| 63 | const t = TOOLS.find((x) => x.name === name)!; | |
| 64 | return (t.inputSchema as any).properties.limit.maximum; | |
| 65 | }; | |
| 66 | ||
| 67 | for (const name of ["pulls", "commits", "issues", "queue_gates"]) { | |
| 68 | test(`${name}: the schema's maximum passes; outside it is invalid_limit naming it`, async () => { | |
| 69 | const max = maxOf(name); | |
| 70 | const base = { repo: "ada/svc" }; | |
| 71 | // An empty repository has no commits to list: that is a refusal of a different kind, not of the | |
| 72 | // limit. | |
| 73 | for (const limit of [max, 1]) expect((await call(name, { ...base, limit })).error ?? "").not.toContain("invalid_limit"); | |
| 74 | for (const limit of [max + 1, 0, 1.5, "7"]) { | |
| 75 | const r = await call(name, { ...base, limit }); | |
| 76 | expect(r.ok).toBe(false); | |
| 77 | expect(r.error).toContain("invalid_limit"); | |
| 78 | expect(r.error).toContain(`limit is a whole number from 1 to ${max}`); | |
| 79 | } | |
| 80 | }); | |
| 81 | } |
new above
offline