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): remember the rotated M2M bearer for later requests in the session
Tin8bfd8baab8cf
diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.pyindex 846017ebd73..9be1121126a 100644--- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/client_credentials.py@@ -340,6 +340,7 @@ class ClientCredentialsBearerAuth(httpx.Auth):fresh = await self._refetch(token)if fresh is None:return+ self._access_token = SecretStr(fresh)request.headers[self.header_name] = f"Bearer {fresh}"yield requestdiff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.pyindex bac6488333a..4e162090fbe 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_client_credentials.py@@ -340,6 +340,27 @@ async def test_bearer_auth_retries_a_401_once_with_a_fresh_token():assert seen == ["Bearer stale-token", "Bearer fresh-token"]+async def test_bearer_auth_remembers_the_rotated_token_for_later_requests():+ # The auth object lives for the whole MCP session (it is the httpx client's auth), so after a+ # 401 recovery it must send the fresh token first on subsequent requests; re-sending the+ # rejected one would burn a 401 round trip and the single retry on every call.+ transport, seen = _upstream([httpx.Response(401), httpx.Response(200), httpx.Response(200)])+ refetched: "list[str]" = []++ async def refetch(failed: str) -> "str | None":+ refetched.append(failed)+ return "fresh-token"++ auth = ClientCredentialsBearerAuth("stale-token", refetch)+ async with httpx.AsyncClient(transport=transport, auth=auth) as client:+ first = await client.get("https://upstream.example.com/mcp")+ second = await client.get("https://upstream.example.com/mcp")+ assert first.status_code == 200 and second.status_code == 200+ assert refetched == ["stale-token"]+ assert seen == ["Bearer stale-token", "Bearer fresh-token", "Bearer fresh-token"]++@pytest.mark.asyncioasync def test_bearer_auth_surfaces_the_401_when_the_refetch_fails():transport, seen = _upstream([httpx.Response(401)])