Class: DebugMcp::Tools::InspectObject

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

Class Method Summary collapse

Class Method Details

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



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

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

  parts = []

  # RT 1: Get the pretty-printed value (primary - if this fails, expression is invalid)
  value_output = client.send_command("pp #{expression}")
  parts << "Value:\n#{value_output}"

  # RT 2: Get class + instance variables (+ class variables if Module) in a single command
  begin
    meta_output = client.send_command(
      "p [(#{expression}).class.to_s, (#{expression}).instance_variables, " \
      "(#{expression}).is_a?(Module) ? (#{expression}).class_variables : nil]",
    )
    class_name, ivars, cvars = parse_meta(meta_output)
    parts << "Class: #{class_name}" if class_name
    parts << "Instance variables: #{ivars}" if ivars

    # RT 3: Get class variable values (only for Module/Class with class variables)
    if cvars && cvars != "[]"
      begin
        cvar_values = client.send_command(
          "pp Hash[(#{expression}).class_variables.map{|v|" \
          "[v,begin;(#{expression}).class_variable_get(v);rescue;'(error)';end]}]",
        )
        parts << "Class variables:\n#{cvar_values}"
      rescue DebugMcp::TimeoutError
        parts << "Class variables: #{cvars}"
      end
    elsif cvars
      parts << "Class variables: #{cvars}"
    end
  rescue DebugMcp::TimeoutError
    parts << "Class: (timed out)"
    parts << "Instance variables: (timed out)"
  end

  text = parts.join("\n\n")
  text = append_trap_context_note(client, text)
  if (http_note = PendingHttpHelper.pending_http_note(client))
    text = "#{text}\n\n#{http_note}"
  end
  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::Error => e
  MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.message}" }])
end