add_expense
Record new expense entries in the database by specifying date, amount, and category. Use this tool to track spending and maintain financial records.
Instructions
Add a new expense entry to the database.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | Yes | ||
| amount | Yes | ||
| category | Yes | ||
| subcategory | No | ||
| note | No |
Implementation Reference
- main.py:41-57 (handler)The handler function decorated with @mcp.tool() that implements the logic to insert a new expense into the SQLite database asynchronously using aiosqlite. It takes date, amount, category, optional subcategory and note, and returns success or error status.@mcp.tool() async def add_expense(date, amount, category, subcategory="", note=""): # Changed: added async '''Add a new expense entry to the database.''' try: async with aiosqlite.connect(DB_PATH) as c: # Changed: added async cur = await c.execute( # Changed: added await "INSERT INTO expenses(date, amount, category, subcategory, note) VALUES (?,?,?,?,?)", (date, amount, category, subcategory, note) ) expense_id = cur.lastrowid await c.commit() # Changed: added await return {"status": "success", "id": expense_id, "message": "Expense added successfully"} except Exception as e: # Changed: simplified exception handling if "readonly" in str(e).lower(): return {"status": "error", "message": "Database is in read-only mode. Check file permissions."} return {"status": "error", "message": f"Database error: {str(e)}"}