Skip to content

fix(openapi): preserve array items for Optional[List] MCP tool params#314

Open
syf2211 wants to merge 1 commit into
tadata-org:mainfrom
syf2211:fix/304-optional-list-array-items
Open

fix(openapi): preserve array items for Optional[List] MCP tool params#314
syf2211 wants to merge 1 commit into
tadata-org:mainfrom
syf2211:fix/304-optional-list-array-items

Conversation

@syf2211

@syf2211 syf2211 commented Jun 27, 2026

Copy link
Copy Markdown

Summary

Fix invalid MCP tool input schemas for Optional[List[X]] request body and query parameters. These previously exposed {type: "array"} without an items field after OpenAPI anyOf composition was simplified.

Motivation

Fixes #304. MCP clients (VS Code Copilot, Claude Code) reject tool schemas missing items on array-typed parameters with errors like: tool parameters array type must have items.

Changes

  • Add _hoist_array_fields_from_composition() to copy items (and type) from anyOf/oneOf array variants before composition keys are removed in clean_schema_for_display()
  • Apply clean_schema_for_display() to each path/query/body property when building MCP tool inputSchema
  • Add regression tests for hoist logic and Optional[List[str]] request body conversion

Tests

uv run pytest -q
# 119 passed, 83% coverage

uv run ruff check fastapi_mcp/openapi/utils.py fastapi_mcp/openapi/convert.py tests/test_openapi_conversion.py
# All checks passed

Notes

  • Composer-2.5 review: APPROVE
  • Optional scalar anyOf params are simplified to a single type; optionality remains expressed via the required list (existing behavior).
  • oneOf array variants are handled by the same hoist logic; only anyOf is covered by a dedicated unit test.

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

Hoist array items from anyOf/oneOf variants before clean_schema_for_display
removes composition keys, and apply schema cleaning to tool input properties
so Optional[List[X]] body/query params expose valid {type, items} schemas.

Fixes tadata-org#304
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.

clean_schema_for_display() strips anyOf and loses items for Optional[List[X]] parameters

1 participant