Skip to content

[BUG] Union types with arrays generate invalid MCP schemas (missing 'items' property) #165

@combiz

Description

@combiz

Describe the bug

When FastAPI endpoints use Pydantic models with Union types containing arrays (e.g., Union[str, List[str]]), the generated MCP tool schema is missing the required items property for the array type. This causes LLMs like OpenAI to reject the schema with: "Invalid schema for function 'xxx': In context=('properties', 'text'), array schema missing items."

To Reproduce

  1. Create a FastAPI app with a Union type parameter:
  from fastapi import FastAPI
  from pydantic import BaseModel
  from typing import Union, List
  from fastapi_mcp import FastApiMCP

  app = FastAPI()

  class TextRequest(BaseModel):
      text: Union[str, List[str]]  # Union with array type
      keywords: List[str]

  @app.post("/process")
  def process_text(request: TextRequest):
      return {"processed": True}

  mcp = FastApiMCP(app)
  1. Connect an LLM (e.g., via LiteLLM) to the MCP server
  2. The LLM will fail to call the tool with error:
  litellm.BadRequestError: OpenAIException - Error code: 400 - {
    'error': {
      'message': "Invalid schema for function 'process_text_process_post':
                  In context=('properties', 'text'), array schema missing items.",
      'type': 'invalid_request_error',
      'param': 'tools[0].function.parameters',
      'code': 'invalid_function_parameters'
    }
  }

System Info

  • OS: Ubuntu 22.04
  • Python version: 3.12.6
  • fastapi-mcp version: 0.4.0
  • FastAPI version: 0.115.6
  • Pydantic version: 2.10.4
  • MCP version: 1.1.3
  • LLM Provider: OpenAI (via LiteLLM)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions