fix(mcp): keep advertised inputs within client reference budgets (#1749)

Squash merge of https://github.com/morluto/rea/pull/1749.

Co-authored-by: akushonkamen <akushonkamen@163.com>
Co-authored-by: GLM <noreply@z.ai>
This commit is contained in:
akushonkamen
2026-10-13 23:49:53 +08:00
committed by GitHub
co-authored by akushonkamen GLM
parent 77e71d55ec
commit d86d29c411
5 changed files with 259 additions and 8 deletions
@@ -217,6 +217,46 @@ function resolveReference(schema: unknown, reference: string): unknown {
return value;
}
function localReferenceExpansions(schema: Record<string, unknown>): number {
// Clients that render SDK declarations from the advertised input expand
// schema-local references against a shared per-input budget; Pi 1.1.0
// code-mode allows 32 and renders every later reference as `unknown`.
// Count conservatively: every resolvable, non-cyclic reference occurrence
// reached while walking the input, never the definitions themselves.
let count = 0;
const visit = (node: unknown, active: ReadonlySet<string>): void => {
if (Array.isArray(node)) {
node.forEach((child) => visit(child, active));
return;
}
if (!isRecord(node)) return;
if (typeof node.$ref === "string" && !active.has(node.$ref)) {
const target = resolveReference(schema, node.$ref);
if (target !== undefined) {
count += 1;
visit(target, new Set([...active, node.$ref]));
return;
}
}
for (const [key, value] of Object.entries(node)) {
if (
[
"$defs",
"definitions",
"examples",
"default",
"const",
"enum",
].includes(key)
)
continue;
visit(value, active);
}
};
visit(schema, new Set());
return count;
}
function recursiveReferences(tools: readonly ToolSchemas[]): string[] {
return tools.flatMap((tool) =>
(["inputSchema", "outputSchema"] as const).flatMap((kind) => {
@@ -674,6 +714,14 @@ it("ships nonrecursive input and output schemas in the generated catalog", () =>
expect(recursiveReferences(GENERATED_MCP_TOOL_CATALOG)).toEqual([]);
});
it("keeps advertised input references within the client expansion budget", () => {
for (const tool of GENERATED_MCP_TOOL_CATALOG)
expect(
localReferenceExpansions(tool.inputSchema),
`${tool.name} exhausts the shared reference-expansion budget`,
).toBeLessThanOrEqual(32);
});
it("rejects unresolved output-schema references even for uncalled tools", () => {
const errors = schemaErrors([
{