Skip to content

Claude Code. Any Model. The most powerful AI coding agent now speaks every language.

fix(mcp): check team verb argument types before resolving the run

Jack Rudenko856e2d5bac19
diff --git a/packages/cli/src/mcp-server.ts b/packages/cli/src/mcp-server.ts
index d3193b23..0aa8951f 100644
--- a/packages/cli/src/mcp-server.ts
+++ b/packages/cli/src/mcp-server.ts
@@ -706,6 +706,22 @@ function optionalString(raw: unknown, name: string): string | undefined {
return raw;
}
+/** A boolean argument: absent → false; anything but a JSON boolean is the caller's error. */
+function optionalBoolean(raw: unknown, name: string): boolean {
+ if (raw === undefined || raw === null) return false;
+ if (typeof raw !== "boolean")
+ throw new ContractErrorException("invalid_args", `'${name}' must be a boolean`);
+ return raw;
+}
+
+/** An integer argument such as `since_seq`; absent → undefined. */
+function optionalInteger(raw: unknown, name: string): number | undefined {
+ if (raw === undefined || raw === null) return undefined;
+ if (typeof raw !== "number" || !Number.isInteger(raw))
+ throw new ContractErrorException("invalid_args", `'${name}' must be an integer`);
+ return raw;
+}
+
/**
* `team(mode="status")` (§8 B): the legacy payload of the newest run at `path` plus
* `contract_version`, `capabilities` and `run`. Given a `run_id` that a newer run at the
@@ -754,9 +770,13 @@ export async function teamContractVerb(
): Promise<ToolAnswer> {
try {
if (mode === "list") return contractAnswer(listTeamRuns());
+ // Every argument's TYPE is checked before the run is looked up: a wrongly typed argument
+ // is the caller's error whether or not the run exists (§8 E).
const path = contractPath(args.path, mode);
const runId = optionalString(args.run_id, "run_id");
const slot = optionalString(args.slot, "slot");
+ const since = optionalInteger(args.since_seq, "since_seq");
+ const spans = optionalBoolean(args.spans, "spans");
switch (mode) {
case "status":
return contractAnswer(teamStatusAnswer(path, runId));
@@ -765,12 +785,7 @@ export async function teamContractVerb(
case "capture": {
if (slot === undefined)
throw new ContractErrorException("invalid_args", "'slot' is required for mode 'capture'");
- const since = args.since_seq;
- if (since !== undefined && (typeof since !== "number" || !Number.isInteger(since)))
- throw new ContractErrorException("invalid_args", "'since_seq' must be an integer");
- return contractAnswer(
- captureTeamSlot(path, slot, since as number | undefined, args.spans === true, runId)
- );
+ return contractAnswer(captureTeamSlot(path, slot, since, spans, runId));
}
}
} catch (e) {
diff --git a/packages/cli/src/team-list-contract.test.ts b/packages/cli/src/team-list-contract.test.ts
index 99dd9351..e77638f2 100644
--- a/packages/cli/src/team-list-contract.test.ts
+++ b/packages/cli/src/team-list-contract.test.ts
@@ -178,6 +178,29 @@ describe.skipIf(!MAGMUX)("team run registry (list / status / cancel / capture)",
expect(existsSync(path)).toBe(false);
});
+ it("checks every argument's type before the run lookup, for status, cancel and capture", async () => {
+ const path = join(base, "nothing-here");
+ const bad: Array<[string, Record<string, unknown>]> = [
+ ["spans", { spans: "yes" }],
+ ["spans", { spans: 1 }],
+ ["since_seq", { since_seq: "3" }],
+ ["since_seq", { since_seq: 1.5 }],
+ ["run_id", { run_id: 7 }],
+ ["slot", { slot: 1 }],
+ ];
+ for (const mode of ["status", "cancel", "capture"] as const)
+ for (const [name, extra] of bad) {
+ const r = json(await teamContractVerb(mode, { path, slot: "01", ...extra }));
+ expect(r.body.error.code).toBe("invalid_args");
+ expect(r.body.error.message).toContain(`'${name}'`);
+ }
+ // well-typed arguments reach the lookup: the run does not exist
+ const ok = json(
+ await teamContractVerb("capture", { path, slot: "01", spans: false, since_seq: 0 })
+ );
+ expect(ok.body.error.code).toBe("unknown_run");
+ });
+
it("answers unknown runs and slots with ContractError JSON", async () => {
const path = join(base, "nothing-here");
const unknownStatus = json(await teamContractVerb("status", { path }));