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
Auth Header Fix for MCP Tool Call (#15736)
Vinod Singhd4aadda692c1
diff --git a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.pyindex f313a673827..c25f3938e43 100644--- a/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py+++ b/litellm/proxy/_experimental/mcp_server/mcp_server_manager.py@@ -1092,12 +1092,18 @@ class MCPServerManager:GuardrailRaisedException: If guardrails block the callHTTPException: If an HTTP error occurs"""- # Get server-specific auth header if available+ # Get server-specific auth header if available (case-insensitive)+ # FIX: Added case-insensitive matching to handle auth header keys that may not match+ # the exact case of server alias/name (e.g., '1litellmagcgateway' vs '1LiteLLMAGCGateway')server_auth_header: Optional[Union[Dict[str, str], str]] = None- if mcp_server_auth_headers and mcp_server.alias:- server_auth_header = mcp_server_auth_headers.get(mcp_server.alias)- elif mcp_server_auth_headers and mcp_server.server_name:- server_auth_header = mcp_server_auth_headers.get(mcp_server.server_name)+ if mcp_server_auth_headers:+ # Normalize keys for case-insensitive lookup+ normalized_headers = {k.lower(): v for k, v in mcp_server_auth_headers.items()}++ if mcp_server.alias:+ server_auth_header = normalized_headers.get(mcp_server.alias.lower())+ if server_auth_header is None and mcp_server.server_name:+ server_auth_header = normalized_headers.get(mcp_server.server_name.lower())# Fall back to deprecated mcp_auth_header if no server-specific header foundif server_auth_header is None:diff --git a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py b/litellm/proxy/_experimental/mcp_server/rest_endpoints.pyindex 6a9c425a81b..e427507a4b8 100644--- a/litellm/proxy/_experimental/mcp_server/rest_endpoints.py+++ b/litellm/proxy/_experimental/mcp_server/rest_endpoints.py@@ -212,6 +212,7 @@ if MCP_AVAILABLE:from litellm.exceptions import BlockedPiiEntityError, GuardrailRaisedExceptionfrom litellm.proxy.proxy_server import add_litellm_data_to_request, proxy_config+ from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import MCPRequestHandlertry:data = await request.json()@@ -221,7 +222,23 @@ if MCP_AVAILABLE:user_api_key_dict=user_api_key_dict,proxy_config=proxy_config,)- return await call_mcp_tool(**data)++ # FIX: Extract MCP auth headers from request+ # The UI sends bearer token in x-mcp-auth header and server-specific headers,+ # but they weren't being extracted and passed to call_mcp_tool.+ # This fix ensures auth headers are properly extracted from the HTTP request+ # and passed through to the MCP server for authentication.+ mcp_auth_header = MCPRequestHandler._get_mcp_auth_header_from_headers(request.headers)+ mcp_server_auth_headers = MCPRequestHandler._get_mcp_server_auth_headers_from_headers(request.headers)++ # Add extracted headers to data dict to pass to call_mcp_tool+ if mcp_auth_header:+ data["mcp_auth_header"] = mcp_auth_header+ if mcp_server_auth_headers:+ data["mcp_server_auth_headers"] = mcp_server_auth_headers++ result = await call_mcp_tool(**data)+ return resultexcept BlockedPiiEntityError as e:verbose_logger.error(f"BlockedPiiEntityError in MCP tool call: {str(e)}")raise HTTPException(diff --git a/tests/mcp_tests/test_mcp_auth_header_extraction.py b/tests/mcp_tests/test_mcp_auth_header_extraction.pynew file mode 100644index 00000000000..608a5400072--- /dev/null+++ b/tests/mcp_tests/test_mcp_auth_header_extraction.py@@ -0,0 +1,162 @@+"""+Test MCP auth header extraction and case-insensitive server name matching.++Tests the fixes for:+1. Auth headers being properly extracted from HTTP request headers in REST endpoints+2. Case-insensitive matching for server-specific auth headers in _call_regular_mcp_tool+"""++import pytest+from unittest.mock import AsyncMock, MagicMock+from starlette.datastructures import Headers++from litellm.proxy._experimental.mcp_server.auth.user_api_key_auth_mcp import (+ MCPRequestHandler,+)+from litellm.proxy._experimental.mcp_server.mcp_server_manager import MCPServerManager+from litellm.types.mcp import MCPAuth, MCPTransport+from litellm.types.mcp_server.mcp_server_manager import MCPServer+++class TestRestEndpointAuthHeaderExtraction:+ """Test Fix 1: REST endpoints properly extract auth headers from HTTP requests"""++ def test_call_tool_rest_api_extracts_mcp_auth_header(self):+ """Test that call_tool REST endpoint extracts x-mcp-auth header"""+ headers = Headers({"x-mcp-auth": "Bearer legacy-token"})++ mcp_auth_header = MCPRequestHandler._get_mcp_auth_header_from_headers(headers)++ assert mcp_auth_header == "Bearer legacy-token"++ def test_call_tool_rest_api_extracts_server_specific_headers(self):+ """Test that call_tool REST endpoint extracts server-specific auth headers"""+ headers = Headers({+ "x-mcp-github-authorization": "Bearer github-token",+ "x-mcp-zapier-x-api-key": "zapier-key-123",+ })++ mcp_server_auth_headers = MCPRequestHandler._get_mcp_server_auth_headers_from_headers(headers)++ assert "github" in mcp_server_auth_headers+ assert mcp_server_auth_headers["github"]["Authorization"] == "Bearer github-token"+ assert "zapier" in mcp_server_auth_headers+ assert mcp_server_auth_headers["zapier"]["x-api-key"] == "zapier-key-123"++ def test_list_tools_rest_api_extracts_auth_headers(self):+ """Test that list_tools REST endpoint extracts auth headers"""+ headers = Headers({+ "x-mcp-auth": "Bearer legacy-token",+ "x-mcp-zapier-authorization": "Bearer zapier-token",+ })++ mcp_auth_header = MCPRequestHandler._get_mcp_auth_header_from_headers(headers)+ mcp_server_auth_headers = MCPRequestHandler._get_mcp_server_auth_headers_from_headers(headers)++ assert mcp_auth_header == "Bearer legacy-token"+ assert "zapier" in mcp_server_auth_headers+ assert mcp_server_auth_headers["zapier"]["Authorization"] == "Bearer zapier-token"+++class TestCaseInsensitiveServerMatching:+ """Test Fix 2: Case-insensitive matching for server names in _call_regular_mcp_tool"""++ def test_case_insensitive_alias_matching(self):+ """Test server auth headers match case-insensitively by alias"""+ server = MCPServer(+ server_id="test-server",+ name="Test Server",+ alias="LiteLLMAGCGateway",+ server_name="litellm_gateway",+ url="https://api.example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.authorization,+ )++ mcp_server_auth_headers = {+ "litellmagcgateway": {"Authorization": "Bearer token"}+ }++ # Test the case-insensitive matching logic from _call_regular_mcp_tool+ normalized_headers = {k.lower(): v for k, v in mcp_server_auth_headers.items()}+ server_auth_header = normalized_headers.get(server.alias.lower())++ assert server_auth_header is not None+ assert server_auth_header["Authorization"] == "Bearer token"++ def test_case_insensitive_server_name_matching(self):+ """Test server auth headers match case-insensitively by server_name"""+ server = MCPServer(+ server_id="test-server",+ name="Test Server",+ alias=None,+ server_name="MyAPIServer",+ url="https://api.example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.authorization,+ )++ mcp_server_auth_headers = {+ "myapiserver": {"Authorization": "Bearer token"}+ }++ # Test the case-insensitive matching logic from _call_regular_mcp_tool+ normalized_headers = {k.lower(): v for k, v in mcp_server_auth_headers.items()}+ server_auth_header = normalized_headers.get(server.server_name.lower())++ assert server_auth_header is not None+ assert server_auth_header["Authorization"] == "Bearer token"++ def test_alias_checked_before_server_name(self):+ """Test that alias is checked before server_name"""+ server = MCPServer(+ server_id="test-server",+ name="Test Server",+ alias="MyAlias",+ server_name="MyServerName",+ url="https://api.example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.authorization,+ )++ mcp_server_auth_headers = {+ "myalias": {"Authorization": "Bearer alias-token"},+ "myservername": {"Authorization": "Bearer servername-token"},+ }++ # Simulate the fix+ normalized_headers = {k.lower(): v for k, v in mcp_server_auth_headers.items()}+ server_auth_header = normalized_headers.get(server.alias.lower())+ if server_auth_header is None and server.server_name:+ server_auth_header = normalized_headers.get(server.server_name.lower())++ assert server_auth_header["Authorization"] == "Bearer alias-token"++ def test_fallback_to_legacy_auth_header(self):+ """Test fallback to legacy auth header when no server-specific header found"""+ server = MCPServer(+ server_id="test-server",+ name="Test Server",+ alias="MyServer",+ server_name="my_server",+ url="https://api.example.com",+ transport=MCPTransport.http,+ auth_type=MCPAuth.authorization,+ )++ mcp_server_auth_headers = {}+ mcp_auth_header = "Bearer legacy-token"++ # Simulate the fix+ normalized_headers = {k.lower(): v for k, v in mcp_server_auth_headers.items()}+ server_auth_header = normalized_headers.get(server.alias.lower())+ if server_auth_header is None and server.server_name:+ server_auth_header = normalized_headers.get(server.server_name.lower())+ if server_auth_header is None:+ server_auth_header = mcp_auth_header++ assert server_auth_header == "Bearer legacy-token"+++if __name__ == "__main__":+ pytest.main([__file__, "-v"])