Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions litellm/llms/custom_httpx/http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,26 @@ async def get(
)
return response

async def head(
self,
url: str,
params: Optional[dict] = None,
headers: Optional[dict] = None,
follow_redirects: Optional[bool] = None,
):
# Set follow_redirects to UseClientDefault if None
_follow_redirects = (
follow_redirects if follow_redirects is not None else USE_CLIENT_DEFAULT
)

params = params or {}
params.update(HTTPHandler.extract_query_params(url))

response = await self.client.head(
url, params=params, headers=headers, follow_redirects=_follow_redirects # type: ignore
)
return response
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

@track_llm_api_timing()
async def post(
self,
Expand Down
103 changes: 103 additions & 0 deletions litellm/proxy/_experimental/mcp_server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
get_server_prefix,
iter_known_server_prefixes,
)
from litellm.llms.custom_httpx.http_handler import (
get_async_httpx_client,
httpxSpecialProvider,
)
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.auth.ip_address_utils import IPAddressUtils
from litellm.proxy.litellm_pre_call_utils import (
Expand Down Expand Up @@ -2754,6 +2758,98 @@ async def _apply_toolset_scope(
)
return user_api_key_auth.model_copy(update={"object_permission": updated_op})

def _get_forwarded_auth_from_scope(scope: Scope) -> Optional[str]:
"""Return the raw Authorization header value from the ASGI scope, or None."""
for key, value in scope.get("headers", []):
if key.lower() == b"authorization":
return value.decode("latin-1")
return None

async def _probe_upstream_auth(
url: str,
auth_header: str,
timeout: float = 5.0,
) -> tuple:
"""HEAD-probe the upstream URL to check whether the token is accepted.

Uses HEAD so the upstream receives no request body and allocates no
session or audit state. Returns (status_code, www_authenticate).
Fails-open with (200, None) on network errors so a transient hiccup
does not block valid requests.
"""
try:
client = get_async_httpx_client(
llm_provider=httpxSpecialProvider.MCP,
params={"timeout": timeout},
)
resp = await client.head(
url,
headers={"Authorization": auth_header},
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
return resp.status_code, resp.headers.get("www-authenticate")
except Exception as exc:
verbose_logger.debug(
f"_probe_upstream_auth: probe to {url} failed ({exc}), allowing request through"
)
return 200, None
Comment thread
Sameerlite marked this conversation as resolved.
Resolved

async def _check_passthrough_upstream_auth(
scope: Scope,
user_api_key_auth: Optional[UserAPIKeyAuth],
mcp_servers: Optional[List[str]],
client_ip: Optional[str],
) -> None:
"""Probe pass-through upstream servers in parallel before the MCP session starts.

Only servers the caller's key is already authorized to reach are probed —
the list is derived from _get_allowed_mcp_servers so that a user cannot
trigger an upstream probe against a server their key is not permitted for.

The MCP SDK commits HTTP 200 headers before invoking handlers, so a 401
can only be returned before that point. This function raises HTTPException(401)
with a WWW-Authenticate header if any upstream rejects the client token.
Fails-open: network errors are logged and the request is allowed through.
"""
forwarded_auth = _get_forwarded_auth_from_scope(scope)
if not forwarded_auth:
return

# Use the authorized server set, not the raw user-supplied names, so that
# a caller cannot force a probe to a server their key is not allowed to use.
allowed_servers = await _get_allowed_mcp_servers(
user_api_key_auth=user_api_key_auth,
mcp_servers=mcp_servers,
client_ip=client_ip,
)
passthrough_servers = [
srv
for srv in allowed_servers
if srv.extra_headers
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
and any(h.lower() == "authorization" for h in srv.extra_headers)
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
]
if not passthrough_servers:
return

probe_results = await asyncio.gather(
*[
_probe_upstream_auth(srv.url or "", forwarded_auth)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

High: Proxy API key forwarded upstream

forwarded_auth is taken from the raw request Authorization header, but that same header is also accepted as the LiteLLM API key when x-litellm-api-key is not present. A configured token-forwarding MCP server can now receive and reuse a user's LiteLLM proxy key during this preflight probe; only run the probe when the proxy was authenticated with a separate credential, or pass an upstream token value that process_mcp_request has confirmed is not the proxy API key.

for srv in passthrough_servers
]
)
request = StarletteRequest(scope)
base_url = get_request_base_url(request)
for srv, (probe_status, _) in zip(passthrough_servers, probe_results):
if probe_status in (401, 403):
authorization_uri = (
f"Bearer authorization_uri="
f"{base_url}/.well-known/oauth-authorization-server/{srv.name}"
)
raise HTTPException(
status_code=401,
detail="Unauthorized",
headers={"WWW-Authenticate": authorization_uri},
)
Comment thread
Sameerlite marked this conversation as resolved.
Resolved

async def handle_streamable_http_mcp(
scope: Scope, receive: Receive, send: Send
) -> None:
Expand Down Expand Up @@ -2827,6 +2923,13 @@ async def handle_streamable_http_mcp(
user_api_key_auth, active_toolset_id
)

# Pre-flight auth check for pass-through servers. Must run after
# toolset scoping so the probe list is derived from the fully-authorized
# server set, not the raw user-supplied names.
await _check_passthrough_upstream_auth(
scope, user_api_key_auth, mcp_servers, _client_ip
)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

# Inject masked debug headers when client sends x-litellm-mcp-debug: true
_debug_headers = MCPDebug.maybe_build_debug_headers(
raw_headers=raw_headers,
Expand Down
33 changes: 33 additions & 0 deletions tests/test_litellm/llms/custom_httpx/test_http_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,39 @@
)


@pytest.mark.asyncio
async def test_async_head_returns_response_without_raise_for_status():
captured_request = None

async def mock_handler(request: httpx.Request) -> httpx.Response:
nonlocal captured_request
captured_request = request
return httpx.Response(
401,
request=request,
headers={"www-authenticate": 'Bearer realm="test"'},
)

litellm_handler = AsyncHTTPHandler()
await litellm_handler.client.aclose()
litellm_handler.client = httpx.AsyncClient(
transport=httpx.MockTransport(mock_handler)
)
try:
response = await litellm_handler.head(
"https://upstream.example/mcp",
headers={"Authorization": "Bearer some-token"},
)

assert response.status_code == 401
assert response.headers["www-authenticate"] == 'Bearer realm="test"'
assert captured_request is not None
assert captured_request.method == "HEAD"
assert captured_request.headers["Authorization"] == "Bearer some-token"
finally:
await litellm_handler.close()


@pytest.mark.asyncio
async def test_async_post_streaming_status_error_should_not_wait_forever_for_body(
monkeypatch,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3256,3 +3256,75 @@ async def capture_create_mcp_client(*args, **kwargs):
), "P2 API consistency issue: expected None for empty extra_headers, got: " + str(
captured_extra_headers
)


# ---------------------------------------------------------------------------
# Pre-flight upstream auth check tests
# ---------------------------------------------------------------------------


@pytest.mark.asyncio
async def test_probe_upstream_auth_returns_upstream_status():
"""_probe_upstream_auth forwards the status code from the upstream server."""
from litellm.proxy._experimental.mcp_server.server import _probe_upstream_auth

mock_response = MagicMock()
mock_response.status_code = 401
mock_response.headers = {"www-authenticate": 'Bearer realm="test"'}

mock_client = AsyncMock()
mock_client.head = AsyncMock(return_value=mock_response)

with patch(
"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"
)

assert status == 401
assert www_auth == 'Bearer realm="test"'


@pytest.mark.asyncio
async def test_probe_upstream_auth_fails_open_on_network_error():
"""_probe_upstream_auth returns (200, None) when the network call fails."""
from litellm.proxy._experimental.mcp_server.server import _probe_upstream_auth

mock_client = AsyncMock()
mock_client.head = AsyncMock(side_effect=Exception("connection refused"))

with patch(
"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"
)

assert status == 200
assert www_auth is None


def test_get_forwarded_auth_from_scope_extracts_header():
"""_get_forwarded_auth_from_scope returns the Authorization value."""
from litellm.proxy._experimental.mcp_server.server import (
_get_forwarded_auth_from_scope,
)

scope = {
"headers": [
(b"content-type", b"application/json"),
(b"authorization", b"Bearer my-token"),
]
}
assert _get_forwarded_auth_from_scope(scope) == "Bearer my-token"


def test_get_forwarded_auth_from_scope_returns_none_when_missing():
from litellm.proxy._experimental.mcp_server.server import (
_get_forwarded_auth_from_scope,
)

assert _get_forwarded_auth_from_scope({"headers": []}) is None
Loading