@@ -11,13 +11,11 @@ class Server
11
11
12
12
class RequestHandlerError < StandardError
13
13
attr_reader :error_type
14
- attr_reader :original_error
15
14
16
- def initialize ( message , request , error_type : :internal_error , original_error : nil )
15
+ def initialize ( message , request , error_type : :internal_error )
17
16
super ( message )
18
17
@request = request
19
18
@error_type = error_type
20
- @original_error = original_error
21
19
end
22
20
end
23
21
@@ -39,8 +37,8 @@ def initialize(
39
37
)
40
38
@name = name
41
39
@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 ] }
44
42
@resources = resources
45
43
@resource_templates = resource_templates
46
44
@resource_index = index_resources_by_uri ( resources )
@@ -88,12 +86,12 @@ def handle_json(request)
88
86
89
87
def define_tool ( name : nil , description : nil , input_schema : nil , annotations : nil , &block )
90
88
tool = Tool . define ( name :, description :, input_schema :, annotations :, &block )
91
- @tools [ tool . name_value ] = tool
89
+ @tools [ tool . name ] = tool
92
90
end
93
91
94
92
def define_prompt ( name : nil , description : nil , arguments : [ ] , &block )
95
93
prompt = Prompt . define ( name :, description :, arguments :, &block )
96
- @prompts [ prompt . name_value ] = prompt
94
+ @prompts [ prompt . name ] = prompt
97
95
end
98
96
99
97
def resources_list_handler ( &block )
@@ -156,14 +154,14 @@ def handle_request(request, method)
156
154
@handlers [ method ] . call ( params )
157
155
end
158
156
rescue => e
159
- report_exception ( e , { request : request } )
157
+ report_exception ( e , { request : } )
160
158
if e . is_a? ( RequestHandlerError )
161
159
add_instrumentation_data ( error : e . error_type )
162
160
raise e
163
161
end
164
162
165
163
add_instrumentation_data ( error : :internal_error )
166
- raise RequestHandlerError . new ( "Internal error handling #{ method } request" , request , original_error : e )
164
+ raise RequestHandlerError . new ( "Internal error handling #{ method } request" , request )
167
165
end
168
166
}
169
167
end
@@ -201,28 +199,24 @@ def call_tool(request)
201
199
arguments = request [ :arguments ]
202
200
add_instrumentation_data ( tool_name :)
203
201
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 )
212
203
213
204
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 )
223
208
end
224
209
end
225
210
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
+
226
220
def list_prompts ( request )
227
221
add_instrumentation_data ( method : Methods ::PROMPTS_LIST )
228
222
@prompts . map { |_ , prompt | prompt . to_h }
@@ -240,9 +234,31 @@ def get_prompt(request)
240
234
add_instrumentation_data ( prompt_name :)
241
235
242
236
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 )
244
248
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
+ )
246
262
end
247
263
248
264
def list_resources ( request )
@@ -273,24 +289,5 @@ def index_resources_by_uri(resources)
273
289
hash [ resource . uri ] = resource
274
290
end
275
291
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
292
end
296
293
end
0 commit comments