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

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/14417-the-mcp-list-tools-refuse-a-bad-limit-wi → main 4 commits fork

  • Merged. mason/14417-the-mcp-list-tools-refuse-a-bad-limit-wi was merged into main as df35f72.

Review

Sign in to review this change.
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`;
188188 | `proposal_tokens` | — | A child's propose-only token: `mint` (`child`, `origin`), `list`, `revoke` (`id`) | 396 |
189189 | `propose_to_parent_forge` | — | Offer this forge's landed change to its parent as a pull on `child/<name>/…` | 221 |
190190 | `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 |
192192 | `queue_gates` | — | Recent merge-queue gates: pulls, verdicts, failing tests | 277 |
193193 | `quotas` | — | Enforced per-account quotas (never an admin): repositories, apps, storage, previews, runs, checks | 342 |
194194 | `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
1717 return n >= 1 && n <= max ? n : undefined;
1818 }
1919
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
2029 /**
2130 * Parse a raw `cursor` against the shape the route issues: null when absent,
2231 * the string when it matches `shape`, undefined when it does not.
src/http/mcp/shared.ts +9
@@ -8,6 +8,7 @@ import { scopeAllows } from "../../core/tokens.ts";
88 import { getUserByName, type User } from "../../core/users.ts";
99 import { applyFleetIds, previewFleetIds } from "../../actions/fleet.ts";
1010 import { KIND_CHIPS, parseChipArgs } from "../../core/feed_chips.ts";
11 import { LIMIT_ERROR, limitSays, parseLimitValue } from "../../contract/page_query.ts";
1112 import type { Ctx } from "../context.ts";
1213 import type { BoardError } from "../../actions/boards.ts";
1314 import { BOARD_NOT_SAVED } from "../../actions/board_words.ts";
@@ -69,6 +70,14 @@ export const badLimit = (limit: unknown): Answer | undefined =>
6970 ? undefined
7071 : bad("invalid_limit", `limit is a whole number from 1 to ${LIST_LIMIT_MAX}`);
7172
73 /**
74 * A tool's `limit` read as the HTTP routes read it: the number, or the one `invalid_limit` refusal.
75 */
76 export function limitArg(args: Args, max: number, fallback: number): number | Answer {
77 const n = parseLimitValue(args["limit"], max, fallback);
78 return n === undefined ? bad(LIMIT_ERROR, limitSays(max)) : n;
79 }
80
7281 export interface Tool {
7382 readonly name: string;
7483 /**
src/http/mcp/tools/commits.ts +3 −4
@@ -3,7 +3,7 @@ import { diffCommit } from "../../../git/diff.ts";
33 import { repoDir } from "../../../git/repos.ts";
44 import { isSafeRef } from "../../../git/guard.ts";
55 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";
77
88 export const tool: Tool = {
99 name: "commits",
@@ -45,9 +45,8 @@ export const tool: Tool = {
4545 const ref = args["ref"] ?? repo.defaultBranch;
4646 if (typeof ref !== "string" || ref.length > 255 || !isSafeRef(ref))
4747 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;
5150 const log = await commitLog(repoDir(ctx.app.reposRoot, repo.ownerName, repo.name), ref, { limit });
5251 return good({ commits: log.ok ? log.value : [] });
5352 },
src/http/mcp/tools/issues.ts +3 −4
@@ -8,7 +8,7 @@ import { markReaders } from "../../../crew/direct.ts";
88 import type { Database } from "bun:sqlite";
99 import type { User } from "../../../core/users.ts";
1010 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";
1212
1313 export const tool: Tool = {
1414 name: "issues",
@@ -33,9 +33,8 @@ export const tool: Tool = {
3333 const state = args["state"] ?? "open";
3434 if (state !== "open" && state !== "closed" && state !== "all")
3535 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;
3938 let cursor: number | undefined;
4039 if (args["cursor"] !== undefined) {
4140 const raw = args["cursor"];
src/http/mcp/tools/pulls.ts +4 −5
@@ -9,7 +9,7 @@ import type { Ctx } from "../../context.ts";
99 import type { User } from "../../../core/users.ts";
1010 import { whyPulls } from "../../../flow/why.ts";
1111 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";
1313
1414 export const tool: Tool = {
1515 name: "pulls",
@@ -21,7 +21,7 @@ export const tool: Tool = {
2121 properties: {
2222 repo: REPO_ARG,
2323 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" },
2525 cursor: { type: "string" },
2626 },
2727 },
@@ -33,9 +33,8 @@ export const tool: Tool = {
3333 const state = args["state"] ?? "open";
3434 if (state !== "open" && state !== "closed")
3535 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;
3938 const db = ctx.app.db;
4039 const pulls = listPulls(db, repo.id, { state, limit, ...sealedScope(db, repo, user) });
4140 const reviews = reviewSummariesFor(db, pulls);
src/http/mcp/tools/queue_gates.ts +3 −4
@@ -1,6 +1,6 @@
11 import { doctorRuns } from "../../../checks/doctor.ts";
22 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";
44
55 export const tool: Tool = {
66 name: "queue_gates",
@@ -18,9 +18,8 @@ export const tool: Tool = {
1818 run(ctx, user, args) {
1919 const repo = repoArg(ctx, user, args);
2020 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;
2423 const gates = recentGates(ctx.app.db, repo.id, limit);
2524 const red = gates.filter((g) => g.state === "red").length;
2625 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 }

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs