From e6824c9ee77f68bb7f14f0e8586859993ace25aa Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 25 Jul 2025 12:47:45 +0200 Subject: [PATCH] Don't crash agent on MCP server disconnected --- pydantic_ai_slim/pydantic_ai/mcp.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pydantic_ai_slim/pydantic_ai/mcp.py b/pydantic_ai_slim/pydantic_ai/mcp.py index c84f4b10b..018f976f4 100644 --- a/pydantic_ai_slim/pydantic_ai/mcp.py +++ b/pydantic_ai_slim/pydantic_ai/mcp.py @@ -110,8 +110,11 @@ async def list_tools(self) -> list[mcp_types.Tool]: - We don't cache tools as they might change. - We also don't subscribe to the server to avoid complexity. """ - async with self: # Ensure server is running - result = await self._client.list_tools() + try: + async with self: # Ensure server is running + result = await self._client.list_tools() + except anyio.get_cancelled_exc_class(): + return [] return result.tools async def direct_call_tool(