Skip to content

fix(openapi): preserve anyOf unions in MCP tool input schemas - #316

Open
syf2211 wants to merge 1 commit into
tadata-org:mainfrom
syf2211:fix/preserve-anyof-union-schemas
Open

fix(openapi): preserve anyOf unions in MCP tool input schemas#316
syf2211 wants to merge 1 commit into
tadata-org:mainfrom
syf2211:fix/preserve-anyof-union-schemas

Conversation

@syf2211

@syf2211 syf2211 commented Jun 28, 2026

Copy link
Copy Markdown

Summary

Stop injecting a single top-level "type" into MCP tool parameter schemas when anyOf is already present. This fixes non-null union types (e.g. dict | list) being collapsed to one arbitrary type and rejected by the MCP server's JSON Schema validator.

Motivation

For request body fields typed as T | U where both types are non-null, convert_openapi_to_mcp_tools was adding a top-level "type" picked arbitrarily from the union. The MCP jsonschema validator then rejected inputs matching the other branch with misleading errors like is not of type 'array'.

Fixes #307

Changes

File Change
fastapi_mcp/openapi/convert.py Only inject "type" when neither "type" nor "anyOf" is present (path, query, and body params)
tests/test_openapi_conversion.py Add regression test for dict | list union; update optional-param assertions to expect preserved anyOf

Tests

  • uv run pytest tests/test_openapi_conversion.py --no-cov -q11 passed
  • Composer-2.5 review: APPROVE after test updates

Notes


View with Codesmith Autofix with Codesmith
Need help on this PR? Tag /codesmith with what you need. Autofix is disabled.

Skip injecting a single top-level type when anyOf is already present so
non-null unions like dict | list are not collapsed to one arbitrary type.

Fixes tadata-org#307
@K4bain

K4bain commented Aug 27, 2026

Copy link
Copy Markdown

@syf2211 — heads up: this PR and #308 address the same root cause (sibling type injected next to anyOf). I adopted the #308 variant as #337 since it extends the guard to oneOf/allOf as well — your analysis and test expectations were a great cross-check and you are credited in the thread there. Either way the bug you found is real and hopefully one of these lands soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants