@@ -38,6 +38,7 @@ export interface ToolProtocol extends SDKTool {
3838 inputSchema : {
3939 type : "object" ;
4040 properties ?: Record < string , unknown > ;
41+ required ?: string [ ] ;
4142 } ;
4243 } ;
4344 toolCall ( request : {
@@ -53,19 +54,34 @@ export abstract class MCPTool<TInput extends Record<string, any> = {}>
5354 protected abstract schema : ToolInputSchema < TInput > ;
5455 [ key : string ] : unknown ;
5556
56- get inputSchema ( ) : { type : "object" ; properties ?: Record < string , unknown > } {
57- return {
58- type : "object" as const ,
59- properties : Object . fromEntries (
60- Object . entries ( this . schema ) . map ( ( [ key , schema ] ) => [
61- key ,
62- {
63- type : this . getJsonSchemaType ( schema . type ) ,
64- description : schema . description ,
65- } ,
66- ] )
67- ) ,
57+ get inputSchema ( ) : { type : "object" ; properties ?: Record < string , unknown > ; required ?: string [ ] } {
58+ const properties : Record < string , unknown > = { } ;
59+ const required : string [ ] = [ ] ;
60+
61+ Object . entries ( this . schema ) . forEach ( ( [ key , schema ] ) => {
62+ // Determine the correct JSON schema type (unwrapping optional if necessary)
63+ const jsonType = this . getJsonSchemaType ( schema . type ) ;
64+ properties [ key ] = {
65+ type : jsonType ,
66+ description : schema . description ,
67+ } ;
68+
69+ // If the field is not an optional, add it to the required array.
70+ if ( ! ( schema . type instanceof z . ZodOptional ) ) {
71+ required . push ( key ) ;
72+ }
73+ } ) ;
74+
75+ const inputSchema : { type : "object" ; properties : Record < string , unknown > ; required ?: string [ ] } = {
76+ type : "object" ,
77+ properties,
6878 } ;
79+
80+ if ( required . length > 0 ) {
81+ inputSchema . required = required ;
82+ }
83+
84+ return inputSchema ;
6985 }
7086
7187 get toolDefinition ( ) {
@@ -102,11 +118,17 @@ export abstract class MCPTool<TInput extends Record<string, any> = {}>
102118 }
103119
104120 private getJsonSchemaType ( zodType : z . ZodType < any > ) : string {
105- if ( zodType instanceof z . ZodString ) return "string" ;
106- if ( zodType instanceof z . ZodNumber ) return "number" ;
107- if ( zodType instanceof z . ZodBoolean ) return "boolean" ;
108- if ( zodType instanceof z . ZodArray ) return "array" ;
109- if ( zodType instanceof z . ZodObject ) return "object" ;
121+ // Unwrap optional types to correctly determine the JSON schema type.
122+ let currentType = zodType ;
123+ if ( currentType instanceof z . ZodOptional ) {
124+ currentType = currentType . unwrap ( ) ;
125+ }
126+
127+ if ( currentType instanceof z . ZodString ) return "string" ;
128+ if ( currentType instanceof z . ZodNumber ) return "number" ;
129+ if ( currentType instanceof z . ZodBoolean ) return "boolean" ;
130+ if ( currentType instanceof z . ZodArray ) return "array" ;
131+ if ( currentType instanceof z . ZodObject ) return "object" ;
110132 return "string" ;
111133 }
112134
0 commit comments