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): keep url redaction total when the port is malformed

Tin Chi Lodf75d298ec25
diff --git a/litellm/proxy/_experimental/mcp_server/oauth_utils.py b/litellm/proxy/_experimental/mcp_server/oauth_utils.py
index 2f92f75a352..8a5f398003b 100644
--- a/litellm/proxy/_experimental/mcp_server/oauth_utils.py
+++ b/litellm/proxy/_experimental/mcp_server/oauth_utils.py
@@ -83,11 +83,13 @@ def _redact_mcp_resource_url(url: Optional[str]) -> Optional[str]:
return None
try:
parts = urlsplit(url)
+ hostname = parts.hostname
+ port = parts.port
except ValueError:
return None
- if not parts.hostname:
+ if not hostname:
return None
- netloc = f"{parts.hostname}:{parts.port}" if parts.port else parts.hostname
+ netloc = f"{hostname}:{port}" if port else hostname
return urlunsplit((parts.scheme, netloc, "", "", "")) or None
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
index ae4f12fc1e1..dff1f1d87c7 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/test_mcp_server.py
@@ -7375,6 +7375,10 @@ async def test_call_tool_with_legacy_db_m2m_server_resolves_oauth2_flow():
("", None),
("not a url", None),
("http://[::1", None),
+ # urlsplit validates the port lazily on attribute access, so a malformed port must not
+ # raise out of the helper: the server loaders call it while warning about exactly this
+ # kind of typo'd url (LIT-4658)
+ ("https://example.com:bad/mcp", None),
],
)
def test_redact_mcp_resource_url_strips_credentials(url, expected):