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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(mcp): bind client-forwarded Authorization to a single upstream
Tin732832d3426a
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.pyindex fd978e5bfc9..acb7c80c5b7 100644--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py@@ -368,6 +368,54 @@ def _take_forwarded_authorization(return value, _without_authorization(headers)+def _passthrough_token_from_mcp_auth_header(+ mcp_auth_header: Optional[Union[str, dict[str, str]]],+) -> Optional[str]:+ """The caller's per-server upstream credential for a passthrough-mode server, or None.++ Sourced from ``x-mcp-{alias}-authorization`` (string or per-header dict form) or the deprecated+ global ``x-mcp-auth`` fallback. Per-server headers are the multi-server shape: they bind one+ token to one server, so an aggregate scope with several passthrough-mode servers never replays+ a single credential across upstreams. The value is forwarded verbatim, so it must be the full+ header value (e.g. ``Bearer <upstream-token>``)."""+ if isinstance(mcp_auth_header, str):+ return mcp_auth_header or None+ if isinstance(mcp_auth_header, dict):+ return next((v for k, v in mcp_auth_header.items() if k.lower() == "authorization"), None)+ return None+++def _consumes_caller_authorization(server: MCPServer) -> bool:+ """True when this server's egress forwards the caller's request-wide ``Authorization`` upstream:+ the client-forwarded token modes, legacy OAuth pass-through, and legacy upstream-delegated+ interactive oauth2. An unstamped oauth2 row (flow column not yet backfilled) reads as a consumer,+ which errs toward suppression — the fail-safe direction."""+ if server.is_true_passthrough or server.is_oauth_delegate or server.is_oauth_passthrough:+ return True+ return (+ server.auth_type == MCPAuth.oauth2+ and getattr(server, "delegate_auth_to_upstream", False) is True+ and not server.has_client_credentials+ )+++def _caller_authorization_fans_out(+ server: MCPServer,+ scope_servers: Optional[list[MCPServer]],+) -> bool:+ """True when forwarding the caller's request-wide ``Authorization`` to ``server`` inside a+ listing fan-out would replay one credential against multiple upstreams: another server in the+ scope also consumes it (RFC 9700 cross-resource replay). ``scope_servers`` is None for+ explicitly-addressed operations (tool call, get_prompt, read_resource, single-server routes),+ where the client named the one target and the gateway is not choosing recipients."""+ if scope_servers is None:+ return False+ return any(+ other is not None and other.server_id != server.server_id and _consumes_caller_authorization(other)+ for other in scope_servers+ )++def _extract_upstream_auth_failure(exc: BaseException,) -> Optional[tuple[int, Optional[str]]]:@@ -2357,6 +2405,9 @@ class MCPServerManager:inbound_token = subject_tokenif isinstance(spec.config, PassthroughConfig):inbound_token, extra_headers = _take_forwarded_authorization(extra_headers)+ per_server_token = _passthrough_token_from_mcp_auth_header(mcp_auth_header)+ if per_server_token is not None:+ inbound_token = per_server_tokenresolved_auth, extra_headers = await self._resolve_v2_auth(server=server,spec=spec,diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.pyindex 7a854f698c9..4fda15f664e 100644--- a/litellm/proxy/_experimental/mcp_server/server.py+++ b/litellm/proxy/_experimental/mcp_server/server.py@@ -331,6 +331,7 @@ if MCP_AVAILABLE:)from litellm.proxy._experimental.mcp_server.mcp_server_manager import (MCPServerManager,+ _caller_authorization_fans_out,_client_forwarded_authorization_headers,_should_strip_caller_authorization,_without_authorization,@@ -1529,8 +1530,16 @@ if MCP_AVAILABLE:oauth2_headers: Optional[Dict[str, str]],raw_headers: Optional[Dict[str, str]],user_api_key_auth: Optional[UserAPIKeyAuth] = None,+ scope_servers: Optional[list[MCPServer]] = None,) -> Tuple[Optional[Union[Dict[str, str], str]], Optional[Dict[str, str]]]:- """Build auth and extra headers for a server."""+ """Build auth and extra headers for a server.++ ``scope_servers`` is the full server list a fan-out handler iterates. Passing it lets the+ client-forwarded token modes withhold the caller's request-wide ``Authorization`` when+ another server in the scope would also receive it (``_caller_authorization_fans_out``);+ explicitly-addressed operations leave it None. Per-server ``x-mcp-{alias}-authorization``+ headers are unaffected — they bind one token to one server and are the multi-server shape.+ """server_auth_header: Optional[Union[Dict[str, str], str]] = Noneif mcp_server_auth_headers:from litellm.proxy._experimental.mcp_server.utils import (@@ -1563,12 +1572,13 @@ if MCP_AVAILABLE:):extra_headers = _without_authorization(extra_headers)elif server.is_true_passthrough or server.is_oauth_delegate:- extra_headers = _client_forwarded_authorization_headers(- mcp_server=server,- oauth2_headers=oauth2_headers,- raw_headers=raw_headers,- user_api_key_auth=user_api_key_auth,- )+ if not _caller_authorization_fans_out(server, scope_servers):+ extra_headers = _client_forwarded_authorization_headers(+ mcp_server=server,+ oauth2_headers=oauth2_headers,+ raw_headers=raw_headers,+ user_api_key_auth=user_api_key_auth,+ )if server.extra_headers and raw_headers:if extra_headers is None:@@ -1793,6 +1803,7 @@ if MCP_AVAILABLE:oauth2_headers=oauth2_headers,raw_headers=raw_headers,user_api_key_auth=user_api_key_auth,+ scope_servers=allowed_mcp_servers,)# Prefer server-stored per-user OAuth when configured, so a stale@@ -1979,6 +1990,7 @@ if MCP_AVAILABLE:oauth2_headers=oauth2_headers,raw_headers=raw_headers,user_api_key_auth=user_api_key_auth,+ scope_servers=allowed_mcp_servers,)try:@@ -2031,6 +2043,7 @@ if MCP_AVAILABLE:oauth2_headers=oauth2_headers,raw_headers=raw_headers,user_api_key_auth=user_api_key_auth,+ scope_servers=allowed_mcp_servers,)try:@@ -2081,6 +2094,7 @@ if MCP_AVAILABLE:oauth2_headers=oauth2_headers,raw_headers=raw_headers,user_api_key_auth=user_api_key_auth,+ scope_servers=allowed_mcp_servers,)try:diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.pyindex b24457deabd..71ece75b2fb 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py@@ -69,9 +69,7 @@ async def test_mcp_server_tool_call_body_contains_request_data():# Mock the add_litellm_data_to_request function to capture the datacaptured_data = {}- async def mock_add_litellm_data_to_request(- data, request, user_api_key_dict, proxy_config- ):+ async def mock_add_litellm_data_to_request(data, request, user_api_key_dict, proxy_config):captured_data.update(data)# Simulate the proxy_server_request creationcaptured_data["proxy_server_request"] = {@@ -355,6 +353,79 @@ def test_prepare_mcp_server_headers_m2m_skips_authorization_from_raw_extra_headeassert extra_headers.get("X-Custom") == "trace"+def _client_forwarded_mode_server(server_id: str, auth_type) -> MCPServer:+ return MCPServer(+ server_id=server_id,+ name=server_id,+ transport=MCPTransport.http,+ auth_type=auth_type,+ )+++def _prepare_headers_in_scope(server: MCPServer, scope_servers):+ from litellm.proxy._experimental.mcp_server.server import (+ _prepare_mcp_server_headers,+ )++ return _prepare_mcp_server_headers(+ server=server,+ mcp_server_auth_headers=None,+ mcp_auth_header=None,+ oauth2_headers={"Authorization": "Bearer upstream-token"},+ raw_headers={+ "x-litellm-api-key": "Bearer sk-litellm-key",+ "authorization": "Bearer upstream-token",+ },+ user_api_key_auth=UserAPIKeyAuth(api_key="sk-litellm-key"),+ scope_servers=scope_servers,+ )+++def test_prepare_mcp_server_headers_withholds_global_authorization_when_scope_fans_out():+ """One caller bearer must not be replayed against multiple upstreams (RFC 9700+ cross-resource replay): in a fan-out scope with a second Authorization-consuming+ server, the client-forwarded modes get no global Authorization."""+ delegate = _client_forwarded_mode_server("od-fanout", MCPAuth.oauth_delegate)+ second_consumer = _client_forwarded_mode_server("tp-fanout", MCPAuth.true_passthrough)++ _, extra_headers = _prepare_headers_in_scope(delegate, [delegate, second_consumer])++ assert not extra_headers or "authorization" not in {k.lower() for k in extra_headers}+++def test_prepare_mcp_server_headers_forwards_global_authorization_to_sole_consumer():+ """Non-consuming servers (static api_key) in scope do not make the forward ambiguous."""+ delegate = _client_forwarded_mode_server("od-sole", MCPAuth.oauth_delegate)+ static_server = MCPServer(+ server_id="static-api-key",+ name="static-api-key",+ transport=MCPTransport.http,+ auth_type=MCPAuth.api_key,+ authentication_token="static-key",+ )++ _, extra_headers = _prepare_headers_in_scope(delegate, [delegate, static_server])++ assert extra_headers == {"Authorization": "Bearer upstream-token"}+++def test_prepare_mcp_server_headers_scope_counts_legacy_delegate_as_consumer():+ """Legacy upstream-delegated oauth2 servers still receive the caller's Authorization on the+ v1 path, so their presence in scope must suppress the new modes' forward too."""+ delegate = _client_forwarded_mode_server("od-vs-legacy", MCPAuth.oauth_delegate)+ legacy_delegate = MCPServer(+ server_id="legacy-delegate",+ name="legacy-delegate",+ transport=MCPTransport.http,+ auth_type=MCPAuth.oauth2,+ delegate_auth_to_upstream=True,+ )++ _, extra_headers = _prepare_headers_in_scope(delegate, [delegate, legacy_delegate])++ assert not extra_headers or "authorization" not in {k.lower() for k in extra_headers}++@pytest.mark.asyncioasync def test_call_tool_m2m_skips_authorization_headers():"""M2M call_tool must not forward caller Authorization in oauth2/raw headers."""@@ -382,9 +453,7 @@ async def test_call_tool_m2m_skips_authorization_headers():mock_client = MagicMock()mock_client.call_tool = AsyncMock(return_value=MagicMock())- with patch.object(- manager, "_create_mcp_client", new=AsyncMock(return_value=mock_client)- ) as create_client_mock:+ with patch.object(manager, "_create_mcp_client", new=AsyncMock(return_value=mock_client)) as create_client_mock:await manager._call_regular_mcp_tool(mcp_server=server,original_tool_name="echo",@@ -879,9 +948,7 @@ async def test_get_tools_from_mcp_servers_continues_when_one_server_fails():# Mock global_mcp_server_managermock_manager = MagicMock()- mock_manager.get_allowed_mcp_servers = AsyncMock(- return_value=["working_server", "failing_server"]- )+ mock_manager.get_allowed_mcp_servers = AsyncMock(return_value=["working_server", "failing_server"])mock_manager.get_mcp_server_by_id = lambda server_id: (working_server if server_id == "working_server" else failing_server)@@ -942,9 +1009,7 @@ async def test_get_tools_from_mcp_servers_continues_when_one_server_fails():)# Verify success logging- mock_logger.info.assert_any_call(- "Successfully fetched 1 tools total from all MCP servers"- )+ mock_logger.info.assert_any_call("Successfully fetched 1 tools total from all MCP servers")@pytest.mark.asyncio@@ -985,9 +1050,7 @@ async def test_get_tools_from_mcp_servers_handles_all_servers_failing():# Mock global_mcp_server_managermock_manager = MagicMock()- mock_manager.get_allowed_mcp_servers = AsyncMock(- return_value=["failing_server1", "failing_server2"]- )+ mock_manager.get_allowed_mcp_servers = AsyncMock(return_value=["failing_server1", "failing_server2"])mock_manager.get_mcp_server_by_id = lambda server_id: (failing_server1 if server_id == "failing_server1" else failing_server2)@@ -1042,9 +1105,7 @@ async def test_get_tools_from_mcp_servers_handles_all_servers_failing():)# Verify total logging- mock_logger.info.assert_any_call(- "Successfully fetched 0 tools total from all MCP servers"- )+ mock_logger.info.assert_any_call("Successfully fetched 0 tools total from all MCP servers")@pytest.mark.asyncio@@ -1069,9 +1130,7 @@ async def test_mcp_server_tool_call_body_with_none_arguments():# Mock the add_litellm_data_to_request function to capture the datacaptured_data = {}- async def mock_add_litellm_data_to_request(- data, request, user_api_key_dict, proxy_config- ):+ async def mock_add_litellm_data_to_request(data, request, user_api_key_dict, proxy_config):captured_data.update(data)captured_data["proxy_server_request"] = {"url": str(request.url),@@ -1176,31 +1235,29 @@ async def test_concurrent_initialize_session_managers():results = await asyncio.gather(*tasks, return_exceptions=True)# All tasks should complete successfully (no exceptions)- assert all(- result == "success" for result in results- ), f"Some tasks failed: {results}"+ assert all(result == "success" for result in results), f"Some tasks failed: {results}"# Each session manager.run() should only be called once due to the lock- assert (- mock_stateless_run.call_count == 1- ), f"Expected 1 call to session_manager_stateless.run(), got {mock_stateless_run.call_count}"- assert (- mock_stateful_run.call_count == 1- ), f"Expected 1 call to session_manager_stateful.run(), got {mock_stateful_run.call_count}"- assert (- mock_sse_run.call_count == 1- ), f"Expected 1 call to sse_session_manager.run(), got {mock_sse_run.call_count}"+ assert mock_stateless_run.call_count == 1, (+ f"Expected 1 call to session_manager_stateless.run(), got {mock_stateless_run.call_count}"+ )+ assert mock_stateful_run.call_count == 1, (+ f"Expected 1 call to session_manager_stateful.run(), got {mock_stateful_run.call_count}"+ )+ assert mock_sse_run.call_count == 1, (+ f"Expected 1 call to sse_session_manager.run(), got {mock_sse_run.call_count}"+ )# The context managers should only be entered once each- assert (- mock_cm_stateless.__aenter__.call_count == 1- ), f"Expected 1 call to stateless __aenter__, got {mock_cm_stateless.__aenter__.call_count}"- assert (- mock_cm_stateful.__aenter__.call_count == 1- ), f"Expected 1 call to stateful __aenter__, got {mock_cm_stateful.__aenter__.call_count}"- assert (- mock_cm_sse.__aenter__.call_count == 1- ), f"Expected 1 call to sse __aenter__, got {mock_cm_sse.__aenter__.call_count}"+ assert mock_cm_stateless.__aenter__.call_count == 1, (+ f"Expected 1 call to stateless __aenter__, got {mock_cm_stateless.__aenter__.call_count}"+ )+ assert mock_cm_stateful.__aenter__.call_count == 1, (+ f"Expected 1 call to stateful __aenter__, got {mock_cm_stateful.__aenter__.call_count}"+ )+ assert mock_cm_sse.__aenter__.call_count == 1, (+ f"Expected 1 call to sse __aenter__, got {mock_cm_sse.__aenter__.call_count}"+ )# State should be properly setassert mcp_server._SESSION_MANAGERS_INITIALIZED is True@@ -1343,16 +1400,12 @@ async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless():# initialize → statefulinit_body = b'{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":"2024-11-05"}}'stateless_called, stateful_called = await make_request(init_body)- assert (- stateful_called and not stateless_called- ), "initialize (no session) should route to stateful, not stateless"+ assert stateful_called and not stateless_called, "initialize (no session) should route to stateful, not stateless"# tools/list → statelesstools_body = b'{"jsonrpc":"2.0","id":2,"method":"tools/list","params":{}}'stateless_called, stateful_called = await make_request(tools_body)- assert (- stateless_called and not stateful_called- ), "tools/list (no session) should route to stateless, not stateful"+ assert stateless_called and not stateful_called, "tools/list (no session) should route to stateless, not stateful"@pytest.mark.asyncio@@ -1437,9 +1490,9 @@ async def test_mcp_routing_chunked_initialize_to_stateful():):await handle_streamable_http_mcp(scope, receive, send)- assert (- stateful_called and not stateless_called- ), "chunked initialize (no session) should route to stateful, not stateless"+ assert stateful_called and not stateless_called, (+ "chunked initialize (no session) should route to stateful, not stateless"+ )@pytest.mark.asyncio@@ -1468,10 +1521,7 @@ async def test_mcp_routing_caps_body_peek_for_oversized_chunked_body():messages = [{"type": "http.request", "body": first_chunk, "more_body": True},- *[- {"type": "http.request", "body": chunk, "more_body": True}- for chunk in oversized_tail- ],+ *[{"type": "http.request", "body": chunk, "more_body": True} for chunk in oversized_tail],{"type": "http.request", "body": b"", "more_body": False},]receive_calls = {"count": 0}@@ -1522,12 +1572,8 @@ async def test_mcp_routing_caps_body_peek_for_oversized_chunked_body():"litellm.proxy._experimental.mcp_server.server._SESSION_MANAGERS_INITIALIZED",True,),- patch.object(- session_manager_stateless, "handle_request", side_effect=stateless_handle- ),- patch.object(- session_manager_stateful, "handle_request", side_effect=stateful_handle- ),+ patch.object(session_manager_stateless, "handle_request", side_effect=stateless_handle),+ patch.object(session_manager_stateful, "handle_request", side_effect=stateful_handle),patch.object(session_manager_stateless, "_server_instances", {}),patch.object(session_manager_stateful, "_server_instances", {}),):@@ -1578,9 +1624,7 @@ async def test_enforce_stateful_session_cap_evicts_oldest_idle_then_rejects():patch.object(session_manager_stateful, "_server_instances", instances),patch.object(mcp_server, "_MAX_STATEFUL_SESSIONS_PER_OWNER", 3),patch.dict(mcp_server._stateful_session_owners, owners, clear=True),- patch.dict(- mcp_server._stateful_session_auth_context_last_seen, last_seen, clear=True- ),+ patch.dict(mcp_server._stateful_session_auth_context_last_seen, last_seen, clear=True),patch.dict(mcp_server._stateful_session_auth_contexts, contexts, clear=True),patch.dict(mcp_server._stateful_session_active_request_counts, {}, clear=True),):@@ -1593,9 +1637,7 @@ async def test_enforce_stateful_session_cap_evicts_oldest_idle_then_rejects():# A different owner at the cap is unaffected by owner-A's sessions.terminated.clear()- allowed_other = await mcp_server._enforce_stateful_session_cap_for_owner(- "owner-B"- )+ allowed_other = await mcp_server._enforce_stateful_session_cap_for_owner("owner-B")assert allowed_other is Trueassert terminated == []@@ -1614,9 +1656,7 @@ async def test_enforce_stateful_session_cap_evicts_oldest_idle_then_rejects():{f"s{i}": float(i) for i in range(3)},clear=True,),- patch.dict(- mcp_server._stateful_session_active_request_counts, active, clear=True- ),+ patch.dict(mcp_server._stateful_session_active_request_counts, active, clear=True),):rejected = await mcp_server._enforce_stateful_session_cap_for_owner("owner-A")assert rejected is False@@ -1662,9 +1702,7 @@ async def test_mcp_routing_initialize_rejected_when_owner_at_session_cap():(b"authorization", b"Bearer test-key"),],}- receive = AsyncMock(- return_value={"type": "http.request", "body": init_body, "more_body": False}- )+ receive = AsyncMock(return_value={"type": "http.request", "body": init_body, "more_body": False})send = AsyncMock()stateful_called = []@@ -1685,9 +1723,7 @@ async def test_mcp_routing_initialize_rejected_when_owner_at_session_cap():),patch.object(mcp_server, "_owner_fingerprint_for", return_value="owner-X"),patch.object(mcp_server, "_MAX_STATEFUL_SESSIONS_PER_OWNER", cap),- patch.object(- session_manager_stateful, "handle_request", side_effect=stateful_handle- ),+ patch.object(session_manager_stateful, "handle_request", side_effect=stateful_handle),patch.object(session_manager_stateful, "_server_instances", instances),patch.object(session_manager_stateless, "_server_instances", {}),patch.dict(mcp_server._stateful_session_owners, owners, clear=True),@@ -1696,18 +1732,14 @@ async def test_mcp_routing_initialize_rejected_when_owner_at_session_cap():{f"s{i}": float(i) for i in range(cap)},clear=True,),- patch.dict(- mcp_server._stateful_session_active_request_counts, active, clear=True- ),+ patch.dict(mcp_server._stateful_session_active_request_counts, active, clear=True),patch.dict(mcp_server._stateful_session_auth_contexts, contexts, clear=True),):await handle_streamable_http_mcp(scope, receive, send)assert not stateful_called, "initialize at session cap must not reach the manager"start_messages = [- call.args[0]- for call in send.call_args_list- if call.args and call.args[0].get("type") == "http.response.start"+ call.args[0] for call in send.call_args_list if call.args and call.args[0].get("type") == "http.response.start"]assert start_messages, "a response should have been sent"assert start_messages[0]["status"] == 429@@ -1743,9 +1775,7 @@ async def test_stateful_mcp_requests_refresh_session_auth_context():None,"1.1.1.1",)- mcp_server._stateful_session_auth_contexts[session_id] = callback_context.run(- mcp_server.auth_context_var.get- )+ mcp_server._stateful_session_auth_contexts[session_id] = callback_context.run(mcp_server.auth_context_var.get)scope = {"type": "http",@@ -2006,24 +2036,14 @@ async def test_initialize_request_with_existing_session_tracks_new_session():)await mcp_server._purge_expired_stateful_session_auth_contexts(now=now)assert new_session_id in mcp_server._stateful_session_auth_contexts- assert (- mcp_server._stateful_session_auth_contexts[new_session_id]- is not existing_auth_user- )- assert (- mcp_server._stateful_session_auth_contexts[new_session_id].mcp_auth_header- == "new-mcp-auth"- )+ assert mcp_server._stateful_session_auth_contexts[new_session_id] is not existing_auth_user+ assert mcp_server._stateful_session_auth_contexts[new_session_id].mcp_auth_header == "new-mcp-auth"async def stateless_handle(s, r, se):- raise AssertionError(- "initialize request with session should use stateful manager"- )+ raise AssertionError("initialize request with session should use stateful manager")try:- mcp_server._stateful_session_auth_contexts[existing_session_id] = (- existing_auth_user- )+ mcp_server._stateful_session_auth_contexts[existing_session_id] = existing_auth_usermcp_server._stateful_session_auth_context_last_seen[existing_session_id] = 1.0mcp_server._stateful_session_owners[existing_session_id] = owner_fingerprint@@ -2065,10 +2085,7 @@ async def test_initialize_request_with_existing_session_tracks_new_session():assert stateful_calledassert new_session_id not in mcp_server._stateful_session_active_request_countsassert new_session_id in mcp_server._stateful_session_auth_contexts- assert (- mcp_server._stateful_session_auth_contexts[existing_session_id]- is existing_auth_user- )+ assert mcp_server._stateful_session_auth_contexts[existing_session_id] is existing_auth_userassert existing_auth_user.mcp_auth_header == "old-mcp-auth"assert existing_auth_user.mcp_servers == ["old-server"]finally:@@ -2191,15 +2208,11 @@ async def test_stateful_mcp_cleanup_loop_survives_purge_errors():except ImportError:pytest.skip("MCP server not available")- purge = AsyncMock(- side_effect=[RuntimeError("terminate failed"), asyncio.CancelledError()]- )+ purge = AsyncMock(side_effect=[RuntimeError("terminate failed"), asyncio.CancelledError()])with (patch.object(mcp_server.asyncio, "sleep", AsyncMock(return_value=None)),- patch.object(- mcp_server, "_purge_expired_stateful_session_auth_contexts", purge- ),+ patch.object(mcp_server, "_purge_expired_stateful_session_auth_contexts", purge),):with pytest.raises(asyncio.CancelledError):await mcp_server._cleanup_expired_stateful_session_auth_contexts()@@ -2289,9 +2302,7 @@ async def test_stateful_mcp_session_owner_mismatch_returns_403():intruder_auth = UserAPIKeyAuth(api_key="intruder-key", user_id="intruder")mcp_server._stateful_session_auth_contexts[session_id] = MagicMock()- mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(- owner_auth- )+ mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(owner_auth)scope = {"type": "http",@@ -2341,9 +2352,7 @@ async def test_stateful_mcp_session_owner_mismatch_returns_403():await handle_streamable_http_mcp(scope, receive, capture_send)handle_request_mock.assert_not_awaited()- statuses = [- m["status"] for m in sent_messages if m.get("type") == "http.response.start"- ]+ statuses = [m["status"] for m in sent_messages if m.get("type") == "http.response.start"]assert statuses == [403]mcp_server._stateful_session_auth_contexts.pop(session_id, None)@@ -2368,12 +2377,10 @@ async def test_stateful_mcp_session_serializes_concurrent_requests():session_id = "serialized-session-1"owner_auth = UserAPIKeyAuth(api_key="owner-key", user_id="owner")- mcp_server._stateful_session_auth_contexts[session_id] = (- mcp_server.MCPAuthenticatedUser(user_api_key_auth=owner_auth)- )- mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(- owner_auth+ mcp_server._stateful_session_auth_contexts[session_id] = mcp_server.MCPAuthenticatedUser(+ user_api_key_auth=owner_auth)+ mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(owner_auth)inside = 0max_inside = 0@@ -2414,9 +2421,7 @@ async def test_stateful_mcp_session_serializes_concurrent_requests():"litellm.proxy._experimental.mcp_server.server._SESSION_MANAGERS_INITIALIZED",True,),- patch.object(- session_manager_stateful, "handle_request", side_effect=slow_handle- ),+ patch.object(session_manager_stateful, "handle_request", side_effect=slow_handle),patch.object(session_manager_stateful,"_server_instances",@@ -2432,9 +2437,7 @@ async def test_stateful_mcp_session_serializes_concurrent_requests():mcp_server._stateful_session_owners.pop(session_id, None)mcp_server._stateful_session_locks.pop(session_id, None)- assert (- max_inside == 1- ), "concurrent requests on same stateful session must be serialized"+ assert max_inside == 1, "concurrent requests on same stateful session must be serialized"@pytest.mark.asyncio@@ -2486,9 +2489,7 @@ async def test_stateful_mcp_lock_does_not_leak_when_auth_context_missing():"litellm.proxy._experimental.mcp_server.server._SESSION_MANAGERS_INITIALIZED",True,),- patch.object(- session_manager_stateful, "handle_request", side_effect=handle- ),+ patch.object(session_manager_stateful, "handle_request", side_effect=handle),patch.object(session_manager_stateful,"_server_instances",@@ -2498,9 +2499,9 @@ async def test_stateful_mcp_lock_does_not_leak_when_auth_context_missing():assert session_id not in mcp_server._stateful_session_auth_contextsawait handle_streamable_http_mcp(scope, receive, AsyncMock())- assert (- session_id not in mcp_server._stateful_session_locks- ), "lock entry must be cleaned up for untracked stateful session"+ assert session_id not in mcp_server._stateful_session_locks, (+ "lock entry must be cleaned up for untracked stateful session"+ )finally:mcp_server._stateful_session_auth_contexts.pop(session_id, None)mcp_server._stateful_session_owners.pop(session_id, None)@@ -2526,12 +2527,10 @@ async def test_stateful_mcp_get_stream_does_not_block_post():session_id = "stream-session-1"owner_auth = UserAPIKeyAuth(api_key="owner-key", user_id="owner")- mcp_server._stateful_session_auth_contexts[session_id] = (- mcp_server.MCPAuthenticatedUser(user_api_key_auth=owner_auth)- )- mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(- owner_auth+ mcp_server._stateful_session_auth_contexts[session_id] = mcp_server.MCPAuthenticatedUser(+ user_api_key_auth=owner_auth)+ mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(owner_auth)stream_release = asyncio.Event()post_finished = asyncio.Event()@@ -2569,9 +2568,7 @@ async def test_stateful_mcp_get_stream_does_not_block_post():"litellm.proxy._experimental.mcp_server.server._SESSION_MANAGERS_INITIALIZED",True,),- patch.object(- session_manager_stateful, "handle_request", side_effect=handle- ),+ patch.object(session_manager_stateful, "handle_request", side_effect=handle),patch.object(session_manager_stateful,"_server_instances",@@ -2615,10 +2612,7 @@ def test_jsonrpc_text_has_top_level_method_ignores_nested_method():assert _jsonrpc_text_has_top_level_method(reordered) is True# response whose result nests a "method" key (and arrays of them)- response = (- '{"jsonrpc":"2.0","id":1,"result":{"toolResult":{"method":"GET"},'- '"steps":[{"method":"x"}]}}'- )+ response = '{"jsonrpc":"2.0","id":1,"result":{"toolResult":{"method":"GET"},"steps":[{"method":"x"}]}}'assert _jsonrpc_text_has_top_level_method(response) is False# truncated response: result value never closes, no top-level method seen@@ -2643,12 +2637,10 @@ async def test_truncated_jsonrpc_response_with_nested_method_skips_lock():session_id = "nested-method-response-session"owner_auth = UserAPIKeyAuth(api_key="owner-key", user_id="owner")- mcp_server._stateful_session_auth_contexts[session_id] = (- mcp_server.MCPAuthenticatedUser(user_api_key_auth=owner_auth)- )- mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(- owner_auth+ mcp_server._stateful_session_auth_contexts[session_id] = mcp_server.MCPAuthenticatedUser(+ user_api_key_auth=owner_auth)+ mcp_server._stateful_session_owners[session_id] = mcp_server._owner_fingerprint_for(owner_auth)gate = asyncio.Event()request_in_handle = asyncio.Event()@@ -2685,8 +2677,7 @@ async def test_truncated_jsonrpc_response_with_nested_method_skips_lock():# parsed, with a nested "method" key in the first bytes to trip a flat# substring heuristic.response_body = (- '{"jsonrpc":"2.0","id":99,"result":{"toolResult":'- '{"method":"GET","payload":"' + ("x" * 5000) + '"}}}'+ '{"jsonrpc":"2.0","id":99,"result":{"toolResult":{"method":"GET","payload":"' + ("x" * 5000) + '"}}}').encode()try:@@ -2700,9 +2691,7 @@ async def test_truncated_jsonrpc_response_with_nested_method_skips_lock():"litellm.proxy._experimental.mcp_server.server._SESSION_MANAGERS_INITIALIZED",True,),- patch.object(- session_manager_stateful, "handle_request", side_effect=handle- ),+ patch.object(session_manager_stateful, "handle_request", side_effect=handle),patch.object(session_manager_stateful,"_server_instances",@@ -2774,13 +2763,9 @@ async def test_mcp_routing_with_conflicting_alias_and_group_name():mock_get_tools_spy = AsyncMock(return_value=[])# Mock the function that checks DB for an access group named "custom_solutions"- mock_db_lookup = AsyncMock(- return_value=[specific_server.server_id, other_server.server_id]- )+ mock_db_lookup = AsyncMock(return_value=[specific_server.server_id, other_server.server_id])- mock_get_allowed = AsyncMock(- return_value=[specific_server.server_id, other_server.server_id]- )+ mock_get_allowed = AsyncMock(return_value=[specific_server.server_id, other_server.server_id])with (patch(@@ -2805,14 +2790,12 @@ async def test_mcp_routing_with_conflicting_alias_and_group_name():)# Get the list of actual server objects that the orchestrator tried to contact- called_servers = [- call.kwargs["server"] for call in mock_get_tools_spy.call_args_list- ]+ called_servers = [call.kwargs["server"] for call in mock_get_tools_spy.call_args_list]assert len(called_servers) == 1, "Should have resolved to exactly one server."- assert (- called_servers[0].server_id == specific_server.server_id- ), "Should have contacted the specific server alias, not the group."+ assert called_servers[0].server_id == specific_server.server_id, (+ "Should have contacted the specific server alias, not the group."+ )@pytest.mark.asyncio@@ -2926,9 +2909,7 @@ async def test_oauth2_caller_headers_not_forwarded_for_migrated_server():)# Verify that _create_mcp_client was called- assert (- mock_create_client.call_count == 1- ), "Expected _create_mcp_client to be called once"+ assert mock_create_client.call_count == 1, "Expected _create_mcp_client to be called once"# Verify the server passed to _create_mcp_client is the OAuth2 serverassert captured_client_args["server"].server_id == oauth2_server.server_id@@ -2938,9 +2919,9 @@ async def test_oauth2_caller_headers_not_forwarded_for_migrated_server():# oauth2 Authorization upstream. The v2 resolver injects the stored per-user token,# so a caller-supplied bearer cannot override another user's stored credential.extra_headers = captured_client_args["extra_headers"]- assert extra_headers is None or "Authorization" not in {- k.lower() for k in extra_headers- }, f"Caller Authorization must not be forwarded, got {extra_headers}"+ assert extra_headers is None or "Authorization" not in {k.lower() for k in extra_headers}, (+ f"Caller Authorization must not be forwarded, got {extra_headers}"+ )@pytest.mark.asyncio@@ -3049,12 +3030,8 @@ async def test_list_tools_multiple_servers_prefixed_names():# Mock managermock_manager = MagicMock()- mock_manager.get_allowed_mcp_servers = AsyncMock(- return_value=["server1", "server2"]- )- mock_manager.get_mcp_server_by_id = lambda server_id: (- server1 if server_id == "server1" else server2- )+ mock_manager.get_allowed_mcp_servers = AsyncMock(return_value=["server1", "server2"])+ mock_manager.get_mcp_server_by_id = lambda server_id: server1 if server_id == "server1" else server2# Mock filter_server_ids_by_ip to return server_ids unchanged (no IP filtering)mock_manager.filter_server_ids_by_ip_with_info = lambda server_ids, client_ip: (server_ids,@@ -3653,9 +3630,7 @@ async def test_list_tools_strips_prefix_when_matching_permissions():tool1.inputSchema = {}tool2 = MagicMock()- tool2.name = (- "GITMCP-search_litellm_documentation" # Prefixed, not in allowed list- )+ tool2.name = "GITMCP-search_litellm_documentation" # Prefixed, not in allowed listtool2.description = "Search docs"tool2.inputSchema = {}@@ -3876,17 +3851,13 @@ class TestMCPServerManagerReload:db_row = _make_db_mcp_server("server-1", timestamp)mock_prisma = MagicMock()- mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(- return_value=[db_row]- )+ mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(return_value=[db_row])with (patch("litellm.proxy.management_endpoints.mcp_management_endpoints.get_prisma_client_or_throw",return_value=mock_prisma,),- patch.object(- manager, "build_mcp_server_from_table", AsyncMock()- ) as mock_build,+ patch.object(manager, "build_mcp_server_from_table", AsyncMock()) as mock_build,):await manager.reload_servers_from_database()@@ -3922,9 +3893,7 @@ class TestMCPServerManagerReload:)mock_prisma = MagicMock()- mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(- return_value=[db_row]- )+ mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(return_value=[db_row])with (patch("litellm.proxy.management_endpoints.mcp_management_endpoints.get_prisma_client_or_throw",@@ -4049,9 +4018,7 @@ class TestMCPServerManagerReload:raise RuntimeError("blocked address")mock_prisma = MagicMock()- mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(- return_value=[healthy_row, bad_openapi_row]- )+ mock_prisma.db.litellm_mcpservertable.find_many = AsyncMock(return_value=[healthy_row, bad_openapi_row])with (patch("litellm.proxy.management_endpoints.mcp_management_endpoints.get_prisma_client_or_throw",@@ -4147,10 +4114,7 @@ async def test_call_mcp_tool_logs_failure_via_post_call_failure_hook():)proxy_logging_mock.post_call_failure_hook.assert_awaited_once()- assert (- proxy_logging_mock.post_call_failure_hook.await_args.kwargs.get("route")- == "/mcp/call_tool"- )+ assert proxy_logging_mock.post_call_failure_hook.await_args.kwargs.get("route") == "/mcp/call_tool"@pytest.mark.asyncio@@ -4232,9 +4196,7 @@ async def test_get_tools_from_mcp_servers_logs_list_tools_to_spendlogs_when_enabassert tools == [tool_1]dummy_logging_obj.async_success_handler.assert_awaited_once()- assert dummy_logging_obj.async_success_handler.await_args.kwargs["result"] == [- tool_1.model_dump(mode="json")- ]+ assert dummy_logging_obj.async_success_handler.await_args.kwargs["result"] == [tool_1.model_dump(mode="json")]assert function_setup_kwargs["metadata"]["tags"] == ["team-a"]spend_meta = dummy_logging_obj.model_call_details["metadata"]["spend_logs_metadata"]@@ -4580,9 +4542,7 @@ async def test_get_tools_from_mcp_servers_injects_stored_oauth2_token():oauth2_server.extra_headers = None# Simulate the DB returning a valid credential for this user+server- prefetched_creds = {- SERVER_ID: {"access_token": STORED_TOKEN, "server_id": SERVER_ID}- }+ prefetched_creds = {SERVER_ID: {"access_token": STORED_TOKEN, "server_id": SERVER_ID}}tool_1 = MagicMock()tool_1.name = "atlassian_test-search"@@ -4689,16 +4649,12 @@ class TestMergeGatewayInitializeInstructions:global_mcp_server_manager,)- global_mcp_server_manager._upstream_initialize_instructions_by_server_id[- "s1"- ] = "upstream"+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id["s1"] = "upstream"try:s = _make_instruction_server(instructions="yaml wins")assert self._merge([s]) == "yaml wins"finally:- global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop(- "s1", None- )+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop("s1", None)def test_upstream_cache_used_when_no_yaml(self):"""Upstream cached instructions are used when no YAML override is set."""@@ -4706,16 +4662,12 @@ class TestMergeGatewayInitializeInstructions:global_mcp_server_manager,)- global_mcp_server_manager._upstream_initialize_instructions_by_server_id[- "s1"- ] = "from upstream"+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id["s1"] = "from upstream"try:s = _make_instruction_server(instructions=None)assert self._merge([s]) == "from upstream"finally:- global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop(- "s1", None- )+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop("s1", None)def test_spec_path_servers_skipped(self):"""OpenAPI (spec_path) servers do not contribute instructions."""@@ -4729,12 +4681,8 @@ class TestMergeGatewayInitializeInstructions:def test_multiple_servers_merged_with_labels(self):"""Multiple servers get label-prefixed and separator-joined."""- s1 = _make_instruction_server(- server_id="a", name="a", alias="Alpha", instructions="instr A"- )- s2 = _make_instruction_server(- server_id="b", name="b", alias="Beta", instructions="instr B"- )+ s1 = _make_instruction_server(server_id="a", name="a", alias="Alpha", instructions="instr A")+ s2 = _make_instruction_server(server_id="b", name="b", alias="Beta", instructions="instr B")result = self._merge([s1, s2])assert result is not Noneassert "[Alpha]" in result and "[Beta]" in result@@ -4754,25 +4702,17 @@ class TestMergeGatewayInitializeInstructions:global_mcp_server_manager,)- global_mcp_server_manager._upstream_initialize_instructions_by_server_id[- "c"- ] = "cached C"+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id["c"] = "cached C"try:- s_yaml = _make_instruction_server(- server_id="a", name="a", alias="A", instructions="yaml A"- )- s_spec = _make_instruction_server(- server_id="b", name="b", alias="B", spec_path="/spec.json", url=None- )+ s_yaml = _make_instruction_server(server_id="a", name="a", alias="A", instructions="yaml A")+ s_spec = _make_instruction_server(server_id="b", name="b", alias="B", spec_path="/spec.json", url=None)s_cached = _make_instruction_server(server_id="c", name="c", alias="C")result = self._merge([s_yaml, s_spec, s_cached])assert "yaml A" in resultassert "cached C" in resultassert "[B]" not in resultfinally:- global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop(- "c", None- )+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop("c", None)class TestEnsureUpstreamInitializeInstructionsCached:@@ -4784,15 +4724,9 @@ class TestEnsureUpstreamInitializeInstructionsCached:global_mcp_server_manager,)- server = _make_instruction_server(- server_id="yaml-only", instructions="from yaml"- )- with patch.object(- global_mcp_server_manager, "_create_mcp_client", AsyncMock()- ) as mock_create:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )+ server = _make_instruction_server(server_id="yaml-only", instructions="from yaml")+ with patch.object(global_mcp_server_manager, "_create_mcp_client", AsyncMock()) as mock_create:+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)mock_create.assert_not_awaited()@pytest.mark.asyncio@@ -4804,21 +4738,13 @@ class TestEnsureUpstreamInitializeInstructionsCached:)server = _make_instruction_server(server_id="cached-only", instructions=None)- global_mcp_server_manager._upstream_initialize_instructions_by_server_id[- "cached-only"- ] = "warm"+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id["cached-only"] = "warm"try:- with patch.object(- global_mcp_server_manager, "_create_mcp_client", AsyncMock()- ) as mock_create:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )+ with patch.object(global_mcp_server_manager, "_create_mcp_client", AsyncMock()) as mock_create:+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)mock_create.assert_not_awaited()finally:- global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop(- "cached-only", None- )+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop("cached-only", None)@pytest.mark.asyncioasync def test_skips_when_spec_path_set(self):@@ -4828,15 +4754,9 @@ class TestEnsureUpstreamInitializeInstructionsCached:global_mcp_server_manager,)- server = _make_instruction_server(- server_id="openapi-spec", spec_path="/openapi.json", url=None- )- with patch.object(- global_mcp_server_manager, "_create_mcp_client", AsyncMock()- ) as mock_create:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )+ server = _make_instruction_server(server_id="openapi-spec", spec_path="/openapi.json", url=None)+ with patch.object(global_mcp_server_manager, "_create_mcp_client", AsyncMock()) as mock_create:+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)mock_create.assert_not_awaited()@pytest.mark.asyncio@@ -4858,22 +4778,14 @@ class TestEnsureUpstreamInitializeInstructionsCached:AsyncMock(return_value=fake_client),):try:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)assert (- global_mcp_server_manager._upstream_initialize_instructions_by_server_id[- "cold-server"- ]+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id["cold-server"]== "upstream says hi")finally:- global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop(- "cold-server", None- )- global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop(- "cold-server", None- )+ global_mcp_server_manager._upstream_initialize_instructions_by_server_id.pop("cold-server", None)+ global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop("cold-server", None)@pytest.mark.asyncioasync def test_cooldown_after_empty_upstream_response(self):@@ -4892,27 +4804,13 @@ class TestEnsureUpstreamInitializeInstructionsCached:create = AsyncMock(return_value=fake_client)with patch.object(global_mcp_server_manager, "_create_mcp_client", create):try:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )- assert (- create.await_count == 1- ), "Second probe within cooldown must not reconnect to upstream"- assert (- "empty-server"- not in global_mcp_server_manager._upstream_initialize_instructions_by_server_id- )- assert (- "empty-server"- in global_mcp_server_manager._upstream_initialize_instructions_probed_at- )+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)+ assert create.await_count == 1, "Second probe within cooldown must not reconnect to upstream"+ assert "empty-server" not in global_mcp_server_manager._upstream_initialize_instructions_by_server_id+ assert "empty-server" in global_mcp_server_manager._upstream_initialize_instructions_probed_atfinally:- global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop(- "empty-server", None- )+ global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop("empty-server", None)@pytest.mark.asyncioasync def test_cooldown_after_upstream_failure(self):@@ -4925,35 +4823,19 @@ class TestEnsureUpstreamInitializeInstructionsCached:server = _make_instruction_server(server_id="boom-server", instructions=None)fake_client = MagicMock()- fake_client.run_with_session = AsyncMock(- side_effect=RuntimeError("upstream down")- )+ fake_client.run_with_session = AsyncMock(side_effect=RuntimeError("upstream down"))fake_client._last_initialize_instructions = Nonecreate = AsyncMock(return_value=fake_client)with patch.object(global_mcp_server_manager, "_create_mcp_client", create):try:- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )- await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(- server- )- assert (- create.await_count == 1- ), "Second probe within cooldown must not reconnect after failure"- assert (- "boom-server"- not in global_mcp_server_manager._upstream_initialize_instructions_by_server_id- )- assert (- "boom-server"- in global_mcp_server_manager._upstream_initialize_instructions_probed_at- )+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)+ await global_mcp_server_manager._ensure_upstream_initialize_instructions_cached(server)+ assert create.await_count == 1, "Second probe within cooldown must not reconnect after failure"+ assert "boom-server" not in global_mcp_server_manager._upstream_initialize_instructions_by_server_id+ assert "boom-server" in global_mcp_server_manager._upstream_initialize_instructions_probed_atfinally:- global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop(- "boom-server", None- )+ global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop("boom-server", None)@pytest.mark.asyncioasync def test_reload_resets_probe_cooldown(self):@@ -4962,19 +4844,12 @@ class TestEnsureUpstreamInitializeInstructionsCached:global_mcp_server_manager,)- global_mcp_server_manager._upstream_initialize_instructions_probed_at[- "reload-target"- ] = 1.0+ global_mcp_server_manager._upstream_initialize_instructions_probed_at["reload-target"] = 1.0try:await global_mcp_server_manager.load_servers_from_config({})- assert (- "reload-target"- not in global_mcp_server_manager._upstream_initialize_instructions_probed_at- )+ assert "reload-target" not in global_mcp_server_manager._upstream_initialize_instructions_probed_atfinally:- global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop(- "reload-target", None- )+ global_mcp_server_manager._upstream_initialize_instructions_probed_at.pop("reload-target", None)class TestGatewayCreateInitializationOptions:@@ -5040,9 +4915,7 @@ class TestGatewayCreateInitializationOptions:):assert server.create_initialization_options().server_name == "grafana"- assert (- server.create_initialization_options().server_name == "litellm-mcp-server"- )+ assert server.create_initialization_options().server_name == "litellm-mcp-server"@pytest.mark.asyncioasync def test_sse_handler_scopes_server_name_from_single_server_path(self):@@ -5119,9 +4992,7 @@ class TestGatewayCreateInitializationOptions:await handle_sse_mcp(scope, AsyncMock(), AsyncMock())assert captured["server_name"] == "grafana"- assert (- server.create_initialization_options().server_name == "litellm-mcp-server"- )+ assert server.create_initialization_options().server_name == "litellm-mcp-server"def test_contextvar_set_injects_instructions(self):"""When ContextVar has a value, it appears in InitializationOptions."""@@ -5239,12 +5110,8 @@ async def test_list_tools_with_legacy_db_m2m_server_resolves_oauth2_flow():):mock_manager.get_allowed_mcp_servers = AsyncMock(return_value=["legacy-m2m-id"])mock_manager.get_mcp_server_by_id = MagicMock(return_value=legacy_server)- mock_manager.filter_server_ids_by_ip_with_info = MagicMock(- return_value=(["legacy-m2m-id"], 0)- )- mock_manager._get_tools_from_server = AsyncMock(- side_effect=capture_extra_headers- )+ mock_manager.filter_server_ids_by_ip_with_info = MagicMock(return_value=(["legacy-m2m-id"], 0))+ mock_manager._get_tools_from_server = AsyncMock(side_effect=capture_extra_headers)tools = await _get_tools_from_mcp_servers(user_api_key_auth=user_auth,@@ -5336,10 +5203,8 @@ async def test_call_tool_empty_extra_headers_returns_none():pass # We only care about the captured headers# With P2 fix: extra_headers should be None (not {}) when all headers filtered- assert (- captured_extra_headers is None- ), "P2 API consistency issue: expected None for empty extra_headers, got: " + str(- captured_extra_headers+ assert captured_extra_headers is None, (+ "P2 API consistency issue: expected None for empty extra_headers, got: " + str(captured_extra_headers))@@ -5364,9 +5229,7 @@ async def test_probe_upstream_auth_returns_upstream_status():"litellm.proxy._experimental.mcp_server.server.get_async_httpx_client",return_value=mock_client,):- status, www_auth = await _probe_upstream_auth(- "http://upstream/mcp", "Bearer some-token"- )+ status, www_auth = await _probe_upstream_auth("http://upstream/mcp", "Bearer some-token")assert status == 401assert www_auth == 'Bearer realm="test"'@@ -5393,9 +5256,7 @@ async def test_probe_upstream_auth_surfaces_httpx_status_error():mock_response.status_code = 401mock_response.headers = {"www-authenticate": 'Bearer realm="test"'}request = httpx.Request("POST", "http://upstream/mcp")- error = httpx.HTTPStatusError(- message="401 Unauthorized", request=request, response=mock_response- )+ error = httpx.HTTPStatusError(message="401 Unauthorized", request=request, response=mock_response)mock_client = MagicMock()mock_client.post = AsyncMock(side_effect=error)@@ -5404,9 +5265,7 @@ async def test_probe_upstream_auth_surfaces_httpx_status_error():"litellm.proxy._experimental.mcp_server.server.get_async_httpx_client",return_value=mock_client,):- status, www_auth = await _probe_upstream_auth(- "http://upstream/mcp", "Bearer some-token"- )+ status, www_auth = await _probe_upstream_auth("http://upstream/mcp", "Bearer some-token")assert status == 401assert www_auth == 'Bearer realm="test"'@@ -5424,9 +5283,7 @@ async def test_probe_upstream_auth_fails_open_on_network_error():"litellm.proxy._experimental.mcp_server.server.get_async_httpx_client",return_value=mock_client,):- status, www_auth = await _probe_upstream_auth(- "http://upstream/mcp", "Bearer some-token"- )+ status, www_auth = await _probe_upstream_auth("http://upstream/mcp", "Bearer some-token")assert status == 200assert www_auth is None@@ -6349,9 +6206,7 @@ class TestProxyExceptionToHttpException:from litellm.proxy._types import ProxyExceptionhttp_exc = _proxy_exception_to_http_exception(- ProxyException(- message="Forbidden", type="auth_error", param="key", code=403- )+ ProxyException(message="Forbidden", type="auth_error", param="key", code=403))assert http_exc.status_code == 403@@ -6415,10 +6270,7 @@ class TestStreamableHttpAuthErrorMapping:assert exc_info.value.status_code == 401assert exc_info.value.headers["WWW-Authenticate"] == "Bearer"# Must not have emitted a 500 body via the generic catch-all.- assert not any(- m.get("type") == "http.response.start" and m.get("status") == 500- for m in sent- )+ assert not any(m.get("type") == "http.response.start" and m.get("status") == 500 for m in sent)@pytest.mark.asyncioasync def test_sse_propagates_proxy_exception_as_401(self):@@ -6458,10 +6310,7 @@ class TestStreamableHttpAuthErrorMapping:assert exc_info.value.status_code == 401assert exc_info.value.headers["WWW-Authenticate"] == "Bearer"- assert not any(- m.get("type") == "http.response.start" and m.get("status") == 500- for m in sent- )+ assert not any(m.get("type") == "http.response.start" and m.get("status") == 500 for m in sent)class TestMCPMetaTraceCarrier:diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.pyindex a7a0379c7ad..dc1a950f706 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server_manager.py@@ -1490,6 +1490,118 @@ class TestMCPServerManager:assert emitted.headers["Authorization"] == "Bearer upstream-token"assert not kwargs["extra_headers"] or "authorization" not in {k.lower() for k in kwargs["extra_headers"]}+ @staticmethod+ def _emitted_authorization(mock_client_cls) -> str:+ kwargs = mock_client_cls.call_args.kwargs+ emitted = httpx.Request("GET", "https://example.com/mcp")+ flow = kwargs["resolved_auth"].auth_flow(emitted)+ next(flow)+ flow.close()+ return emitted.headers["Authorization"]++ @pytest.mark.asyncio+ @pytest.mark.parametrize(+ "per_server_header",+ ["Bearer per-server-token", {"Authorization": "Bearer per-server-token"}],+ )+ async def test_create_mcp_client_passthrough_prefers_per_server_token(self, per_server_header):+ """A per-server x-mcp-{alias}-authorization value is the explicit one-token-one-server+ binding, so it must win over the request-wide Authorization and reach the upstream+ verbatim through the passthrough arm."""+ manager = MCPServerManager()+ server = MCPServer(+ server_id="tp-per-server",+ name="tp",+ url="https://example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.true_passthrough,+ )+ with (+ patch(+ "litellm.proxy._experimental.mcp_server.mcp_server_manager.resolve_mcp_auth",+ new_callable=AsyncMock,+ ) as mock_resolve,+ patch("litellm.proxy._experimental.mcp_server.mcp_server_manager.MCPClient") as mock_client_cls,+ ):+ await manager._create_mcp_client(+ server=server,+ mcp_auth_header=per_server_header,+ extra_headers={"Authorization": "Bearer global-token"},+ )+ mock_resolve.assert_not_awaited()+ assert self._emitted_authorization(mock_client_cls) == "Bearer per-server-token"+ kwargs = mock_client_cls.call_args.kwargs+ assert not kwargs["extra_headers"] or "authorization" not in {k.lower() for k in kwargs["extra_headers"]}++ def test_consumes_caller_authorization_per_mode(self):+ from litellm.proxy._experimental.mcp_server.mcp_server_manager import (+ _consumes_caller_authorization,+ )++ def build(**kwargs) -> MCPServer:+ return MCPServer(+ server_id="s",+ name="s",+ url="https://example.com",+ transport=MCPTransport.http,+ **kwargs,+ )++ assert _consumes_caller_authorization(build(auth_type=MCPAuth.true_passthrough)) is True+ assert _consumes_caller_authorization(build(auth_type=MCPAuth.oauth_delegate)) is True+ assert (+ _consumes_caller_authorization(+ build(auth_type=MCPAuth.none, extra_headers=["Authorization"], oauth_passthrough=True)+ )+ is True+ )+ assert _consumes_caller_authorization(build(auth_type=MCPAuth.oauth2, delegate_auth_to_upstream=True)) is True+ assert _consumes_caller_authorization(build(auth_type=MCPAuth.api_key, authentication_token="x")) is False+ assert (+ _consumes_caller_authorization(+ build(+ auth_type=MCPAuth.oauth2,+ delegate_auth_to_upstream=True,+ oauth2_flow="client_credentials",+ token_url="https://idp/token",+ )+ )+ is False+ )++ def test_caller_authorization_fans_out_only_with_second_consumer(self):+ from litellm.proxy._experimental.mcp_server.mcp_server_manager import (+ _caller_authorization_fans_out,+ )++ delegate = MCPServer(+ server_id="od",+ name="od",+ url="https://example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.oauth_delegate,+ )+ second = MCPServer(+ server_id="tp",+ name="tp",+ url="https://example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.true_passthrough,+ )+ static_server = MCPServer(+ server_id="static",+ name="static",+ url="https://example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.api_key,+ authentication_token="x",+ )++ assert _caller_authorization_fans_out(delegate, None) is False+ assert _caller_authorization_fans_out(delegate, [delegate]) is False+ assert _caller_authorization_fans_out(delegate, [delegate, static_server]) is False+ assert _caller_authorization_fans_out(delegate, [delegate, second]) is True+@pytest.mark.asyncioasync def test_get_prompts_from_server_success(self):"""Ensure prompts are fetched and prefixed when requested."""