Class: RailsActiveMcp::ConsoleExecutor
- Inherits:
-
Object
- Object
- RailsActiveMcp::ConsoleExecutor
- Defined in:
- lib/rails_active_mcp/console_executor.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: ExecutionError, ThreadSafetyError
Constant Summary collapse
- EXECUTION_MUTEX =
Mutex.new
Instance Method Summary collapse
- #build_model_columns(model_class) ⇒ Object
- #build_model_enums(model_class) ⇒ Object
- #build_model_reflections(model_class) ⇒ Object
- #build_model_validator_info(model_class) ⇒ Object
- #dry_run(code) ⇒ Object
- #execute(code, timeout: nil, safe_mode: nil, capture_output: true) ⇒ Object
- #execute_safe_query(model:, method:, args: [], where: nil, limit: nil) ⇒ Object
- #get_model_info(model_name) ⇒ Object
-
#initialize(config) ⇒ ConsoleExecutor
constructor
A new instance of ConsoleExecutor.
Constructor Details
#initialize(config) ⇒ ConsoleExecutor
Returns a new instance of ConsoleExecutor.
18 19 20 21 |
# File 'lib/rails_active_mcp/console_executor.rb', line 18 def initialize(config) @config = config @safety_checker = config.safety_checker.new(config) end |
Instance Method Details
#build_model_columns(model_class) ⇒ Object
188 189 190 191 192 193 194 195 196 |
# File 'lib/rails_active_mcp/console_executor.rb', line 188 def build_model_columns(model_class) model_class.columns.map do |column| { name: column.name, type: column.type, primary: column.name == model_class.primary_key } end end |
#build_model_enums(model_class) ⇒ Object
212 213 214 215 216 217 218 |
# File 'lib/rails_active_mcp/console_executor.rb', line 212 def build_model_enums(model_class) return {} unless model_class.respond_to?(:defined_enums) model_class.defined_enums.transform_values do |mapping| mapping.transform_values(&:to_s) end end |
#build_model_reflections(model_class) ⇒ Object
178 179 180 181 182 183 184 185 186 |
# File 'lib/rails_active_mcp/console_executor.rb', line 178 def build_model_reflections(model_class) model_class.reflect_on_all_associations.map do |association| { name: association.name, type: association.macro, class_name: association.class_name } end end |
#build_model_validator_info(model_class) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/rails_active_mcp/console_executor.rb', line 198 def build_model_validator_info(model_class) if model_class.respond_to?(:validators) model_class.validators.map do |validator| { type: validator.class.name, attributes: validator.attributes, options: validator. } end else [] end end |
#dry_run(code) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/rails_active_mcp/console_executor.rb', line 220 def dry_run(code) # Analyze without executing safety_analysis = @safety_checker.analyze(code) { code: code, safety_analysis: safety_analysis, would_execute: safety_analysis[:safe] || !@config.safe_mode, estimated_risk: estimate_risk(safety_analysis), recommendations: generate_recommendations(safety_analysis) } end |
#execute(code, timeout: nil, safe_mode: nil, capture_output: true) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rails_active_mcp/console_executor.rb', line 23 def execute(code, timeout: nil, safe_mode: nil, capture_output: true) timeout ||= @config.command_timeout safe_mode = @config.safe_mode if safe_mode.nil? # Pre-execution safety check if safe_mode safety_analysis = @safety_checker.analyze(code) raise RailsActiveMcp::SafetyError, "Code failed safety check: #{safety_analysis[:summary]}" unless safety_analysis[:safe] end # Log execution if enabled log_execution(code) if @config.log_executions # Execute with Rails 7.1 compatible thread safety result = execute_with_rails_executor(code, timeout, capture_output) # Post-execution processing process_result(result) rescue RailsActiveMcp::SafetyError => e { success: false, error: e., error_class: 'SafetyError', code: code } end |
#execute_safe_query(model:, method:, args: [], where: nil, limit: nil) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rails_active_mcp/console_executor.rb', line 50 def execute_safe_query(model:, method:, args: [], where: nil, limit: nil) limit ||= @config.max_results begin # Validate model access raise RailsActiveMcp::SafetyError, "Access to model '#{model}' is not allowed" unless @config.model_allowed?(model) # Validate method safety raise RailsActiveMcp::SafetyError, "Method '#{method}' is not allowed for safe queries" unless safe_query_method?(method) # Execute with proper Rails executor and connection management execute_with_rails_executor_and_connection do model_class = model.to_s.constantize # Build base relation, applying optional WHERE conditions relation = model_class relation = relation.where(where) if where.is_a?(Hash) && where.any? # Build and execute query query = if args.empty? relation.public_send(method) else relation.public_send(method, *args) end # Apply limit for enumerable results query = query.limit(limit) if query.respond_to?(:limit) && !count_method?(method) result = execute_query_with_timeout(query) { success: true, model: model, method: method, args: args, where: where, result: serialize_result(result), count: calculate_count(result), executed_at: Time.now } end rescue RailsActiveMcp::SafetyError => e { success: false, error: e., error_class: 'SafetyError', model: model, method: method, args: args, where: where } rescue StandardError => e log_error(e, { model: model, method: method, args: args, where: where }) { success: false, error: e., error_class: e.class.name, model: model, method: method, args: args, where: where } end end |
#get_model_info(model_name) ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/rails_active_mcp/console_executor.rb', line 115 def get_model_info(model_name) # Validate model access unless @config.model_allowed?(model_name) return { success: false, error: "Access to model '#{model_name}' is not allowed", error_class: 'SafetyError', model_name: model_name } end execute_with_rails_executor_and_connection do model_class = model_name.to_s.constantize # Ensure it's an ActiveRecord model unless model_class.respond_to?(:columns) && model_class.respond_to?(:reflect_on_all_associations) raise RailsActiveMcp::SafetyError, "#{model_name} is not a valid ActiveRecord model" end # Extract model information columns_info = build_model_columns(model_class) associations_info = build_model_reflections(model_class) validators_info = build_model_validator_info(model_class) enums_info = build_model_enums(model_class) { success: true, model_name: model_name, table_name: model_class.table_name, primary_key: model_class.primary_key, columns: columns_info, associations: associations_info, validators: validators_info, enums: enums_info, extracted_at: Time.now } end rescue RailsActiveMcp::SafetyError => e { success: false, error: e., error_class: 'SafetyError', model_name: model_name } rescue NameError => e { success: false, error: "Model '#{model_name}' not found: #{e.}", error_class: 'NameError', model_name: model_name } rescue StandardError => e { success: false, error: e., error_class: e.class.name, model_name: model_name } end |