Skip to main content
Glama

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
NameRequiredDescriptionDefault
dateYes
meal_nameYes
food_idYes
food_nameYes
serving_quantityYes
serving_unitYes
caloriesYes
protein_gYes
carbs_gYes
fats_gYes
fiber_gYes
brand_nameNo
gramsNo
notesNo
item_idNo

Implementation Reference

  • 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}

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/JohnZolton/MCP-logger'

If you have feedback or need assistance with the MCP directory API, please join our Discord server