This commit is contained in:
David Koh 2026-10-30 13:47:59 -04:00 • committed by GitHub
commit 9cd97e917f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 54 additions and 4 deletions

View file

@ -122,6 +122,7 @@ from starlette.types import Message, Send
from litellm.litellm_core_utils.secret_redaction import REDACTED, redact_string
from litellm.litellm_core_utils.sensitive_data_masker import SensitiveDataMasker
from litellm.proxy._types import UserAPIKeyAuth, user_api_key_has_admin_view
if TYPE_CHECKING:
from litellm.proxy.mcp_server.outbound_credentials.types import AuthResolution
@ -361,12 +362,15 @@ class MCPDebug:
mcp_servers: list[str] | None,
oauth2_headers: dict[str, str] | None,
client_ip: str | None,
user_api_key_auth: UserAPIKeyAuth | None = None,
) -> dict[str, str]:
"""
Build debug headers if debug mode is enabled, otherwise return empty dict.
This is the single entry point called from the MCP request handler.
"""
if user_api_key_auth is None or not user_api_key_has_admin_view(user_api_key_auth):
return {}
if not raw_headers or not MCPDebug.is_debug_enabled(raw_headers):
return {}

View file

@ -2508,6 +2508,7 @@ if MCP_AVAILABLE:
mcp_servers=mcp_servers,
oauth2_headers=oauth2_headers,
client_ip=_client_ip,
user_api_key_auth=user_api_key_auth,
)
diagnostics: Final = MCPAuthDiagnostics() if _debug_headers else None
if diagnostics is not None:

View file

@ -9,6 +9,7 @@ import httpx
import pytest
from starlette.types import Message
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.mcp_server.mcp_debug import (
MCP_DEBUG_REQUEST_HEADER,
MCPAuthDiagnostics,
@ -18,6 +19,43 @@ from litellm.proxy.mcp_server.mcp_debug import (
from litellm.proxy.mcp_server.outbound_credentials.types import AuthResolution
def test_mcp_debug_requires_authenticated_admin() -> None:
headers: Final = MCPDebug.maybe_build_debug_headers(
raw_headers={MCP_DEBUG_REQUEST_HEADER: "true"},
scope={"type": "http", "headers": []},
mcp_servers=None,
oauth2_headers=None,
client_ip=None,
)
assert headers == {}
@pytest.mark.parametrize(
"role,enabled,expected",
[
("proxy_admin", True, True),
("proxy_admin_viewer", True, True),
("proxy_admin", False, False),
("internal_user", True, False),
("internal_user_viewer", True, False),
("org_admin", True, False),
(None, True, False),
],
)
def test_mcp_debug_uses_authenticated_role(role: str | None, enabled: bool, expected: bool) -> None:
headers: Final = MCPDebug.maybe_build_debug_headers(
raw_headers={MCP_DEBUG_REQUEST_HEADER: str(enabled), "user-role": "proxy_admin"},
scope={"type": "http", "headers": []},
mcp_servers=None,
oauth2_headers=None,
client_ip=None,
user_api_key_auth=UserAPIKeyAuth(user_role=role, metadata={"user_role": "proxy_admin"}),
)
assert bool(headers) is expected
assert ("x-mcp-debug-auth-resolution" in headers) is expected
assert ("x-mcp-debug-outbound-url" in headers) is expected
class TestIsDebugEnabled:
def test_enabled_true(self):
assert MCPDebug.is_debug_enabled({MCP_DEBUG_REQUEST_HEADER: "true"}) is True

View file

@ -2076,6 +2076,7 @@ async def test_streamable_http_session_manager_is_stateless():
@pytest.mark.asyncio
@pytest.mark.parametrize("debug", (False, True))
@pytest.mark.parametrize("role,diagnostics_allowed", [("proxy_admin", True), ("internal_user", False), (None, False)])
@pytest.mark.parametrize(
("method", "request_body", "stateful"),
(
@ -2087,7 +2088,13 @@ async def test_streamable_http_session_manager_is_stateless():
),
)
async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless(
_mcp_request_ctx, debug: bool, method: str, request_body: bytes, stateful: bool
_mcp_request_ctx,
debug: bool,
role: str | None,
diagnostics_allowed: bool,
method: str,
request_body: bytes,
stateful: bool,
) -> None:
from starlette.requests import Request
from starlette.types import Message, Receive, Scope, Send
@ -2120,7 +2127,7 @@ async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless(
"litellm.proxy.mcp_server.server.extract_mcp_auth_context",
new_callable=AsyncMock,
return_value=(
UserAPIKeyAuth(user_id="debug-user"),
UserAPIKeyAuth(user_id="debug-user", user_role=role),
None,
None,
None,
@ -2142,12 +2149,12 @@ async def test_mcp_routing_initialize_to_stateful_no_session_to_stateless(
assert stateful_handle.await_count == (1 if stateful else 0)
assert stateless_handle.await_count == (0 if stateful else 1)
observe_start.assert_awaited_once_with(0 if debug and method == "POST" else 1)
observe_start.assert_awaited_once_with(0 if debug and diagnostics_allowed and method == "POST" else 1)
assert send.await_count == 2
assert send.call_args_list[0].args[0]["status"] == 200
assert send.call_args_list[1].args[0] == body
headers: Final = dict(send.call_args_list[0].args[0]["headers"])
if debug:
if debug and diagnostics_allowed:
assert headers[b"x-mcp-debug-auth-resolution"] == (b"stored-user-token" if method == "POST" else b"unresolved")
else:
assert not any(name.startswith(b"x-mcp-debug") for name in headers)