diff --git a/src/mcp/cli/cli.py b/src/mcp/cli/cli.py index 203034c6ef..d706dc23ca 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,27 @@ 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) - else: - file_str, server_object = file_spec, None + # 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 + # 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 309e5035cd..bbdcb8397e 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,36 @@ 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") +# 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() + file.write_text("x = 1", encoding="utf-8") + path, obj = _parse_file_path(f"{file}:app") + assert path == file.resolve() + assert obj == "app" + + +@pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names") +# 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() + 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): + _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):