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): redact upstream URL in tool-call logs and plug fan-out Authorization bypass

Tin7c52cde50571
diff --git a/litellm/proxy/_experimental/mcp_server/server.py b/litellm/proxy/_experimental/mcp_server/server.py
index 5c814774fda..938cc2bc43b 100644
--- a/litellm/proxy/_experimental/mcp_server/server.py
+++ b/litellm/proxy/_experimental/mcp_server/server.py
@@ -27,6 +27,7 @@ from typing import (
Union,
cast,
)
+from urllib.parse import urlsplit, urlunsplit
import httpx
from fastapi import FastAPI, HTTPException
@@ -105,6 +106,26 @@ _MAX_STATEFUL_SESSIONS_PER_OWNER = 100
_MCP_ROUTING_PEEK_MAX_BYTES = 4096
+def _redact_mcp_resource_url(url: Optional[str]) -> Optional[str]:
+ """Reduce an MCP server URL to its bare resource identifier for logging.
+
+ Keeps scheme, host, and path; drops userinfo (``user:pass@``), the query string,
+ and the fragment, so an upstream URL carrying an embedded token, userinfo, or a
+ secret query parameter never reaches spend-log metadata or logging callbacks.
+ Returns None when the URL has no host to identify (nothing safe to log).
+ """
+ if not isinstance(url, str) or not url:
+ return None
+ try:
+ parts = urlsplit(url)
+ except ValueError:
+ return None
+ if not parts.hostname:
+ return None
+ netloc = f"{parts.hostname}:{parts.port}" if parts.port else parts.hostname
+ return urlunsplit((parts.scheme, netloc, parts.path, "", "")) or None
+
+
def _invalidate_byok_cred_cache(user_id: str, server_id: str) -> None:
"""Remove a (user_id, server_id) entry from the BYOK credential cache.
@@ -3065,7 +3086,7 @@ if MCP_AVAILABLE:
namespaced_tool_name=namespaced_tool_name,
mcp_session_id=session_id,
mcp_auth_mode=mcp_server.auth_type,
- mcp_server_resource=mcp_server.url,
+ mcp_server_resource=_redact_mcp_resource_url(mcp_server.url),
)
else:
return StandardLoggingMCPToolCall(
diff --git a/litellm/types/utils.py b/litellm/types/utils.py
index e71c42084d1..6b99cfa3314 100644
--- a/litellm/types/utils.py
+++ b/litellm/types/utils.py
@@ -2533,7 +2533,9 @@ class StandardLoggingMCPToolCall(TypedDict, total=False):
mcp_server_resource: Optional[str]
"""
- The upstream MCP server URL (the RFC 8707 resource) the tool call was forwarded to.
+ The upstream MCP server resource identifier (scheme + host + path) the tool call was
+ forwarded to. Redacted for logging: userinfo, query string, and fragment are stripped so an
+ upstream URL carrying an embedded token or secret query parameter never reaches log metadata.
Records which upstream received a relayed request; never a credential.
"""
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 e9dccdcd4ad..0db36e75e48 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
@@ -6854,3 +6854,25 @@ async def test_call_tool_with_legacy_db_m2m_server_resolves_oauth2_flow():
resolved = captured_servers["allowed"]
assert resolved and resolved[0].oauth2_flow == "client_credentials"
assert resolved[0].has_client_credentials is True
+
+
+ "url, expected",
+ [
+ # userinfo + secret query param must both be stripped from the logged resource
+ ("https://user:[email protected]/mcp?token=abcd1234&x=1", "https://mcp.example.com/mcp"),
+ ("https://mcp.example.com/mcp#frag", "https://mcp.example.com/mcp"),
+ ("https://host:8443/a/b?q=1", "https://host:8443/a/b"),
+ ("https://mcp.notion.com/mcp", "https://mcp.notion.com/mcp"),
+ (None, None),
+ ("", None),
+ ("not a url", None),
+ ],
+)
+def test_redact_mcp_resource_url_strips_credentials(url, expected):
+ """The MCP tool-call log records the upstream resource, so the URL must be redacted to
+ scheme+host+path: userinfo, query string, and fragment (which can carry embedded tokens or
+ secret parameters) must never reach spend-log metadata or logging callbacks."""
+ from litellm.proxy._experimental.mcp_server.server import _redact_mcp_resource_url
+
+ assert _redact_mcp_resource_url(url) == expected