Class: Profiler::MCP::Tools::GetProfileHttp

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/tools/get_profile_http.rb

Class Method Summary collapse

Class Method Details

.call(params) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/profiler/mcp/tools/get_profile_http.rb', line 12

def self.call(params)
  token = params["token"]
  unless token
    return [{ type: "text", text: "Error: token parameter is required" }]
  end

  storage = MCP::SlaveSupport.resolve_storage(params)
  profile = if token == "latest"
    storage.list(limit: 1).first
  else
    storage.load(token)
  end
  unless profile
    return [{ type: "text", text: "Profile not found: #{token}" }]
  end

  http_data = profile.collector_data("http")
  unless http_data && http_data["total_requests"].to_i > 0
    return [{ type: "text", text: "No outbound HTTP requests found in this profile" }]
  end

  domain_filter = params["domain"]
  [{ type: "text", text: format_http(profile, http_data, domain_filter, params) }]
end

.format_http(profile, http_data, domain_filter, params) ⇒ Object



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
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
# File 'lib/profiler/mcp/tools/get_profile_http.rb', line 39

def self.format_http(profile, http_data, domain_filter, params)
  threshold = Profiler.configuration.slow_http_threshold
  requests = http_data["requests"] || []

  if domain_filter && !domain_filter.empty?
    requests = requests.select do |req|
      host = begin
        URI.parse(req["url"]).host.to_s
      rescue URI::InvalidURIError
        ""
      end
      host.include?(domain_filter)
    end
  end

  lines = []
  lines << "# Outbound HTTP Analysis: #{profile.token}\n"
  lines << "**Request:** #{profile.method} #{profile.path}"
  lines << "**Started at:** #{profile.started_at&.strftime('%Y-%m-%d %H:%M:%S')}"
  lines << "**Total Outbound Requests:** #{http_data['total_requests']}"
  lines << "**Showing:** #{requests.size} request(s)#{domain_filter ? " (filtered by domain: #{domain_filter})" : ""}"
  lines << "**Total Duration:** #{http_data['total_duration'].round(2)} ms"
  lines << "**Slow Requests (>#{threshold}ms):** #{http_data['slow_requests']}"
  lines << "**Error Requests:** #{http_data['error_requests']}\n"

  if http_data["by_host"] && !http_data["by_host"].empty? && !domain_filter
    lines << "## By Host"
    http_data["by_host"].each { |host, count| lines << "- **#{host}**: #{count}" }
    lines << ""
  end

  if http_data["by_status"] && !http_data["by_status"].empty? && !domain_filter
    lines << "## By Status"
    http_data["by_status"].each { |status, count| lines << "- **#{status}**: #{count}" }
    lines << ""
  end

  if requests && !requests.empty?
    lines << "## Request Details"
    requests.each_with_index do |req, i|
      slow_flag = req["duration"] >= threshold ? " [SLOW]" : ""
      err_flag = req["status"] >= 400 || req["status"] == 0 ? " [ERROR]" : ""
      lines << "\n### Request #{i + 1}#{slow_flag}#{err_flag}"
      lines << "- **ID:** #{req['id']}" if req["id"]
      lines << "- **Started at:** #{req['started_at']}" if req["started_at"]
      lines << "- **Method:** #{req['method']}"
      lines << "- **URL:** #{req['url']}"
      lines << "- **Status:** #{req['status'] == 0 ? 'connection error' : req['status']}"
      lines << "- **Duration:** #{req['duration'].round(2)} ms"
      lines << "- **Request Size:** #{req['request_size']} bytes"
      lines << "- **Response Size:** #{req['response_size']} bytes"
      lines << "- **Error:** #{req['error']}" if req["error"]

      if req["request_headers"] && !req["request_headers"].empty?
        lines << "- **Request Headers:**"
        req["request_headers"].each { |k, v| lines << "  - `#{k}`: #{v}" }
      end

      if req["request_body"] && !req["request_body"].empty?
        lines << "- **Request Body:**"
        formatted = BodyFormatter.format_body(
          profile.token,
          "http_#{i}_request_body",
          req["request_body"],
          req["request_body_encoding"],
          params
        )
        lines << formatted if formatted
      end

      if req["response_headers"] && !req["response_headers"].empty?
        lines << "- **Response Headers:**"
        req["response_headers"].each { |k, v| lines << "  - `#{k}`: #{v}" }
      end

      if req["response_body"] && !req["response_body"].empty?
        lines << "- **Response Body:**"
        formatted = BodyFormatter.format_body(
          profile.token,
          "http_#{i}_response_body",
          req["response_body"],
          req["response_body_encoding"],
          params
        )
        lines << formatted if formatted
      end

      if req["backtrace"] && !req["backtrace"].empty?
        lines << "- **Called from:**"
        req["backtrace"].first(3).each { |frame| lines << "  - #{frame}" }
      end
      lines << "- **Curl:**"
      lines << "  ```bash"
      lines << "  #{generate_curl_for_outbound(req)}"
      lines << "  ```"
    end
    lines << ""
  end

  lines.join("\n")
end

.generate_curl_for_outbound(req) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/profiler/mcp/tools/get_profile_http.rb', line 141

def self.generate_curl_for_outbound(req)
  parts = ["curl -X #{req['method']}"]
  req["request_headers"]
    &.reject { |k, _| k.downcase == "user-agent" }
    &.each { |k, v| parts << "  -H #{Shellwords.shellescape("#{k}: #{v}")}" }
  if req["request_body"] && !req["request_body"].empty? && req["request_body_encoding"] != "base64"
    parts << "  -d #{Shellwords.shellescape(req['request_body'])}"
  end
  parts << "  #{Shellwords.shellescape(req['url'])}"
  parts.join(" \\\n")
end