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 remote-tracking branch 'origin/main' into litellm_mcp_oauth_happy_path_e2e

Devin AI788e158e67ca
diff --cc tests/e2e/mcp/oauth_chat_client.py
index f6e72fb37dc,763b348b197..d2fca790132
--- a/tests/e2e/mcp/oauth_chat_client.py
+++ b/tests/e2e/mcp/oauth_chat_client.py
@@@ -29,17 -29,8 +30,17 @@@ from idp import Identit
from mcp import ClientSession
from mcp.client.auth import OAuthClientProvider
from mcp.client.streamable_http import streamable_http_client
- from mcp.shared.auth import OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
+ from mcp.shared.auth import AuthorizationCodeResult, OAuthClientInformationFull, OAuthClientMetadata, OAuthToken
-from models import ChatBody, ChatResponse, McpServerCreateBody, McpServerInfo
+from mcp.types import TextContent
+from models import (
+ ChatBody,
+ ChatResponse,
+ McpOauthUserCredentialStatus,
+ McpServerCreateBody,
+ McpServerInfo,
+ McpServerUserCredentialListResponse,
+ McpServerUserCredentialRow,
+)
from proxy_client import ProxyClient
if TYPE_CHECKING:
@@@ -190,12 -139,10 +191,12 @@@ def _oauth_provider
code_holder["code"] = code
code_holder["state"] = state
+ redirect_handler: Final = _reject_redirect if storage_state_path is None else _follow_redirect
+
- async def callback_handler() -> tuple[str, str | None]:
+ async def callback_handler() -> AuthorizationCodeResult:
code = code_holder.get("code")
assert code is not None, "callback_handler ran before the authorize redirect completed"
- return code, code_holder.get("state")
+ return AuthorizationCodeResult(code=code, state=code_holder.get("state"))
return OAuthClientProvider(
server_url=url,
@@@ -220,45 -167,24 +221,45 @@@ class _HeaderInjectingTransport(httpx2.
store the upstream token for from the key on the token exchange, exactly
like a production MCP host configured with a LiteLLM key header."""
- def __init__(self, inner: httpx.AsyncBaseTransport, headers: dict[str, str], gateway_url: str) -> None:
- def __init__(self, inner: httpx2.AsyncBaseTransport, headers: dict[str, str]) -> None:
++ def __init__(self, inner: httpx2.AsyncBaseTransport, headers: dict[str, str], gateway_url: str) -> None:
self._inner = inner
self._headers = headers
- self._gateway_url = httpx.URL(gateway_url)
++ self._gateway_url = httpx2.URL(gateway_url)
+
+ @staticmethod
- def _port(url: httpx.URL) -> int | None:
++ def _port(url: httpx2.URL) -> int | None:
+ if url.port is not None:
+ return url.port
+ return {"http": 80, "https": 443}.get(url.scheme)
- async def handle_async_request(self, request: httpx.Request) -> httpx.Response:
+ async def handle_async_request(self, request: httpx2.Request) -> httpx2.Response:
- for name, value in self._headers.items():
- if name not in request.headers:
- request.headers[name] = value
+ same_origin: Final = (
+ request.url.scheme == self._gateway_url.scheme
+ and request.url.host == self._gateway_url.host
+ and self._port(request.url) == self._port(self._gateway_url)
+ )
+ if same_origin:
+ for name, value in self._headers.items():
+ if name not in request.headers:
+ request.headers[name] = value
+ else:
+ for name, value in self._headers.items():
+ if request.headers.get(name) == value:
+ del request.headers[name]
return await self._inner.handle_async_request(request)
+ async def aclose(self) -> None:
+ await self._inner.aclose()
+
-def _oauth_http_client(headers: dict[str, str], auth: OAuthClientProvider) -> httpx2.AsyncClient:
+def _oauth_http_client(
+ headers: dict[str, str], auth: OAuthClientProvider, gateway_url: str = PROXY_BASE_URL
- ) -> httpx.AsyncClient:
- return httpx.AsyncClient(
++) -> httpx2.AsyncClient:
+ return httpx2.AsyncClient(
- headers=headers,
auth=auth,
- timeout=httpx.Timeout(REQUEST_TIMEOUT),
+ timeout=httpx2.Timeout(REQUEST_TIMEOUT),
follow_redirects=True,
- transport=_HeaderInjectingTransport(httpx.AsyncHTTPTransport(), headers, gateway_url),
- transport=_HeaderInjectingTransport(httpx2.AsyncHTTPTransport(), headers),
++ transport=_HeaderInjectingTransport(httpx2.AsyncHTTPTransport(), headers, gateway_url),
)
@@@ -273,43 -199,6 +274,43 @@@ async def _seed_via_dance
return tuple(sorted(tool.name for tool in listed.tools))
+@dataclass(frozen=True, slots=True)
+class OauthToolRun:
+ tools: tuple[str, ...]
+ is_error: bool
+ text: str
+
+
+async def _list_and_call(
+ url: str,
+ headers: dict[str, str],
+ storage: InMemoryTokenStorage,
+ storage_state_path: str | None,
+ tool: str,
+ arguments: dict[str, str],
+ gateway_url: str = PROXY_BASE_URL,
+ identity: Identity | None = None,
+ server_alias: str | None = None,
+ allow_upstream_consent: bool = True,
+) -> OauthToolRun:
+ async with _oauth_http_client(
+ headers,
+ _oauth_provider(url, storage, storage_state_path, identity, server_alias, allow_upstream_consent),
+ gateway_url,
+ ) as http_client:
- async with streamable_http_client(url, http_client=http_client) as (read, write, _):
++ async with streamable_http_client(url, http_client=http_client) as (read, write):
+ async with ClientSession(read, write) as session:
+ await session.initialize()
+ listed: Final = await session.list_tools()
+ result: Final = await session.call_tool(tool, arguments)
+ text: Final = "".join(content.text for content in result.content if isinstance(content, TextContent))
+ return OauthToolRun(
+ tools=tuple(sorted(tool_item.name for tool_item in listed.tools)),
- is_error=result.isError,
++ is_error=result.is_error,
+ text=text,
+ )
+
+
@dataclass(frozen=True, slots=True)
class ChatMcpClient:
proxy: ProxyClient