Skip to content

Commit 26055d9

Browse files
committed
Update README snippets
1 parent 9375927 commit 26055d9

File tree

1 file changed

+15
-22
lines changed

1 file changed

+15
-22
lines changed

README.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -502,21 +502,22 @@ cd to the `examples/snippets/clients` directory and run:
502502
uv run server async_tool_basic stdio
503503
"""
504504

505-
import asyncio
505+
import anyio
506506

507507
from mcp.server.fastmcp import Context, FastMCP
508+
from mcp.server.session import ServerSession
508509

509510
mcp = FastMCP("Async Tool Basic")
510511

511512

512513
@mcp.tool(invocation_modes=["async"])
513-
async def analyze_data(dataset: str, ctx: Context) -> str: # type: ignore[type-arg]
514+
async def analyze_data(dataset: str, ctx: Context[ServerSession, None]) -> str:
514515
"""Analyze a dataset asynchronously with progress updates."""
515516
await ctx.info(f"Starting analysis of {dataset}")
516517

517518
# Simulate analysis with progress updates
518519
for i in range(5):
519-
await asyncio.sleep(0.5)
520+
await anyio.sleep(0.5)
520521
progress = (i + 1) / 5
521522
await ctx.report_progress(progress, 1.0, f"Processing step {i + 1}/5")
522523

@@ -525,21 +526,11 @@ async def analyze_data(dataset: str, ctx: Context) -> str: # type: ignore[type-
525526

526527

527528
@mcp.tool(invocation_modes=["sync", "async"])
528-
def process_text(text: str, ctx: Context | None = None) -> str: # type: ignore[type-arg]
529+
async def process_text(text: str, ctx: Context[ServerSession, None]) -> str:
529530
"""Process text in sync or async mode."""
530-
if ctx:
531-
# Async mode with context
532-
import asyncio
533531

534-
async def async_processing():
535-
await ctx.info(f"Processing text asynchronously: {text[:20]}...")
536-
await asyncio.sleep(0.3)
537-
538-
try:
539-
loop = asyncio.get_event_loop()
540-
loop.create_task(async_processing())
541-
except RuntimeError:
542-
pass
532+
await ctx.info(f"Processing text asynchronously: {text[:20]}...")
533+
await anyio.sleep(0.3)
543534

544535
return f"Processed: {text.upper()}"
545536

@@ -562,10 +553,11 @@ cd to the `examples/snippets/clients` directory and run:
562553
uv run server async_tool_immediate stdio
563554
"""
564555

565-
import asyncio
556+
import anyio
566557

567558
from mcp import types
568559
from mcp.server.fastmcp import Context, FastMCP
560+
from mcp.server.session import ServerSession
569561

570562
mcp = FastMCP("Async Tool Immediate")
571563

@@ -576,13 +568,13 @@ async def provide_immediate_feedback(operation: str) -> list[types.ContentBlock]
576568

577569

578570
@mcp.tool(invocation_modes=["async"], immediate_result=provide_immediate_feedback)
579-
async def long_analysis(operation: str, ctx: Context) -> str: # type: ignore[type-arg]
571+
async def long_analysis(operation: str, ctx: Context[ServerSession, None]) -> str:
580572
"""Perform long-running analysis with immediate user feedback."""
581573
await ctx.info(f"Beginning {operation} analysis")
582574

583575
# Simulate long-running work
584576
for i in range(4):
585-
await asyncio.sleep(1)
577+
await anyio.sleep(1)
586578
progress = (i + 1) / 4
587579
await ctx.report_progress(progress, 1.0, f"Analysis step {i + 1}/4")
588580

@@ -607,9 +599,10 @@ cd to the `examples/snippets` directory and run:
607599
uv run async-tool-client
608600
"""
609601

610-
import asyncio
611602
import os
612603

604+
import anyio
605+
613606
from mcp import ClientSession, StdioServerParameters, types
614607
from mcp.client.stdio import stdio_client
615608

@@ -646,7 +639,7 @@ async def call_async_tool(session: ClientSession):
646639
print(f"Operation failed: {status.error}")
647640
break
648641

649-
await asyncio.sleep(0.5)
642+
await anyio.sleep(0.5)
650643

651644

652645
async def run():
@@ -658,7 +651,7 @@ async def run():
658651

659652

660653
if __name__ == "__main__":
661-
asyncio.run(run())
654+
anyio.run(run)
662655
```
663656

664657
_Full example: [examples/snippets/clients/async_tool_client.py](https://github.com/modelcontextprotocol/python-sdk/blob/main/examples/snippets/clients/async_tool_client.py)_

0 commit comments

Comments
 (0)