Skip to content

Commit eadfb25

Browse files
committed
chore: use AsyncIterator for lifespan annotations, sync README snippets
- Switch AsyncGenerator[None, None] to AsyncIterator[None] in 4 example snippets to match codebase convention for @asynccontextmanager lifespans - Regenerate README.v2.md to sync snippet changes from eb3d0d0
1 parent eb3d0d0 commit eadfb25

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

README.v2.md

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ class UserProfile:
518518
age: int
519519
email: str | None = None
520520

521-
def __init__(self, name: str, age: int, email: str | None = None):
521+
def __init__(self, name: str, age: int, email: str | None = None) -> None:
522522
self.name = name
523523
self.age = age
524524
self.email = email
@@ -532,7 +532,7 @@ def get_user(user_id: str) -> UserProfile:
532532

533533
# Classes WITHOUT type hints cannot be used for structured output
534534
class UntypedConfig:
535-
def __init__(self, setting1, setting2): # type: ignore[reportMissingParameterType]
535+
def __init__(self, setting1, setting2) -> None: # type: ignore[reportMissingParameterType] # noqa: ANN001
536536
self.setting1 = setting1
537537
self.setting2 = setting2
538538

@@ -744,7 +744,7 @@ server_params = StdioServerParameters(
744744
)
745745

746746

747-
async def run():
747+
async def run() -> None:
748748
"""Run the completion client example."""
749749
async with stdio_client(server_params) as (read, write):
750750
async with ClientSession(read, write) as session:
@@ -795,7 +795,7 @@ async def run():
795795
print(f"Completions for 'style' argument: {result.completion.values}")
796796

797797

798-
def main():
798+
def main() -> None:
799799
"""Entry point for the completion client."""
800800
asyncio.run(run())
801801

@@ -1210,7 +1210,7 @@ def hello(name: str = "World") -> str:
12101210
return f"Hello, {name}!"
12111211

12121212

1213-
def main():
1213+
def main() -> None:
12141214
"""Entry point for the direct execution server."""
12151215
mcp.run()
12161216

@@ -1280,6 +1280,7 @@ uvicorn examples.snippets.servers.streamable_starlette_mount:app --reload
12801280
"""
12811281

12821282
import contextlib
1283+
from collections.abc import AsyncIterator
12831284

12841285
from starlette.applications import Starlette
12851286
from starlette.routing import Mount
@@ -1308,7 +1309,7 @@ def add_two(n: int) -> int:
13081309

13091310
# Create a combined lifespan to manage both session managers
13101311
@contextlib.asynccontextmanager
1311-
async def lifespan(app: Starlette):
1312+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
13121313
async with contextlib.AsyncExitStack() as stack:
13131314
await stack.enter_async_context(echo_mcp.session_manager.run())
13141315
await stack.enter_async_context(math_mcp.session_manager.run())
@@ -1392,6 +1393,7 @@ Run from the repository root:
13921393
"""
13931394

13941395
import contextlib
1396+
from collections.abc import AsyncIterator
13951397

13961398
from starlette.applications import Starlette
13971399
from starlette.routing import Mount
@@ -1410,7 +1412,7 @@ def hello() -> str:
14101412

14111413
# Create a lifespan context manager to run the session manager
14121414
@contextlib.asynccontextmanager
1413-
async def lifespan(app: Starlette):
1415+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
14141416
async with mcp.session_manager.run():
14151417
yield
14161418

@@ -1439,6 +1441,7 @@ Run from the repository root:
14391441
"""
14401442

14411443
import contextlib
1444+
from collections.abc import AsyncIterator
14421445

14431446
from starlette.applications import Starlette
14441447
from starlette.routing import Host
@@ -1457,7 +1460,7 @@ def domain_info() -> str:
14571460

14581461
# Create a lifespan context manager to run the session manager
14591462
@contextlib.asynccontextmanager
1460-
async def lifespan(app: Starlette):
1463+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
14611464
async with mcp.session_manager.run():
14621465
yield
14631466

@@ -1486,6 +1489,7 @@ Run from the repository root:
14861489
"""
14871490

