Skip to content

fix(mcp): type mixed-type body params as real unions instead of str - #139

Merged
Ooscaar merged 3 commits into
developfrom
mcp-watch/time-increment-type
Nov 27, 2026
Merged

Ooscaar merged 3 commits into
developfrom
mcp-watch/time-increment-type

Conversation

@Ooscaar

@Ooscaar Ooscaar commented Nov 27, 2026

Copy link
Copy Markdown
Member

Problem

  • metrics_query_meta_ads returns 400: 565 errors from 194 users in 7 days, about 10% of calls on every release since Nov 20. time_increment ("days per row, 1 to 90, or monthly / all_days") was typed str. Our own validation rejected time_increment=1, and "1" reached the API as the string "1". The API body schema is union([enum, int]) with no coercion, so it answered 400. Only monthly / all_days worked, so no agent could get daily or N-day rows. ad_insights_create_ad_insights_report had the same bug.
  • advantage_audience was rejected by our own validation when an agent sent true (string_type): 8 users in 2 weeks, on ad_campaigns_create_standalone_ad and messaging_ads_create_messaging_ad.
  • flow_json and mailer_contact (object or string) rejected an object. No failures seen yet.

Root cause

scripts/generate_mcp_tools.py turned every mixed-type anyOf / oneOf into str.

Fix

Each branch of a mixed anyOf / oneOf now maps to its own Python type, and the param becomes their union:

JSON branch Python member
integer int, or Literal[0, 1] for an integer enum
number float
boolean bool
object dict[str, Any]
array list[...]
string str, or a merged Literal[...] for string enums
null | None

Members always come in the same order (bool, int, float, dict, list, str), whatever order the spec uses. Pydantic's smart mode keeps an exact type match (True stays a bool, 1 an int, a dict a dict), and lax mode still turns "7" into 7. If any branch has no concrete JSON type, the param keeps the str fallback.

This applies to JSON body params only. Query and path params keep str:

  • Query values reach the API as text it already coerces, and a str param forwards the agent's text unchanged.
  • A union would let lax mode rewrite calls that work today. Under bool | int, "1" would become True, and httpx would send p=true instead of p=1.
  • No query or path param in today's spec has a mixed type, so nothing changes for them.

Changed parameters (8 params, 7 tools)

Tool Param Old New
metrics_query_meta_ads time_increment str | None int | Literal["monthly", "all_days"] | None
ad_insights_create_ad_insights_report time_increment str | None int | Literal["monthly", "all_days"] | None
ad_campaigns_create_standalone_ad advantage_audience str | None bool | Literal[0, 1] | None
messaging_ads_create_messaging_ad advantage_audience str | None bool | Literal[0, 1] | None
messaging_ads_create_call_ad advantage_audience str | None bool | Literal[0, 1] | None
messaging_ads_create_ctwa_ad advantage_audience str | None bool | Literal[0, 1] | None
gmb_verifications_start_google_business_verification mailer_contact str | None dict[str, Any] | str | None
whatsapp_flows_upload_whats_app_flow_json flow_json (required) str dict[str, Any] | str

advantageAudience uses Literal[0, 1] because the spec declares enum: [0, 1], and the API accepts exactly 0 | 1 | boolean and rejects 2. Regenerating gives only these 8 lines in generated_tools.py.

Before / after (JSON body sent)

Call Before After
metrics_query_meta_ads(time_increment=1) rejected by the tool "timeIncrement": 1
metrics_query_meta_ads(time_increment="7") "timeIncrement": "7", API 400 "timeIncrement": 7
metrics_query_meta_ads(time_increment="monthly") "timeIncrement": "monthly" unchanged
metrics_query_meta_ads(time_increment="weekly") sent, API 400 rejected by the tool, nothing sent
ad_campaigns_create_standalone_ad(advantage_audience=true) rejected by the tool "advantageAudience": true
ad_campaigns_create_standalone_ad(advantage_audience=1) rejected by the tool "advantageAudience": 1
whatsapp_flows_upload_whats_app_flow_json(flow_json={"version": "6.0"}) rejected by the tool "flow_json": {"version": "6.0"}
whatsapp_flows_upload_whats_app_flow_json(flow_json="{\"version\": \"6.0\"}") sent unchanged unchanged

