Class: LanguageOperator::ToolLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/language_operator/tool_loader.rb

Overview

Loads tool definitions from Ruby files

Scans a directory for Ruby files containing tool definitions and loads them into a registry. Provides hot-reloading capability for development.

Examples:

Basic usage

registry = LanguageOperator::Dsl::Registry.new
loader = LanguageOperator::ToolLoader.new(registry, '/mcp/tools')
loader.load_tools
puts "Loaded #{registry.all.length} tools"

With custom context

loader = LanguageOperator::ToolLoader.new(registry)
loader.load_tools
loader.reload  # Hot reload tools

Start MCP server

LanguageOperator::ToolLoader.start  # Loads tools and starts MCP server

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry, tools_dir = '/mcp') ⇒ ToolLoader

Initialize tool loader

Parameters:



31
32
33
34
# File 'lib/language_operator/tool_loader.rb', line 31

def initialize(registry, tools_dir = '/mcp')
  @registry = registry
  @tools_dir = tools_dir
end

Class Method Details

.create_mcp_tool(tool_def) ⇒ Class

Convert a DSL tool definition to an MCP::Tool class

rubocop:disable Metrics/MethodLength, Metrics/AbcSize

Parameters:

Returns:

  • (Class)

    MCP::Tool subclass



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/language_operator/tool_loader.rb', line 177

def self.create_mcp_tool(tool_def)
  # Capture tool name and tracer for use in the dynamic class
  tool_name = tool_def.name
  tracer = OpenTelemetry.tracer_provider.tracer('language-operator-agent', LanguageOperator::VERSION)

  # Create a dynamic MCP::Tool class
  Class.new(MCP::Tool) do
    # Set tool name (required for MCP protocol)
    tool_name tool_def.name

    description tool_def.description || "Tool: #{tool_def.name}"

    # Build input schema from parameters
    properties = {}
    required_params = []

    tool_def.parameters.each do |param_name, param_def|
      properties[param_name] = {
        type: param_def.type&.to_s || 'string',
        description: param_def.description
      }
      required_params << param_name if param_def.required?
    end

    input_schema(
      properties: properties,
      required: required_params
    )

    # Store the execute block
    @execute_block = tool_def.execute_block

    # Define the call method with OpenTelemetry instrumentation
    define_singleton_method(:call) do |**params|
      tracer.in_span('agent.tool.execute', attributes: {
                       'tool.name' => tool_name,
                       'tool.type' => 'custom'
                     }) do |span|
        # Execute the tool's block
        # Convert symbol keys to string keys for consistency with DSL expectations
        string_params = params.transform_keys(&:to_s)
        result = @execute_block.call(string_params)

        # Set success attribute
        span.set_attribute('tool.result', 'success')

        # Return MCP response
        MCP::Tool::Response.new([
                                  {
                                    type: 'text',
                                    text: result.to_s
                                  }
                                ])
      rescue StandardError => e
        # Record exception and set failure status
        span.record_exception(e)
        span.set_attribute('tool.result', 'failure')
        span.status = OpenTelemetry::Trace::Status.error(e.message)
        raise
      end
    end
  end
end

.start(tools_dir: '/mcp', server_name: 'language-operator-tool') ⇒ void

This method returns an undefined value.

Start an MCP server with loaded tools

This class method creates a registry, loads tools from /mcp directory, wraps them as MCP::Tool classes, and starts an MCP server.

Transport mode is automatically detected:

  • If PORT environment variable is set: HTTP server mode (for Kubernetes)
  • Otherwise: stdio transport mode (for local development)

Parameters:

  • tools_dir (String) (defaults to: '/mcp')

    Directory containing tool definition files (default: '/mcp')

  • server_name (String) (defaults to: 'language-operator-tool')

    Name of the MCP server (default: 'language-operator-tool')



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/language_operator/tool_loader.rb', line 101

