Skip to content

Commit 794181b

Browse files
committed
fix fastmcp pep 604 union output schema
1 parent 161834d commit 794181b

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

src/mcp/server/mcpserver/utilities/func_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ def _create_wrapped_model(func_name: str, annotation: Any) -> type[BaseModel]:
478478
"""
479479
model_name = f"{func_name}Output"
480480

481-
return create_model(model_name, result=annotation)
481+
return create_model(model_name, result=(annotation, ...))
482482

483483

484484
def _create_dict_model(func_name: str, dict_annotation: Any) -> type[BaseModel]:

tests/server/mcpserver/test_func_metadata.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,30 @@ def func_optional() -> str | None: # pragma: no cover
716716
}
717717

718718

719+
def test_structured_output_pep604_union_of_container_and_primitives():
720+
"""Test structured output with PEP 604 unions that combine containers and primitives."""
721+
722+
def func_container_union() -> dict | list | str: # pragma: no cover
723+
return {"hello": "world"}
724+
725+
meta = func_metadata(func_container_union)
726+
assert meta.output_schema == {
727+
"type": "object",
728+
"properties": {
729+
"result": {
730+
"title": "Result",
731+
"anyOf": [
732+
{"additionalProperties": True, "type": "object"},
733+
{"items": {}, "type": "array"},
734+
{"type": "string"},
735+
],
736+
}
737+
},
738+
"required": ["result"],
739+
"title": "func_container_unionOutput",
740+
}
741+
742+
719743
def test_structured_output_dataclass():
720744
"""Test structured output with dataclass return types"""
721745

0 commit comments

Comments
 (0)