MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / jsonSchemaToZod

Function jsonSchemaToZod

packages/mcp-server/src/schema.ts:11–46  ·  view source on GitHub ↗
(
  properties: Record<string, { type: string; description?: string; default?: unknown; enum?: unknown[] }>,
  required?: string[]
)

Source from the content-addressed store, hash-verified

9 * Supports string / number / boolean / enum types.
10 */
11export function jsonSchemaToZod(
12 properties: Record<string, { type: string; description?: string; default?: unknown; enum?: unknown[] }>,
13 required?: string[]
14): Record<string, z.ZodTypeAny> {
15 const shape: Record<string, z.ZodTypeAny> = {}
16 const requiredSet = new Set(required ?? [])
17
18 for (const [key, prop] of Object.entries(properties)) {
19 let zodType: z.ZodTypeAny
20
21 switch (prop.type) {
22 case 'number':
23 zodType = z.number().describe(prop.description ?? '')
24 break
25 case 'boolean':
26 zodType = z.boolean().describe(prop.description ?? '')
27 break
28 case 'string':
29 default:
30 if (prop.enum) {
31 zodType = z.enum(prop.enum as [string, ...string[]]).describe(prop.description ?? '')
32 } else {
33 zodType = z.string().describe(prop.description ?? '')
34 }
35 break
36 }
37
38 if (!requiredSet.has(key)) {
39 zodType = zodType.optional()
40 }
41
42 shape[key] = zodType
43 }
44
45 return shape
46}

Callers 1

registerToolsFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected