add_expense
Record new expenses by specifying amount, category, and optional description to track spending in the Expense Tracker MCP Server.
Instructions
Add a new expense
Args: amount: Expense amount category: Expense category (e.g., 'food', 'transport', 'utilities') description: Optional description
Returns: ID of the added expense
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| amount | Yes | ||
| category | Yes | ||
| description | No |
Implementation Reference
- main.py:36-36 (registration)The @mcp.tool decorator registers the add_expense function as an MCP tool.@mcp.tool
- main.py:37-57 (handler)The core handler for the 'add_expense' tool: validates inputs via type hints, initializes DB, inserts the expense record into SQLite with current date, commits, and returns the new expense ID.async def add_expense(amount: float, category: str, description: str = "") -> int: """ Add a new expense Args: amount: Expense amount category: Expense category (e.g., 'food', 'transport', 'utilities') description: Optional description Returns: ID of the added expense """ await init_db() date = datetime.now().strftime('%Y-%m-%d %H:%M:%S') cursor = conn.cursor() cursor.execute(''' INSERT INTO expenses (amount, category, description, date) VALUES (?, ?, ?, ?) ''', (amount, category, description, date)) conn.commit() return cursor.lastrowid
- main.py:37-48 (schema)The function signature and docstring define the input schema (amount: float, category: str, optional description: str) and output (int ID). Used by MCP for tool schema.async def add_expense(amount: float, category: str, description: str = "") -> int: """ Add a new expense Args: amount: Expense amount category: Expense category (e.g., 'food', 'transport', 'utilities') description: Optional description Returns: ID of the added expense """
- main.py:15-34 (helper)Helper function called by add_expense to initialize the SQLite connection and create the expenses table if needed.async def init_db(): """Initialize SQLite database for expenses""" global conn if conn is not None: return conn = sqlite3.connect(DB_FILE) conn.row_factory = sqlite3.Row cursor = conn.cursor() cursor.execute(''' CREATE TABLE IF NOT EXISTS expenses ( id INTEGER PRIMARY KEY AUTOINCREMENT, amount REAL NOT NULL, category TEXT NOT NULL, description TEXT, date TEXT NOT NULL ) ''') conn.commit()