UNPKG

autosnippet

Version:

Extract code patterns into a knowledge base for AI coding assistants

32 lines (31 loc) 1.25 kB
/** * zodToMcpSchema.ts — Zod v4 → MCP-compatible JSON Schema 转换器 * * 将 Zod schema 转为 MCP SDK 所需的 inputSchema 格式: * 1. 移除 $schema(MCP 不需要 meta-schema 声明) * 2. 带 default 值的字段从 required 中移除(Agent 可省略,Zod parse 自动填充) * 3. 移除 additionalProperties: false(允许前向兼容的额外字段) * 4. 清理 integer 的冗余 min/max 边界(Zod v4 自动加的 ±MAX_SAFE_INTEGER) * * @module external/mcp/zodToMcpSchema */ import { z } from 'zod'; /** MCP 工具 inputSchema 的类型化结构 */ export interface McpInputSchema { type: 'object'; properties: Record<string, Record<string, unknown>>; required: string[]; [key: string]: unknown; } /** * 将 Zod schema 转换为 MCP 兼容的 JSON Schema * * @param schema Zod 类型定义 * @returns MCP inputSchema 格式的 JSON Schema 对象 * * @example * import { SearchInput } from '#shared/schemas/mcp-tools.js'; * const inputSchema = zodToMcpSchema(SearchInput); * // → { type: 'object', properties: { query: { type: 'string', minLength: 1 }, ... }, required: ['query'] } */ export declare function zodToMcpSchema(schema: z.ZodType): McpInputSchema;