fix(mcp-server): zodToJsonSchema emits correct JSON type for number/boolean/array fields (was always string)
This commit is contained in:
parent
22ad88dd13
commit
26d3403d5a
@ -33,14 +33,17 @@ function zodToJsonSchema(schema: ZodTypeAny): Record<string, unknown> {
|
|||||||
for (const [key, field] of Object.entries(shape)) {
|
for (const [key, field] of Object.entries(shape)) {
|
||||||
const fieldDef = (
|
const fieldDef = (
|
||||||
field as unknown as {
|
field as unknown as {
|
||||||
_def: { typeName: string; description?: string; innerType?: unknown };
|
_def: {
|
||||||
|
typeName: string;
|
||||||
|
description?: string;
|
||||||
|
innerType?: { _def: { typeName: string } };
|
||||||
|
};
|
||||||
}
|
}
|
||||||
)._def;
|
)._def;
|
||||||
const isOptional = fieldDef.typeName === 'ZodOptional';
|
const isOptional = fieldDef.typeName === 'ZodOptional';
|
||||||
properties[key] = {
|
const innerTypeName = isOptional ? fieldDef.innerType?._def.typeName : fieldDef.typeName;
|
||||||
type: 'string',
|
const jsonType = zodTypeNameToJsonType(innerTypeName ?? '');
|
||||||
description: fieldDef.description ?? key,
|
properties[key] = { type: jsonType, description: fieldDef.description ?? key };
|
||||||
};
|
|
||||||
if (!isOptional) required.push(key);
|
if (!isOptional) required.push(key);
|
||||||
}
|
}
|
||||||
return { type: 'object', properties, required };
|
return { type: 'object', properties, required };
|
||||||
@ -50,3 +53,10 @@ function zodToJsonSchema(schema: ZodTypeAny): Record<string, unknown> {
|
|||||||
}
|
}
|
||||||
return { type: 'object' };
|
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
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user