Class: DebugMcp::Tools::ContinueExecution

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

Constant Summary collapse

HTTP_JOIN_TIMEOUT =
10

Class Method Summary collapse

Class Method Details

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



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

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

  # Check breakpoint existence before continuing (for timeout message)
  has_breakpoints = check_breakpoints(client)

  # Retrieve and clear pending HTTP info before continuing
  pending = client.pending_http
  client.pending_http = nil if pending

  output = if pending
    client.send_continue { pending[:holder][:done] }
  else
    client.send_continue
  end

  # The debug gem may send an `input` prompt just before the process exits
  # (e.g., on a return event from the main script). When output is empty,
  # check if the process has actually exited.
  if output.strip.empty? && client.process_finished?
    text = DebugMcp::ExitMessageBuilder.build_exit_message(
      "Program finished execution.", output, client,
    )
    text = append_http_response(text, pending)
    return MCP::Tool::Response.new([{ type: "text", text: text }])
  end

  client.cleanup_one_shot_breakpoints(output)
  output = DebugMcp::StopEventAnnotator.annotate_breakpoint_hit(output)
  output = DebugMcp::StopEventAnnotator.enrich_stop_context(output, client)

  text = "Execution resumed.\n\n#{output}"
  text = append_http_response(text, pending)
  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::SessionError => e
  text = if e.message.include?("session ended") || e.message.include?("finished execution")
    DebugMcp::ExitMessageBuilder.build_exit_message("Program finished execution.", e.final_output, client)
  else
    "Error: #{e.message}"
  end
  text = append_http_response(text, pending)
  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::TimeoutError
  text = if has_breakpoints
    ">>> PROCESS STATE: RUNNING (not paused) <<<\n\n" \
    "No breakpoint was hit within the timeout period.\n\n" \
    "Next steps:\n" \
    "1. set_breakpoint on a specific code path\n" \
    "2. trigger_request to send an HTTP request (auto-resumes)\n" \
    "3. disconnect to detach"
  else
    ">>> PROCESS STATE: RUNNING (not paused) <<<\n\n" \
    "Process resumed successfully (no breakpoints set).\n\n" \
    "Next steps:\n" \
    "1. set_breakpoint to add breakpoints\n" \
    "2. trigger_request to send an HTTP request and hit a breakpoint"
  end
  timeout_sec = server_context[:session_manager]&.timeout
  if timeout_sec
    text += "\n\nNote: The debug session will remain active for " \
            "#{timeout_sec / 60} minutes of inactivity. Any tool call resets the timer."
  end
  text = append_http_response(text, pending)
  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::ConnectionError => e
  text = if e.message.include?("Connection lost") || e.message.include?("connection closed")
    DebugMcp::ExitMessageBuilder.build_exit_message(
      "Program finished execution (connection closed).", e.final_output, client,
    )
  else
    "Error: #{e.message}"
  end
  text = append_http_response(text, pending)
  MCP::Tool::Response.new([{ type: "text", text: text }])
rescue DebugMcp::Error => e
  MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.message}" }])
end