def self.start(tools_dir: '/mcp', server_name: 'language-operator-tool')
  # Create registry and load tools
  registry = LanguageOperator::Dsl::Registry.new
  loader = new(registry, tools_dir)
  loader.load_tools

  # Convert DSL tools to MCP::Tool classes
  mcp_tools = registry.all.map do |tool_def|
    create_mcp_tool(tool_def)
  end

  # Create MCP server
  server = MCP::Server.new(
    name: server_name,
    tools: mcp_tools
  )

  # Auto-detect transport mode based on PORT environment variable
  if ENV['PORT']
    start_http_server(server, mcp_tools.length, ENV['PORT'].to_i)
  else
    start_stdio_server(server, mcp_tools.length)
  end
end

.start_http_server(server, tool_count, port) ⇒ void

This method returns an undefined value.

Start MCP server in HTTP mode

Parameters:

  • server (MCP::Server)

    The MCP server instance

  • tool_count (Integer)

    Number of tools loaded

  • port (Integer)

    Port to bind to



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/language_operator/tool_loader.rb', line 132

def self.start_http_server(server, tool_count, port)
  require 'rack'
  require 'rackup'

  # Create the Streamable HTTP transport
  transport = MCP::Server::Transports::StreamableHTTPTransport.new(server)
  server.transport = transport

  # Create the Rack application
  app = proc do |env|
    request = Rack::Request.new(env)
    transport.handle_request(request)
  end

  # Build the Rack application with middleware
  rack_app = Rack::Builder.new do
    use Rack::CommonLogger
    use Rack::ShowExceptions
    run app
  end

  puts "Starting MCP HTTP server on http://0.0.0.0:#{port}"
  puts "Loaded #{tool_count} tools"

  # Start the server with Puma
  Rackup::Handler.get('puma').run(rack_app, Port: port, Host: '0.0.0.0')
end

.start_stdio_server(server, tool_count) ⇒ void

This method returns an undefined value.

Start MCP server in stdio mode

Parameters:

  • server (MCP::Server)

    The MCP server instance

  • tool_count (Integer)

    Number of tools loaded



165
166
167
168
169
170
# File 'lib/language_operator/tool_loader.rb', line 165

def self.start_stdio_server(server, tool_count)
  # Use stdio transport
  transport = MCP::Server::Transports::StdioTransport.new(server)
  puts "Starting MCP server with #{tool_count} tools (stdio mode)"
  transport.open
end

Instance Method Details

#load_tool_file(file) ⇒ void

This method returns an undefined value.

Load a single tool file

Parameters:

  • file (String)

    Path to tool definition file



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/language_operator/tool_loader.rb', line 65

def load_tool_file(file)
  puts "Loading tools from: #{file}"

  begin
    context = LanguageOperator::Dsl::Context.new(@registry)
    code = File.read(file)

    # Tools are trusted code - execute directly without sandbox validation
    # Only synthesized agent code should be sandboxed
    context.instance_eval(code, file)
  rescue StandardError => e
    warn "Error loading tool file #{file}: #{e.message}"
    warn e.backtrace.join("\n")
  end
end

#load_toolsvoid

This method returns an undefined value.

Load all tool files from the tools directory



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/language_operator/tool_loader.rb', line 39

def load_tools
  @registry.clear

  unless Dir.exist?(@tools_dir)
    puts "Tools directory #{@tools_dir} does not exist. Skipping tool loading."
    return
  end

  tool_files = Dir.glob(File.join(@tools_dir, '**', '*.rb'))

  if tool_files.empty?
    puts "No tool files found in #{@tools_dir}"
    return
  end

  tool_files.each do |file|
    load_tool_file(file)
  end

  puts "Loaded #{@registry.all.length} tools from #{tool_files.length} files"
end

#reloadvoid

This method returns an undefined value.

Reload all tools (hot reload)



84
85
86
87
# File 'lib/language_operator/tool_loader.rb', line 84

def reload
  puts 'Reloading tools...'
  load_tools
end