Class: LanguageOperator::ToolLoader
- Inherits:
-
Object
- Object
- LanguageOperator::ToolLoader
- 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.
Class Method Summary collapse
-
.create_mcp_tool(tool_def) ⇒ Class
Convert a DSL tool definition to an MCP::Tool class.
-
.start(tools_dir: '/mcp', server_name: 'language-operator-tool') ⇒ void
Start an MCP server with loaded tools.
-
.start_http_server(server, tool_count, port) ⇒ void
Start MCP server in HTTP mode.
-
.start_stdio_server(server, tool_count) ⇒ void
Start MCP server in stdio mode.
Instance Method Summary collapse
-
#initialize(registry, tools_dir = '/mcp') ⇒ ToolLoader
constructor
Initialize tool loader.
-
#load_tool_file(file) ⇒ void
Load a single tool file.
-
#load_tools ⇒ void
Load all tool files from the tools directory.
-
#reload ⇒ void
Reload all tools (hot reload).
Constructor Details
#initialize(registry, tools_dir = '/mcp') ⇒ ToolLoader
Initialize tool loader
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
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.) 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)
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
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
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
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.}" warn e.backtrace.join("\n") end end |
#load_tools ⇒ void
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 |
#reload ⇒ void
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 |