From 28877b85b675d8f33795a111bd7ce04e9358e3b4 Mon Sep 17 00:00:00 2001 From: MIA_Ether Date: Thu, 3 Dec 2026 10:48:57 +0000 Subject: [PATCH 1/3] fix(cli): parse module:object server specs in _parse_file_path _parse_file_path split on the last colon, so entry.py:pkg_srv:app became file entry.py:pkg_srv and failed with File not found, making the module:object branch of _import_server unreachable from the CLI. Split on the first colon after the file path whose left side is an existing file, falling back to the last-colon split so missing files report the same path as before. Fixes #3966 --- src/mcp/cli/cli.py | 24 ++++++++++++++++++------ tests/cli/test_utils.py | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 203034c6ef..0f73c2f17b 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -89,7 +89,7 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]: """Parse a file path that may include a server object specification. Args: - file_spec: Path to file, optionally with :object suffix + file_spec: Path to file, optionally with :object or :module:object suffix Returns: Tuple of (file_path, server_object) @@ -97,12 +97,24 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]: # First check if we have a Windows path (e.g., C:\...) has_windows_drive = len(file_spec) > 1 and file_spec[1] == ":" - # Split on the last colon, but only if it's not part of the Windows drive letter - # and there's actually another colon in the string after the drive letter - if ":" in (file_spec[2:] if has_windows_drive else file_spec): - file_str, server_object = file_spec.rsplit(":", 1) + # The server object may itself contain a colon ("module:object"), so split + # on the first colon after the file path rather than the last one. A colon + # can also appear inside the file path, so take the first split whose left + # side is an existing file. + search_from = 2 if has_windows_drive else 0 + file_str, server_object = file_spec, None + colon = file_spec.find(":", search_from) + while colon != -1: + candidate = file_spec[:colon] + if Path(candidate).expanduser().is_file(): + file_str, server_object = candidate, file_spec[colon + 1 :] + break + colon = file_spec.find(":", colon + 1) else: - file_str, server_object = file_spec, None + # No split left an existing file; fall back to the last-colon split so + # a missing file reports the same path as before. + if ":" in file_spec[search_from:]: + file_str, server_object = file_spec.rsplit(":", 1) # Resolve the file path file_path = Path(file_str).expanduser().resolve() diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index 309e5035cd..1583c24d20 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -23,6 +23,7 @@ def fake_version(distribution_name: str) -> str: [ ("server.py", None), ("foo.py:srv_obj", "srv_obj"), + ("entry.py:pkg_srv:app", "pkg_srv:app"), ], ) def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected_obj: str | None): @@ -34,6 +35,22 @@ def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected assert obj == expected_obj +def test_parse_file_path_with_colon_in_directory(tmp_path: Path): + """Should split after the file path when a directory name contains a colon.""" + file = tmp_path / "we:ird" / "server.py" + file.parent.mkdir() + file.write_text("x = 1", encoding="utf-8") + path, obj = _parse_file_path(f"{file}:app") + assert path == file.resolve() + assert obj == "app" + + +def test_parse_file_path_missing_with_module_object(tmp_path: Path): + """Should system exit if a file with a module:object suffix is missing.""" + with pytest.raises(SystemExit): + _parse_file_path(f"{tmp_path / 'missing.py'}:mod:obj") + + def test_parse_file_path_missing(tmp_path: Path): """Should system exit if a file is missing.""" with pytest.raises(SystemExit): From 3bcf6c546f79535ef00c412960fffc8e02c326cd Mon Sep 17 00:00:00 2001 From: mia03ther Date: Thu, 3 Dec 2026 12:07:06 +0000 Subject: [PATCH 2/3] fix(cli): accept existing colon-containing file paths in _parse_file_path If the whole file spec is an existing file, take it as-is instead of splitting on its colons: a colon can be part of the file path itself. Also skip the colon-in-directory tests on Windows, where ':' is not allowed in directory names. --- src/mcp/cli/cli.py | 27 +++++++++++++++------------ tests/cli/test_utils.py | 12 ++++++++++++ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 0f73c2f17b..d706dc23ca 100644 --- a/src/mcp/cli/cli.py +++ b/src/mcp/cli/cli.py @@ -103,18 +103,21 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]: # side is an existing file. search_from = 2 if has_windows_drive else 0 file_str, server_object = file_spec, None - colon = file_spec.find(":", search_from) - while colon != -1: - candidate = file_spec[:colon] - if Path(candidate).expanduser().is_file(): - file_str, server_object = candidate, file_spec[colon + 1 :] - break - colon = file_spec.find(":", colon + 1) - else: - # No split left an existing file; fall back to the last-colon split so - # a missing file reports the same path as before. - if ":" in file_spec[search_from:]: - file_str, server_object = file_spec.rsplit(":", 1) + # If the whole spec is an existing file, there is no object suffix to + # split off — the colon is just part of the file path. + if not Path(file_spec).expanduser().is_file(): + colon = file_spec.find(":", search_from) + while colon != -1: + candidate = file_spec[:colon] + if Path(candidate).expanduser().is_file(): + file_str, server_object = candidate, file_spec[colon + 1 :] + break + colon = file_spec.find(":", colon + 1) + else: + # No split left an existing file; fall back to the last-colon split so + # a missing file reports the same path as before. + if ":" in file_spec[search_from:]: + file_str, server_object = file_spec.rsplit(":", 1) # Resolve the file path file_path = Path(file_str).expanduser().resolve() diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index 1583c24d20..39e299dbaf 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -35,6 +35,7 @@ def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected assert obj == expected_obj +@pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names") def test_parse_file_path_with_colon_in_directory(tmp_path: Path): """Should split after the file path when a directory name contains a colon.""" file = tmp_path / "we:ird" / "server.py" @@ -45,6 +46,17 @@ def test_parse_file_path_with_colon_in_directory(tmp_path: Path): assert obj == "app" +@pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names") +def test_parse_file_path_with_colon_in_directory_no_object(tmp_path: Path): + """Should accept an existing colon-containing file passed without a suffix.""" + file = tmp_path / "we:ird" / "server.py" + file.parent.mkdir() + file.write_text("x = 1", encoding="utf-8") + path, obj = _parse_file_path(str(file)) + assert path == file.resolve() + assert obj is None + + def test_parse_file_path_missing_with_module_object(tmp_path: Path): """Should system exit if a file with a module:object suffix is missing.""" with pytest.raises(SystemExit): From 7b074a29a46c1a9a5e8c8b0ebf8442e363dcc914 Mon Sep 17 00:00:00 2001 From: MIA_Ether Date: Fri, 4 Dec 2026 12:57:00 +0000 Subject: [PATCH 3/3] test(cli): exclude Windows-skipped colon-dir tests from coverage Windows CI enforces 100% coverage per job, but these tests are skipped on Windows (':' is illegal in directory names), so their bodies never execute there. Mark them '# pragma: lax no cover', the repo's sanctioned escape for platform-dependent coverage (cf. tests/client/test_stdio.py). --- tests/cli/test_utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index 39e299dbaf..bbdcb8397e 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -36,7 +36,8 @@ def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected @pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names") -def test_parse_file_path_with_colon_in_directory(tmp_path: Path): +# lax no cover: Windows CI jobs enforce 100% coverage per job and skip this test. +def test_parse_file_path_with_colon_in_directory(tmp_path: Path): # pragma: lax no cover """Should split after the file path when a directory name contains a colon.""" file = tmp_path / "we:ird" / "server.py" file.parent.mkdir() @@ -47,7 +48,8 @@ def test_parse_file_path_with_colon_in_directory(tmp_path: Path): @pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names") -def test_parse_file_path_with_colon_in_directory_no_object(tmp_path: Path): +# lax no cover: Windows CI jobs enforce 100% coverage per job and skip this test. +def test_parse_file_path_with_colon_in_directory_no_object(tmp_path: Path): # pragma: lax no cover """Should accept an existing colon-containing file passed without a suffix.""" file = tmp_path / "we:ird" / "server.py" file.parent.mkdir()