From 141b84bae12e7ebc306b15ef4b87ca1050c5c6b4 Mon Sep 17 00:00:00 2001 From: Honey Date: Fri, 31 Jul 2026 09:46:26 +0200 Subject: [PATCH] Read patched recipes by stable id Co-authored-by: Bumble Co-authored-by: fredamn76 Signed-off-by: fredamn76 --- mealie_mcp/server.py | 8 ++++++-- tests/test_server_contracts.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/mealie_mcp/server.py b/mealie_mcp/server.py index 2617136..7b76c3f 100644 --- a/mealie_mcp/server.py +++ b/mealie_mcp/server.py @@ -329,10 +329,14 @@ def patch_recipe(slug_or_id: str, patch: dict[str, Any]) -> dict[str, Any]: """Apply an explicit Mealie recipe patch, then read the recipe back. Use this for tags and categories too — the bulk-action endpoints return 500 on - this instance. Patch ``recipeCategory`` and ``tags`` directly. + this instance. Patch ``recipeCategory`` and ``tags`` directly. The stable + recipe id is captured before the PATCH because changing ``name`` also changes + Mealie's slug, making the old slug invalid for read-back. """ + before = get_recipe(slug_or_id) + stable_id = before.get("id") or slug_or_id _request("PATCH", f"/api/recipes/{slug_or_id}", json=patch) - return get_recipe(slug_or_id) + return get_recipe(stable_id) @mcp.tool() diff --git a/tests/test_server_contracts.py b/tests/test_server_contracts.py index a9210f8..b3f59b3 100644 --- a/tests/test_server_contracts.py +++ b/tests/test_server_contracts.py @@ -62,14 +62,24 @@ class TestUrlImportResponse: class TestPatchContract: def test_patch_recipe_reads_back_the_final_object(self, monkeypatch): seen = [] - updated = {"slug": "soppa", "name": "Soppa", "tags": [{"name": "📅 Vardag"}]} + before = {"id": "stable-id", "slug": "soppa", "name": "Soppa"} + updated = { + "id": "stable-id", + "slug": "ny-soppa", + "name": "Ny soppa", + "tags": [{"name": "📅 Vardag"}], + } def request(method, path, **kwargs): seen.append((method, path, kwargs)) return None monkeypatch.setattr(server, "_request", request) - monkeypatch.setattr(server, "get_recipe", lambda slug: updated if slug == "soppa" else None) + monkeypatch.setattr( + server, + "get_recipe", + lambda key: before if key == "soppa" else updated if key == "stable-id" else None, + ) assert server.patch_recipe("soppa", {"tags": updated["tags"]}) == updated assert seen == [("PATCH", "/api/recipes/soppa", {"json": {"tags": updated["tags"]}})]