fix(gemini): inject missing items schema for array typed mcp tools (#10578) (#10605)

This commit is contained in:
Sahil Singh
2026-08-18 19:22:56 +05:30
committed by GitHub
parent a7b96b44e9
commit 70f94685e6
2 changed files with 75 additions and 0 deletions

View File

@@ -727,5 +727,33 @@ export function cleanJSONSchemaForAntigravity(schema: unknown): unknown {
injectObjectType(cleaned);
// Phase 8: Ensure array types have an items schema (#10578).
// Gemini strictly requires array parameters to define their `items` schema.
// If an MCP tool defines an array but forgets the items, inject a safe default.
function ensureArrayItems(obj: unknown): void {
if (!obj || typeof obj !== "object") return;
if (Array.isArray(obj)) {
for (const item of obj) {
ensureArrayItems(item);
}
return;
}
const record = obj as JsonRecord;
if (record.type === "array" && !record.items) {
record.items = { type: "string" };
}
// Recurse into remaining values.
for (const value of Object.values(record)) {
if (value && typeof value === "object") {
ensureArrayItems(value);
}
}
}
ensureArrayItems(cleaned);
return cleaned;
}

View File

@@ -0,0 +1,47 @@
import { describe, it } from "node:test";
import assert from "node:assert/strict";
import { cleanJSONSchemaForAntigravity } from "../../open-sse/translator/helpers/geminiHelper";
type SchemaNode = {
type?: string;
properties?: Record<string, SchemaNode>;
items?: SchemaNode;
required?: string[];
[key: string]: unknown;
};
describe("Gemini array items sanitizer (#10578)", () => {
it("should inject a string items schema for array types that are missing it", () => {
const sloppySchema = {
type: "object",
properties: {
emails: {
type: "array",
},
},
required: ["emails"],
};
const cleanedSchema = cleanJSONSchemaForAntigravity(sloppySchema as unknown) as SchemaNode;
assert.equal(cleanedSchema.properties?.emails?.type, "array");
assert.ok(cleanedSchema.properties?.emails?.items, "items should be injected");
assert.equal(cleanedSchema.properties?.emails?.items?.type, "string");
});
it("should safely ignore arrays that already have valid items", () => {
const goodSchema = {
type: "object",
properties: {
tags: {
type: "array",
items: { type: "number" },
},
},
};
const cleanedSchema = cleanJSONSchemaForAntigravity(goodSchema as unknown) as SchemaNode;
assert.equal(cleanedSchema.properties?.tags?.items?.type, "number");
});
});