Skip to content

Commit e34533a

Browse files
committed
fix(server): clamp t3_thread_read page sizes instead of rejecting them
Codex hides the minimum/maximum bounds on `limit`, `runLimit` and `maxCharsPerItem`, so models sent values like `maxCharsPerItem: 60000` and the call failed with -32602 (19% of calls in one report). Values above the maximum now clamp to it on decode, and the existing nextPosition/nextTextOffset pagination covers the rest. Each field's description states its bound. Values below 1 still fail. Fixes #22618
1 parent 4741dc5 commit e34533a

2 files changed

Lines changed: 95 additions & 3 deletions

File tree

‎apps/server/src/mcp/McpHttpServer.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,68 @@ it.effect("decodes null optional arguments the way it decodes omitted ones", ()
375375
}).pipe(Effect.provide(layerRecordingTools(calls)));
376376
});
377377

378+
it.effect(
379+
"clamps t3_thread_read page sizes above their maximum instead of rejecting the call",
380+
() => {
381+
const calls: Array<unknown> = [];
382+
return Effect.gen(function* () {
383+
const thread = { threadId: "thread-clamped-arguments" };
384+
// Codex never shows the model these bounds, so an over-large page must still read.
385+
yield* callTool("t3_thread_read", {
386+
...thread,
387+
limit: 150,
388+
runLimit: 80,
389+
maxCharsPerItem: 60_000,
390+
});
391+
yield* callTool("t3_thread_read", {
392+
...thread,
393+
limit: 100,
394+
runLimit: 50,
395+
maxCharsPerItem: 50_000,
396+
});
397+
yield* callTool("t3_thread_read", { ...thread, limit: 7, runLimit: 1, maxCharsPerItem: 1 });
398+
399+
expect(calls).toEqual([
400+
{ ...thread, limit: 100, runLimit: 50, maxCharsPerItem: 50_000 },
401+
{ ...thread, limit: 100, runLimit: 50, maxCharsPerItem: 50_000 },
402+
{ ...thread, limit: 7, runLimit: 1, maxCharsPerItem: 1 },
403+
]);
404+
}).pipe(Effect.provide(layerRecordingTools(calls)));
405+
},
406+
);
407+
408+
it.effect.each(["limit", "runLimit", "maxCharsPerItem"])(
409+
"still rejects a t3_thread_read %s below its minimum",
410+
(field) => {
411+
const calls: Array<unknown> = [];
412+
return Effect.gen(function* () {
413+
// Zero is a different request than one item, so it fails rather than answering another question.
414+
const rejected = yield* callTool("t3_thread_read", {
415+
threadId: "thread-below-minimum",
416+
[field]: 0,
417+
}).pipe(Effect.flip);
418+
419+
expect(String(rejected)).toContain("greater than or equal to 1");
420+
expect(calls).toEqual([]);
421+
}).pipe(Effect.provide(layerRecordingTools(calls)));
422+
},
423+
);
424+
425+
it.effect("tells the model each t3_thread_read page-size bound in the field description", () =>
426+
Effect.gen(function* () {
427+
const server = yield* McpServer.McpServer;
428+
const schema = server.tools.find(({ tool }) => tool.name === "t3_thread_read")?.tool
429+
.inputSchema as { properties?: Record<string, unknown> } | undefined;
430+
const advertised = (field: string) => JSON.stringify(schema?.properties?.[field]);
431+
432+
expect(advertised("limit")).toContain("1 to 100. Larger values are clamped to 100");
433+
expect(advertised("runLimit")).toContain("1 to 50. Larger values are clamped to 50");
434+
expect(advertised("maxCharsPerItem")).toContain(
435+
"1 to 50000. Larger values are clamped to 50000",
436+
);
437+
}).pipe(Effect.provide(layerRecordingTools([]))),
438+
);
439+
378440
it.effect(
379441
"registers the pull request toolkit and surfaces a missing capability as a tool error",
380442
() =>

‎packages/contracts/src/orchestratorMcp.ts‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,15 +367,45 @@ export const OrchestratorMcpThreadListResult = Schema.Struct({
367367
});
368368
export type OrchestratorMcpThreadListResult = typeof OrchestratorMcpThreadListResult.Type;
369369

370+
/**
371+
* Codex drops `minimum` and `maximum` from the declaration it shows the model, so it cannot see
372+
* these bounds and a rejected call only costs a retry. The description carries the bound instead,
373+
* and a larger value clamps on decode (the advertised schema therefore has no such maximum).
374+
* Values below 1 still fail: 0 asks for nothing, and answering it with one item would be a
375+
* different request.
376+
*/
377+
const clampedPositiveInt = (maximum: number, description: string) =>
378+
PositiveInt.annotate({ description }).pipe(
379+
Schema.decodeTo(
380+
PositiveInt.check(Schema.isLessThanOrEqualTo(maximum)),
381+
SchemaTransformation.transform({
382+
decode: (value) => Math.min(value, maximum),
383+
encode: (value) => value,
384+
}),
385+
),
386+
);
387+
370388
export const OrchestratorMcpThreadReadInput = Schema.Struct({
371389
threadId: ThreadId,
372390
itemId: Schema.optional(TurnItemId),
373391
textOffset: Schema.optional(NonNegativeInt),
374392
view: Schema.optional(Schema.Literals(["messages", "activity"])),
375393
afterPosition: Schema.optional(NonNegativeInt),
376-
limit: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(100))),
377-
runLimit: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(50))),
378-
maxCharsPerItem: Schema.optional(PositiveInt.check(Schema.isLessThanOrEqualTo(50_000))),
394+
limit: Schema.optional(
395+
clampedPositiveInt(
396+
100,
397+
"Timeline items per page, 1 to 100. Larger values are clamped to 100; continue with afterPosition=nextPosition.",
398+
),
399+
),
400+
runLimit: Schema.optional(
401+
clampedPositiveInt(50, "Recent runs to include, 1 to 50. Larger values are clamped to 50."),
402+
),
403+
maxCharsPerItem: Schema.optional(
404+
clampedPositiveInt(
405+
50_000,
406+
"Characters of text per timeline item, 1 to 50000. Larger values are clamped to 50000; recover the rest with itemId and textOffset=nextTextOffset.",
407+
),
408+
),
379409
});
380410
export type OrchestratorMcpThreadReadInput = typeof OrchestratorMcpThreadReadInput.Type;
381411

0 commit comments

Comments
 (0)