From 021ec023ffbe3b658a4b641ba65eeeeeed40cc2b Mon Sep 17 00:00:00 2001 From: bfs2021 <101915083+bfs2021@users.noreply.github.com> Date: Sun, 25 Oct 2026 18:01:04 +0800 Subject: [PATCH] Bracket an IPv6 host in the advertised local URL With ACAD_MCP_HOST=::1, local_url() produced http://::1:8770/... which urllib.parse.urlsplit cannot read a port from. Bracket the host when it contains a colon; IPv4 and hostname addresses are unchanged. --- config.py | 4 +++- tests/test_config.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/config.py b/config.py index c33877f..1fc05a7 100644 --- a/config.py +++ b/config.py @@ -70,4 +70,6 @@ def ensure_dirs() -> None: def local_url() -> str: - return f"http://{HOST}:{PORT}{MCP_PATH}" + # An IPv6 host needs brackets before the port, or the URL cannot be parsed. + host = f"[{HOST}]" if ":" in HOST else HOST + return f"http://{host}:{PORT}{MCP_PATH}" diff --git a/tests/test_config.py b/tests/test_config.py index 4143a05..12ab51b 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,6 +11,7 @@ import os import tempfile import unittest +import urllib.parse from pathlib import Path from unittest import mock @@ -19,7 +20,12 @@ import config # noqa: E402 -_VARIABLES = ("ACAD_MCP_PORT", "ACAD_MCP_SAVE_DIR", "ACAD_MCP_SECRET_PATH") +_VARIABLES = ( + "ACAD_MCP_HOST", + "ACAD_MCP_PORT", + "ACAD_MCP_SAVE_DIR", + "ACAD_MCP_SECRET_PATH", +) def load(**env): @@ -43,6 +49,33 @@ def test_empty_variable_counts_as_unset(self): self.assertEqual(load(ACAD_MCP_PORT="").PORT, 8770) +class LocalUrl(unittest.TestCase): + """The advertised connector URL must parse for every host shape.""" + + def test_ipv6_host_is_bracketed_and_parses(self): + url = load( + ACAD_MCP_HOST="::1", ACAD_MCP_SECRET_PATH="offline-fixture" + ).local_url() + self.assertEqual(url, "http://[::1]:8770/offline-fixture/mcp") + parts = urllib.parse.urlsplit(url) + self.assertEqual(parts.hostname, "::1") + self.assertEqual(parts.port, 8770) + self.assertEqual(parts.path, "/offline-fixture/mcp") + + def test_ipv4_and_hostname_hosts_keep_their_shape(self): + cases = [ + ("127.0.0.1", "http://127.0.0.1:8770/offline-fixture/mcp"), + ("localhost", "http://localhost:8770/offline-fixture/mcp"), + ] + for host, expected in cases: + with self.subTest(host=host): + url = load( + ACAD_MCP_HOST=host, ACAD_MCP_SECRET_PATH="offline-fixture" + ).local_url() + self.assertEqual(url, expected) + self.assertEqual(urllib.parse.urlsplit(url).hostname, host) + + class SaveDirDefault(unittest.TestCase): def test_default_lives_in_the_current_users_profile(self): home = Path(tempfile.gettempdir()) / "someone-else"