Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions src/mcp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,35 @@ 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)
"""
# 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()
Expand Down
29 changes: 29 additions & 0 deletions tests/cli/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -34,6 +35,34 @@ 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"
Comment thread
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
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")
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):
_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):
Expand Down
Loading