@@ -201,28 +201,24 @@ def call_tool(request)
201
201
arguments = request [ :arguments ]
202
202
add_instrumentation_data ( tool_name :)
203
203
204
- if tool . input_schema &.missing_required_arguments? ( arguments )
205
- add_instrumentation_data ( error : :missing_required_arguments )
206
- raise RequestHandlerError . new (
207
- "Missing required arguments: #{ tool . input_schema . missing_required_arguments ( arguments ) . join ( ", " ) } " ,
208
- request ,
209
- error_type : :missing_required_arguments ,
210
- )
211
- end
204
+ validate_tool_arguments! ( tool , arguments , request )
212
205
213
206
begin
214
- call_params = tool_call_parameters ( tool )
215
-
216
- if call_params . include? ( :server_context )
217
- tool . call ( **arguments . transform_keys ( &:to_sym ) , server_context :) . to_h
218
- else
219
- tool . call ( **arguments . transform_keys ( &:to_sym ) ) . to_h
220
- end
207
+ tool . call ( **arguments . transform_keys ( &:to_sym ) , server_context :) . to_h
221
208
rescue => e
222
209
raise RequestHandlerError . new ( "Internal error calling tool #{ tool_name } " , request , original_error : e )
223
210
end
224
211
end
225
212
213
+ def validate_tool_arguments! ( tool , arguments , request )
214
+ input_schema = tool . input_schema
215
+ return unless input_schema
216
+
217
+ missing_arguments = input_schema . required - arguments . keys . map ( &:to_sym )
218
+
219
+ missing_required_arguments! ( missing_arguments , request ) unless missing_arguments . empty?
220
+ end
221
+
226
222
def list_prompts ( request )
227
223
add_instrumentation_data ( method : Methods ::PROMPTS_LIST )
228
224
@prompts . map { |_ , prompt | prompt . to_h }
@@ -240,9 +236,31 @@ def get_prompt(request)
240
236
add_instrumentation_data ( prompt_name :)
241
237
242
238
prompt_args = request [ :arguments ]
243
- prompt . validate_arguments! ( prompt_args )
239
+ validate_prompt_arguments! ( prompt , prompt_args , request )
240
+
241
+ prompt . call ( prompt_args , server_context :) . to_h
242
+ end
243
+
244
+ def validate_prompt_arguments! ( prompt , provided_arguments , request )
245
+ missing_arguments = prompt . arguments . filter_map do |configured_argument |
246
+ next unless configured_argument . required?
247
+
248
+ key = configured_argument . name . to_sym
249
+ next if provided_arguments . key? ( key )
244
250
245
- prompt . template ( prompt_args , server_context :) . to_h
251
+ key
252
+ end
253
+
254
+ missing_required_arguments! ( missing_arguments , request ) unless missing_arguments . empty?
255
+ end
256
+
257
+ def missing_required_arguments! ( missing_arguments , request )
258
+ add_instrumentation_data ( error : :missing_required_arguments )
259
+ raise RequestHandlerError . new (
260
+ "Missing required arguments: #{ missing_arguments . join ( ", " ) } " ,
261
+ request ,
262
+ error_type : :missing_required_arguments ,
263
+ )
246
264
end
247
265
248
266
def list_resources ( request )
@@ -273,24 +291,5 @@ def index_resources_by_uri(resources)
273
291
hash [ resource . uri ] = resource
274
292
end
275
293
end
276
-
277
- def tool_call_parameters ( tool )
278
- method_def = tool_call_method_def ( tool )
279
- method_def . parameters . flatten
280
- end
281
-
282
- def tool_call_method_def ( tool )
283
- method = tool . method ( :call )
284
-
285
- if defined? ( T ::Utils ) && T ::Utils . respond_to? ( :signature_for_method )
286
- sorbet_typed_method_definition = T ::Utils . signature_for_method ( method ) &.method
287
-
288
- # Return the Sorbet typed method definition if it exists, otherwise fallback to original method
289
- # definition if Sorbet is defined but not used by this tool.
290
- sorbet_typed_method_definition || method
291
- else
292
- method
293
- end
294
- end
295
294
end
296
295
end
0 commit comments