zod-to-json-schema.tsโข1.75 kB
import { z } from 'zod';
/**
* Convert a Zod schema to JSON Schema format for MCP SDK
*/
export function zodToJsonSchema(schema: z.ZodType<any>): any {
if (schema instanceof z.ZodObject) {
const shape = schema.shape;
const properties: Record<string, any> = {};
const required: string[] = [];
for (const [key, value] of Object.entries(shape)) {
properties[key] = zodToJsonSchema(value as z.ZodType<any>);
// Check if field is required (not optional)
if (!(value instanceof z.ZodOptional)) {
required.push(key);
}
}
return {
type: 'object',
properties,
...(required.length > 0 ? { required } : {}),
};
}
if (schema instanceof z.ZodArray) {
return {
type: 'array',
items: zodToJsonSchema(schema.element),
};
}
if (schema instanceof z.ZodString) {
return { type: 'string' };
}
if (schema instanceof z.ZodNumber) {
return { type: 'number' };
}
if (schema instanceof z.ZodBoolean) {
return { type: 'boolean' };
}
if (schema instanceof z.ZodEnum) {
return {
type: 'string',
enum: schema.options,
};
}
if (schema instanceof z.ZodOptional) {
return zodToJsonSchema(schema.unwrap());
}
if (schema instanceof z.ZodDefault) {
const innerSchema = zodToJsonSchema(schema._def.innerType);
return {
...innerSchema,
default: schema._def.defaultValue(),
};
}
if (schema instanceof z.ZodRecord) {
return {
type: 'object',
additionalProperties: zodToJsonSchema((schema as any)._def.valueType),
};
}
if (schema instanceof z.ZodAny) {
return {}; // Any type - no schema validation
}
// Fallback for unknown types
return {};
}