@@ -156,6 +156,11 @@ async def handle_list_tools() -> list[Tool]:
156156 description = "A long-running tool that sends periodic notifications" ,
157157 inputSchema = {"type" : "object" , "properties" : {}},
158158 ),
159+ Tool (
160+ name = "long_running_with_no_checkpoints" ,
161+ description = "A long-running tool that does not send periodic notifications" ,
162+ inputSchema = {"type" : "object" , "properties" : {}},
163+ ),
159164 Tool (
160165 name = "test_sampling_tool" ,
161166 description = "A tool that triggers server-side sampling" ,
@@ -204,6 +209,12 @@ async def handle_call_tool(name: str, args: dict[str, Any]) -> list[TextContent]
204209
205210 return [TextContent (type = "text" , text = "Completed!" )]
206211
212+ elif name == "long_running_with_no_checkpoints" :
213+ # Send notifications that are part of the response stream
214+ # This simulates a long-running tool does not send any events
215+ await anyio .sleep (1 )
216+
217+ return [TextContent (type = "text" , text = "Completed!" )]
207218 elif name == "test_sampling_tool" :
208219 # Test sampling by requesting the client to sample a message
209220 sampling_result = await ctx .session .create_message (
@@ -1348,14 +1359,12 @@ async def run_tool():
13481359
13491360@pytest .mark .anyio
13501361async def test_streamablehttp_client_non_blocking_timeout (event_server : tuple [SimpleEventStore , str ]):
1351- """Test client session start timeout."""
1362+ """Test client session start timeout due to no notifications from server ."""
13521363 _ , server_url = event_server
13531364
13541365 with anyio .fail_after (10 ):
13551366 # Variables to track the state
13561367 captured_notifications : list [types .ServerNotification ] = []
1357- tool_started = anyio .Event ()
1358- tool_cancelled = anyio .Event ()
13591368
13601369 request_state_manager = InMemoryRequestStateManager [types .ClientRequest , types .ClientResult ]()
13611370
@@ -1364,49 +1373,35 @@ async def message_handler(
13641373 ) -> None :
13651374 if isinstance (message , types .ServerNotification ):
13661375 captured_notifications .append (message )
1367- # Look for our special notification that indicates the tool is running
1368- if isinstance (message .root , types .LoggingMessageNotification ):
1369- if message .root .params .data == "Tool started" :
1370- nonlocal tool_started
1371- tool_started .set ()
1372- else :
1373- await tool_cancelled .wait ()
1374-
1375- if isinstance (message .root , types .CancelledNotification ):
1376- nonlocal tool_cancelled
1377- tool_cancelled .set ()
13781376
13791377 # First, start the client session and begin the long-running tool
13801378 async with streamablehttp_client (f"{ server_url } /mcp" , terminate_on_close = False ) as (
13811379 read_stream ,
13821380 write_stream ,
13831381 _ ,
13841382 ):
1385- async with ClientSession (
1386- read_stream ,
1387- write_stream ,
1388- message_handler = message_handler ,
1389- request_state_manager = request_state_manager ,
1390- ) as session :
1391- # Initialize the session
1392- result = await session .initialize ()
1393- assert isinstance (result , InitializeResult )
1394-
1383+ async with (
1384+ ClientSession (
1385+ read_stream ,
1386+ write_stream ,
1387+ message_handler = message_handler ,
1388+ request_state_manager = request_state_manager ,
1389+ ) as session ,
1390+ ):
13951391 # Start a long-running tool in a task
13961392 async with anyio .create_task_group () as tg :
13971393
13981394 async def run_tool ():
1395+ # Initialize the session
1396+ result = await session .initialize ()
1397+ assert isinstance (result , InitializeResult )
13991398 request_id = await session .request_call_tool (
1400- "long_running_with_checkpoints " , arguments = {}, timeout = 0.01 , cancel_if_not_resumable = True
1399+ "long_running_with_no_checkpoints " , arguments = {}, timeout = 0.01 , cancel_if_not_resumable = True
14011400 )
14021401 assert request_id is None
14031402
14041403 tg .start_soon (run_tool )
14051404
1406- await tool_started .wait ()
1407- await tool_cancelled .wait ()
1408-
1409- assert tool_started .is_set () and tool_cancelled .is_set ()
14101405 assert len (request_state_manager ._progress_callbacks ) == 0
14111406 assert len (request_state_manager ._response_streams ) == 0
14121407
0 commit comments