Class: DebugMcp::Tools::RunScript

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

Class Method Summary collapse

Class Method Details

.call(file:, args: [], port: nil, breakpoints: nil, restore_breakpoints: nil, server_context:) ⇒ Object



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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/debug_mcp/tools/run_script.rb', line 56

def call(file:, args: [], port: nil, breakpoints: nil, restore_breakpoints: nil, server_context:)
  manager = server_context[:session_manager]

  # Clean up dead sessions from previous runs
  cleaned = manager.cleanup_dead_sessions
  # Check if there are still active sessions
  still_active = manager.active_sessions.select { |s| s[:connected] }

  # Clear saved breakpoints unless explicitly restoring.
  # Explicit breakpoints parameter takes precedence over restore.
  manager.clear_breakpoint_specs if !restore_breakpoints || breakpoints&.any?

  unless File.exist?(file)
    return MCP::Tool::Response.new([{ type: "text", text: "Error: File not found: #{file}" }])
  end

  # Verify rdbg is available
  unless system("which rdbg > /dev/null 2>&1")
    return MCP::Tool::Response.new([{ type: "text", text:
      "Error: 'rdbg' command not found. Install the debug gem: gem install debug" }])
  end

  # Start rdbg with --open so we can connect to it.
  # When initial breakpoints are specified, omit --nonstop so the program
  # pauses at line 1, giving us time to set breakpoints before execution.
  debug_port = port || find_available_port
  has_initial_bps = breakpoints&.any?
  cmd = ["rdbg", "--open", "--port=#{debug_port}"]
  cmd << "--nonstop" unless has_initial_bps
  cmd += ["--", file, *args]

  # Capture stdout/stderr to temp files for post-mortem diagnostics
  stdout_tmpfile = Tempfile.create(["debug-mcp-stdout-", ".log"])
  stdout_path = stdout_tmpfile.path
  stdout_tmpfile.close

  stderr_tmpfile = Tempfile.create(["debug-mcp-stderr-", ".log"])
  stderr_path = stderr_tmpfile.path
  stderr_tmpfile.close

  pid = spawn(*cmd, out: stdout_path, err: stderr_path)
  wait_thread = Process.detach(pid)

  # Wait for the debug server to be ready
  connected = false
  10.times do
    sleep 0.5

    # Check if the process is still alive
    unless process_alive?(pid)
      return MCP::Tool::Response.new([{ type: "text", text:
        "Error: Script exited immediately (PID: #{pid}). " \
        "Check the script for syntax errors or missing dependencies." }])
    end

    begin
      result = manager.connect(host: "localhost", port: debug_port)
      connected = true

      # Store metadata on the client for post-mortem diagnostics and rerun
      client = manager.client(result[:session_id])
      client.stdout_file = stdout_path
      client.stderr_file = stderr_path
      client.wait_thread = wait_thread
      client.script_file = file
      client.script_args = args

      initial_output = result[:output]

      # Auto-skip if stopped at internal Ruby code (e.g., bundled_gems.rb due to SIGURG)
      initial_output, skipped = skip_internal_code(client, initial_output)

      # Set initial breakpoints and continue past the line-1 stop
      bp_results = []
      deferred_bps = []
      if has_initial_bps
        breakpoints.each do |bp|
          bp_cmd = bp.start_with?("catch ") ? bp : "break #{bp}"
          bp_output = client.send_command(bp_cmd)
          first_line = bp_output.lines.first&.strip || ""

          if first_line.include?("Unknown") || first_line.include?("not found")
            # Class not defined yet at line 1 — defer until after continue
            deferred_bps << { spec: bp, cmd: bp_cmd, reason: first_line }
          else
            display = first_line.include?("duplicated") ? "Already set (reused existing)" : first_line
            bp_results << { spec: bp, output: display }
            manager.record_breakpoint(bp_cmd)
          end
        rescue DebugMcp::Error => e
          bp_results << { spec: bp, error: e.message }
        end

        # Continue past the initial line-1 stop (loads class definitions)
        begin
          initial_output = client.send_continue
          initial_output, skipped = skip_internal_code(client, initial_output) unless skipped
        rescue DebugMcp::SessionError => e
          # Program exited before hitting any breakpoint
          text = DebugMcp::ExitMessageBuilder.build_exit_message(
            "Program finished before hitting any breakpoint.", e.final_output, client,
          )
          return MCP::Tool::Response.new([{ type: "text", text: text }])
        end

        # Retry deferred breakpoints now that classes should be defined.
        # Don't continue again — the program is already stopped at a useful
        # point (debugger statement or an immediate breakpoint).
        deferred_bps.each do |db|
          bp_output = client.send_command(db[:cmd])
          first_line = bp_output.lines.first&.strip || ""
          display = first_line.include?("duplicated") ? "Already set (reused existing)" : first_line
          bp_results << { spec: db[:spec], output: display, deferred: true }
          manager.record_breakpoint(db[:cmd])
        rescue DebugMcp::Error => e
          bp_results << { spec: db[:spec], error: e.message }
        end
      end

      session_notes = []
      if cleaned.any?
        session_notes << "Cleaned up #{cleaned.size} previous session(s): " \
                         "#{cleaned.map { |c| c[:session_id] }.join(", ")}"
      end
      if still_active.any?
        session_notes << "Note: #{still_active.size} other session(s) still active " \
                         "(#{still_active.map { |s| s[:session_id] }.join(", ")})"
      end

      text = ""
      text += session_notes.join("\n") + "\n\n" if session_notes.any?
      text += "Script started (PID: #{pid}) and connected via port #{debug_port}.\n" \
              "Session ID: #{result[:session_id]}"
      text += "\n(auto-skipped internal code stop)" if skipped
      text += "\n\n#{initial_output}"

      # Show initial breakpoint results
      if bp_results.any?
        text += "\n\nSet #{bp_results.size} initial breakpoint(s):"
        bp_results.each do |r|
          text += if r[:error]
            "\n  #{r[:spec]} -> Error: #{r[:error]}"
          elsif r[:deferred]
            "\n  #{r[:spec]} -> #{r[:output]} (set after class loaded)"
          else
            "\n  #{r[:spec]} -> #{r[:output]}"
          end
        end
      end

      # Restore breakpoints from previous sessions (skip when initial BPs were provided)
      restored = has_initial_bps ? [] : manager.restore_breakpoints(client)
      if restored.any?
        text += "\n\nRestored #{restored.size} breakpoint(s) from previous session:"
        restored.each do |r|
          text += if r[:error]
            "\n  #{r[:spec]} -> Error: #{r[:error]}"
          else
            "\n  #{r[:spec]} -> #{r[:output]}"
          end
        end
      end

      return MCP::Tool::Response.new([{ type: "text", text: text }])
    rescue DebugMcp::Error
      next
    end
  end

  unless connected
    Process.kill("TERM", pid) rescue nil
    # Read any captured output for diagnostics
    stdout_output = File.read(stdout_path, encoding: "UTF-8").strip rescue nil
    stderr_output = File.read(stderr_path, encoding: "UTF-8").strip rescue nil
    File.delete(stdout_path) rescue nil
    File.delete(stderr_path) rescue nil
    msg = "Error: Script started (PID: #{pid}) but could not connect to debug session " \
          "on port #{debug_port} within 5 seconds. The script may have exited early."
    msg += "\n\nProgram output (stdout):\n#{stdout_output}" if stdout_output && !stdout_output.empty?
    msg += "\n\nProcess stderr:\n#{stderr_output}" if stderr_output && !stderr_output.empty?
    return MCP::Tool::Response.new([{ type: "text", text: msg }])
  end
rescue Errno::ENOENT => e
  MCP::Tool::Response.new([{ type: "text", text: "Error: Command not found: #{e.message}" }])
rescue StandardError => e
  MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.class}: #{e.message}" }])
end