Class: Mathpix::MCP::Tools::GetUsageTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/mathpix/mcp/tools/get_usage_tool.rb

Overview

Get Usage Tool

Retrieves API usage statistics and account limits Thin delegate to Mathpix::Client (usage endpoint)

Class Method Summary collapse

Class Method Details

.call(server_context:, from_date: nil, detailed: false) ⇒ Object



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
55
56
57
# File 'lib/mathpix/mcp/tools/get_usage_tool.rb', line 30

def self.call(server_context:, from_date: nil, detailed: false)
  safe_execute do
    client = mathpix_client(server_context)

    # Mathpix exposes usage at /v3/ocr-usage; there is no /v3/usage.
    params = {}
    params[:from_date] = from_date if from_date && !from_date.empty?
    usage_data = client.get('/ocr-usage', params: params)

    rows = usage_data['ocr_usage'] || []
    by_type = rows.each_with_object(Hash.new(0)) do |row, acc|
      acc[row['usage_type']] += row['count'] || 0
    end

    response_data = {
      success: true,
      usage: {
        app_id: rows.first&.fetch('app_id', nil),
        group_id: rows.first&.fetch('group_id', nil),
        total_requests: rows.sum { |row| row['count'] || 0 },
        by_usage_type: by_type
      }
    }
    response_data[:rows] = rows if detailed

    json_response(response_data)
  end
end