@@ -502,21 +502,22 @@ cd to the `examples/snippets/clients` directory and run:
502
502
uv run server async_tool_basic stdio
503
503
"""
504
504
505
- import asyncio
505
+ import anyio
506
506
507
507
from mcp.server.fastmcp import Context, FastMCP
508
+ from mcp.server.session import ServerSession
508
509
509
510
mcp = FastMCP(" Async Tool Basic" )
510
511
511
512
512
513
@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 :
514
515
""" Analyze a dataset asynchronously with progress updates."""
515
516
await ctx.info(f " Starting analysis of { dataset} " )
516
517
517
518
# Simulate analysis with progress updates
518
519
for i in range (5 ):
519
- await asyncio .sleep(0.5 )
520
+ await anyio .sleep(0.5 )
520
521
progress = (i + 1 ) / 5
521
522
await ctx.report_progress(progress, 1.0 , f " Processing step { i + 1 } /5 " )
522
523
@@ -525,21 +526,11 @@ async def analyze_data(dataset: str, ctx: Context) -> str: # type: ignore[type-
525
526
526
527
527
528
@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 :
529
530
""" Process text in sync or async mode."""
530
- if ctx:
531
- # Async mode with context
532
- import asyncio
533
531
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 )
543
534
544
535
return f " Processed: { text.upper()} "
545
536
@@ -562,10 +553,11 @@ cd to the `examples/snippets/clients` directory and run:
562
553
uv run server async_tool_immediate stdio
563
554
"""
564
555
565
- import asyncio
556
+ import anyio
566
557
567
558
from mcp import types
568
559
from mcp.server.fastmcp import Context, FastMCP
560
+ from mcp.server.session import ServerSession
569
561
570
562
mcp = FastMCP(" Async Tool Immediate" )
571
563
@@ -576,13 +568,13 @@ async def provide_immediate_feedback(operation: str) -> list[types.ContentBlock]
576
568
577
569
578
570
@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 :
580
572
""" Perform long-running analysis with immediate user feedback."""
581
573
await ctx.info(f " Beginning { operation} analysis " )
582
574
583
575
# Simulate long-running work
584
576
for i in range (4 ):
585
- await asyncio .sleep(1 )
577
+ await anyio .sleep(1 )
586
578
progress = (i + 1 ) / 4
587
579
await ctx.report_progress(progress, 1.0 , f " Analysis step { i + 1 } /4 " )
588
580
@@ -607,9 +599,10 @@ cd to the `examples/snippets` directory and run:
607
599
uv run async-tool-client
608
600
"""
609
601
610
- import asyncio
611
602
import os
612
603
604
+ import anyio
605
+
613
606
from mcp import ClientSession, StdioServerParameters, types
614
607
from mcp.client.stdio import stdio_client
615
608
@@ -646,7 +639,7 @@ async def call_async_tool(session: ClientSession):
646
639
print (f " Operation failed: { status.error} " )
647
640
break
648
641
649
- await asyncio .sleep(0.5 )
642
+ await anyio .sleep(0.5 )
650
643
651
644
652
645
async def run ():
@@ -658,7 +651,7 @@ async def run():
658
651
659
652
660
653
if __name__ == " __main__" :
661
- asyncio .run(run() )
654
+ anyio .run(run)
662
655
```
663
656
664
657
_ 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