Class: RailsMcpInsight::Tools::ExplainAssociationChain

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

Class Method Summary collapse

Class Method Details

.call(from:, to:, server_context:) ⇒ Object



21
22
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
# File 'lib/rails_mcp_insight/tools/explain_association_chain.rb', line 21

def self.call(from:, to:, server_context:)
  config = Configuration.new(project_path: server_context&.dig(:project_path) || Dir.pwd)
  analyzer = Analyzers::ModelAnalyzer.new(config)

  # Build association graph
  all_models = analyzer.list_all
  graph = {}

  all_models.each do |model|
    full_info = analyzer.analyze(model[:name])
    next unless full_info

    graph[model[:name]] = (full_info[:associations] || []).map do |assoc|
      target = classify_name(assoc[:name])
      { type: assoc[:type], name: assoc[:name], target: target }
    end
  end

  # BFS to find all paths (up to depth 5)
  paths = find_paths(graph, from, to, max_depth: 5)

  result = {
    from: from,
    to: to,
    paths_found: paths.length,
    paths: paths.map { |path| format_path(path) }
  }

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