Skip to content

fix(cli): parse module:object server specs in _parse_file_path - #3969

Open
mia03ther wants to merge 1 commit into
modelcontextprotocol:mainfrom
mia03ther:fix/3966-parse-file-path
Open

mia03ther wants to merge 1 commit into
modelcontextprotocol:mainfrom
mia03ther:fix/3966-parse-file-path

Conversation

@mia03ther

Copy link
Copy Markdown

Fixes #3966.

_parse_file_path() split the file spec 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 (mcp dev / run / install).

This splits on the first colon after the file path whose left side is an existing file, so the server object may itself contain a colon. A colon can also appear inside the file path, so each candidate split is checked for an existing file; when no split leaves an existing file it falls back to the last-colon split, so a genuinely missing file reports the same path as before.

Tests: added a entry.py:pkg_srv:app case to test_parse_file_path_accepts_valid_specs, plus a colon-in-directory case and a missing-file-with-module:object case. tests/cli/test_utils.py: 13 passed; src/mcp/cli/cli.py coverage 100%, ruff and pyright clean.

_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 modelcontextprotocol#3966

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 2 files

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="tests/cli/test_utils.py">

<violation number="1" location="tests/cli/test_utils.py:40">
P2: This test fails on Windows because `:` is not allowed in a directory name, so `file.parent.mkdir()` raises before `_parse_file_path()` runs. Skip this platform-specific case on Windows.</violation>
</file>

<file name="src/mcp/cli/cli.py">

<violation number="1" location="src/mcp/cli/cli.py:105">
P2: An existing colon-containing file passed without an object suffix is still split as object syntax and rejected as missing. Check whether the complete `file_spec` is a file before interpreting its colons.</violation>
</file>

Reply to a comment to ask cubic a question or push back. It learns from your replies.

Re-trigger cubic

Comment thread tests/cli/test_utils.py

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"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This test fails on Windows because : is not allowed in a directory name, so file.parent.mkdir() raises before _parse_file_path() runs. Skip this platform-specific case on Windows.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At tests/cli/test_utils.py, line 40:

<comment>This test fails on Windows because `:` is not allowed in a directory name, so `file.parent.mkdir()` raises before `_parse_file_path()` runs. Skip this platform-specific case on Windows.</comment>

<file context>
@@ -34,6 +35,22 @@ def test_parse_file_path_accepts_valid_specs(tmp_path: Path, spec: str, expected
 
+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")
</file context>

Comment thread src/mcp/cli/cli.py
Comment on lines +105 to +117
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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: An existing colon-containing file passed without an object suffix is still split as object syntax and rejected as missing. Check whether the complete file_spec is a file before interpreting its colons.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/mcp/cli/cli.py, line 105:

<comment>An existing colon-containing file passed without an object suffix is still split as object syntax and rejected as missing. Check whether the complete `file_spec` is a file before interpreting its colons.</comment>

<file context>
@@ -89,20 +89,32 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]:
+    # 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:
</file context>
Suggested change
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)
if Path(file_spec).expanduser().is_file():
file_str, server_object = file_spec, None
else:
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)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI _parse_file_path() splits with rsplit(":", 1), making _import_server()'s "module:object" syntax unreachable

1 participant