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
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.

[BUG] Union types of two real types (non-null) rejected at MCP-server jsonschema validator with misleading "is not of type 'X'"

1 participant