pharos-agent-kit
Version:
Connect AI Agents to Pharos protocols
33 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.zodToMCPShape = zodToMCPShape;
const zod_1 = require("zod");
// // Type guards for Zod schema types
// function isZodOptional(schema: z.ZodTypeAny): schema is z.ZodOptional<any> {
// return schema instanceof z.ZodOptional;
// }
function isZodObject(schema) {
// Check both instanceof and the typeName property
return (schema instanceof zod_1.z.ZodObject || schema?._def?.typeName === "ZodObject");
}
/**
* Converts a Zod object schema to a flat shape for MCP tools
* @param schema The Zod schema to convert
* @returns A flattened schema shape compatible with MCP tools
* @throws Error if the schema is not an object type
*/
function zodToMCPShape(schema) {
if (!isZodObject(schema)) {
throw new Error("MCP tools require an object schema at the top level");
}
const shape = schema.shape;
const result = {};
for (const [key, value] of Object.entries(shape)) {
result[key] = value;
}
return {
result,
keys: Object.keys(result),
};
}
//# sourceMappingURL=zodToMCPSchema.js.map