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
fix(mcp): merge query params when authorization_url already contains them (#20968)
michelligabriele81a1cb13186f
diff --git a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.pyindex bdf4cc312d9..b0ae03c94d3 100644--- a/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py+++ b/litellm/proxy/_experimental/mcp_server/discoverable_endpoints.py@@ -1,6 +1,6 @@import jsonfrom typing import Optional-from urllib.parse import urlencode, urlparse, urlunparse+from urllib.parse import parse_qsl, urlencode, urlparse, urlunparsefrom fastapi import APIRouter, Form, HTTPException, Requestfrom fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse@@ -194,7 +194,13 @@ async def authorize_with_server(if code_challenge_method:params["code_challenge_method"] = code_challenge_method- return RedirectResponse(f"{mcp_server.authorization_url}?{urlencode(params)}")+ parsed_auth_url = urlparse(mcp_server.authorization_url)+ existing_params = dict(parse_qsl(parsed_auth_url.query))+ existing_params.update(params)+ final_url = urlunparse(+ parsed_auth_url._replace(query=urlencode(existing_params))+ )+ return RedirectResponse(final_url)async def exchange_token_with_server(diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.pyindex faabe40f2dc..d2b00d61d1b 100644--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_discoverable_endpoints.py@@ -88,6 +88,70 @@ async def test_authorize_endpoint_includes_response_type():assert "scope=read+write" in response.headers["location"]+async def test_authorize_endpoint_preserves_existing_query_params():+ """Test that authorize endpoint merges OAuth params with existing query params in authorization_url"""+ try:+ from litellm.proxy._experimental.mcp_server.discoverable_endpoints import (+ authorize,+ )+ from litellm.proxy._experimental.mcp_server.mcp_server_manager import (+ global_mcp_server_manager,+ )+ from litellm.types.mcp import MCPAuth+ from litellm.types.mcp_server.mcp_server_manager import MCPServer+ from litellm.proxy._types import MCPTransport+ from fastapi import Request+ except ImportError:+ pytest.skip("MCP discoverable endpoints not available")++ global_mcp_server_manager.registry.clear()++ # Authorization URL already has query params (e.g. multi-tenant OAuth)+ oauth2_server = MCPServer(+ server_id="test_oauth_server",+ name="test_oauth",+ server_name="test_oauth",+ alias="test_oauth",+ transport=MCPTransport.http,+ auth_type=MCPAuth.oauth2,+ client_id="test_client_id",+ client_secret="test_client_secret",+ authorization_url="https://provider.com/oauth/authorize?tenant=system",+ token_url="https://provider.com/oauth/token",+ scopes=["read", "write"],+ )+ global_mcp_server_manager.registry[oauth2_server.server_id] = oauth2_server++ mock_request = MagicMock(spec=Request)+ mock_request.base_url = "https://litellm.example.com/"+ mock_request.headers = {}++ with patch(+ "litellm.proxy._experimental.mcp_server.discoverable_endpoints.encrypt_value_helper"+ ) as mock_encrypt:+ mock_encrypt.return_value = "mocked_encrypted_state"++ response = await authorize(+ request=mock_request,+ client_id="test_client_id",+ mcp_server_name="test_oauth",+ redirect_uri="https://client.example.com/callback",+ state="test_state",+ )++ location = response.headers["location"]++ # Must NOT have double '?' — existing params must be merged correctly+ assert location.count("?") == 1, (+ f"Expected exactly one '?' in URL but got {location.count('?')}: {location}"+ )+ assert "tenant=system" in location+ assert "client_id=test_client_id" in location+ assert "response_type=code" in location+ assert "scope=read+write" in location++@pytest.mark.asyncioasync def test_authorize_endpoint_forwards_pkce_parameters():"""Test that authorize endpoint forwards PKCE parameters (code_challenge and code_challenge_method)"""