Class: RailsVitals::MCP::Tools::GetSlowQueries

Inherits:
Base
  • Object
show all
Defined in:
lib/rails_vitals/mcp/tools/get_slow_queries.rb

Constant Summary collapse

TOOL_NAME =
"railsvitals_get_slow_queries"
DESCRIPTION =
<<~DESC.strip
  Returns individual slow queries detected across recent requests, ordered by
  duration descending. Each result includes the SQL, execution time, and the
  endpoint that fired it. Use threshold_ms to override the default slow query
  threshold (configured via mcp_slow_query_threshold_ms, default 100ms).
  Use this after railsvitals_get_score to find which specific queries are
  dragging down database performance.
DESC
INPUT_SCHEMA =
{
  type: "object",
  properties: {
    threshold_ms: {
      type: "integer",
      description: "Minimum query duration in ms to include. Defaults to config.mcp_slow_query_threshold_ms (100ms)."
    },
    limit: {
      type: "integer",
      description: "Maximum number of queries to return, ordered by duration desc. Defaults to 10."
    }
  }
}.freeze
DEFAULT_LIMIT =
10

Instance Method Summary collapse

Methods inherited from Base

definition, tool_name

Instance Method Details

#call(params) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rails_vitals/mcp/tools/get_slow_queries.rb', line 32

def call(params)
  records = RailsVitals.store.all
  return no_data_response if records.empty?

  threshold = (params[:threshold_ms] || params["threshold_ms"] || RailsVitals.config.mcp_slow_query_threshold_ms).to_i
  limit = (params[:limit] || params["limit"] || DEFAULT_LIMIT).to_i

  slow = collect_slow_queries(records, threshold)
  return no_slow_queries_response(threshold) if slow.empty?

  {
    total_slow_queries: slow.size,
    shown: [ limit, slow.size ].min,
    threshold_ms: threshold,
    queries: slow.first(limit).map { |q| serialize(q) }
  }
end