Class: RailsMcpInsight::Tools::TraceRequest

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/rails_mcp_insight/tools/trace_request.rb

Class Method Summary collapse

Class Method Details

.call(method:, path:, server_context:) ⇒ 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rails_mcp_insight/tools/trace_request.rb', line 23

def self.call(method:, path:, server_context:)
  config = Configuration.new(project_path: server_context&.dig(:project_path) || Dir.pwd)
  ast_parser = Analyzers::AstParser.new

  # Step 1: Find the matching route
  route_analyzer = Analyzers::RouteAnalyzer.new(config)
  routes = route_analyzer.search(query: path, method: method)
  matched_route = find_best_route_match(routes, path, method)

  unless matched_route
    return MCP::Tool::Response.new([{
                                     type: "text",
                                     text: JSON.pretty_generate({ error: "No route matches #{method} #{path}" })
                                   }])
  end

  # Step 2: Analyze the controller
  controller_name, action_name = parse_controller_action(matched_route[:controller_action])
  controller_analyzer = Analyzers::ControllerAnalyzer.new(config, ast_parser: ast_parser)
  controller_info = controller_analyzer.analyze(controller_name)

  # Step 3: Find model operations from the controller action code
  model_analyzer = Analyzers::ModelAnalyzer.new(config, ast_parser: ast_parser)
  models_used = detect_models_in_controller(config, controller_name, action_name, model_analyzer)

  # Step 4: Find side effects (jobs, mailers)
  Analyzers::JobAnalyzer.new(config, ast_parser: ast_parser)
  side_effects = detect_side_effects(config, controller_name, action_name)

  result = {
    route: matched_route,
    controller: if controller_info
                  {
                    name: controller_name,
                    action: action_name,
                    before_actions: filter_relevant_filters(controller_info[:before_actions],
                                                            action_name),
                    after_actions: filter_relevant_filters(controller_info[:after_actions], action_name),
                    strong_params: controller_info[:strong_params]
                  }
                end,
    model_operations: models_used,
    side_effects: side_effects
  }

  MCP::Tool::Response.new([{ type: "text", text: JSON.pretty_generate(result) }])
end