Repository navigation
fix(mcp): type mixed-type body params as real unions instead of str - #139
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
metrics_query_meta_adsreturns 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 typedstr. Our own validation rejectedtime_increment=1, and"1"reached the API as the string"1". The API body schema isunion([enum, int])with no coercion, so it answered 400. Onlymonthly/all_daysworked, so no agent could get daily or N-day rows.ad_insights_create_ad_insights_reporthad the same bug.advantage_audiencewas rejected by our own validation when an agent senttrue(string_type): 8 users in 2 weeks, onad_campaigns_create_standalone_adandmessaging_ads_create_messaging_ad.flow_jsonandmailer_contact(object or string) rejected an object. No failures seen yet.Root cause
scripts/generate_mcp_tools.pyturned every mixed-typeanyOf/oneOfintostr.Fix
Each branch of a mixed
anyOf/oneOfnow maps to its own Python type, and the param becomes their union:int, orLiteral[0, 1]for an integer enumfloatbooldict[str, Any]list[...]str, or a mergedLiteral[...]for string enums| NoneMembers 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 (
Truestays a bool,1an int, a dict a dict), and lax mode still turns"7"into7. If any branch has no concrete JSON type, the param keeps thestrfallback.This applies to JSON body params only. Query and path params keep
str:strparam forwards the agent's text unchanged.bool | int,"1"would becomeTrue, and httpx would sendp=trueinstead ofp=1.Changed parameters (8 params, 7 tools)
metrics_query_meta_adstime_incrementstr | Noneint | Literal["monthly", "all_days"] | Nonead_insights_create_ad_insights_reporttime_incrementstr | Noneint | Literal["monthly", "all_days"] | Nonead_campaigns_create_standalone_adadvantage_audiencestr | Nonebool | Literal[0, 1] | Nonemessaging_ads_create_messaging_adadvantage_audiencestr | Nonebool | Literal[0, 1] | Nonemessaging_ads_create_call_adadvantage_audiencestr | Nonebool | Literal[0, 1] | Nonemessaging_ads_create_ctwa_adadvantage_audiencestr | Nonebool | Literal[0, 1] | Nonegmb_verifications_start_google_business_verificationmailer_contactstr | Nonedict[str, Any] | str | Nonewhatsapp_flows_upload_whats_app_flow_jsonflow_json(required)strdict[str, Any] | stradvantageAudienceusesLiteral[0, 1]because the spec declaresenum: [0, 1], and the API accepts exactly0 | 1 | booleanand rejects2. Regenerating gives only these 8 lines ingenerated_tools.py.Before / after (JSON body sent)
metrics_query_meta_ads(time_increment=1)"timeIncrement": 1metrics_query_meta_ads(time_increment="7")"timeIncrement": "7", API 400"timeIncrement": 7metrics_query_meta_ads(time_increment="monthly")"timeIncrement": "monthly"metrics_query_meta_ads(time_increment="weekly")ad_campaigns_create_standalone_ad(advantage_audience=true)"advantageAudience": truead_campaigns_create_standalone_ad(advantage_audience=1)"advantageAudience": 1whatsapp_flows_upload_whats_app_flow_json(flow_json={"version": "6.0"})"flow_json": {"version": "6.0"}whatsapp_flows_upload_whats_app_flow_json(flow_json="{\"version\": \"6.0\"}")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 bothtime_incrementtools through the FastMCPClient, withrespxcapturing the JSON body.1,90,"1"and"7"arrive as JSON numbers;monthly/all_daysarrive unchanged.weekly,"",1.5andsevenare rejected before the API.tests/test_mcp_mixed_union_params.py, end to end:advantage_audiencetrue/false/1/0/"true"/"1"on the standalone ad creator, andtrue/1on all three messaging ad creators;2,-1and"maybe"are rejected with nothing sent;flow_jsonas an object and as a string, andmailer_contactas an object;tests/test_mcp_nullable_params.py, generator unit cases:int | str,bool | int | float,list[str] | str, merged string enums and$refbranches;| None, and member order doesn't depend on the spec;strfallback, and query/path unions keepstr.mypy src: no issues.ruff check: clean.Open points
time_incrementis not enforced by our validation. The generator has noAnnotated/Fieldpattern, so0and91still reach the API. Its body schema has no bounds either, so Meta would be the one to reject them.setContactFieldValue.valueis untyped and left out. Its schema has no type, so the tool still types it asstr, while the API stores any JSON value. Typing it asAnyis a separate decision.🤖 Generated with Claude Code