14881491
import contextlib
1492+
from collections.abc import AsyncIterator
14891493

14901494
from starlette.applications import Starlette
14911495
from starlette.routing import Mount
@@ -1511,7 +1515,7 @@ def send_message(message: str) -> str:
15111515

15121516
# Create a combined lifespan to manage both session managers
15131517
@contextlib.asynccontextmanager
1514-
async def lifespan(app: Starlette):
1518+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
15151519
async with contextlib.AsyncExitStack() as stack:
15161520
await stack.enter_async_context(api_mcp.session_manager.run())
15171521
await stack.enter_async_context(chat_mcp.session_manager.run())
@@ -1718,7 +1722,7 @@ server = Server(
17181722
)
17191723

17201724

1721-
async def run():
1725+
async def run() -> None:
17221726
"""Run the server with lifespan management."""
17231727
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
17241728
await server.run(
@@ -1796,7 +1800,7 @@ server = Server(
17961800
)
17971801

17981802

1799-
async def run():
1803+
async def run() -> None:
18001804
"""Run the basic low-level server."""
18011805
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
18021806
await server.run(
@@ -1889,7 +1893,7 @@ server = Server(
18891893
)
18901894

18911895

1892-
async def run():
1896+
async def run() -> None:
18931897
"""Run the structured output server."""
18941898
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
18951899
await server.run(
@@ -1964,7 +1968,7 @@ server = Server(
19641968
)
19651969

19661970

1967-
async def run():
1971+
async def run() -> None:
19681972
"""Run the server."""
19691973
async with mcp.server.stdio.stdio_server() as (read_stream, write_stream):
19701974
await server.run(
@@ -2127,7 +2131,7 @@ async def handle_sampling_message(
21272131
)
21282132

21292133

2130-
async def run():
2134+
async def run() -> None:
21312135
async with stdio_client(server_params) as (read, write):
21322136
async with ClientSession(read, write, sampling_callback=handle_sampling_message) as session:
21332137
# Initialize the connection
@@ -2165,7 +2169,7 @@ async def run():
21652169
print(f"Structured tool result: {result_structured}")
21662170

21672171

2168-
def main():
2172+
def main() -> None:
21692173
"""Entry point for the client script."""
21702174
asyncio.run(run())
21712175

@@ -2191,7 +2195,7 @@ from mcp import ClientSession
21912195
from mcp.client.streamable_http import streamable_http_client
21922196

21932197

2194-
async def main():
2198+
async def main() -> None:
21952199
# Connect to a streamable HTTP server
21962200
async with streamable_http_client("http://localhost:8000/mcp") as (read_stream, write_stream):
21972201
# Create a session using the client streams
@@ -2235,7 +2239,7 @@ server_params = StdioServerParameters(
22352239
)
22362240

22372241

2238-
async def display_tools(session: ClientSession):
2242+
async def display_tools(session: ClientSession) -> None:
22392243
"""Display available tools with human-readable names"""
22402244
tools_response = await session.list_tools()
22412245

@@ -2247,7 +2251,7 @@ async def display_tools(session: ClientSession):
22472251
print(f" {tool.description}")
22482252

22492253

2250-
async def display_resources(session: ClientSession):
2254+
async def display_resources(session: ClientSession) -> None:
22512255
"""Display available resources with human-readable names"""
22522256
resources_response = await session.list_resources()
22532257

@@ -2261,7 +2265,7 @@ async def display_resources(session: ClientSession):
22612265
print(f"Resource Template: {display_name}")
22622266

22632267

2264-
async def run():
2268+
async def run() -> None:
22652269
"""Run the display utilities example."""
22662270
async with stdio_client(server_params) as (read, write):
22672271
async with ClientSession(read, write) as session:
@@ -2275,7 +2279,7 @@ async def run():
22752279
await display_resources(session)
22762280

22772281

2278-
def main():
2282+
def main() -> None:
22792283
"""Entry point for the display utilities client."""
22802284
asyncio.run(run())
22812285

@@ -2323,7 +2327,7 @@ from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAu
23232327
class InMemoryTokenStorage(TokenStorage):
23242328
"""Demo In-memory token storage implementation."""
23252329

2326-
def __init__(self):
2330+
def __init__(self) -> None:
23272331
self.tokens: OAuthToken | None = None
23282332
self.client_info: OAuthClientInformationFull | None = None
23292333

@@ -2354,7 +2358,7 @@ async def handle_callback() -> tuple[str, str | None]:
23542358
return params["code"][0], params.get("state", [None])[0]
23552359

23562360

2357-
async def main():
2361+
async def main() -> None:
23582362
"""Run the OAuth client example."""
23592363
oauth_auth = OAuthClientProvider(
23602364
server_url="http://localhost:8001",
@@ -2382,7 +2386,7 @@ async def main():
23822386
print(f"Available resources: {[r.uri for r in resources.resources]}")
23832387

23842388

2385-
def run():
2389+
def run() -> None:
23862390
asyncio.run(main())
23872391

23882392

examples/snippets/servers/streamable_http_basic_mounting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import contextlib
8-
from collections.abc import AsyncGenerator
8+
from collections.abc import AsyncIterator
99

1010
from starlette.applications import Starlette
1111
from starlette.routing import Mount
@@ -24,7 +24,7 @@ def hello() -> str:
2424

2525
# Create a lifespan context manager to run the session manager
2626
@contextlib.asynccontextmanager
27-
async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
27+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
2828
async with mcp.session_manager.run():
2929
yield
3030

examples/snippets/servers/streamable_http_host_mounting.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import contextlib
8-
from collections.abc import AsyncGenerator
8+
from collections.abc import AsyncIterator
99

1010
from starlette.applications import Starlette
1111
from starlette.routing import Host
@@ -24,7 +24,7 @@ def domain_info() -> str:
2424

2525
# Create a lifespan context manager to run the session manager
2626
@contextlib.asynccontextmanager
27-
async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
27+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
2828
async with mcp.session_manager.run():
2929
yield
3030

examples/snippets/servers/streamable_http_multiple_servers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"""
66

77
import contextlib
8-
from collections.abc import AsyncGenerator
8+
from collections.abc import AsyncIterator
99

1010
from starlette.applications import Starlette
1111
from starlette.routing import Mount
@@ -31,7 +31,7 @@ def send_message(message: str) -> str:
3131

3232
# Create a combined lifespan to manage both session managers
3333
@contextlib.asynccontextmanager
34-
async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
34+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
3535
async with contextlib.AsyncExitStack() as stack:
3636
await stack.enter_async_context(api_mcp.session_manager.run())
3737
await stack.enter_async_context(chat_mcp.session_manager.run())

examples/snippets/servers/streamable_starlette_mount.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"""
44

55
import contextlib
6-
from collections.abc import AsyncGenerator
6+
from collections.abc import AsyncIterator
77

88
from starlette.applications import Starlette
99
from starlette.routing import Mount
@@ -32,7 +32,7 @@ def add_two(n: int) -> int:
3232

3333
# Create a combined lifespan to manage both session managers
3434
@contextlib.asynccontextmanager
35-
async def lifespan(app: Starlette) -> AsyncGenerator[None, None]:
35+
async def lifespan(app: Starlette) -> AsyncIterator[None]:
3636
async with contextlib.AsyncExitStack() as stack:
3737
await stack.enter_async_context(echo_mcp.session_manager.run())
3838
await stack.enter_async_context(math_mcp.session_manager.run())

0 commit comments

Comments
 (0)