update
Modify existing expense records by updating specific fields like amount, category, or date while keeping other details unchanged.
Instructions
Update an expense.Only provided fields will be updated.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| cid | Yes | ||
| date | No | ||
| amount | No | ||
| category | No | ||
| subcategory | No | ||
| note | No |
Implementation Reference
- main.py:82-129 (handler)The handler function for the 'update' tool, decorated with @mcp.tool(), which updates an existing expense by ID with any combination of provided optional fields (date, amount, category, subcategory, note). It dynamically builds the SQL UPDATE query based on provided fields and returns the updated expense or error.def update( cid: int, date: Optional[str] = None, amount: Optional[float] = None, category: Optional[str] = None, subcategory: Optional[str] = None, note: Optional[str] = None ) -> list[dict]: """Update an expense.Only provided fields will be updated.""" with sqlite3.connect(DB_PATH) as c: update_fields=[] params=[] if date is not None: update_fields.append("date=?") params.append(date) if amount is not None: update_fields.append("amount=?") params.append(amount) if category is not None: update_fields.append("category=?") params.append(category) if subcategory is not None: update_fields.append("subcategory=?") params.append(subcategory) if note is not None: update_fields.append("note=?") params.append(note) if not update_fields: return {"No fields provided to update"} params.append(cid) query=f"UPDATE expenses SET {', '.join(update_fields)} WHERE id=?" c.execute(query,params) c.commit() select_query="select * from expenses where id=?" cur=c.execute(select_query,[cid]) cols = [d[0] for d in cur.description] rows = cur.fetchall() if rows: return [dict(zip(cols, r)) for r in rows] else: return {"error": f"No expense found with id {cid}"}