Skip to content

Commit 3bcf6c5

Browse files
committed
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.
1 parent 28877b8 commit 3bcf6c5

2 files changed

Lines changed: 27 additions & 12 deletions

File tree

‎src/mcp/cli/cli.py‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,21 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]:
103103
# side is an existing file.
104104
search_from = 2 if has_windows_drive else 0
105105
file_str, server_object = file_spec, None
106-
colon = file_spec.find(":", search_from)
107-
while colon != -1:
108-
candidate = file_spec[:colon]
109-
if Path(candidate).expanduser().is_file():
110-
file_str, server_object = candidate, file_spec[colon + 1 :]
111-
break
112-
colon = file_spec.find(":", colon + 1)
113-
else:
114-
# No split left an existing file; fall back to the last-colon split so
115-
# a missing file reports the same path as before.
116-
if ":" in file_spec[search_from:]:
117-
file_str, server_object = file_spec.rsplit(":", 1)
106+
# If the whole spec is an existing file, there is no object suffix to
107+
# split off — the colon is just part of the file path.
108+
if not Path(file_spec).expanduser().is_file():
109+
colon = file_spec.find(":", search_from)
110+
while colon != -1:
111+
candidate = file_spec[:colon]
112+
if Path(candidate).expanduser().is_file():
113+
file_str, server_object = candidate, file_spec[colon + 1 :]
114+
break
115+
colon = file_spec.find(":", colon + 1)
116+
else:
117+
# No split left an existing file; fall back to the last-colon split so
118+
# a missing file reports the same path as before.
119+
if ":" in file_spec[search_from:]:
120+
file_str, server_object = file_spec.rsplit(":", 1)
118121

119122
# Resolve the file path
120123
file_path = Path(file_str).expanduser().resolve()

‎tests/cli/test_utils.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected
3535
assert obj == expected_obj
3636

3737

38+
@pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names")
3839
def test_parse_file_path_with_colon_in_directory(tmp_path: Path):
3940
"""Should split after the file path when a directory name contains a colon."""
4041
file = tmp_path / "we:ird" / "server.py"
@@ -45,6 +46,17 @@ def test_parse_file_path_with_colon_in_directory(tmp_path: Path):
4546
assert obj == "app"
4647

4748

49+
@pytest.mark.skipif(sys.platform == "win32", reason="':' is not allowed in Windows directory names")
50+
def test_parse_file_path_with_colon_in_directory_no_object(tmp_path: Path):
51+
"""Should accept an existing colon-containing file passed without a suffix."""
52+
file = tmp_path / "we:ird" / "server.py"
53+
file.parent.mkdir()
54+
file.write_text("x = 1", encoding="utf-8")
55+
path, obj = _parse_file_path(str(file))
56+
assert path == file.resolve()
57+
assert obj is None
58+
59+
4860
def test_parse_file_path_missing_with_module_object(tmp_path: Path):
4961
"""Should system exit if a file with a module:object suffix is missing."""
5062
with pytest.raises(SystemExit):

0 commit comments

Comments
 (0)