add_expense
Record an expense against a trip budget by specifying trip, category, amount, and description.
Instructions
Record an expense against a trip budget.
Args: trip_id: The trip identifier used when creating the budget category: Expense category — flights, hotels, food, activities, transport, shopping, or misc amount: Amount spent description: Brief description of the expense (e.g. "ANA flight JFK→NRT")
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| trip_id | Yes | ||
| category | Yes | ||
| amount | Yes | ||
| description | Yes |
Implementation Reference
- src/travel_mcp/tools/budget.py:36-41 (handler)Core implementation of add_expense: validates trip_id exists, appends expense to in-memory budget record, and returns the updated budget summary via get_summary().
def add_expense(trip_id: str, category: str, amount: float, description: str) -> dict: if trip_id not in _budgets: return {"error": f"No budget found for trip_id '{trip_id}'. Use create_trip_budget first."} cat = category if category in CATEGORIES else "misc" _budgets[trip_id].expenses.append(Expense(category=cat, amount=amount, description=description)) return get_summary(trip_id) - src/travel_mcp/server.py:136-152 (registration)Registers add_expense as an MCP tool via @mcp.tool() decorator, with typed parameters (trip_id, category as Literal of valid categories, amount, description) and delegates to budget.add_expense().
@mcp.tool() def add_expense( trip_id: str, category: Literal["flights", "hotels", "food", "activities", "transport", "shopping", "misc"], amount: float, description: str, ) -> dict: """ Record an expense against a trip budget. Args: trip_id: The trip identifier used when creating the budget category: Expense category — flights, hotels, food, activities, transport, shopping, or misc amount: Amount spent description: Brief description of the expense (e.g. "ANA flight JFK→NRT") """ return budget.add_expense(trip_id, category, amount, description) - src/travel_mcp/server.py:222-226 (registration)Registers add_expense as an HTTP API endpoint via @mcp.custom_route('/api/tools/add_expense', methods=['POST']), parsing JSON body and delegating to budget.add_expense().
@mcp.custom_route("/api/tools/add_expense", methods=["POST"]) async def api_add_expense(request: Request) -> JSONResponse: body = await request.json() result = budget.add_expense(body["trip_id"], body["category"], body["amount"], body["description"]) return JSONResponse(result) - src/travel_mcp/tools/budget.py:1-19 (schema)Defines the Expense and BudgetRecord Pydantic models (schemas) used internally by add_expense, along with the CATEGORIES constant.
from __future__ import annotations from pydantic import BaseModel CATEGORIES = ["flights", "hotels", "food", "activities", "transport", "shopping", "misc"] class Expense(BaseModel): category: str amount: float description: str class BudgetRecord(BaseModel): trip_id: str total_budget: float currency: str expenses: list[Expense] = [] - Itinerary planning references add_expense in a docstring/note, instructing users to use trip_id with add_expense and get_budget_summary.
budget_module.create(trip_id, total_budget) result["budget"] = { "trip_id": trip_id, "total_budget": total_budget, "currency": "USD", "suggested_allocation": allocation, "note": f"Use trip_id '{trip_id}' with add_expense and get_budget_summary to track spending.", }