fix: support FastMCP union types in tool parameter forms

This commit is contained in:
Mats Julius Funke 2025-08-02 15:59:45 +02:00
commit f73d8e6f8a
No known key found for this signature in database

View file

@ -145,6 +145,33 @@ export function isPropertyRequired(
return schema.required?.includes(propertyName) ?? false;
}
/**
* Normalizes union types (like string|null from FastMCP) to simple types for form rendering
* @param schema The JSON schema to normalize
* @returns A normalized schema or the original schema
*/
export function normalizeUnionType(schema: JsonSchemaType): JsonSchemaType {
// Handle anyOf with string and null (FastMCP pattern)
if (
schema.anyOf &&
schema.anyOf.some((t) => (t as JsonSchemaType).type === "string") &&
schema.anyOf.some((t) => (t as JsonSchemaType).type === "null")
) {
return { ...schema, type: "string", anyOf: undefined };
}
// Handle array type with string and null
if (
Array.isArray(schema.type) &&
schema.type.includes("string") &&
schema.type.includes("null")
) {
return { ...schema, type: "string" };
}
return schema;
}
/**
* Formats a field key into a human-readable label
* @param key The field key to format