Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion scripts/generate_mcp_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/late/mcp/generated_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -7227,6 +7227,7 @@ def ad_campaigns_list_ad_campaigns(
"completed",
"cancelled",
"error",
"not_deleted",
]
| None = None,
ad_account_id: str | None = None,
Expand Down Expand Up @@ -8950,6 +8951,7 @@ def ad_campaigns_get_ad_tree(
"completed",
"cancelled",
"error",
"not_deleted",
]
| None = None,
ad_account_id: str | None = None,
Expand Down
24 changes: 24 additions & 0 deletions tests/test_mcp_nullable_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading