Skip to content
Open
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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
Expand Down
9 changes: 5 additions & 4 deletions src/claude_agent_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions tests/test_sdk_mcp_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@
import mcp.types
import pytest
import sniffio
import typing_extensions
from mcp.server import Server

from claude_agent_sdk import (
Expand Down Expand Up @@ -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
Expand Down