Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(mcp): use anyio.fail_after instead of asyncio.wait_for for StreamableHTTP backends (#20891)

shin-bot-litellm8c13001eb110
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
index 24eae430d85..a3dc876d379 100644
--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
@@ -14,6 +14,8 @@ import re
from typing import Any, Callable, Dict, List, Literal, Optional, Set, Tuple, Union, cast
from urllib.parse import urlparse
+import anyio
+
from fastapi import HTTPException
from httpx import HTTPStatusError
from mcp import ReadResourceResult, Resource
@@ -1416,6 +1418,9 @@ class MCPServerManager:
"""
Fetch tools from MCP client with timeout and error handling.
+ Uses anyio.fail_after() instead of asyncio.wait_for() to avoid conflicts
+ with the MCP SDK's anyio TaskGroup. See GitHub issue #20715 for details.
+
Args:
client: MCP client instance
server_name: Name of the server for logging
@@ -1423,24 +1428,12 @@ class MCPServerManager:
Returns:
List of tools from the server
"""
-
- async def _list_tools_task():
- try:
+ try:
+ with anyio.fail_after(30.0):
tools = await client.list_tools()
verbose_logger.debug(f"Tools from {server_name}: {tools}")
return tools
- except asyncio.CancelledError:
- verbose_logger.warning(f"Client operation cancelled for {server_name}")
- return []
- except Exception as e:
- verbose_logger.warning(
- f"Client operation failed for {server_name}: {str(e)}"
- )
- return []
-
- try:
- return await asyncio.wait_for(_list_tools_task(), timeout=30.0)
- except asyncio.TimeoutError:
+ except TimeoutError:
verbose_logger.warning(f"Timeout while listing tools from {server_name}")
return []
except asyncio.CancelledError: