@@ -38,6 +38,7 @@ export interface ToolProtocol extends SDKTool {
38
38
inputSchema : {
39
39
type : "object" ;
40
40
properties ?: Record < string , unknown > ;
41
+ required ?: string [ ] ;
41
42
} ;
42
43
} ;
43
44
toolCall ( request : {
@@ -54,19 +55,34 @@ export abstract class MCPTool<TInput extends Record<string, any> = {}>
54
55
protected useStringify : boolean = true ;
55
56
[ key : string ] : unknown ;
56
57
57
- get inputSchema ( ) : { type : "object" ; properties ?: Record < string , unknown > } {
58
- return {
59
- type : "object" as const ,
60
- properties : Object . fromEntries (
61
- Object . entries ( this . schema ) . map ( ( [ key , schema ] ) => [
62
- key ,
63
- {
64
- type : this . getJsonSchemaType ( schema . type ) ,
65
- description : schema . description ,
66
- } ,
67
- ] )
68
- ) ,
58
+ get inputSchema ( ) : { type : "object" ; properties ?: Record < string , unknown > ; required ?: string [ ] } {
59
+ const properties : Record < string , unknown > = { } ;
60
+ const required : string [ ] = [ ] ;
61
+
62
+ Object . entries ( this . schema ) . forEach ( ( [ key , schema ] ) => {
63
+ // Determine the correct JSON schema type (unwrapping optional if necessary)
64
+ const jsonType = this . getJsonSchemaType ( schema . type ) ;
65
+ properties [ key ] = {
66
+ type : jsonType ,
67
+ description : schema . description ,
68
+ } ;
69
+
70
+ // If the field is not an optional, add it to the required array.
71
+ if ( ! ( schema . type instanceof z . ZodOptional ) ) {
72
+ required . push ( key ) ;
73
+ }
74
+ } ) ;
75
+
76
+ const inputSchema : { type : "object" ; properties : Record < string , unknown > ; required ?: string [ ] } = {
77
+ type : "object" ,
78
+ properties,
69
79
} ;
80
+
81
+ if ( required . length > 0 ) {
82
+ inputSchema . required = required ;
83
+ }
84
+
85
+ return inputSchema ;
70
86
}
71
87
72
88
get toolDefinition ( ) {
@@ -103,11 +119,17 @@ export abstract class MCPTool<TInput extends Record<string, any> = {}>
103
119
}
104
120
105
121
private getJsonSchemaType ( zodType : z . ZodType < any > ) : string {
106
- if ( zodType instanceof z . ZodString ) return "string" ;
107
- if ( zodType instanceof z . ZodNumber ) return "number" ;
108
- if ( zodType instanceof z . ZodBoolean ) return "boolean" ;
109
- if ( zodType instanceof z . ZodArray ) return "array" ;
110
- if ( zodType instanceof z . ZodObject ) return "object" ;
122
+ // Unwrap optional types to correctly determine the JSON schema type.
123
+ let currentType = zodType ;
124
+ if ( currentType instanceof z . ZodOptional ) {
125
+ currentType = currentType . unwrap ( ) ;
126
+ }
127
+
128
+ if ( currentType instanceof z . ZodString ) return "string" ;
129
+ if ( currentType instanceof z . ZodNumber ) return "number" ;
130
+ if ( currentType instanceof z . ZodBoolean ) return "boolean" ;
131
+ if ( currentType instanceof z . ZodArray ) return "array" ;
132
+ if ( currentType instanceof z . ZodObject ) return "object" ;
111
133
return "string" ;
112
134
}
113
135
0 commit comments