Skip to content

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]

fix(mcp): strip scheme default port from get_request_base_url netloc

Tin Chi Lo168bc38d814a
diff --git a/litellm/proxy/_experimental/mcp_server/oauth_utils.py b/litellm/proxy/_experimental/mcp_server/oauth_utils.py
index 4d5813dbc5b..6edb22dd858 100644
--- a/litellm/proxy/_experimental/mcp_server/oauth_utils.py
+++ b/litellm/proxy/_experimental/mcp_server/oauth_utils.py
@@ -129,7 +129,7 @@ def get_request_base_url(request: Request) -> str:
if x_forwarded_port and ":" not in netloc:
netloc = f"{netloc}:{x_forwarded_port}"
- return urlunparse((scheme, netloc, parsed.path, "", "", ""))
+ return urlunparse((scheme, _strip_default_port(scheme, netloc), parsed.path, "", "", ""))
def validate_loopback_redirect_uri(redirect_uri: str) -> None:
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.py
index 11a6fea0cea..258156ede51 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
@@ -1848,6 +1848,41 @@ async def test_token_endpoint_respects_x_forwarded_host():
None,
"https://external.com",
),
+ (
+ "http://localhost:4000/",
+ "https",
+ "proxy.example.com",
+ "443",
+ "https://proxy.example.com",
+ ),
+ (
+ "http://localhost:4000/",
+ "http",
+ "proxy.example.com",
+ "80",
+ "http://proxy.example.com",
+ ),
+ (
+ "http://internal.local/",
+ "https",
+ None,
+ "443",
+ "https://internal.local",
+ ),
+ (
+ "http://localhost:4000/",
+ "https",
+ "proxy.example.com",
+ "8443",
+ "https://proxy.example.com:8443",
+ ),
+ (
+ "http://localhost:4000/",
+ "https",
+ "proxy.example.com:443",
+ None,
+ "https://proxy.example.com",
+ ),
],
)
def test_get_request_base_url_comprehensive(