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]

Merge origin/litellm_internal_staging into litellm_mcp_aggregate_outcomes

Tin Chi Lo98bf25e8affd
diff --cc tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
index 099350d2e78,7e59904a39c..21e7cafe046
--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
@@@ -7444,121 -7439,137 +7444,257 @@@ async def test_call_mcp_tool_skips_fail
proxy_logging_mock.post_call_failure_hook.assert_not_awaited()
+async def test_aggregate_listing_reports_per_server_outcomes():
+ """A failed server must contribute a classified outcome, not just silently shrink the list:
+ without the outcome a broken upstream is indistinguishable from a healthy server with no tools."""
+ try:
+ from litellm.proxy._experimental.mcp_server.server import (
+ _get_tools_from_mcp_servers,
+ set_auth_context,
+ )
+ except ImportError:
+ pytest.skip("MCP server not available")
+
+ from litellm.proxy._experimental.mcp_server.exceptions import MCPServerListError
+ from litellm.proxy._experimental.mcp_server.faults.list_outcomes import ServerListFault
+
+ user_api_key_auth = UserAPIKeyAuth(api_key="test_key", user_id="test_user")
+ set_auth_context(user_api_key_auth)
+
+ working_server = MagicMock()
+ working_server.name = "working_server"
+ working_server.alias = "working"
+ working_server.allowed_tools = None
+ working_server.disallowed_tools = None
+ working_server.server_id = "working_server"
+ working_server.server_name = "working_server"
+ working_server.auth_type = None
+ working_server.extra_headers = None
+
+ broken_server = MagicMock()
+ broken_server.name = "broken_server"
+ broken_server.alias = "broken"
+ broken_server.allowed_tools = None
+ broken_server.disallowed_tools = None
+ broken_server.server_id = "broken_server"
+ broken_server.server_name = "broken_server"
+ broken_server.auth_type = None
+ broken_server.extra_headers = None
+
+ mock_manager = MagicMock()
+ mock_manager.get_allowed_mcp_servers = AsyncMock(return_value=["working_server", "broken_server"])
+ mock_manager.get_mcp_server_by_id = lambda server_id: (
+ working_server if server_id == "working_server" else broken_server
+ )
+ mock_manager.filter_server_ids_by_ip_with_info = lambda server_ids, client_ip: (server_ids, 0)
+
+ async def mock_get_tools_from_server(server, **kwargs):
+ if server.name == "working_server":
+ tool1 = MagicMock()
+ tool1.name = "working_tool_1"
+ tool1.description = "Working tool 1"
+ tool1.inputSchema = {}
+ return [tool1]
+ raise MCPServerListError(ServerListFault(tag="upstream_error", status_code=500), server.name)
+
+ mock_manager._get_tools_from_server = mock_get_tools_from_server
+
+ with patch(
+ "litellm.proxy._experimental.mcp_server.server.global_mcp_server_manager",
+ mock_manager,
+ ):
+ listing = await _get_tools_from_mcp_servers(
+ user_api_key_auth=user_api_key_auth,
+ mcp_auth_header=None,
+ mcp_servers=["working_server", "broken_server"],
+ mcp_server_auth_headers=None,
+ )
+
+ assert [tool.name for tool in listing.tools] == ["working_tool_1"]
+ assert listing.outcomes["working_server"].tag == "ok"
+ assert listing.outcomes["working_server"].tool_count == 1
+ assert listing.outcomes["broken_server"].tag == "upstream_error"
+ assert listing.outcomes["broken_server"].status_code == 500
+
+
+async def test_handle_list_tools_attaches_outcome_meta():
+ """The protocol handler returns a ListToolsResult whose _meta carries the per-server outcomes,
+ so MCP clients can tell a degraded listing from a genuinely empty one."""
+ try:
+ from litellm.proxy._experimental.mcp_server.server import handle_list_tools
+ except ImportError:
+ pytest.skip("MCP server not available")
+
+ from mcp.types import ListToolsResult, Tool
+
+ from litellm.proxy._experimental.mcp_server.faults.list_outcomes import (
+ SERVER_OUTCOMES_META_KEY,
+ AggregateToolListing,
+ ServerListFault,
+ ServerListOk,
+ )
+
+ tool = Tool(name="t1", inputSchema={"type": "object"})
+ listing = AggregateToolListing(
+ tools=[tool],
+ outcomes={"healthy": ServerListOk(tool_count=1), "broken": ServerListFault(tag="unreachable")},
+ )
+
+ async def fake_auth_context():
+ return (None, None, None, None, None, None, None)
+
+ with (
+ patch(
+ "litellm.proxy._experimental.mcp_server.server.get_or_extract_auth_context",
+ new=AsyncMock(return_value=(None, None, None, None, None, None, None)),
+ ),
+ patch(
+ "litellm.proxy._experimental.mcp_server.server._list_mcp_tools",
+ new=AsyncMock(return_value=listing),
+ ),
+ ):
+ result = await handle_list_tools()
+
+ assert isinstance(result, ListToolsResult)
+ wire = result.model_dump(by_alias=True)
+ outcomes_meta = wire["_meta"][SERVER_OUTCOMES_META_KEY]
+ assert outcomes_meta["healthy"] == {"status": "ok", "tool_count": 1}
+ assert outcomes_meta["broken"] == {"status": "unreachable"}
++
++
+ def _make_oauth2_server(
+ alias: str,
+ *,
+ oauth2_flow=None,
+ delegate_auth_to_upstream: bool = False,
+ client_id=None,
+ client_secret=None,
+ token_url=None,
+ ) -> MCPServer:
+ """An auth_type=oauth2 MCP server in one of its sub-modes. oauth2_flow
+ 'client_credentials' is M2M; delegate_auth_to_upstream toggles the
+ upstream-PKCE delegate mode; the default is gateway-managed interactive
+ (authorization_code). client_id/client_secret/token_url set the M2M shape
+ that effective_oauth2_flow infers as client_credentials when oauth2_flow is
+ left unstamped (null)."""
+ return MCPServer(
+ server_id=f"id-{alias}",
+ name=alias,
+ alias=alias,
+ server_name=alias,
+ url=f"https://{alias}.test/mcp",
+ transport=MCPTransport.http,
+ auth_type=MCPAuth.oauth2,
+ oauth2_flow=oauth2_flow,
+ delegate_auth_to_upstream=delegate_auth_to_upstream,
+ client_id=client_id,
+ client_secret=client_secret,
+ token_url=token_url,
+ mcp_info={"server_name": alias},
+ )
+
+
+ class TestPreemptive401ModeAware:
+ """The preemptive-401 challenge for auth_type=oauth2 servers is decided by
+ the server's sub-mode, not by whether an Authorization header is present.
+
+ Regression guard for the bug where a LiteLLM virtual key presented as
+ ``Authorization: Bearer sk-...`` (indistinguishable at header-parse time
+ from an upstream OAuth bearer, so it lands in oauth2_headers) suppressed
+ the challenge on a gateway-managed authorization_code server, opening a
+ session with no upstream token whose tools/list masks as 200 + empty.
+ """
+
+ LITELLM_KEY_HEADERS = {"Authorization": "Bearer sk-litellm-virtual-key"}
+
+ def _scope(self, alias: str):
+ return {"type": "http", "method": "POST", "path": f"/mcp/{alias}", "headers": []}
+
+ async def _run(self, server, oauth2_headers, has_stored_token: bool):
+ from litellm.proxy._experimental.mcp_server import server as server_module
+
+ with (
+ patch.object(
+ server_module.global_mcp_server_manager,
+ "get_mcp_server_by_name",
+ return_value=server,
+ ),
+ patch.object(
+ server_module.global_mcp_server_manager,
+ "has_user_oauth_token",
+ new_callable=AsyncMock,
+ return_value=has_stored_token,
+ ),
+ ):
+ await server_module._raise_preemptive_401_for_unauthenticated_servers(
+ scope=self._scope(server.alias),
+ mcp_servers=[server.alias],
+ oauth2_headers=oauth2_headers,
+ mcp_server_auth_headers=None,
+ user_api_key_auth=UserAPIKeyAuth(api_key="sk-litellm-virtual-key"),
+ client_ip=None,
+ )
+
+ @pytest.mark.asyncio
+ async def test_gateway_managed_interactive_no_token_challenges_with_x_litellm_api_key(self):
+ """No stored token, key in x-litellm-api-key (oauth2_headers empty): 401."""
+ with pytest.raises(HTTPException) as exc:
+ await self._run(_make_oauth2_server("interactive"), None, has_stored_token=False)
+ assert exc.value.status_code == 401
+ assert "www-authenticate" in {k.lower() for k in exc.value.headers}
+
+ @pytest.mark.asyncio
+ async def test_gateway_managed_interactive_no_token_challenges_with_authorization_bearer(self):
+ """The bug fix: no stored token, key in Authorization (oauth2_headers
+ populated) must still get the 401 challenge, not a suppressed session."""
+ with pytest.raises(HTTPException) as exc:
+ await self._run(
+ _make_oauth2_server("interactive"),
+ self.LITELLM_KEY_HEADERS,
+ has_stored_token=False,
+ )
+ assert exc.value.status_code == 401
+ assert "www-authenticate" in {k.lower() for k in exc.value.headers}
+
+ @pytest.mark.asyncio
+ async def test_gateway_managed_interactive_with_stored_token_does_not_challenge(self):
+ """A stored per-user token exists: no challenge, under either header."""
+ await self._run(_make_oauth2_server("interactive"), None, has_stored_token=True)
+ await self._run(_make_oauth2_server("interactive"), self.LITELLM_KEY_HEADERS, has_stored_token=True)
+
+ @pytest.mark.asyncio
+ async def test_m2m_never_challenges(self):
+ """client_credentials (M2M): the gateway mints its own token, so no
+ challenge regardless of header or stored-token state."""
+ m2m = _make_oauth2_server("m2m", oauth2_flow="client_credentials")
+ await self._run(m2m, None, has_stored_token=False)
+ await self._run(m2m, self.LITELLM_KEY_HEADERS, has_stored_token=False)
+
+ @pytest.mark.asyncio
+ async def test_unstamped_m2m_shape_never_challenges(self):
+ """A legacy row with oauth2_flow left null but the M2M shape
+ (client_id + client_secret + token_url) resolves to client_credentials
+ via effective_oauth2_flow exactly as egress does, so it is treated as
+ M2M and never challenged. The bare oauth2_flow column would misread it
+ as interactive and raise a spurious 401."""
+ unstamped = _make_oauth2_server(
+ "unstampedm2m",
+ oauth2_flow=None,
+ client_id="cid",
+ client_secret="csecret",
+ token_url="https://idp.test/token",
+ )
+ await self._run(unstamped, None, has_stored_token=False)
+ await self._run(unstamped, self.LITELLM_KEY_HEADERS, has_stored_token=False)
+
+ @pytest.mark.asyncio
+ async def test_delegate_challenges_only_when_bearer_absent(self):
+ """delegate_auth_to_upstream: a present bearer IS the upstream token,
+ so challenge only when it is absent."""
+ delegate = _make_oauth2_server("delegate", delegate_auth_to_upstream=True)
+ with pytest.raises(HTTPException) as exc:
+ await self._run(delegate, None, has_stored_token=False)
+ assert exc.value.status_code == 401
+ await self._run(delegate, self.LITELLM_KEY_HEADERS, has_stored_token=False)