Class: DebugMcp::Tools::RailsModel

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/debug_mcp/tools/rails_model.rb

Class Method Summary collapse

Class Method Details

.call(model_name: nil, session_id: nil, server_context:) ⇒ Object



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
# File 'lib/debug_mcp/tools/rails_model.rb', line 36

def call(model_name: nil, session_id: nil, server_context:)
  client = server_context[:session_manager].client(session_id)
  client.auto_repause!
  RailsHelper.require_rails!(client)

  # List models when model_name is omitted
  return list_models(client) unless model_name

  # Verify model exists and is an ActiveRecord model.
  # Uses a single rescue-wrapped expression to distinguish:
  #   "ar"        — confirmed ActiveRecord model
  #   "not_ar"    — constant exists but not AR
  #   "undefined" — constant not defined (autoloading may have failed)
  #   "err:Class" — evaluation raised (ThreadError in trap context, etc.)
  verify_result = verify_ar_model(client, model_name)
  return verify_result if verify_result.is_a?(MCP::Tool::Response)

  parts = []

  # Header with table name
  table_name = eval_expr(client, "#{model_name}.table_name")
  parts << "=== #{model_name} (table: #{table_name || "unknown"}) ==="

  # Columns
  parts << build_columns_section(client, model_name)

  # Associations
  section = build_associations_section(client, model_name)
  parts << section if section

  # Validations
  section = build_validations_section(client, model_name)
  parts << section if section

  # Enums
  section = build_enums_section(client, model_name)
  parts << section if section

  # Scopes
  section = build_scopes_section(client, model_name)
  parts << section if section

  # Callbacks
  section = build_callbacks_section(client, model_name)
  parts << section if section

  text = parts.compact.join("\n\n")

  # If columns section shows an error, it likely failed due to trap context
  if text.include?("unable to retrieve") || text.include?("Error:")
    text += "\n\n#{RailsHelper::TRAP_CONTEXT_HINT}" if RailsHelper.trap_context?(client)
  end

  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::Error => e
  text = "Error: #{e.message}"
  text += "\n\n#{RailsHelper::TRAP_CONTEXT_HINT}" if begin
    RailsHelper.trap_context?(client)
  rescue StandardError
    false
  end
  MCP::Tool::Response.new([{ type: "text", text: text }])
end