@@ -11,13 +11,11 @@ class Server
1111
1212    class  RequestHandlerError  < StandardError 
1313      attr_reader  :error_type 
14-       attr_reader  :original_error 
1514
16-       def  initialize ( message ,  request ,  error_type : :internal_error ,   original_error :  nil ) 
15+       def  initialize ( message ,  request ,  error_type : :internal_error ) 
1716        super ( message ) 
1817        @request  =  request 
1918        @error_type  =  error_type 
20-         @original_error  =  original_error 
2119      end 
2220    end 
2321
@@ -39,8 +37,8 @@ def initialize(
3937    ) 
4038      @name  =  name 
4139      @version  =  version 
42-       @tools  =  tools . to_h  {  |t | [ t . name_value ,  t ]  } 
43-       @prompts  =  prompts . to_h  {  |p | [ p . name_value ,  p ]  } 
40+       @tools  =  tools . to_h  {  |t | [ t . name ,  t ]  } 
41+       @prompts  =  prompts . to_h  {  |p | [ p . name ,  p ]  } 
4442      @resources  =  resources 
4543      @resource_templates  =  resource_templates 
4644      @resource_index  =  index_resources_by_uri ( resources ) 
@@ -88,12 +86,12 @@ def handle_json(request)
8886
8987    def  define_tool ( name : nil ,  description : nil ,  input_schema : nil ,  annotations : nil ,  &block ) 
9088      tool  =  Tool . define ( name :,  description :,  input_schema :,  annotations :,  &block ) 
91-       @tools [ tool . name_value ]  =  tool 
89+       @tools [ tool . name ]  =  tool 
9290    end 
9391
9492    def  define_prompt ( name : nil ,  description : nil ,  arguments : [ ] ,  &block ) 
9593      prompt  =  Prompt . define ( name :,  description :,  arguments :,  &block ) 
96-       @prompts [ prompt . name_value ]  =  prompt 
94+       @prompts [ prompt . name ]  =  prompt 
9795    end 
9896
9997    def  resources_list_handler ( &block ) 
@@ -156,14 +154,14 @@ def handle_request(request, method)
156154            @handlers [ method ] . call ( params ) 
157155          end 
158156        rescue  =>  e 
159-           report_exception ( e ,  {  request : request   } ) 
157+           report_exception ( e ,  {  request : } ) 
160158          if  e . is_a? ( RequestHandlerError ) 
161159            add_instrumentation_data ( error : e . error_type ) 
162160            raise  e 
163161          end 
164162
165163          add_instrumentation_data ( error : :internal_error ) 
166-           raise  RequestHandlerError . new ( "Internal error handling #{ method }  ,  request ,   original_error :  e ) 
164+           raise  RequestHandlerError . new ( "Internal error handling #{ method }  ,  request ) 
167165        end 
168166      } 
169167    end 
@@ -201,28 +199,24 @@ def call_tool(request)
201199      arguments  =  request [ :arguments ] 
202200      add_instrumentation_data ( tool_name :) 
203201
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 
202+       validate_tool_arguments! ( tool ,  arguments ,  request ) 
212203
213204      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 
221-       rescue  =>  e 
222-         raise  RequestHandlerError . new ( "Internal error calling tool #{ tool_name }  ,  request ,  original_error : e ) 
205+         tool . call ( arguments . transform_keys ( &:to_sym ) ,  server_context :) . to_h 
206+       rescue 
207+         raise  RequestHandlerError . new ( "Internal error calling tool #{ tool_name }  ,  request ) 
223208      end 
224209    end 
225210
211+     def  validate_tool_arguments! ( tool ,  arguments ,  request ) 
212+       input_schema  =  tool . input_schema 
213+       return  unless  input_schema 
214+ 
215+       missing_arguments  =  input_schema . required  - arguments . keys . map ( &:to_sym ) 
216+ 
217+       missing_required_arguments! ( missing_arguments ,  request )  unless  missing_arguments . empty? 
218+     end 
219+ 
226220    def  list_prompts ( request ) 
227221      add_instrumentation_data ( method : Methods ::PROMPTS_LIST ) 
228222      @prompts . map  {  |_ ,  prompt | prompt . to_h  } 
@@ -240,9 +234,31 @@ def get_prompt(request)
240234      add_instrumentation_data ( prompt_name :) 
241235
242236      prompt_args  =  request [ :arguments ] 
243-       prompt . validate_arguments! ( prompt_args ) 
237+       validate_prompt_arguments! ( prompt ,  prompt_args ,  request ) 
238+ 
239+       prompt . call ( prompt_args ,  server_context :) . to_h 
240+     end 
241+ 
242+     def  validate_prompt_arguments! ( prompt ,  provided_arguments ,  request ) 
243+       missing_arguments  =  prompt . arguments . filter_map  do  |configured_argument |
244+         next  unless  configured_argument . required 
245+ 
246+         key  =  configured_argument . name 
247+         next  if  provided_arguments . key? ( key . to_s )  || provided_arguments . key? ( key . to_sym ) 
244248
245-       prompt . template ( prompt_args ,  server_context :) . to_h 
249+         key 
250+       end 
251+ 
252+       missing_required_arguments! ( missing_arguments ,  request )  unless  missing_arguments . empty? 
253+     end 
254+ 
255+     def  missing_required_arguments! ( missing_arguments ,  request ) 
256+       add_instrumentation_data ( error : :missing_required_arguments ) 
257+       raise  RequestHandlerError . new ( 
258+         "Missing required arguments: #{ missing_arguments . join ( ", " ) }  , 
259+         request , 
260+         error_type : :missing_required_arguments , 
261+       ) 
246262    end 
247263
248264    def  list_resources ( request ) 
@@ -273,24 +289,5 @@ def index_resources_by_uri(resources)
273289        hash [ resource . uri ]  =  resource 
274290      end 
275291    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 
295292  end 
296293end 
0 commit comments