Class: RailsMcp::ToolDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_mcp/tool_dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ToolDSL

Returns a new instance of ToolDSL.



7
8
9
10
11
12
# File 'lib/rails_mcp/tool_dsl.rb', line 7

def initialize(name)
  @name             = name.to_s
  @description_text = nil
  @parameters       = []
  @call_block       = nil
end

Instance Method Details

#call(&block) ⇒ Object



22
23
24
# File 'lib/rails_mcp/tool_dsl.rb', line 22

def call(&block)
  @call_block = block
end

#description(text) ⇒ Object



14
15
16
# File 'lib/rails_mcp/tool_dsl.rb', line 14

def description(text)
  @description_text = text
end

#parameter(name, type:, description: nil, required: false) ⇒ Object



18
19
20
# File 'lib/rails_mcp/tool_dsl.rb', line 18

def parameter(name, type:, description: nil, required: false)
  @parameters << { name: name.to_s, type: type.to_s, description: description, required: required }
end

#to_mcp_toolObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rails_mcp/tool_dsl.rb', line 26

def to_mcp_tool
  name        = @name
  description = @description_text
  parameters  = @parameters
  call_block  = @call_block

  properties = parameters.to_h do |p|
    schema = { type: p[:type] }
    schema[:description] = p[:description] if p[:description]
    [p[:name].to_sym, schema]
  end
  required = parameters.select { |p| p[:required] }.map { |p| p[:name] }
  schema = { properties: properties }
  schema[:required] = required if required.any?

  Class.new(MCP::Tool) do
    tool_name name
    description description
    input_schema(**schema)

    define_singleton_method(:call) do |server_context:, **args|
      call_block.call(args, server_context)
    end
  end
end