Class: Profiler::MCP::Tools::ExplainQuery

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

Class Method Summary collapse

Class Method Details

.call(params) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/profiler/mcp/tools/explain_query.rb', line 11

def self.call(params)
  token       = params["token"]
  query_index = params["query_index"]

  unless token
    return [{ type: "text", text: "Error: token parameter is required" }]
  end

  if query_index.nil?
    return [{ type: "text", text: "Error: query_index parameter is required" }]
  end

  if (proxy = MCP::SlaveSupport.with_slave_proxy(params))
    result = proxy.post_json("/_profiler/api/explain", { token: token, query_index: query_index.to_i })
    text = result["error"] ? "Error: #{result["error"]}" : result.inspect
    return [{ type: "text", text: text }]
  end

  unless Profiler.configuration.enabled
    return [{ type: "text", text: "Error: EXPLAIN is only available when the profiler is enabled" }]
  end

  data = Profiler::ExplainRunner.run(token, query_index.to_i)

  lines = []
  lines << "# EXPLAIN ANALYZE — Query ##{query_index}"
  lines << "Adapter: `#{data[:adapter]}`\n"

  if data[:format] == "json"
    lines << "```json"
    lines << JSON.pretty_generate(data[:result])
    lines << "```"
  else
    lines << "```"
    lines << data[:result].to_s
    lines << "```"
  end

  [{ type: "text", text: lines.join("\n") }]
rescue ArgumentError => e
  [{ type: "text", text: "Error: #{e.message}" }]
rescue => e
  [{ type: "text", text: "EXPLAIN failed: #{e.message}" }]
end