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]
cleaned = manager.cleanup_dead_sessions
still_active = manager.active_sessions.select { |s| s[:connected] }
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
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
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]
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)
connected = false
10.times do
sleep 0.5
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
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]
initial_output, skipped = skip_internal_code(client, initial_output)
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")
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
begin
initial_output = client.send_continue
initial_output, skipped = skip_internal_code(client, initial_output) unless skipped
rescue DebugMcp::SessionError => e
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
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}"
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
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
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
|