@fiveohhwon/workflows-mcp
Version:
A Model Context Protocol (MCP) implementation for workflow management and automation.
61 lines • 1.73 kB
JavaScript
import { z } from 'zod';
export function zodToJsonSchema(schema) {
if (schema instanceof z.ZodObject) {
const shape = schema.shape;
const properties = {};
const required = [];
for (const [key, value] of Object.entries(shape)) {
properties[key] = zodToJsonSchema(value);
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._def.valueType),
};
}
if (schema instanceof z.ZodAny) {
return {};
}
return {};
}
//# sourceMappingURL=zod-to-json-schema.js.map