add_or_update_meal_item
Add or update food items in meal logs with nutritional data for accurate fitness tracking.
Instructions
Add or update a food item within a meal.
The AI should first use OpenNutrition MCP to find food_id and get macros, then call this tool with the calculated values for the serving quantity.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| date | Yes | ||
| meal_name | Yes | ||
| food_id | Yes | ||
| food_name | Yes | ||
| serving_quantity | Yes | ||
| serving_unit | Yes | ||
| calories | Yes | ||
| protein_g | Yes | ||
| carbs_g | Yes | ||
| fats_g | Yes | ||
| fiber_g | Yes | ||
| brand_name | No | ||
| grams | No | ||
| notes | No | ||
| item_id | No |
Implementation Reference
- src/main.py:422-489 (handler)The tool `add_or_update_meal_item` is implemented as a decorated function in `src/main.py`, which handles both the insertion of new meal items and updating of existing ones by interacting with the database.
@app.tool() def add_or_update_meal_item( date: str, meal_name: str, food_id: str, food_name: str, serving_quantity: float, serving_unit: str, calories: float, protein_g: float, carbs_g: float, fats_g: float, fiber_g: float, brand_name: Optional[str] = None, grams: Optional[float] = None, notes: Optional[str] = None, item_id: Optional[int] = None, ) -> dict[str, int]: """Add or update a food item within a meal. The AI should first use OpenNutrition MCP to find food_id and get macros, then call this tool with the calculated values for the serving quantity. """ date = _ensure_date(date) conn = get_connection() cursor = conn.cursor() # Ensure day and meal exist 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] cursor.execute( "INSERT OR IGNORE INTO meals (day_id, name, order_index) VALUES (?, ?, 0)", (day_id, meal_name), ) cursor.execute("SELECT id FROM meals WHERE day_id = ? AND name = ?", (day_id, meal_name)) meal_id = cursor.fetchone()[0] if item_id: # Update existing item cursor.execute( """UPDATE meal_items SET food_id=?, food_name=?, brand_name=?, serving_quantity=?, serving_unit=?, grams=?, calories=?, protein_g=?, carbs_g=?, fats_g=?, fiber_g=?, notes=? WHERE id=?""", ( food_id, food_name, brand_name, serving_quantity, serving_unit, grams, calories, protein_g, carbs_g, fats_g, fiber_g, notes, item_id, ), ) else: # Insert new item cursor.execute( """INSERT INTO meal_items ( meal_id, food_id, food_name, brand_name, serving_quantity, serving_unit, grams, calories, protein_g, carbs_g, fats_g, fiber_g, notes ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", ( meal_id, food_id, food_name, brand_name, serving_quantity, serving_unit, grams, calories, protein_g, carbs_g, fats_g, fiber_g, notes, ), ) item_id = cursor.lastrowid conn.commit() conn.close() return {"item_id": item_id, "meal_id": meal_id, "day_id": day_id}