|
| 1 | +import { |
| 2 | + ZodTypeAny, |
| 3 | + ZodTypeDef, |
| 4 | + ZodType, |
| 5 | + ParseInput, |
| 6 | + ParseReturnType, |
| 7 | + RawCreateParams, |
| 8 | + ZodErrorMap, |
| 9 | + ProcessedCreateParams, |
| 10 | +} from "zod"; |
| 11 | + |
| 12 | +export enum McpZodTypeKind { |
| 13 | + Completable = "McpCompletable", |
| 14 | +} |
| 15 | + |
| 16 | +export type CompleteCallback<T extends ZodTypeAny = ZodTypeAny> = ( |
| 17 | + value: T["_input"], |
| 18 | +) => T["_input"][] | Promise<T["_input"][]>; |
| 19 | + |
| 20 | +export interface CompletableDef<T extends ZodTypeAny = ZodTypeAny> |
| 21 | + extends ZodTypeDef { |
| 22 | + type: T; |
| 23 | + complete: CompleteCallback<T>; |
| 24 | + typeName: McpZodTypeKind.Completable; |
| 25 | +} |
| 26 | + |
| 27 | +export class Completable<T extends ZodTypeAny> extends ZodType< |
| 28 | + T["_output"], |
| 29 | + CompletableDef<T>, |
| 30 | + T["_input"] |
| 31 | +> { |
| 32 | + _parse(input: ParseInput): ParseReturnType<this["_output"]> { |
| 33 | + const { ctx } = this._processInputParams(input); |
| 34 | + const data = ctx.data; |
| 35 | + return this._def.type._parse({ |
| 36 | + data, |
| 37 | + path: ctx.path, |
| 38 | + parent: ctx, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + unwrap() { |
| 43 | + return this._def.type; |
| 44 | + } |
| 45 | + |
| 46 | + static create = <T extends ZodTypeAny>( |
| 47 | + type: T, |
| 48 | + params: RawCreateParams & { |
| 49 | + complete: CompleteCallback<T>; |
| 50 | + }, |
| 51 | + ): Completable<T> => { |
| 52 | + return new Completable({ |
| 53 | + type, |
| 54 | + typeName: McpZodTypeKind.Completable, |
| 55 | + complete: params.complete, |
| 56 | + ...processCreateParams(params), |
| 57 | + }); |
| 58 | + }; |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Wraps a Zod type to provide autocompletion capabilities. Useful for, e.g., prompt arguments in MCP. |
| 63 | + */ |
| 64 | +export function completable<T extends ZodTypeAny>( |
| 65 | + schema: T, |
| 66 | + complete: CompleteCallback<T>, |
| 67 | +): Completable<T> { |
| 68 | + return Completable.create(schema, { ...schema._def, complete }); |
| 69 | +} |
| 70 | + |
| 71 | +// Not sure why this isn't exported from Zod: |
| 72 | +// https://github.com/colinhacks/zod/blob/f7ad26147ba291cb3fb257545972a8e00e767470/src/types.ts#L130 |
| 73 | +function processCreateParams(params: RawCreateParams): ProcessedCreateParams { |
| 74 | + if (!params) return {}; |
| 75 | + const { errorMap, invalid_type_error, required_error, description } = params; |
| 76 | + if (errorMap && (invalid_type_error || required_error)) { |
| 77 | + throw new Error( |
| 78 | + `Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`, |
| 79 | + ); |
| 80 | + } |
| 81 | + if (errorMap) return { errorMap: errorMap, description }; |
| 82 | + const customMap: ZodErrorMap = (iss, ctx) => { |
| 83 | + const { message } = params; |
| 84 | + |
| 85 | + if (iss.code === "invalid_enum_value") { |
| 86 | + return { message: message ?? ctx.defaultError }; |
| 87 | + } |
| 88 | + if (typeof ctx.data === "undefined") { |
| 89 | + return { message: message ?? required_error ?? ctx.defaultError }; |
| 90 | + } |
| 91 | + if (iss.code !== "invalid_type") return { message: ctx.defaultError }; |
| 92 | + return { message: message ?? invalid_type_error ?? ctx.defaultError }; |
| 93 | + }; |
| 94 | + return { errorMap: customMap, description }; |
| 95 | +} |
0 commit comments