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): keep the minted M2M bearer authoritative over injected Authorization headers

Tind2342296ae01
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
index 1ba608b9510..d06dad34d8c 100644
--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py
@@ -93,6 +93,7 @@ from litellm.proxy._experimental.mcp_server.outbound_credentials.token_exchange_
)
from litellm.proxy._experimental.mcp_server.outbound_credentials.types import (
AuthorizationCodeConfig,
+ ClientCredentialsConfig,
CredError,
IdJagConfig,
PassthroughConfig,
@@ -2739,14 +2740,18 @@ class MCPServerManager:
)
if not conflicts:
return auth, extra_headers
- if isinstance(spec.config, (TokenExchangeConfig, AuthorizationCodeConfig, IdJagConfig)):
- # The resolver owns the per-user credential here (token_exchange's exchanged
- # token, authorization_code's stored token, id_jag's minted assertion). It is
- # authoritative: a guardrail such
- # as MCPJWTSigner, static_headers, or any other injected Authorization must NOT
- # shadow it (otherwise the upstream gets e.g. the signer's JWT instead of the
- # exchanged token and rejects it). Drop the conflicting header so the resolved
- # token reaches upstream.
+ if isinstance(
+ spec.config,
+ (TokenExchangeConfig, AuthorizationCodeConfig, IdJagConfig, ClientCredentialsConfig),
+ ):
+ # The resolver owns the credential here (token_exchange's exchanged token,
+ # authorization_code's stored token, id_jag's minted assertion,
+ # client_credentials' gateway-minted M2M token). It is authoritative: a
+ # guardrail such as MCPJWTSigner, static_headers, or any other injected
+ # Authorization must NOT shadow it (otherwise the upstream gets e.g. the
+ # signer's JWT instead of the minted token and rejects it, and for M2M the
+ # one-shot 401 refetch is lost with it). Drop the conflicting header so the
+ # resolved token reaches upstream.
return auth, _without_authorization(extra_headers)
# Other modes: an Authorization already supplied via extra_headers (a forwarded caller
# header or static_headers) is intentional and wins; v1 applies those last.
diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py
index 3bf9fd1c12e..8ecef0c95c5 100644
--- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py
+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/adapter.py
@@ -21,12 +21,9 @@ from typing_extensions import assert_never
from litellm.proxy._experimental.mcp_server.outbound_credentials.types import (
ApiKeyConfig,
AuthorizationCodeConfig,
-<<<<<<< HEAD
ClientAuth,
- ClientSecretAuth,
-=======
ClientCredentialsConfig,
->>>>>>> 73df37ca23 (feat(mcp): migrate client_credentials (M2M) onto the v2 resolver arm)
+ ClientSecretAuth,
CredError,
IdJagConfig,
NoneConfig,
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.py
index f0ca75b8cdb..03f91260955 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
@@ -1759,6 +1759,42 @@ class TestMCPServerManager:
assert client._resolved_auth is not None
assert "authorization" not in {k.lower() for k in (client.extra_headers or {})}
+ @pytest.mark.asyncio
+ async def test_injected_authorization_does_not_shadow_m2m_minted_token(self):
+ """The M2M twin of the OBO shadow test: a guardrail/static Authorization must not displace
+ the gateway-minted client_credentials bearer. Dropping the resolved auth here would also
+ drop the one-shot 401 refetch that rides on it, so the resolver-owned credential is
+ authoritative exactly as for token_exchange and authorization_code."""
+ from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import (
+ StaticHeaderAuth,
+ )
+ from litellm.proxy._experimental.mcp_server.outbound_credentials.result import Ok
+
+ class _FakeProvider:
+ async def resolve_credentials(self, subject, server):
+ return Ok(StaticHeaderAuth("Bearer MINTED-M2M", header_name="Authorization"))
+
+ manager = MCPServerManager(cred_provider=_FakeProvider())
+ server = MCPServer(
+ server_id="m2m-shadow",
+ name="m2m-shadow-server",
+ url="https://up.example.com/mcp",
+ transport=MCPTransport.http,
+ auth_type=MCPAuth.oauth2,
+ oauth2_flow="client_credentials",
+ client_id="cid",
+ client_secret="csec",
+ token_url="https://idp.example.com/token",
+ )
+
+ client = await manager._create_mcp_client(
+ server,
+ extra_headers={"Authorization": "Bearer signer-jwt"}, # simulate the JWT signer
+ )
+
+ assert client._resolved_auth is not None
+ assert "authorization" not in {k.lower() for k in (client.extra_headers or {})}
+
@pytest.mark.asyncio
async def test_preflight_token_exchange_challenges_on_rejected_subject(self):
"""A subject the IdP rejects must raise the RFC 9728 401 challenge from the preflight, so a