Skip to content

Commit e6824c9

Browse files
committed
Don't crash agent on MCP server disconnected
1 parent 41dd069 commit e6824c9

File tree

1 file changed

+5
-2
lines changed
  • pydantic_ai_slim/pydantic_ai

1 file changed

+5
-2
lines changed

pydantic_ai_slim/pydantic_ai/mcp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ async def list_tools(self) -> list[mcp_types.Tool]:
110110
- We don't cache tools as they might change.
111111
- We also don't subscribe to the server to avoid complexity.
112112
"""
113-
async with self: # Ensure server is running
114-
result = await self._client.list_tools()
113+
try:
114+
async with self: # Ensure server is running
115+
result = await self._client.list_tools()
116+
except anyio.get_cancelled_exc_class():
117+
return []
115118
return result.tools
116119

117120
async def direct_call_tool(

0 commit comments

Comments
 (0)