|
| 1 | +from typing import Dict, Optional |
| 2 | + |
| 3 | +import pytest |
| 4 | +from starlette.requests import Request |
| 5 | + |
| 6 | +from litellm.proxy._experimental.mcp_server import rest_endpoints |
| 7 | +from litellm.proxy._experimental.mcp_server.auth import ( |
| 8 | + user_api_key_auth_mcp as auth_mcp, |
| 9 | +) |
| 10 | +from litellm.proxy._types import NewMCPServerRequest, UserAPIKeyAuth |
| 11 | +from litellm.types.mcp import MCPAuth |
| 12 | + |
| 13 | + |
| 14 | +def _build_request(headers: Optional[Dict[str, str]] = None) -> Request: |
| 15 | + headers = headers or {} |
| 16 | + raw_headers = [ |
| 17 | + (key.lower().encode("latin-1"), value.encode("latin-1")) |
| 18 | + for key, value in headers.items() |
| 19 | + ] |
| 20 | + scope = { |
| 21 | + "type": "http", |
| 22 | + "http_version": "1.1", |
| 23 | + "method": "POST", |
| 24 | + "path": "/mcp-rest/test/tools/list", |
| 25 | + "headers": raw_headers, |
| 26 | + } |
| 27 | + |
| 28 | + async def receive(): |
| 29 | + return {"type": "http.request", "body": b"", "more_body": False} |
| 30 | + |
| 31 | + return Request(scope, receive=receive) |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.asyncio |
| 35 | +async def test_test_tools_list_forwards_mcp_auth_header(monkeypatch): |
| 36 | + """Ensure credential-based auth forwards the auth_value to the MCP client.""" |
| 37 | + |
| 38 | + captured: dict = {} |
| 39 | + |
| 40 | + async def fake_execute(request, operation, mcp_auth_header=None, oauth2_headers=None): |
| 41 | + captured["mcp_auth_header"] = mcp_auth_header |
| 42 | + captured["oauth2_headers"] = oauth2_headers |
| 43 | + return { |
| 44 | + "tools": [], |
| 45 | + "error": None, |
| 46 | + "message": "Successfully retrieved tools", |
| 47 | + } |
| 48 | + |
| 49 | + monkeypatch.setattr( |
| 50 | + rest_endpoints, "_execute_with_mcp_client", fake_execute, raising=False |
| 51 | + ) |
| 52 | + |
| 53 | + oauth_call_counter = {"count": 0} |
| 54 | + |
| 55 | + def fake_oauth(headers): |
| 56 | + oauth_call_counter["count"] += 1 |
| 57 | + return {"Authorization": "Bearer oauth"} |
| 58 | + |
| 59 | + monkeypatch.setattr( |
| 60 | + auth_mcp.MCPRequestHandler, |
| 61 | + "_get_oauth2_headers_from_headers", |
| 62 | + staticmethod(fake_oauth), |
| 63 | + raising=False, |
| 64 | + ) |
| 65 | + |
| 66 | + request = _build_request() |
| 67 | + payload = NewMCPServerRequest( |
| 68 | + server_name="example", |
| 69 | + url="https://example.com", |
| 70 | + auth_type=MCPAuth.api_key, |
| 71 | + credentials={"auth_value": "secret-key"}, |
| 72 | + ) |
| 73 | + |
| 74 | + result = await rest_endpoints.test_tools_list( |
| 75 | + request, payload, user_api_key_dict=UserAPIKeyAuth() |
| 76 | + ) |
| 77 | + |
| 78 | + assert result["message"] == "Successfully retrieved tools" |
| 79 | + assert captured["mcp_auth_header"] == "secret-key" |
| 80 | + assert captured["oauth2_headers"] is None |
| 81 | + assert oauth_call_counter["count"] == 0 |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_test_tools_list_extracts_oauth2_headers(monkeypatch): |
| 86 | + """Ensure oauth2 auth type pulls oauth headers and omits MCP auth header.""" |
| 87 | + |
| 88 | + captured: dict = {} |
| 89 | + |
| 90 | + async def fake_execute(request, operation, mcp_auth_header=None, oauth2_headers=None): |
| 91 | + captured["mcp_auth_header"] = mcp_auth_header |
| 92 | + captured["oauth2_headers"] = oauth2_headers |
| 93 | + return { |
| 94 | + "tools": [], |
| 95 | + "error": None, |
| 96 | + "message": "Successfully retrieved tools", |
| 97 | + } |
| 98 | + |
| 99 | + monkeypatch.setattr( |
| 100 | + rest_endpoints, "_execute_with_mcp_client", fake_execute, raising=False |
| 101 | + ) |
| 102 | + |
| 103 | + oauth_headers = {"Authorization": "Bearer oauth"} |
| 104 | + oauth_call_counter = {"count": 0} |
| 105 | + |
| 106 | + def fake_oauth(headers): |
| 107 | + oauth_call_counter["count"] += 1 |
| 108 | + return oauth_headers |
| 109 | + |
| 110 | + monkeypatch.setattr( |
| 111 | + auth_mcp.MCPRequestHandler, |
| 112 | + "_get_oauth2_headers_from_headers", |
| 113 | + staticmethod(fake_oauth), |
| 114 | + raising=False, |
| 115 | + ) |
| 116 | + |
| 117 | + request = _build_request({"authorization": "Bearer incoming"}) |
| 118 | + payload = NewMCPServerRequest( |
| 119 | + server_name="example", |
| 120 | + url="https://example.com", |
| 121 | + auth_type=MCPAuth.oauth2, |
| 122 | + ) |
| 123 | + |
| 124 | + result = await rest_endpoints.test_tools_list( |
| 125 | + request, payload, user_api_key_dict=UserAPIKeyAuth() |
| 126 | + ) |
| 127 | + |
| 128 | + assert result["message"] == "Successfully retrieved tools" |
| 129 | + assert captured["mcp_auth_header"] is None |
| 130 | + assert captured["oauth2_headers"] == oauth_headers |
| 131 | + assert oauth_call_counter["count"] == 1 |
0 commit comments