Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Give array fields items in Gemini tool lists that already fit

AdityaUmale8ad9b18eda7e
diff --git a/server/mcp-tool-schema.test.ts b/server/mcp-tool-schema.test.ts
index 94a84dad79..b63a624389 100644
--- a/server/mcp-tool-schema.test.ts
+++ b/server/mcp-tool-schema.test.ts
@@ -205,6 +205,14 @@ describe("a tool list under a provider's schema profile", () => {
expect(fitToolListResult(result, undefined)).toBe(result);
});
+ it("adds items to a connected tool's array field in Gemini's tools/list", () => {
+ const tool = { name: "create_event", inputSchema: { type: "object", properties: { attendees: { type: "array" }, title: { type: "string" } } } };
+ const [fitted] = fitToolList([tool], toolSchemaProfile("gemini")!) as Array<{ inputSchema: Record<string, any> }>;
+ expect(fitted!.inputSchema.properties.attendees).toEqual({ type: "array", items: { type: "string" } });
+ expect(fitted!.inputSchema.properties.title).toEqual({ type: "string" });
+ expect(tool.inputSchema.properties.attendees).not.toHaveProperty("items");
+ });
+
it("keeps a tool within the limit as the same object, shortens one over it with a note, and leaves out one that cannot fit", () => {
const lines: string[] = [];
const tools = [small, { name: "create_page", description: "Create a page.", inputSchema: createPageSchema() },
diff --git a/server/tool-schema-profile.test.ts b/server/tool-schema-profile.test.ts
index 878209cbae..865cdbf25f 100644
--- a/server/tool-schema-profile.test.ts
+++ b/server/tool-schema-profile.test.ts
@@ -207,6 +207,32 @@ describe("fitting one tool schema to Gemini's per-tool limit", () => {
const GEMINI = toolSchemaProfile("gemini")!;
const keys = () => ({ type: "array", items: { type: "integer" }, allOf: Array.from({ length: 200 }, () => ({ maxItems: 16 })) });
+ it("gives every array without items a string item type in a schema that already fits, and keeps declared items", () => {
+ const schema = { type: "object", $defs: { tags: { type: "array" } }, properties: {
+ attendees: { type: "array", description: "Email addresses" }, include: { type: ["array", "null"] },
+ ids: { type: "array", items: { type: "integer" } }, tags: { $ref: "#/$defs/tags" },
+ group: { type: "object", properties: { domains: { type: "array" } } },
+ } };
+ const original = structuredClone(schema);
+ const fit = fitToolSchema(schema, GEMINI);
+ expect(fit.outcome).toBe("kept");
+ if (fit.outcome !== "kept") return;
+ expect(fit.schema).toEqual({ type: "object", $defs: { tags: { type: "array", items: { type: "string" } } }, properties: {
+ attendees: { type: "array", description: "Email addresses", items: { type: "string" } },
+ include: { type: ["array", "null"], items: { type: "string" } },
+ ids: { type: "array", items: { type: "integer" } }, tags: { $ref: "#/$defs/tags" },
+ group: { type: "object", properties: { domains: { type: "array", items: { type: "string" } } } },
+ } });
+ expect(schema).toEqual(original);
+ });
+
+ it("returns a schema whose arrays all declare items as the same object", () => {
+ const schema = { type: "object", properties: { ids: { type: "array", items: { type: "integer" } }, name: { type: "string" } } };
+ const fit = fitToolSchema(schema, GEMINI);
+ expect(fit.outcome).toBe("kept");
+ if (fit.outcome === "kept") expect(fit.schema).toBe(schema);
+ });
+
it("keeps an integer array's items when a nested field is collapsed", () => {
const schema = { type: "object", properties: { group: { type: "object", properties: { keys: keys() } } } };
expect(schemaBytes(schema)).toBeGreaterThan(GEMINI.maxSchemaBytes);
diff --git a/server/tool-schema-profile.ts b/server/tool-schema-profile.ts
index e142148066..cc124a202d 100644
--- a/server/tool-schema-profile.ts
+++ b/server/tool-schema-profile.ts
@@ -243,7 +243,7 @@ const ITEM_TYPE_VISITS = 4_096;
type TypeVisits = { left: number; spent: boolean };
-function includesArray(type: string | readonly unknown[]): boolean {
+function includesArray(type: unknown): boolean {
return type === "array" || (Array.isArray(type) && type.includes("array"));
}
@@ -273,6 +273,18 @@ function schemaItems(node: Json, root: Json, seen: Set<string>, hops: number): u
return schemaItems(target, root, seen, hops + 1);
}
+/** Gemini rejects any array schema without `items`, which many connected tools leave out. */
+function withArrayItems(schema: Json): Json {
+ const copy = structuredClone(schema);
+ let changed = false;
+ walk(copy, 0, (node) => {
+ if ("items" in node || !includesArray(node.type)) return;
+ node.items = itemsOf(node, copy);
+ changed = true;
+ });
+ return changed ? copy : schema;
+}
+
/** A subschema's type and nothing inside it, valid where Moonshot wants every subschema typed; a free-form object names
* additionalProperties because a bare `{"type":"object"}` reads there as recursion without end. An array names `items`
* because Gemini rejects an array without one. */
@@ -329,7 +341,7 @@ export type SchemaFit =
/** Least loss first, stopping at the first form that fits. */
export function fitToolSchema(schema: Json, profile: ToolSchemaProfile): SchemaFit {
- const typed = profile.id === "moonshot" ? withTypedMixedEnums(schema) : schema;
+ const typed = profile.id === "moonshot" ? withTypedMixedEnums(schema) : profile.id === "gemini" ? withArrayItems(schema) : schema;
const measured = profile.measureInlined ? inlineRefs(typed) : typed;
const from = schemaBytes(measured ?? typed);
if (measured && fits(measured, profile)) return { outcome: "kept", schema: typed, bytes: from };