sum_last_n_topups
Calculate the total amount of the last n successful airtime top-ups using Africa's Talking Airtime MCP. Defaults to the last 3 transactions for quick insights.
Instructions
Calculate the sum of the last n successful top-ups, defaulting to 3.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| n | No |
Implementation Reference
- main.py:217-260 (handler)The handler function for the 'sum_last_n_topups' tool, decorated with @mcp.tool() for registration. It queries the database for the last n transactions, checks if they share the same currency, and returns their total sum if valid.@mcp.tool() async def sum_last_n_topups(n: int = 3) -> str: """Calculates the sum of the last 'n' successful top-ups. This tool retrieves the last 'n' transactions from the database and calculates their total sum. It ensures that all transactions are in the same currency before summing. Args: n (int, optional): The number of recent top-ups to sum. Defaults to 3. Returns: str: The total sum of the last 'n' top-ups or an error message if the currencies are mixed or no transactions are found. """ if n <= 0: return "Please provide the number of top-ups whose total you need." try: with sqlite3.connect(DB_PATH) as conn: cursor = conn.cursor() cursor.execute( """ SELECT amount, currency_code FROM transactions ORDER BY transaction_time DESC LIMIT ? """, (n,), ) rows = cursor.fetchall() if not rows: return "No successful top-ups found." currencies = set(row[1] for row in rows) if len(currencies) > 1: return "Cannot sum amounts with different currencies." total = sum(amount for (amount, _) in rows) currency = rows[0][1] return f"Sum of last {n} successful top-ups:\n- {currency} {total:.2f}" except Exception as e: return f"Error calculating sum: {str(e)}"