Class: ToolSchema::FastMcpFactory

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
app/lib/tool_schema/fast_mcp_factory.rb

Class Method Summary collapse

Class Method Details

.build(service_class, schema, before_call: nil, after_call: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/lib/tool_schema/fast_mcp_factory.rb', line 19

def self.build(service_class, schema, before_call: nil, after_call: nil)
  tool_constant = tool_class_name(service_class)
  parent = Mcp
  parent.send(:remove_const, tool_constant) if parent.const_defined?(tool_constant, false)

  klass = Class.new(ApplicationTool) do
    description(schema[:description])
    tool_name(schema[:name])
    arguments(&FastMcpBuilder.arguments_block(schema[:params]))

    define_method(:call) do |**kwargs|
      before_call&.call(kwargs)
      result = service_class.new.public_send(schema[:entrypoint], **kwargs)
      after_call&.call(result)
      result
    end
  end

  parent.const_set(tool_constant, klass)
end

.tool_class_name(service_class) ⇒ Object



41
42
43
44
# File 'app/lib/tool_schema/fast_mcp_factory.rb', line 41

def self.tool_class_name(service_class)
  base = service_class.name.split('::').last
  base.gsub(/Service$/, '')
end