@@ -342,3 +342,81 @@ async def list_empty_tasks(name: str, arguments: dict[str, Any]) -> list[types.T
342342 assert len (result .content ) == 1
343343 assert isinstance (result .content [0 ], types .TextContent )
344344 assert "Found 0 tasks" in result .content [0 ].text
345+
346+
347+ @pytest .mark .anyio
348+ async def test_server_list_tasks_without_task_store ():
349+ """Test server querying client list tasks without task store configured."""
350+ server = Server ("test" , task_store = InMemoryTaskStore ())
351+
352+ @server .list_tools ()
353+ async def list_tools () -> list [types .Tool ]:
354+ return [
355+ types .Tool (
356+ name = "list_tasks_no_store" ,
357+ description = "List tasks without store" ,
358+ inputSchema = {"type" : "object" , "properties" : {}, "required" : []},
359+ )
360+ ]
361+
362+ @server .call_tool ()
363+ async def list_tasks_no_store (name : str , arguments : dict [str , Any ]) -> list [types .TextContent ]:
364+ try :
365+ session = server .request_context .session
366+ await session .list_tasks ()
367+ return [types .TextContent (type = "text" , text = "Should have failed" )]
368+ except Exception as e :
369+ return [types .TextContent (type = "text" , text = f"Error: { str (e )} " )]
370+
371+ async with create_connected_server_and_client_session (server ) as client_session :
372+ # No task store on client
373+ await client_session .initialize ()
374+
375+ result = await client_session .call_tool ("list_tasks_no_store" , {})
376+
377+ assert len (result .content ) == 1
378+ assert isinstance (result .content [0 ], types .TextContent )
379+ assert "Error" in result .content [0 ].text
380+ assert "Task store not configured" in result .content [0 ].text or "INVALID_REQUEST" in result .content [0 ].text
381+
382+
383+ @pytest .mark .anyio
384+ async def test_server_list_tasks_exception ():
385+ """Test server querying client list tasks when task store raises exception."""
386+ from unittest .mock import AsyncMock
387+
388+ server = Server ("test" , task_store = InMemoryTaskStore ())
389+ client_task_store = InMemoryTaskStore ()
390+
391+ # Mock list_tasks to raise an exception
392+ client_task_store .list_tasks = AsyncMock (side_effect = RuntimeError ("Database error" ))
393+
394+ @server .list_tools ()
395+ async def list_tools () -> list [types .Tool ]:
396+ return [
397+ types .Tool (
398+ name = "list_tasks_error" ,
399+ description = "List tasks with error" ,
400+ inputSchema = {"type" : "object" , "properties" : {}, "required" : []},
401+ )
402+ ]
403+
404+ @server .call_tool ()
405+ async def list_tasks_error (name : str , arguments : dict [str , Any ]) -> list [types .TextContent ]:
406+ try :
407+ session = server .request_context .session
408+ await session .list_tasks ()
409+ return [types .TextContent (type = "text" , text = "Should have failed" )]
410+ except Exception as e :
411+ return [types .TextContent (type = "text" , text = f"Error: { str (e )} " )]
412+
413+ async with create_connected_server_and_client_session (server ) as client_session :
414+ client_session ._task_store = client_task_store
415+ await client_session .initialize ()
416+
417+ result = await client_session .call_tool ("list_tasks_error" , {})
418+
419+ assert len (result .content ) == 1
420+ assert isinstance (result .content [0 ], types .TextContent )
421+ assert "Error" in result .content [0 ].text
422+ assert "Failed to list tasks" in result .content [0 ].text or "Database error" in result .content [0 ].text
0 commit comments