@@ -243,13 +243,7 @@ def call_tool(request)
243
243
end
244
244
245
245
begin
246
- call_params = tool_call_parameters ( tool )
247
-
248
- if call_params . include? ( :server_context )
249
- tool . call ( **arguments . transform_keys ( &:to_sym ) , server_context :) . to_h
250
- else
251
- tool . call ( **arguments . transform_keys ( &:to_sym ) ) . to_h
252
- end
246
+ call_tool_with_args ( tool , arguments )
253
247
rescue => e
254
248
raise RequestHandlerError . new ( "Internal error calling tool #{ tool_name } " , request , original_error : e )
255
249
end
@@ -272,7 +266,7 @@ def get_prompt(request)
272
266
prompt_args = request [ :arguments ]
273
267
prompt . validate_arguments! ( prompt_args )
274
268
275
- prompt . template ( prompt_args , server_context : ) . to_h
269
+ call_prompt_template_with_args ( prompt , prompt_args )
276
270
end
277
271
278
272
def list_resources ( request )
@@ -299,22 +293,29 @@ def index_resources_by_uri(resources)
299
293
end
300
294
end
301
295
302
- def tool_call_parameters ( tool )
303
- method_def = tool_call_method_def ( tool )
304
- method_def . parameters . flatten
296
+ def accepts_server_context? ( method_object )
297
+ parameters = method_object . parameters
298
+ accepts_server_context = parameters . any? { |_type , name | name == :server_context }
299
+ has_kwargs = parameters . any? { |type , _ | type == :keyrest }
300
+
301
+ accepts_server_context || has_kwargs
305
302
end
306
303
307
- def tool_call_method_def ( tool )
308
- method = tool . method ( :call )
304
+ def call_tool_with_args ( tool , arguments )
305
+ args = arguments . transform_keys ( & :to_sym )
309
306
310
- if defined? ( T ::Utils ) && T ::Utils . respond_to? ( :signature_for_method )
311
- sorbet_typed_method_definition = T ::Utils . signature_for_method ( method ) &.method
307
+ if accepts_server_context? ( tool . method ( :call ) )
308
+ tool . call ( **args , server_context : server_context ) . to_h
309
+ else
310
+ tool . call ( **args ) . to_h
311
+ end
312
+ end
312
313
313
- # Return the Sorbet typed method definition if it exists, otherwise fallback to original method
314
- # definition if Sorbet is defined but not used by this tool.
315
- sorbet_typed_method_definition || method
314
+ def call_prompt_template_with_args ( prompt , args )
315
+ if accepts_server_context? ( prompt . method ( :template ) )
316
+ prompt . template ( args , server_context : server_context ) . to_h
316
317
else
317
- method
318
+ prompt . template ( args ) . to_h
318
319
end
319
320
end
320
321
end
0 commit comments