55 uv run server async_tool_elicitation stdio
66"""
77
8- import asyncio
9-
8+ import anyio
109from pydantic import BaseModel , Field
1110
1211from mcp .server .fastmcp import Context , FastMCP
12+ from mcp .server .session import ServerSession
1313
1414mcp = FastMCP ("Async Tool Elicitation" )
1515
@@ -32,12 +32,12 @@ class FileOperationChoice(BaseModel):
3232
3333
3434@mcp .tool (invocation_modes = ["async" ])
35- async def process_with_confirmation (operation : str , ctx : Context ) -> str : # type: ignore[type-arg]
35+ async def process_with_confirmation (operation : str , ctx : Context [ ServerSession , None ] ) -> str :
3636 """Process an operation that requires user confirmation."""
3737 await ctx .info (f"Starting operation: { operation } " )
3838
3939 # Simulate some initial processing
40- await asyncio .sleep (0.5 )
40+ await anyio .sleep (0.5 )
4141 await ctx .report_progress (0.3 , 1.0 , "Initial processing complete" )
4242
4343 # Ask user for preferences
@@ -51,7 +51,7 @@ async def process_with_confirmation(operation: str, ctx: Context) -> str: # typ
5151 await ctx .info (f"Continuing with { result .data .priority_level } priority" )
5252 # Simulate processing based on user choice
5353 processing_time = {"low" : 0.5 , "normal" : 1.0 , "high" : 1.5 }.get (result .data .priority_level , 1.0 )
54- await asyncio .sleep (processing_time )
54+ await anyio .sleep (processing_time )
5555 await ctx .report_progress (1.0 , 1.0 , "Operation complete" )
5656 return f"Operation '{ operation } ' completed successfully with { result .data .priority_level } priority"
5757 else :
@@ -63,12 +63,12 @@ async def process_with_confirmation(operation: str, ctx: Context) -> str: # typ
6363
6464
6565@mcp .tool (invocation_modes = ["async" ])
66- async def file_operation (file_path : str , operation_type : str , ctx : Context ) -> str : # type: ignore[type-arg]
66+ async def file_operation (file_path : str , operation_type : str , ctx : Context [ ServerSession , None ] ) -> str :
6767 """Perform file operation with user confirmation."""
6868 await ctx .info (f"Analyzing file: { file_path } " )
6969
7070 # Simulate initial analysis
71- await asyncio .sleep (1 )
71+ await anyio .sleep (1 )
7272 await ctx .report_progress (0.3 , 1.0 , "File analysis complete" )
7373
7474 # Simulate finding something that requires user confirmation
@@ -84,11 +84,11 @@ async def file_operation(file_path: str, operation_type: str, ctx: Context) -> s
8484 if result .data .confirm_operation :
8585 if result .data .backup_first :
8686 await ctx .info ("Creating backup first..." )
87- await asyncio .sleep (0.5 )
87+ await anyio .sleep (0.5 )
8888 await ctx .report_progress (0.7 , 1.0 , "Backup created" )
8989
9090 await ctx .info (f"Performing { operation_type } operation..." )
91- await asyncio .sleep (1 )
91+ await anyio .sleep (1 )
9292 await ctx .report_progress (1.0 , 1.0 , "Operation complete" )
9393
9494 backup_msg = " (with backup)" if result .data .backup_first else " (no backup)"
0 commit comments