diff --git a/src/mcp/server/mcpserver/utilities/func_metadata.py b/src/mcp/server/mcpserver/utilities/func_metadata.py index e5721488f5..24f3ed3047 100644 --- a/src/mcp/server/mcpserver/utilities/func_metadata.py +++ b/src/mcp/server/mcpserver/utilities/func_metadata.py @@ -3,8 +3,15 @@ import json import sys from collections.abc import Awaitable, Callable, Sequence +from dataclasses import is_dataclass +from datetime import date, time, timedelta +from decimal import Decimal +from enum import Enum +from ipaddress import IPv4Address, IPv4Interface, IPv4Network, IPv6Address, IPv6Interface, IPv6Network from itertools import chain +from pathlib import PurePath from typing import Annotated, Any, NotRequired, Union, cast, get_args, get_origin +from uuid import UUID import anyio import anyio.to_thread @@ -43,6 +50,24 @@ def _is_input_required_type(obj: Any) -> bool: return isinstance(obj, type) and issubclass(obj, InputRequiredResult) +_WRAPPED_SCALAR_EXACT_TYPES = (str, int, float, bool, bytes, type(None), list, tuple, set, frozenset) +_WRAPPED_SCALAR_BASE_TYPES = ( + Enum, + date, + time, + timedelta, + Decimal, + UUID, + PurePath, + IPv4Address, + IPv6Address, + IPv4Interface, + IPv6Interface, + IPv4Network, + IPv6Network, +) + + _CONTENT_TYPES = (*get_args(ContentBlock), Image, Audio) # `_convert_to_content` unrolls list/tuple values; a `Sequence[...]` annotation is one of those at runtime. _CONTENT_SEQUENCE_ORIGINS = (list, tuple, Sequence) @@ -545,8 +570,13 @@ def _create_output_model(original_annotation: Any, type_expr: Any, func_name: st elif type_annotation is dict: model = dict[str, Any] - # Case 4: Primitive types and bare collections that need wrapping - elif type_annotation in (str, int, float, bool, bytes, type(None), list, tuple, set, frozenset): + # Case 4: Primitive types, bare collections, stdlib scalar/enum types, and Pydantic scalar types + # (e.g. AnyUrl/AnyHttpUrl implementing __get_pydantic_core_schema__) that need wrapping + elif ( + type_annotation in _WRAPPED_SCALAR_EXACT_TYPES + or issubclass(type_annotation, _WRAPPED_SCALAR_BASE_TYPES) + or (hasattr(type_annotation, "__get_pydantic_core_schema__") and not is_dataclass(type_annotation)) + ): model = _create_wrapped_model(func_name, original_annotation) wrap_output = True diff --git a/tests/server/mcpserver/test_func_metadata.py b/tests/server/mcpserver/test_func_metadata.py index f929fb3954..9bb6f141da 100644 --- a/tests/server/mcpserver/test_func_metadata.py +++ b/tests/server/mcpserver/test_func_metadata.py @@ -5,17 +5,23 @@ # pyright: reportUnknownLambdaType=false # pyright: reportMissingTypeArgument=false # pyright: reportUnknownVariableType=false +import datetime as dt +import decimal +import ipaddress import typing from collections.abc import Callable from dataclasses import dataclass +from enum import Enum, IntEnum, StrEnum +from pathlib import Path, PurePath from typing import TYPE_CHECKING, Annotated, Any, Final, NamedTuple, NotRequired, Required, TypedDict +from uuid import UUID import annotated_types import pytest from dirty_equals import IsPartialDict from inline_snapshot import snapshot from mcp_types import CallToolResult, ContentBlock, EmbeddedResource, InputRequiredResult, TextContent -from pydantic import BaseModel, Field, ValidationError +from pydantic import AnyHttpUrl, AnyUrl, BaseModel, Field, ValidationError from typing_extensions import ReadOnly from mcp import MCPDeprecationWarning @@ -1647,3 +1653,60 @@ def fn() -> StepA | StepB: ... # pragma: no branch meta = func_metadata(fn) assert meta.output_schema is None + + +class _SampleEnum(Enum): + A = "a" + + +class _SampleStrEnum(StrEnum): + RED = "red" + + +class _SampleIntEnum(IntEnum): + ONE = 1 + + +@pytest.mark.parametrize( + ("ret_type", "sample", "expected_json"), + [ + pytest.param(AnyUrl, AnyUrl("https://example.com/a"), "https://example.com/a", id="AnyUrl"), + pytest.param(AnyHttpUrl, AnyHttpUrl("https://example.com"), "https://example.com/", id="AnyHttpUrl"), + pytest.param(_SampleEnum, _SampleEnum.A, "a", id="Enum"), + pytest.param(_SampleStrEnum, _SampleStrEnum.RED, "red", id="StrEnum"), + pytest.param(_SampleIntEnum, _SampleIntEnum.ONE, 1, id="IntEnum"), + pytest.param( + UUID, + UUID("12345678-1234-5678-1234-567812345678"), + "12345678-1234-5678-1234-567812345678", + id="UUID", + ), + pytest.param(dt.datetime, dt.datetime(2026, 1, 2, 3, 4, 5), "2026-01-02T03:04:05", id="datetime"), + pytest.param(dt.date, dt.date(2026, 1, 2), "2026-01-02", id="date"), + pytest.param(dt.time, dt.time(3, 4, 5), "03:04:05", id="time"), + pytest.param(dt.timedelta, dt.timedelta(seconds=90), "PT1M30S", id="timedelta"), + pytest.param(decimal.Decimal, decimal.Decimal("12.34"), "12.34", id="Decimal"), + pytest.param(Path, Path("foo/bar.txt"), str(Path("foo/bar.txt")), id="Path"), + pytest.param(PurePath, PurePath("foo/bar.txt"), str(PurePath("foo/bar.txt")), id="PurePath"), + pytest.param(ipaddress.IPv4Address, ipaddress.IPv4Address("192.0.2.1"), "192.0.2.1", id="IPv4Address"), + pytest.param(ipaddress.IPv6Address, ipaddress.IPv6Address("2001:db8::1"), "2001:db8::1", id="IPv6Address"), + pytest.param(ipaddress.IPv4Interface, ipaddress.IPv4Interface("192.0.2.1/24"), "192.0.2.1/24", id="IPv4Interface"), + pytest.param(ipaddress.IPv6Interface, ipaddress.IPv6Interface("2001:db8::1/64"), "2001:db8::1/64", id="IPv6Interface"), + pytest.param(ipaddress.IPv4Network, ipaddress.IPv4Network("192.0.2.0/24"), "192.0.2.0/24", id="IPv4Network"), + pytest.param(ipaddress.IPv6Network, ipaddress.IPv6Network("2001:db8::/32"), "2001:db8::/32", id="IPv6Network"), + ], +) +def test_structured_output_scalar_enum_and_pydantic_url_types(ret_type: type[Any], sample: Any, expected_json: Any): + def fn() -> Any: + return sample + + fn.__annotations__ = {"return": ret_type} + + meta = func_metadata(fn, structured_output=True) + assert meta.wrap_output is True + assert meta.output_schema is not None + assert "result" in meta.output_schema["properties"] + + converted = meta.convert_result(sample) + assert isinstance(converted, CallToolResult) + assert converted.structured_content == {"result": expected_json}