Class: DebugMcp::Tools::TriggerRequest

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

Constant Summary collapse

DEFAULT_TIMEOUT =
30
HTTP_BREAKPOINT_TIMEOUT =
300
HTTP_JOIN_TIMEOUT =
5
MAX_LOG_BYTES =
4000

Class Method Summary collapse

Class Method Details

.call(method:, url:, headers: {}, body: nil, cookies: nil, skip_csrf: nil, timeout: nil, session_id: nil, server_context:) ⇒ Object



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

def call(method:, url:, headers: {}, body: nil, cookies: nil, skip_csrf: nil,
         timeout: nil, session_id: nil, server_context:)
  manager = server_context[:session_manager]
  timeout_sec = timeout || DEFAULT_TIMEOUT

  # Auto-detect Content-Type if body is present and no Content-Type header set
  headers = (headers || {}).dup
  if body && !headers.any? { |k, _| k.to_s.downcase == "content-type" }
    headers["Content-Type"] = detect_content_type(body)
  end

  # Build Cookie header from cookies hash
  if cookies && !cookies.empty?
    cookie_str = cookies.map { |k, v| "#{k}=#{v}" }.join("; ")
    existing = headers.find { |k, _| k.to_s.downcase == "cookie" }
    if existing
      headers[existing[0]] = "#{existing[1]}; #{cookie_str}"
    else
      headers["Cookie"] = cookie_str
    end
  end

  # CSRF handling: disable forgery protection for non-GET requests on Rails
  csrf_disabled = false
  client = nil
  log_capture = nil
  begin
    client = manager.client(session_id)
    # Recover paused state if a previous timeout left @paused=false
    # while the process is actually still paused in the debugger.
    begin
      client.auto_repause!
    rescue DebugMcp::Error
      # Best-effort: proceed with current paused state
    end
    # Only send debug commands if the process is paused (at an input prompt).
    # After a continue_execution timeout, the process is running and sending
    # commands would violate the debug protocol, causing connection loss.
    if client.paused
      if method != "GET" && should_disable_csrf?(skip_csrf, client)
        csrf_disabled = temporarily_disable_csrf(client)
      end
      # Snapshot log file position before request for Rails log capture
      log_capture = start_log_capture(client)
    end
  rescue DebugMcp::SessionError
    client = nil
  end

  begin
    response = if client&.connected?
      handle_with_debug_session(client, method, url, headers, body, timeout_sec)
    else
      handle_without_session(method, url, headers, body, timeout_sec)
    end

    append_captured_logs(response, log_capture)
  ensure
    # Only restore CSRF when the process is paused (at a breakpoint).
    # If the process is running (interrupted/timeout), sending commands
    # would corrupt the debug protocol and cause session disconnection.
    if csrf_disabled && client&.connected? && client.paused
      restore_csrf(client)
    end
  end
rescue StandardError => e
  MCP::Tool::Response.new([{ type: "text", text: "Error: #{e.class}: #{e.message}" }])
end