The advertised schema now lists every type, for example time_increment: {"anyOf": [{"type": "integer"}, {"type": "string", "enum": ["monthly", "all_days"]}, {"type": "null"}]}.

Tests

  • tests/test_mcp_time_increment.py: calls both time_increment tools through the FastMCP Client, with respx capturing the JSON body.
    • 1, 90, "1" and "7" arrive as JSON numbers; monthly / all_days arrive unchanged.
    • weekly, "", 1.5 and seven are rejected before the API.
    • The advertised schema lists both integer and the enum.
  • tests/test_mcp_mixed_union_params.py, end to end:
    • advantage_audience true / false / 1 / 0 / "true" / "1" on the standalone ad creator, and true / 1 on all three messaging ad creators;
    • 2, -1 and "maybe" are rejected with nothing sent;
    • flow_json as an object and as a string, and mailer_contact as an object;
    • the advertised schema lists both types for all 6 of these params.
  • tests/test_mcp_nullable_params.py, generator unit cases:
    • the three real shapes, plus int | str, bool | int | float, list[str] | str, merged string enums and $ref branches;
    • required params get no | None, and member order doesn't depend on the spec;
    • an untyped branch keeps the str fallback, and query/path unions keep str.
  • Against the old generated file, the end-to-end tests fail 16/22 and 14/17.
  • Full suite: 3928 passed, 44 skipped. mypy src: no issues. ruff check: clean.

Open points

  • The 1–90 bound on time_increment is not enforced by our validation. The generator has no Annotated / Field pattern, so 0 and 91 still reach the API. Its body schema has no bounds either, so Meta would be the one to reject them.
  • setContactFieldValue.value is untyped and left out. Its schema has no type, so the tool still types it as str, while the API stores any JSON value. Typing it as Any is a separate decision.

🤖 Generated with Claude Code

Ooscaar and others added 3 commits November 27, 2026 17:24
The generator turned every mixed-type anyOf into `str`. For the Meta
`timeIncrement` body field (1-90 days, or monthly / all_days) that broke
daily and N-day rows: the tool rejected `7`, and forwarded `"7"` as a
string, which the API body schema (union of enum and int, no coercion)
answered with a 400.

An anyOf/oneOf of one integer and string enums now becomes
`int | Literal[...]`; pydantic's lax mode still turns "7" into 7, so the
SDK sends a JSON number. Every other mixed union keeps the str fallback.
Regenerated: only metrics_query_meta_ads and
ad_insights_create_ad_insights_report change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…sent

Two more mixed body unions fell back to `str`:

- advantageAudience (true/false or 0/1) on the standalone, messaging,
  call and CTWA ad creators: the tool rejected `true`.
- flow_json and mailerContact (object or string): the tool rejected an
  object.

boolean + integer now becomes `bool | int` (`bool | Literal[0, 1]` for an
integer enum), and object + string becomes `dict[str, Any] | str`. Other
mixed unions keep the str fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ches

Replace the three special cases (integer + string enum, boolean +
integer, object + string) with one rule: each branch maps to its own
Python type (bool, int or an int Literal, float, dict, list[...], str or
a string Literal) and the param becomes their union, ordered so
Pydantic's smart mode keeps an exact match and coerces "7" to 7 before
trying str. A branch with no concrete JSON type keeps the str fallback.

JSON body fields only. Query and path values reach the API as text it
coerces, and a union would let lax mode rewrite working calls ("1" ->
True -> "true"); none are mixed in today's spec.

The regenerated tools are unchanged from the special-case version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ooscaar
Ooscaar merged commit 7aeb28f into develop Nov 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant