Class: RailsMcpServer::GetSchema

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

Instance Method Summary collapse

Instance Method Details

#call(table_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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
# File 'lib/rails-mcp-server/tools/get_schema.rb', line 11

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

    return message
  end

  if table_name
    log(:info, "Getting schema for table: #{table_name}")

    # Execute the Rails schema command for a specific table
    schema_output = RailsMcpServer::RunProcess.execute_rails_command(
      active_project_path,
      "bin/rails runner \"require 'active_record'; puts ActiveRecord::Base.connection.columns('#{table_name}').map{|c| [c.name, c.type, c.null, c.default].inspect}.join('\\n')\""
    )

    if schema_output.strip.empty?
      message = "Table '#{table_name}' not found or has no columns."
      log(:warn, message)

      return message
    end

    # Parse the column information
    columns = schema_output.strip.split("\\n").map do |column_info|
      eval(column_info) # This is safe because we're generating the string ourselves # rubocop:disable Security/Eval
    end

    # Format the output
    formatted_columns = columns.map do |name, type, nullable, default|
      "#{name} (#{type})#{nullable ? ", nullable" : ""}#{default ? ", default: #{default}" : ""}"
    end

    output = "      Table: \#{table_name}\n\n      Columns:\n      \#{formatted_columns.join(\"\\n\")}\n    SCHEMA\n\n    # Try to get foreign keys\n    begin\n      fk_output = RailsMcpServer::RunProcess.execute_rails_command(\n        active_project_path,\n        \"bin/rails runner \\\"require 'active_record'; puts ActiveRecord::Base.connection.foreign_keys('\#{table_name}').map{|fk| [fk.from_table, fk.to_table, fk.column, fk.primary_key].inspect}.join('\\n')\\\"\"\n      )\n\n      unless fk_output.strip.empty?\n        foreign_keys = fk_output.strip.split(\"\\n\").map do |fk_info|\n          eval(fk_info) # This is safe because we're generating the string ourselves # rubocop:disable Security/Eval\n        end\n\n        formatted_fks = foreign_keys.map do |from_table, to_table, column, primary_key|\n          \"\#{column} -> \#{to_table}.\#{primary_key}\"\n        end\n\n        output += <<~FK\n\n          Foreign Keys:\n          \#{formatted_fks.join(\"\\n\")}\n        FK\n      end\n    rescue => e\n      log(:warn, \"Error fetching foreign keys: \#{e.message}\")\n    end\n\n    output\n  else\n    log(:info, \"Getting full schema\")\n\n    # Execute the Rails schema:dump command\n    # First, check if we need to create the schema file\n    schema_file = File.join(active_project_path, \"db\", \"schema.rb\")\n    unless File.exist?(schema_file)\n      log(:info, \"Schema file not found, attempting to generate it\")\n      RailsMcpServer::RunProcess.execute_rails_command(active_project_path, \"db:schema:dump\")\n    end\n\n    if File.exist?(schema_file)\n      # Read the schema file\n      schema_content = File.read(schema_file)\n\n      # Try to get table list\n      tables_output = RailsMcpServer::RunProcess.execute_rails_command(\n        active_project_path,\n        \"bin/rails runner \\\"require 'active_record'; puts ActiveRecord::Base.connection.tables.sort.join('\\n')\\\"\"\n      )\n\n      tables = tables_output.strip.split(\"\\n\")\n\n      <<~SCHEMA\n        Database Schema\n\n        Tables:\n        \#{tables.join(\"\\n\")}\n\n        Schema Definition:\n        ```ruby\n        \#{schema_content}\n        ```\n      SCHEMA\n    else\n      # If we can't get the schema file, try to get the table list\n      tables_output = RailsMcpServer::RunProcess.execute_rails_command(\n        active_project_path,\n        \"bin/rails runner \\\"require 'active_record'; puts ActiveRecord::Base.connection.tables.sort.join('\\n')\\\"\"\n      )\n\n      if tables_output.strip.empty?\n        message = \"Could not retrieve schema information. Try running 'rails db:schema:dump' in your project first.\"\n        log(:warn, message)\n\n        return message\n      end\n\n      tables = tables_output.strip.split(\"\\n\")\n\n      <<~SCHEMA\n        Database Schema\n\n        Tables:\n        \#{tables.join(\"\\n\")}\n\n        Note: Full schema definition is not available. Run 'rails db:schema:dump' to generate the schema.rb file.\n      SCHEMA\n    end\n  end\nend\n"