fix(mcp): flatten root union input schemas (#1062)

* fix(mcp): flatten root union input schemas

* fix(mcp): preserve minimum input requirements in object schemas

---------

Co-authored-by: N0zoM1z0 <1705004398@qq.com>
This commit is contained in:
cevieiras
2026-10-08 20:54:51 +08:00
committed by GitHub
co-authored by N0zoM1z0
parent 170600541b
commit 6a7650e93c
2 changed files with 68 additions and 8 deletions
+12 -4
View File
@@ -328,8 +328,10 @@ describe("MCP JSON Schema validity", () => {
await Promise.allSettled([client.close(), server.close()]);
}
});
});
it("advertises the structural request requirements enforced at runtime", async () => {
describe("MCP root input schemas", () => {
it("advertises object roots while enforcing union requirements at runtime", async () => {
const server = new McpServer({ name: "schema-constraints", version: "0" });
const client = new Client({ name: "schema-constraints", version: "0" });
const [clientTransport, serverTransport] =
@@ -353,12 +355,18 @@ describe("MCP JSON Schema validity", () => {
if (graphContract === undefined || graphTool === undefined)
throw new Error("Managed application graph tool was not advertised");
expect(graphContract.inputSchema.safeParse({}).success).toBe(false);
expect(graphTool.inputSchema).toMatchObject({ type: "object" });
expect(ajv.compile(graphTool.inputSchema)({})).toBe(false);
expect(ajv.compile(graphTool.inputSchema)({ unrelated: true })).toBe(
false,
);
for (const contract of TOOL_CONTRACTS) {
const validate = ajv.compile(
advertised.get(contract.name)!.inputSchema,
);
const inputSchema = advertised.get(contract.name)!.inputSchema;
expect(inputSchema.type, contract.name).toBe("object");
expect(inputSchema.properties, contract.name).toBeDefined();
expect(inputSchema.anyOf, contract.name).toBeUndefined();
const validate = ajv.compile(inputSchema);
for (const example of contract.examples)
expect(
validate(example.input),