Class: RailsMcpServer::GetModels

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/rails-mcp-server/tools/get_model.rb

Instance Method Summary collapse

Instance Method Details

#call(model_name: nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
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
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
# File 'lib/rails-mcp-server/tools/get_model.rb', line 11

def call(model_name: nil)
  unless current_project
    message = "No active project. Please switch to a project first."
    log(:warn, message)

    return message
  end

  if model_name
    log(:info, "Getting info for specific model: #{model_name}")

    # Check if the model file exists
    model_file = File.join(active_project_path, "app", "models", "#{underscore(model_name)}.rb")
    unless File.exist?(model_file)
      log(:warn, "Model file not found: #{model_name}")
      message = "Model '#{model_name}' not found."
      log(:warn, message)

      return message
    end

    log(:debug, "Reading model file: #{model_file}")

    # Get the model file content
    model_content = File.read(model_file)

    # Try to get schema information
    log(:debug, "Executing Rails runner to get schema information")
    schema_info = execute_rails_command(
      active_project_path,
      "runner \"puts #{model_name}.column_names\""
    )

    # Try to get associations
    associations = []
    if model_content.include?("has_many")
      has_many = model_content.scan(/has_many\s+:(\w+)/).flatten
      associations << "Has many: #{has_many.join(", ")}" unless has_many.empty?
    end

    if model_content.include?("belongs_to")
      belongs_to = model_content.scan(/belongs_to\s+:(\w+)/).flatten
      associations << "Belongs to: #{belongs_to.join(", ")}" unless belongs_to.empty?
    end

    if model_content.include?("has_one")
      has_one = model_content.scan(/has_one\s+:(\w+)/).flatten
      associations << "Has one: #{has_one.join(", ")}" unless has_one.empty?
    end

    log(:debug, "Found #{associations.size} associations for model: #{model_name}")

    # Format the output
    "      Model: \#{model_name}\n      \n      Schema:\n      \#{schema_info}\n      \n      Associations:\n      \#{associations.empty? ? \"None found\" : associations.join(\"\\n\")}\n      \n      Model Definition:\n      ```ruby\n      \#{model_content}\n      ```\n    INFO\n  else\n    log(:info, \"Listing all models\")\n\n    # List all models\n    models_dir = File.join(active_project_path, \"app\", \"models\")\n    unless File.directory?(models_dir)\n      message = \"Models directory not found.\"\n      log(:warn, message)\n\n      return message\n    end\n\n    # Get all .rb files in the models directory and its subdirectories\n    model_files = Dir.glob(File.join(models_dir, \"**\", \"*.rb\"))\n      .map { |f| f.sub(\"\#{models_dir}/\", \"\").sub(/\\.rb$/, \"\") }\n      .sort # rubocop:disable Performance/ChainArrayAllocation\n\n    log(:debug, \"Found \#{model_files.size} model files\")\n\n    \"Models in the project:\\n\\n\#{model_files.join(\"\\n\")}\"\n  end\nend\n"