From 70741ebdce618ce767345a92bcd0733d9c4c6a4e Mon Sep 17 00:00:00 2001 From: Oscar Perez Date: Thu, 19 Nov 2026 15:21:53 +0100 Subject: [PATCH] fix(mcp): a string anyOf/oneOf keeps every branch's enum values _resolve_schema returned the first string branch of a union, so `status: anyOf [$ref AdStatus, {enum: [not_deleted]}]` on listAdCampaigns and getAdTree became a Literal without `not_deleted`. FastMCP rejected the value the tools' own docs recommend, before any API call (ValidationError in zernio-mcp logs). String branches now merge their enums; a branch with no enum makes the param plain str. Co-Authored-By: Claude Opus 5.5 --- scripts/generate_mcp_tools.py | 13 ++++++++++++- src/late/mcp/generated_tools.py | 2 ++ tests/test_mcp_nullable_params.py | 24 ++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/scripts/generate_mcp_tools.py b/scripts/generate_mcp_tools.py index 47091bf2..d8accf14 100644 --- a/scripts/generate_mcp_tools.py +++ b/scripts/generate_mcp_tools.py @@ -153,7 +153,8 @@ def _resolve_schema(schema: dict[str, Any], spec: dict[str, Any] | None) -> dict `type` (we mostly see single-element allOf as a "ref + description" pattern, but this generalises safely). - `{"oneOf"|"anyOf": [...]}` -> if every non-null branch resolves to the - same scalar/object/array `type`, return that branch; otherwise leave the + same scalar/object/array `type`, return that branch (string branches + merge their enums, or drop the enum if any branch has none); otherwise leave the schema alone so the caller falls back to `str`. A deliberately over-broad type is worse than no type for the LLM. @@ -187,6 +188,16 @@ def _resolve_schema(schema: dict[str, Any], spec: dict[str, Any] | None) -> dict types.discard("null") if len(types) == 1: only = types.pop() + if only == "string": + # `anyOf: [$ref AdStatus, {type: string, enum: [not_deleted]}]` + # accepts every branch's values, so merge the enums. One + # branch without an enum means any string goes: an + # over-narrow Literal rejects valid calls before the API. + strings = [b for b in resolved_branches if b.get("type") == "string"] + if all(b.get("enum") for b in strings): + merged = list(dict.fromkeys(v for b in strings for v in b["enum"])) + return {**strings[0], "enum": merged} + return {"type": "string"} for b in resolved_branches: if b.get("type") == only: return b diff --git a/src/late/mcp/generated_tools.py b/src/late/mcp/generated_tools.py index e5487f0f..a5fc6487 100644 --- a/src/late/mcp/generated_tools.py +++ b/src/late/mcp/generated_tools.py @@ -7227,6 +7227,7 @@ def ad_campaigns_list_ad_campaigns( "completed", "cancelled", "error", + "not_deleted", ] | None = None, ad_account_id: str | None = None, @@ -8950,6 +8951,7 @@ def ad_campaigns_get_ad_tree( "completed", "cancelled", "error", + "not_deleted", ] | None = None, ad_account_id: str | None = None, diff --git a/tests/test_mcp_nullable_params.py b/tests/test_mcp_nullable_params.py index 9989de51..b880d5ad 100644 --- a/tests/test_mcp_nullable_params.py +++ b/tests/test_mcp_nullable_params.py @@ -36,3 +36,27 @@ def test_nullable_param_keeps_its_type(generator, schema, expected): def test_a_real_union_still_falls_back_to_str(generator): schema = {"oneOf": [{"type": "string"}, {"type": "integer"}, {"type": "null"}]} assert generator.get_python_type(schema, required=False, spec=SPEC)[0] == "str | None" + + +AD_STATUS_SPEC = { + "components": { + "schemas": { + "AdStatus": { + "type": "string", + "enum": ["active", "paused", "pending_review", "rejected", "completed", "cancelled", "error"], + }, + } + } +} + + +def test_string_union_keeps_every_branch_enum(generator): + schema = {"anyOf": [{"$ref": "#/components/schemas/AdStatus"}, {"type": "string", "enum": ["not_deleted"]}]} + assert generator.get_python_type(schema, required=False, spec=AD_STATUS_SPEC)[0] == ( + "Literal['active', 'paused', 'pending_review', 'rejected', 'completed', 'cancelled', 'error', 'not_deleted'] | None" + ) + + +def test_string_union_with_an_open_branch_is_plain_str(generator): + schema = {"oneOf": [{"$ref": "#/components/schemas/AdStatus"}, {"type": "string"}]} + assert generator.get_python_type(schema, required=False, spec=AD_STATUS_SPEC)[0] == "str | None"