Support Sorbet typed tools

This commit is contained in:
Christopher Butcher 2025-05-16 10:09:31 -04:00
commit 84e83841b0
No known key found for this signature in database
GPG key ID: 604E358E88E26254

View file

@ -197,7 +197,8 @@ module ModelContextProtocol
end
begin
call_params = tool.method(:call).parameters.flatten
call_params = tool_call_parameters(tool)
if call_params.include?(:server_context)
tool.call(**arguments.transform_keys(&:to_sym), server_context:).to_h
else
@ -258,5 +259,24 @@ module ModelContextProtocol
hash[resource.uri] = resource
end
end
def tool_call_parameters(tool)
method_def = tool_call_method_def(tool)
method_def.parameters.flatten
end
def tool_call_method_def(tool)
method = tool.method(:call)
if defined?(T::Utils) && T::Utils.respond_to?(:signature_for_method)
sorbet_typed_method_definition = T::Utils.signature_for_method(method)&.method
# Return the Sorbet typed method definition if it exists, otherwise fallback to original method
# definition if Sorbet is defined but not used by this tool.
sorbet_typed_method_definition || method
else
method
end
end
end
end