fix(mcp-server): zodToJsonSchema emits correct JSON type for number/boolean/array fields (was always string)

This commit is contained in:
saravanakumardb1 2026-03-05 12:09:38 -08:00
parent 22ad88dd13
commit 26d3403d5a

View File

@ -33,14 +33,17 @@ function zodToJsonSchema(schema: ZodTypeAny): Record<string, unknown> {
for (const [key, field] of Object.entries(shape)) {
const fieldDef = (
field as unknown as {
_def: { typeName: string; description?: string; innerType?: unknown };
_def: {
typeName: string;
description?: string;
innerType?: { _def: { typeName: string } };
};
}
)._def;
const isOptional = fieldDef.typeName === 'ZodOptional';
properties[key] = {
type: 'string',
description: fieldDef.description ?? key,
};
const innerTypeName = isOptional ? fieldDef.innerType?._def.typeName : fieldDef.typeName;
const jsonType = zodTypeNameToJsonType(innerTypeName ?? '');
properties[key] = { type: jsonType, description: fieldDef.description ?? key };
if (!isOptional) required.push(key);
}
return { type: 'object', properties, required };
@ -50,3 +53,10 @@ function zodToJsonSchema(schema: ZodTypeAny): Record<string, unknown> {
}
return { type: 'object' };
}
function zodTypeNameToJsonType(typeName: string): string {
if (typeName === 'ZodNumber' || typeName === 'ZodCoerce') return 'number';
if (typeName === 'ZodBoolean') return 'boolean';
if (typeName === 'ZodArray') return 'array';
return 'string'; // ZodString, ZodEnum, ZodLiteral, unknown
}