fix(openapi): preserve anyOf unions in MCP tool input schemas - #316
Open
syf2211 wants to merge 1 commit into
Open
fix(openapi): preserve anyOf unions in MCP tool input schemas#316syf2211 wants to merge 1 commit into
syf2211 wants to merge 1 commit into
Conversation
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
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.
Summary
Stop injecting a single top-level
"type"into MCP tool parameter schemas whenanyOfis 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 | Uwhere both types are non-null,convert_openapi_to_mcp_toolswas adding a top-level"type"picked arbitrarily from the union. The MCP jsonschema validator then rejected inputs matching the other branch with misleading errors likeis not of type 'array'.Fixes #307
Changes
fastapi_mcp/openapi/convert.py"type"when neither"type"nor"anyOf"is present (path, query, and body params)tests/test_openapi_conversion.pydict | listunion; update optional-param assertions to expect preservedanyOfTests
uv run pytest tests/test_openapi_conversion.py --no-cov -q— 11 passedNotes
Optional[T]schemas (anyOf: [T, null]), which is the correct JSON Schema representation and matches MCP validator behavior.Need help on this PR? Tag
/codesmithwith what you need. Autofix is disabled.