|
1 | | -import { ZodTypeAny } from "zod/v4"; |
| 1 | +import { ZodTypeAny } from 'zod'; |
2 | 2 |
|
3 | 3 | export enum McpZodTypeKind { |
4 | 4 | Completable = 'McpCompletable' |
5 | 5 | } |
6 | 6 |
|
7 | 7 | export type CompleteCallback<T extends ZodTypeAny = ZodTypeAny> = ( |
8 | | - value: T["_input"], |
9 | | - context?: { |
10 | | - arguments?: Record<string, string>; |
11 | | - } |
12 | | -) => T["_input"][] | Promise<T["_input"][]>; |
| 8 | + value: T['_input'], |
| 9 | + context?: { |
| 10 | + arguments?: Record<string, string>; |
| 11 | + } |
| 12 | +) => T['_input'][] | Promise<T['_input'][]>; |
13 | 13 |
|
14 | 14 | export interface CompletableDef<T extends ZodTypeAny = ZodTypeAny> { |
15 | | - type: T; |
16 | | - complete: CompleteCallback<T>; |
17 | | - typeName: McpZodTypeKind.Completable; |
| 15 | + type: T; |
| 16 | + complete: CompleteCallback<T>; |
| 17 | + typeName: McpZodTypeKind.Completable; |
18 | 18 | } |
19 | 19 |
|
20 | 20 | /** |
21 | 21 | * Wraps a Zod type to provide autocompletion capabilities. Useful for, e.g., prompt arguments in MCP. |
22 | 22 | */ |
23 | 23 | export function completable<T extends ZodTypeAny>( |
24 | | - schema: T, |
25 | | - complete: CompleteCallback<T> |
| 24 | + schema: T, |
| 25 | + complete: CompleteCallback<T> |
26 | 26 | ): T & { |
27 | | - _def: (T extends { _def: infer D } ? D : unknown) & CompletableDef<T>; |
28 | | -} { |
29 | | - const target = schema as unknown as { _def?: Record<string, unknown> }; |
30 | | - const originalDef = (target._def ?? {}) as Record<string, unknown>; |
31 | | - // Only mutate the existing _def object to respect read-only property semantics |
32 | | - if ( |
33 | | - (originalDef as { typeName?: unknown }).typeName !== |
34 | | - McpZodTypeKind.Completable |
35 | | - ) { |
36 | | - (originalDef as { typeName?: McpZodTypeKind; type?: ZodTypeAny }).typeName = |
37 | | - McpZodTypeKind.Completable; |
38 | | - (originalDef as { typeName?: McpZodTypeKind; type?: ZodTypeAny }).type = |
39 | | - schema; |
40 | | - } |
41 | | - (originalDef as { complete?: CompleteCallback<T> }).complete = complete; |
42 | | - return schema as unknown as T & { |
43 | 27 | _def: (T extends { _def: infer D } ? D : unknown) & CompletableDef<T>; |
44 | | - }; |
| 28 | +} { |
| 29 | + const target = schema as unknown as { _def?: Record<string, unknown> }; |
| 30 | + const originalDef = (target._def ?? {}) as Record<string, unknown>; |
| 31 | + // Only mutate the existing _def object to respect read-only property semantics |
| 32 | + if ((originalDef as { typeName?: unknown }).typeName !== McpZodTypeKind.Completable) { |
| 33 | + (originalDef as { typeName?: McpZodTypeKind; type?: ZodTypeAny }).typeName = McpZodTypeKind.Completable; |
| 34 | + (originalDef as { typeName?: McpZodTypeKind; type?: ZodTypeAny }).type = schema; |
| 35 | + } |
| 36 | + (originalDef as { complete?: CompleteCallback<T> }).complete = complete; |
| 37 | + return schema as unknown as T & { |
| 38 | + _def: (T extends { _def: infer D } ? D : unknown) & CompletableDef<T>; |
| 39 | + }; |
45 | 40 | } |
0 commit comments