Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/server/src/mcp/advertisedJsonSchema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ it("drops JSON noise, merges tagged unions, and keeps null where it is required"
target: {
type: "object",
properties: {
kind: { type: "string", enum: ["url", "number"] },
kind: {
type: "string",
enum: ["url", "number"],
description: "`url` requires `url`. `number` requires `number`.",
},
url: { type: "string" },
number: { type: "integer" },
},
Expand Down
32 changes: 23 additions & 9 deletions apps/server/src/mcp/advertisedJsonSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,28 @@ const mergeTaggedObjects = (branches: ReadonlyArray<Json>): JsonObject | undefin
return values.every((value) => value !== undefined) && new Set(values).size === values.length;
});
if (tag === undefined) return undefined;
const requireds = objects.map((object) =>
Array.isArray(object.required) ? object.required : [],
);
const [first = [], ...others] = requireds;
const requiredByAll = first.filter(
(name) => name !== tag && others.every((required) => required.includes(name)),
);
// Merging drops each branch's own required fields, so the tag names them.
const ownRequired = objects.flatMap((object, index) => {
const own = requireds[index]!.filter((name) => name !== tag && !requiredByAll.includes(name));
return own.length === 0
? []
: [
`\`${String(tagOf(object, tag))}\` requires ${own.map((name) => `\`${name}\``).join(", ")}.`,
];
});
const properties: JsonObject = {
[tag]: { type: "string", enum: objects.map((object) => tagOf(object, tag)) },
[tag]: {
type: "string",
enum: objects.map((object) => tagOf(object, tag)),
...(ownRequired.length === 0 ? {} : { description: ownRequired.join(" ") }),
},
};
for (const object of objects) {
for (const [name, property] of Object.entries(object.properties)) {
Expand All @@ -62,12 +82,6 @@ const mergeTaggedObjects = (branches: ReadonlyArray<Json>): JsonObject | undefin
}
}
}
const [first = [], ...others] = objects.map((object) =>
Array.isArray(object.required) ? object.required : [],
);
const requiredByAll = first.filter(
(name) => name !== tag && others.every((required) => required.includes(name)),
);
return { type: "object", properties, required: [tag, ...requiredByAll] };
};

Expand Down Expand Up @@ -104,8 +118,8 @@ const compact = (schema: Json, optional: boolean): Json => {
/**
* The input schema T3 advertises for a tool. Drops JSON noise that every thread pays for:
* `additionalProperties: true`, safe-integer bounds, nested `anyOf`, JSON-string fallbacks, and
* the `null` branch of optional properties. A tagged union of objects becomes one object.
* Decoding still uses the full schema, so each tag's own required fields still apply.
* the `null` branch of optional properties. A tagged union of objects becomes one object whose
* tag names each branch's own required fields. Decoding still uses the full schema.
*/
export const advertisedJsonSchema = (tool: Tool.Any) =>
compact(Tool.getJsonSchema(tool), false) as ReturnType<typeof Tool.getJsonSchema>;
2 changes: 1 addition & 1 deletion apps/server/src/provider/agentContextBudget.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"claudeSystemAppendBytes": 6245,
"codexAdditionalContextBytes": 7639,
"mcpToolListJsonBytes": 57321
"mcpToolListJsonBytes": 57786
}
Loading