upsert_meal
Create or update a meal entry within a nutrition day for fitness tracking, enabling food logging and nutrition management in the MCP Logger server.
Instructions
Create or update a meal within a nutrition day.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | Yes | ||
| name | Yes | ||
| order_index | No |
Implementation Reference
- src/main.py:396-419 (handler)Implementation of the upsert_meal tool which creates or updates a meal entry within a specific nutrition day.
def upsert_meal(date: str, name: str, order_index: int = 0) -> dict[str, int]: """Create or update a meal within a nutrition day.""" date = _ensure_date(date) conn = get_connection() cursor = conn.cursor() # Ensure day exists cursor.execute("INSERT OR IGNORE INTO nutrition_days (date) VALUES (?)", (date,)) cursor.execute("SELECT id FROM nutrition_days WHERE date = ?", (date,)) day_id = cursor.fetchone()[0] # Upsert meal cursor.execute( """INSERT INTO meals (day_id, name, order_index) VALUES (?, ?, ?) ON CONFLICT(day_id, name) DO UPDATE SET order_index = ?""", (day_id, name, order_index, order_index), ) meal_id = cursor.lastrowid or cursor.execute( "SELECT id FROM meals WHERE day_id = ? AND name = ?", (day_id, name) ).fetchone()[0] conn.commit() conn.close() return {"meal_id": meal_id}