Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions fastapi_mcp/openapi/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,10 @@ def convert_openapi_to_mcp_tools(
properties[param_name]["description"] = param_desc

if "type" not in properties[param_name]:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)
any_of = param_schema.get("anyOf") or []
non_null_types = {v.get("type") for v in any_of if v.get("type") and v.get("type") != "null"}
if len(non_null_types) <= 1:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)

if "default" in param_schema:
properties[param_name]["default"] = param_schema["default"]
Expand All @@ -245,7 +248,10 @@ def convert_openapi_to_mcp_tools(
properties[param_name]["description"] = param_desc

if "type" not in properties[param_name]:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)
any_of = param_schema.get("anyOf") or []
non_null_types = {v.get("type") for v in any_of if v.get("type") and v.get("type") != "null"}
if len(non_null_types) <= 1:
properties[param_name]["type"] = get_single_param_type_from_schema(param_schema)

if "default" in param_schema:
properties[param_name]["default"] = param_schema["default"]
Expand Down
12 changes: 6 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ classifiers = [
keywords = ["fastapi", "openapi", "mcp", "llm", "claude", "ai", "tools", "api", "conversion", "modelcontextprotocol"]
dependencies = [
"fastapi>=0.100.0",
"typer>=0.9.0",
"rich>=13.0.0",
"mcp>=1.12.0",
"pydantic>=2.0.0",
"pydantic-settings>=2.5.2",
"uvicorn>=0.20.0",
"httpx>=0.24.0",
"requests>=2.25.0",
"tomli>=2.2.1",
]

[project.optional-dependencies]
# Needed to run the generated MCP server behind a real HTTP server
# (uvicorn is used by the examples in examples/ and by the real-transport tests).
deploy = ["uvicorn>=0.20.0"]

[dependency-groups]
dev = [
"mypy>=1.15.0",
Expand All @@ -50,6 +49,7 @@ dev = [
"pyjwt>=2.10.1",
"cryptography>=44.0.2",
"types-jsonschema>=4.25.0.20250720",
"uvicorn>=0.20.0",
]

[project.urls]
Expand Down
39 changes: 39 additions & 0 deletions tests/test_openapi_conversion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Dict, List, Union

from fastapi import FastAPI
from fastapi.openapi.utils import get_openapi
from pydantic import BaseModel, Field
import mcp.types as types

from fastapi_mcp.openapi.convert import convert_openapi_to_mcp_tools
Expand Down Expand Up @@ -422,3 +425,39 @@ def test_body_params_edge_cases(complex_fastapi_app: FastAPI):
if "items" in properties:
item_props = properties["items"]["items"]["properties"]
assert "total" in item_props


def test_union_type_schema_not_collapsed():
"""A body field typed as a union of two real types (e.g. dict | list) must keep its
anyOf in the projected MCP tool schema.

Injecting a single arbitrary "type" alongside anyOf makes the MCP server's jsonschema
validator reject every variant except the one picked (issue #307).
"""

app = FastAPI()

class SaveRequest(BaseModel):
tags: Union[Dict[str, List[str]], List[str]] = Field(default_factory=dict)

@app.post("/save", operation_id="save")
def save(body: SaveRequest):
return {"ok": True}

openapi_schema = get_openapi(
title=app.title,
version=app.version,
openapi_version=app.openapi_version,
routes=app.routes,
)

tools, _ = convert_openapi_to_mcp_tools(openapi_schema)

save_tool = next(tool for tool in tools if tool.name == "save")
tags_prop = save_tool.inputSchema["properties"]["tags"]

assert "anyOf" in tags_prop
assert "type" not in tags_prop
variant_types = {v.get("type") for v in tags_prop["anyOf"]}
assert "object" in variant_types
assert "array" in variant_types
Loading