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
feat(mcp): add resolve_credentials dispatch skeleton (#31056)
tin-berri7020e1e5f75f
diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/__init__.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/__init__.pyindex b357504979d..73166a45d6e 100644--- a/litellm/proxy/_experimental/mcp_server/outbound_credentials/__init__.py+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/__init__.py@@ -1,16 +1,20 @@"""Typed upstream-credential resolution for MCP servers.-This subpackage houses the typed credential vocabulary and (in a later PR) the-``resolve_credentials`` dispatch. A server declares one per-mode config from the-``AuthConfig`` discriminated union; failures are modeled as values via :mod:`.result`-(``Result[T, CredError]``) rather than raised, so every seam is total. Nothing here is-wired onto a live request path yet.+This subpackage houses the typed credential vocabulary and the ``resolve_credentials``+dispatch. A server declares one per-mode config from the ``AuthConfig`` discriminated union;+``UpstreamCredentialProvider.resolve_credentials`` selects one arm and returns an ``httpx.Auth``+or a typed ``CredError``. Failures are modeled as values via :mod:`.result` (``Result[T,+CredError]``) rather than raised, so every seam is total. Nothing here is wired onto a live+request path yet."""from litellm.proxy._experimental.mcp_server.outbound_credentials.httpx_auth import (NoOpAuth,StaticHeaderAuth,)+from litellm.proxy._experimental.mcp_server.outbound_credentials.resolver import (+ UpstreamCredentialProvider,+)from litellm.proxy._experimental.mcp_server.outbound_credentials.result import (Error,Ok,@@ -45,6 +49,7 @@ __all__ = ["Result","NoOpAuth","StaticHeaderAuth",+ "UpstreamCredentialProvider","AuthSpecKind","CredError","Subject",diff --git a/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py b/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.pynew file mode 100644index 00000000000..7bcdb3e6529--- /dev/null+++ b/litellm/proxy/_experimental/mcp_server/outbound_credentials/resolver.py@@ -0,0 +1,70 @@+"""The one credential resolver: dispatch on the declared mode, fail closed.++`resolve_credentials` selects exactly one arm off the server's typed `config` and either+produces an `httpx.Auth` or returns a typed `CredError`. The `match` is over the `AuthConfig`+variant, so each arm receives its own fully-typed config with no field-presence inference and+no precedence cascade. It is wildcard-free with an `assert_never` tail, so adding a mode without+an arm fails the type gate (basedpyright `reportMatchNotExhaustive`); a bypassed gate fails loudly+at runtime instead of returning `None`.++This skeleton ships every arm as a `not_implemented` stub. Each mode's real body, with its+injected seam, lands in its own follow-up PR; until then the arm returns a typed error rather+than silently producing no credential. Pure v2: no imports from v1.+"""++from __future__ import annotations++import httpx+from typing_extensions import assert_never++from litellm.proxy._experimental.mcp_server.outbound_credentials.result import (+ Error,+ Result,+)+from litellm.proxy._experimental.mcp_server.outbound_credentials.types import (+ ApiKeyConfig,+ AuthorizationCodeConfig,+ AuthSpecKind,+ AwsSigV4Config,+ ClientCredentialsConfig,+ CredError,+ NoneConfig,+ PassthroughConfig,+ ServerSpec,+ Subject,+ TokenExchangeConfig,+)+++class UpstreamCredentialProvider:+ """Produces the one `httpx.Auth` for a `(subject, upstream)` pair, per declared mode.++ Collaborators (the per-mode credential stores and token fetchers) are injected as each arm+ is built; the skeleton needs none, since every arm is a stub.+ """++ async def resolve_credentials(+ self, subject: Subject, server: ServerSpec+ ) -> Result[httpx.Auth, CredError]:+ match server.config:+ case NoneConfig():+ return _not_implemented(AuthSpecKind.none)+ case ApiKeyConfig():+ return _not_implemented(AuthSpecKind.api_key)+ case PassthroughConfig():+ return _not_implemented(AuthSpecKind.passthrough)+ case ClientCredentialsConfig():+ return _not_implemented(AuthSpecKind.client_credentials)+ case TokenExchangeConfig():+ return _not_implemented(AuthSpecKind.token_exchange)+ case AuthorizationCodeConfig():+ return _not_implemented(AuthSpecKind.authorization_code)+ case AwsSigV4Config():+ return _not_implemented(AuthSpecKind.aws_sigv4)+ assert_never(server.config)+++def _not_implemented(kind: AuthSpecKind) -> Result[httpx.Auth, CredError]:+ return Error(+ CredError.of_not_implemented(f"{kind.value}: resolver arm not implemented yet")+ )diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.pynew file mode 100644index 00000000000..7885617aa46--- /dev/null+++ b/tests/test_litellm/proxy/_experimental/mcp_server/outbound_credentials/test_resolver.py@@ -0,0 +1,57 @@+"""Tests for the resolver dispatch skeleton.++Every mode must reach its own arm and, until that arm is built, return a typed+`not_implemented` CredError rather than silently producing no credential. Parametrizing over+one config per mode also guards reachability: if a `case` were dropped, that mode would fall to+the `assert_never` tail and raise here instead of returning the stub.+"""++import pytest+from pydantic import SecretStr++from litellm.proxy._experimental.mcp_server.outbound_credentials import (+ ApiKeyConfig,+ AuthorizationCodeConfig,+ AuthSpecKind,+ AwsSigV4Config,+ ClientCredentialsConfig,+ Error,+ NoneConfig,+ PassthroughConfig,+ ServerSpec,+ SharedKey,+ Subject,+ TokenExchangeConfig,+ UpstreamCredentialProvider,+)++_ONE_CONFIG_PER_MODE = [+ (AuthSpecKind.none, NoneConfig()),+ (AuthSpecKind.api_key, ApiKeyConfig(key_source=SharedKey(value=SecretStr("k")))),+ (AuthSpecKind.passthrough, PassthroughConfig()),+ (AuthSpecKind.client_credentials, ClientCredentialsConfig()),+ (AuthSpecKind.token_exchange, TokenExchangeConfig()),+ (AuthSpecKind.authorization_code, AuthorizationCodeConfig()),+ (AuthSpecKind.aws_sigv4, AwsSigV4Config(region="us-east-1")),+]++[email protected]("kind, config", _ONE_CONFIG_PER_MODE)+async def test_every_mode_reaches_its_arm_and_returns_not_implemented(kind, config):+ spec = ServerSpec(+ server_id="s", resource="https://upstream.example.com", config=config+ )+ subject = Subject(tenant_id="", subject_id="")++ result = await UpstreamCredentialProvider().resolve_credentials(subject, spec)++ assert isinstance(result, Error)+ assert result.error.tag == "not_implemented"+ assert kind.value in result.error.summary+++def test_all_seven_modes_are_covered():+ # Guards that the parametrization (and therefore the dispatch) spans every AuthSpecKind, so a+ # newly added mode without a test row is caught here rather than slipping through.+ assert {kind for kind, _ in _ONE_CONFIG_PER_MODE} == set(AuthSpecKind)