fix(mcp): keep url redaction total when the port is malformed

urlsplit validates the port lazily, so a non-numeric port raised
ValueError out of _redact_mcp_resource_url after the urlsplit try
had already passed; the server loaders now call the helper while
warning about typo'd urls, which would have turned the warning into
a load failure. Resolve hostname and port inside the guard and pin
the malformed-port case in the redaction test
This commit is contained in:
Tin Chi Lo 2026-07-21 22:45:39 -07:00
parent cfbcef319c
commit df75d298ec
2 changed files with 8 additions and 2 deletions

View file

@ -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

View file

@ -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):