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
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
|
# File 'lib/debug_mcp/tools/evaluate_code.rb', line 46
def call(code:, session_id: nil, acknowledge_mutations: nil, server_context:)
manager = server_context[:session_manager]
client = manager.client(session_id)
client.auto_repause!
if acknowledge_mutations
manager.acknowledge_warning(session_id, :mutation_operations)
end
safety_warnings = CodeSafetyAnalyzer.analyze(code)
acknowledged = manager.acknowledged_warnings(session_id)
safety_warnings = CodeSafetyAnalyzer.filter_acknowledged(safety_warnings, acknowledged)
warning_text = CodeSafetyAnalyzer.format_warnings(safety_warnings)
if client.trap_context
return call_in_trap_context(client, code, warning_text: warning_text)
end
stdout_redirected = false
suspended_catch_bps = []
begin
begin
client.send_command('$stdout = STDOUT if $stdout != STDOUT; nil')
rescue DebugMcp::Error
end
if client.suspended_catch_bps&.any?
restore_catch_breakpoints(client, client.suspended_catch_bps)
client.suspended_catch_bps = []
end
suspended_catch_bps = suspend_catch_breakpoints(client)
client.send_command(
'$__debug_mcp_cap = StringIO.new; $stdout = $__debug_mcp_cap',
)
stdout_redirected = true
output = client.send_command(build_eval_command(code))
captured = restore_and_read_stdout(client)
stdout_redirected = false
err_info = read_eval_error(client)
if err_info
text = "Error: #{err_info}"
text += "\n\nDebugger output:\n#{output}" if output && !output.strip.empty? && output.strip != "nil"
text += "\n\nCaptured stdout:\n#{captured}" if captured
if err_info.include?("ThreadError")
text += "\n\nThis error occurs in signal trap context (common when connecting to Puma/Rails via SIGURG).\n" \
"Thread operations (Mutex, DB queries, model autoloading) are not available here.\n\n" \
"To escape trap context:\n" \
" 1. set_breakpoint on a line in your controller/action\n" \
" 2. trigger_request to send an HTTP request (this auto-resumes the process)\n" \
" 3. Once stopped at the breakpoint, all operations work normally"
end
elsif captured
return_val = output.strip.sub(/\A=> /, "")
if captured.strip == return_val.strip
text = output
else
text = "Return value:\n#{output}\n\nCaptured stdout:\n#{captured}"
end
else
text = output
end
text = append_frame_info(client, text)
text = append_trap_context_note(client, text)
text = append_pending_http_note(client, text)
text = prepend_warning(text, warning_text)
MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::TimeoutError => e
MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.message}\n\n" \
"The code may be taking too long to execute. Consider:\n" \
"- Breaking the expression into smaller parts\n" \
"- Using 'run_debug_command' with a custom timeout" }])
rescue DebugMcp::Error => e
text = "Error: #{e.message}"
if e.message.include?("ThreadError")
text += "\n\nThis error occurs in signal trap context. " \
"Use set_breakpoint + trigger_request to escape to normal context first."
end
MCP::Tool::Response.new([{ type: "text", text: text }])
ensure
if stdout_redirected
client.send_command('$stdout = STDOUT') rescue nil
end
client.suspended_catch_bps = suspended_catch_bps if suspended_catch_bps.any?
restore_catch_breakpoints(client, suspended_catch_bps)
client.suspended_catch_bps = []
end
end
|