Class: AdminSuite::Mcp::Tools::Aggregate

Inherits:
MCP::Tool
  • Object
show all
Defined in:
lib/admin_suite/mcp/tools/aggregate.rb

Class Method Summary collapse

Class Method Details

.call(resource:, server_context:, q: nil, filters: {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/admin_suite/mcp/tools/aggregate.rb', line 18

def self.call(resource:, server_context:, q: nil, filters: {})
  AdminSuite::Mcp.instrument(tool: "aggregate", resource: resource, actor: server_context[:actor], request: server_context[:request], filters: filters, q: q) do
    allowed = false
    config = Authorization.authorize_resource!(name: resource, actor: server_context[:actor], action: :read, request: server_context[:request])
    next [Authorization.denied_response, nil, false] if config.nil?

    allowed = true
    params = (filters || {}).merge(search: q).compact
    scope = AdminSuite::Query.new(
      resource_config: config,
      params: params,
      max_page_size: AdminSuite.config.mcp.max_page_size
    ).scope
    payload = { resource: resource, count: scope.count, stats: stats_for(config, scope) }
    response = ::MCP::Tool::Response.new([{ type: "text", text: Serializer.dump(payload) }])
    [response, payload[:count], true]
  rescue StandardError => e
    Rails.logger&.warn("AdminSuite MCP aggregate failed: #{e.class}: #{e.message}")
    [Authorization.error_response("aggregate failed"), nil, allowed]
  end
end

.stats_for(config, scope) ⇒ Object



40
41
42
43
44
# File 'lib/admin_suite/mcp/tools/aggregate.rb', line 40

def self.stats_for(config, scope)
  Array(config.index_config&.stats_list).each_with_object({}) do |stat, stats|
    stats[stat.name] = calculate(stat, scope)
  end
end