From eeeb1c5b34c058f2c1a7ea2f3c6b179f503d2aa4 Mon Sep 17 00:00:00 2001 From: Tom Flux Date: Tue, 23 Jun 2026 21:24:09 +0100 Subject: Fix bulk-pantry-add crash on null unit (found in live MCP testing) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit End-to-end testing (MCP client -> FastMCP -> Django) surfaced a 500: adding an item with an explicit `unit: null` (as add_to_pantry sends for items without a unit) hit a NOT NULL violation, because `item_data.get("unit", "items")` returns None when the key is present. - views.bulk_pantry_add: `item_data.get("unit") or "items"` — tolerate null/empty unit. - mcp_server add_to_pantry: omit quantity/unit from the payload when unset, so the API applies its own defaults. - test: bulk-add with unit/quantity = null returns 201 (25 pass). Co-Authored-By: Claude Opus 4.8 --- mcp_server/server.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'mcp_server/server.py') diff --git a/mcp_server/server.py b/mcp_server/server.py index cc8fe4a..dcc960b 100644 --- a/mcp_server/server.py +++ b/mcp_server/server.py @@ -73,15 +73,18 @@ def add_to_pantry(items: list[dict]) -> dict: """Add items the user bought. Each item is {name, location?, quantity?, unit?} — e.g. {"name": "pork mince", "location": "fridge"}. Existing items are restocked (set back to In) rather than duplicated.""" - payload = [ - { + payload = [] + for it in items: + entry = { "ingredient_name": it.get("name") or it.get("ingredient_name"), "location": it.get("location", "fridge"), - "quantity": it.get("quantity"), - "unit": it.get("unit"), } - for it in items - ] + # Only forward optional fields when set, so the API applies its defaults. + if it.get("quantity") is not None: + entry["quantity"] = it["quantity"] + if it.get("unit"): + entry["unit"] = it["unit"] + payload.append(entry) try: return _api.bulk_add(payload) except FoodApiError as e: -- cgit v1.2.3