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): report per-server outcomes in aggregate REST tools/list (#39232)

devin-ai-integration[bot]69029c139e3e
diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
index 2b89dba0e4f..d1ef73a15cd 100644
--- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
+++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py
@@ -1,7 +1,8 @@
import asyncio
import importlib
-from collections.abc import Awaitable, Callable, Mapping
+from collections.abc import Awaitable, Callable, Mapping, Sequence
from datetime import datetime
+from types import MappingProxyType
from typing import TYPE_CHECKING, Any, Final, Literal
import anyio
@@ -20,8 +21,11 @@ from litellm.proxy._experimental.mcp_server.exceptions import (
MCPUpstreamAuthError,
)
from litellm.proxy._experimental.mcp_server.faults.list_outcomes import (
+ ServerListOk,
+ ServerOutcome,
classify_list_exception,
list_fault_http_status,
+ outcome_wire_value,
)
from litellm.proxy._experimental.mcp_server.ui_session_utils import (
acting_user_auth,
@@ -99,6 +103,7 @@ if MCP_AVAILABLE:
ListMCPToolsRestAPIResponseObject,
MCPInfo,
MCPServer,
+ _aggregate_server_key, # pyright: ignore[reportPrivateUsage] # same per-server key as the tools/list _meta outcomes
_apply_toolset_scope,
_fire_mcp_tool_call_logging,
execute_mcp_tool,
@@ -803,9 +808,6 @@ if MCP_AVAILABLE:
list(allowed_server_ids_set), _rest_client_ip
)
- list_tools_result: Final = []
- error_message = None
-
# If server_id is specified, only query that specific server
if server_id:
return await _list_tools_for_single_server(
@@ -849,22 +851,19 @@ if MCP_AVAILABLE:
else {}
)
- # Query all servers the user has access to
- errors: Final = []
- for allowed_server_id in allowed_server_ids:
- server = global_mcp_server_manager.get_mcp_server_by_id(allowed_server_id)
- if server is None:
- continue
-
- server_auth_header = _get_server_auth_header(server, mcp_server_auth_headers, mcp_auth_header)
- user_oauth_extra_headers = await _get_user_oauth_extra_headers(
+ async def list_server(
+ server: MCPServer,
+ ) -> tuple[Sequence[ListMCPToolsRestAPIResponseObject], ServerOutcome]:
+ server_auth_header: Final = _get_server_auth_header(
+ server, mcp_server_auth_headers, mcp_auth_header
+ )
+ user_oauth_extra_headers: Final = await _get_user_oauth_extra_headers(
server,
user_api_key_dict,
prefetched_creds=prefetched_oauth_creds,
)
-
try:
- tools_result = await _get_tools_for_single_server(
+ tools_result: Final = await _get_tools_for_single_server(
server,
server_auth_header,
raw_headers_from_request,
@@ -872,24 +871,36 @@ if MCP_AVAILABLE:
extra_headers=user_oauth_extra_headers,
apply_tool_filters=apply_tool_filters,
)
- list_tools_result.extend(tools_result)
except Exception as e:
verbose_logger.exception("Error getting tools from %s: %s", server.name, e)
- errors.append(
- f"{get_server_prefix(server)}: {classify_list_exception(e).tag}"
- if isinstance(e, (MCPServerListError, MCPUpstreamAuthError))
- else f"{get_server_prefix(server)}: {e}"
- )
- continue
-
- if errors and not list_tools_result:
- error_message = "Failed to get tools from servers: " + "; ".join(errors)
+ return (), classify_list_exception(e)
+ return tools_result, ServerListOk(tool_count=len(tools_result))
- return {
- "tools": list_tools_result,
- "error": "partial_failure" if error_message else None,
- "message": (error_message if error_message else "Successfully retrieved tools"),
- }
+ # Query all servers the user has access to
+ queried_servers: Final = tuple(
+ server
+ for server in map(global_mcp_server_manager.get_mcp_server_by_id, allowed_server_ids)
+ if server is not None
+ )
+ listings: Final = tuple([await list_server(server) for server in queried_servers])
+ list_tools_result: Final = [tool for tools, _ in listings for tool in tools]
+ server_outcomes: Final = MappingProxyType(
+ {_aggregate_server_key(server): outcome for server, (_, outcome) in zip(queried_servers, listings)}
+ )
+ errors: Final = tuple(
+ f"{key}: {outcome.tag}" for key, outcome in server_outcomes.items() if outcome.tag != "ok"
+ )
+ error_message: Final = (
+ "Failed to get tools from servers: " + "; ".join(errors)
+ if errors and not list_tools_result
+ else None
+ )
+ return {
+ "tools": list_tools_result,
+ "error": "partial_failure" if error_message else None,
+ "message": (error_message if error_message else "Successfully retrieved tools"),
+ "server_outcomes": {key: outcome_wire_value(outcome) for key, outcome in server_outcomes.items()},
+ }
except MCPUpstreamAuthError as e:
# Surface upstream pass-through 401/403 challenges to the client so
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py
index e36bef229f6..0480bbc40a7 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_rest_endpoints.py
@@ -1431,7 +1431,11 @@ class TestListToolsRestAPI:
async def test_aggregate_list_absorbs_one_server_auth_failure(self, monkeypatch):
"""The multi-server aggregate listing degrades a server whose upstream
rejects auth to an empty contribution and still returns the healthy
- server's tools with a 200, rather than surfacing a 401."""
+ server's tools with a 200, rather than surfacing a 401. The absorbed
+ server must still show up as a classified per-server outcome so a REST
+ caller can tell "needs upstream auth" apart from "has no tools"."""
+ from pydantic import TypeAdapter
+
from litellm.proxy._experimental.mcp_server.exceptions import (
MCPUpstreamAuthError,
)
@@ -1497,6 +1501,11 @@ class TestListToolsRestAPI:
assert result["tools"] == ["good-tool"]
assert result["error"] is None
+ wire_body = json.loads(TypeAdapter(dict).dump_json(result))
+ assert wire_body["server_outcomes"] == {
+ "good": {"status": "ok", "tool_count": 1},
+ "bad": {"status": "auth_required", "http_status": 401},
+ }
async def test_name_resolution_finds_server_by_uuid(self, monkeypatch):
"""When server_id is a name string, it should be resolved to its UUID