Skip to content

Commit 24ffb7c

Browse files
committed
fix: wrap pipe union structured outputs
Signed-off-by: Yufeng He <40085740+he-yufeng@users.noreply.github.com>
1 parent e8e6484 commit 24ffb7c

2 files changed

Lines changed: 22 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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,9 @@ def func_dict_str_int() -> dict[str, int]: # pragma: no cover
677677
def func_union() -> str | int: # pragma: no cover
678678
return "hello"
679679

680+
def func_pipe_union_containers() -> dict | list | str: # pragma: no cover
681+
return {"a": 1}
682+
680683
def func_optional() -> str | None: # pragma: no cover
681684
return None
682685

@@ -706,6 +709,24 @@ def func_optional() -> str | None: # pragma: no cover
706709
"title": "func_unionOutput",
707710
}
708711

712+
# Test PEP 604 union with containers
713+
meta = func_metadata(func_pipe_union_containers)
714+
assert meta.output_schema == {
715+
"type": "object",
716+
"properties": {
717+
"result": {
718+
"title": "Result",
719+
"anyOf": [
720+
{"additionalProperties": True, "type": "object"},
721+
{"items": {}, "type": "array"},
722+
{"type": "string"},
723+
],
724+
}
725+
},
726+
"required": ["result"],
727+
"title": "func_pipe_union_containersOutput",
728+
}
729+
709730
# Test Optional
710731
meta = func_metadata(func_optional)
711732
assert meta.output_schema == {

0 commit comments

Comments
 (0)