diff --git a/pyproject.toml b/pyproject.toml index 822ccbc58..6eaaf4dce 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -27,7 +27,7 @@ keywords = ["claude", "ai", "sdk", "anthropic"] dependencies = [ "anyio>=4.0.0", "sniffio>=1.0.0", - "typing_extensions>=4.0.0; python_version<'3.11'", + "typing_extensions>=4.1.0", "mcp>=1.23.0,<3.0.0", "jsonschema>=4.20.0", ] diff --git a/src/claude_agent_sdk/__init__.py b/src/claude_agent_sdk/__init__.py index d8c83bcac..fe452654c 100644 --- a/src/claude_agent_sdk/__init__.py +++ b/src/claude_agent_sdk/__init__.py @@ -16,14 +16,15 @@ get_origin, ) +# typing_extensions ships its own TypedDict on every Python version, and the +# stdlib is_typeddict doesn't recognize classes built from it. +from typing_extensions import is_typeddict + if sys.version_info >= (3, 11): from typing import get_type_hints as _get_type_hints - from typing import is_typeddict else: - # On 3.10, stdlib is_typeddict doesn't recognize typing_extensions.TypedDict - # subclasses, and stdlib get_type_hints doesn't strip NotRequired markers. + # On 3.10, stdlib get_type_hints doesn't strip NotRequired markers. from typing_extensions import get_type_hints as _get_type_hints - from typing_extensions import is_typeddict import jsonschema from mcp.types import CallToolResult, Tool diff --git a/tests/test_sdk_mcp_integration.py b/tests/test_sdk_mcp_integration.py index a0eaca6d0..658c5700c 100644 --- a/tests/test_sdk_mcp_integration.py +++ b/tests/test_sdk_mcp_integration.py @@ -10,7 +10,9 @@ import gc import json import logging +import sys import threading +import typing import warnings from collections.abc import AsyncIterator from contextlib import asynccontextmanager, suppress @@ -22,6 +24,7 @@ import mcp.types import pytest import sniffio +import typing_extensions from mcp.server import Server from claude_agent_sdk import ( @@ -2131,6 +2134,59 @@ async def search(args: dict[str, Any]) -> dict[str, Any]: assert schema["properties"]["max_results"] == {"type": "integer"} assert sorted(schema["required"]) == ["max_results", "query"] + @pytest.mark.anyio + @pytest.mark.parametrize( + "typed_dict", + [ + pytest.param( + typing.TypedDict, + marks=pytest.mark.skipif( + sys.version_info < (3, 11), + reason="stdlib TypedDict ignores NotRequired before 3.11", + ), + id="typing", + ), + pytest.param(typing_extensions.TypedDict, id="typing_extensions"), + ], + ) + async def test_typeddict_schema_for_either_typeddict_spelling( + self, typed_dict: Any + ) -> None: + """A TypedDict built on typing or typing_extensions yields the same + schema on every Python version, rather than an empty one on 3.11+ + where stdlib is_typeddict rejects the typing_extensions spelling.""" + from typing import Annotated + + from typing_extensions import NotRequired + + class Address(typed_dict): + city: str + + class Forecast(typed_dict): + latitude: Annotated[float, "Latitude coordinate"] + hours: NotRequired[Annotated[int, "Hours of forecast"]] + address: Address + + @tool("forecast", "Get a forecast", Forecast) + async def forecast(args: dict[str, Any]) -> dict[str, Any]: + return {"content": [{"type": "text", "text": "ok"}]} + + config = create_sdk_mcp_server(name="te-typeddict-test", tools=[forecast]) + async with connected(config) as client: + [listed] = await client.list_tools("srv") + + schema = listed["inputSchema"] + assert schema["properties"] == { + "latitude": {"type": "number", "description": "Latitude coordinate"}, + "hours": {"type": "integer", "description": "Hours of forecast"}, + "address": { + "type": "object", + "properties": {"city": {"type": "string"}}, + "required": ["city"], + }, + } + assert sorted(schema["required"]) == ["address", "latitude"] + @pytest.mark.anyio async def test_typeddict_tool_call_works(self) -> None: from typing import TypedDict