Skip to content

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

fix: type mixed tool enums for Kimi

jonathanwxh-cell59fa34818ddb
diff --git a/server/mcp-tool-schema.test.ts b/server/mcp-tool-schema.test.ts
index aeba30b2b8..c97b958bb9 100644
--- a/server/mcp-tool-schema.test.ts
+++ b/server/mcp-tool-schema.test.ts
@@ -171,6 +171,24 @@ describe("createToolListNormalizer", () => {
describe("a tool list under a provider's schema profile", () => {
const small = { name: "search", description: "Find pages.", inputSchema: { type: "object", properties: { query: { type: "string" } } } };
+ it("keeps nullable and mixed enum choices usable in Kimi's tools/list", () => {
+ const genre = { enum: ["puzzle", "rpg", null], description: "Choose a genre" };
+ const values = { enum: [1, 2.5, false, null, { name: "one" }, []] };
+ const inputSchema = { type: "object", $defs: { genre }, properties: {
+ genre: { $ref: "#/$defs/genre" }, options: { type: "array", items: values },
+ typed: { type: "string", enum: ["one", null] }, numeric: { enum: [1, 2.5] },
+ } };
+ const result = { tools: [{ name: "game", inputSchema }] };
+ expect(fitToolListResult(result, MOONSHOT)).toMatchObject({ tools: [{ inputSchema: { $defs: { genre: { ...genre, type: ["string", "null"] } }, properties: {
+ genre: { $ref: "#/$defs/genre" },
+ options: { items: { ...values, type: ["number", "boolean", "null", "object", "array"] } },
+ typed: inputSchema.properties.typed, numeric: inputSchema.properties.numeric,
+ } } }] });
+ expect(genre).not.toHaveProperty("type");
+ expect(values).not.toHaveProperty("type");
+ expect(fitToolListResult(result, undefined)).toBe(result);
+ });
+
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.ts b/server/tool-schema-profile.ts
index c28ccd488c..771c700943 100644
--- a/server/tool-schema-profile.ts
+++ b/server/tool-schema-profile.ts
@@ -58,8 +58,8 @@ export function schemaBytes(value: unknown): number {
return Buffer.byteLength(text) + 5 * (text.match(/[<>&]/g)?.length ?? 0) + 3 * (text.match(/[\u2028\u2029]/g)?.length ?? 0);
}
-const MAP_SLOTS = ["properties", "patternProperties", "dependentSchemas", "$defs", "definitions"] as const;
-const NESTED_SLOTS = ["additionalProperties", "items", "additionalItems", "contains", "propertyNames", "unevaluatedProperties", "unevaluatedItems"] as const;
+const MAP_SLOTS = ["properties", "patternProperties", "dependencies", "dependentSchemas", "$defs", "definitions"] as const;
+const NESTED_SLOTS = ["additionalProperties", "items", "additionalItems", "contains", "propertyNames", "contentSchema", "unevaluatedProperties", "unevaluatedItems"] as const;
const NESTED_LISTS = ["prefixItems", "items"] as const;
/** Alternatives describe the same value, so they sit at its depth. */
const ALTERNATIVE_LISTS = ["anyOf", "oneOf", "allOf"] as const;
@@ -170,6 +170,21 @@ function valueType(value: unknown): string {
return typeof value === "object" ? "object" : typeof value;
}
+/** Kimi Code 2.7.2 tries to infer a single type from each untyped enum, even for a nullable field. */
+function withTypedMixedEnums(schema: Json): Json {
+ const copy = structuredClone(schema);
+ let changed = false;
+ walk(copy, 0, (node) => {
+ if ("type" in node || !Array.isArray(node.enum)) return;
+ const types = new Set(node.enum.map(valueType));
+ if (types.has("number")) types.delete("integer");
+ if (types.size < 2) return;
+ node.type = [...types];
+ changed = true;
+ });
+ return changed ? copy : schema;
+}
+
function typesOf(node: Json, root: Json, hops = 0): string[] {
if (hops > 16) return [];
if (typeof node.type === "string") return [node.type];
@@ -228,7 +243,7 @@ function topLevelOnly(schema: Json, descriptionChars: number): Json {
return { type: "object", properties, ...(required.length ? { required } : {}), additionalProperties: true };
}
-/** kept: as the server sent it. trimmed: every field and constraint kept, fewer words. loosened: fields deeper in are
+/** kept: every field, constraint and annotation kept. trimmed: every field and constraint kept, fewer words. loosened: fields deeper in are
* left to the tool's description. minimal: only the top-level fields. dropped: not even those fit. */
export type SchemaFit =
| { outcome: "kept"; schema: Json; bytes: number }
@@ -237,12 +252,13 @@ export type SchemaFit =
/** Least loss first, stopping at the first form that fits. */
export function fitToolSchema(schema: Json, profile: ToolSchemaProfile): SchemaFit {
- const measured = profile.measureInlined ? inlineRefs(schema) : schema;
- const from = schemaBytes(measured ?? schema);
- if (measured && fits(measured, profile)) return { outcome: "kept", schema, bytes: from };
+ const typed = profile.id === "moonshot" ? withTypedMixedEnums(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 };
const fitted = (outcome: "trimmed" | "loosened" | "minimal", form: Json): SchemaFit => ({ outcome, schema: form, bytes: schemaBytes(form), from });
- const working = structuredClone(measured ?? schema);
+ const working = structuredClone(measured ?? typed);
if (measured) {
stripAnnotations(working);
if (fits(working, profile)) return fitted("trimmed", working);