Skip to content

Fix _parse_file_path splitting module:object server specs - #3981

Open
sashaosman wants to merge 1 commit into
modelcontextprotocol:mainfrom
sashaosman:fix/parse-file-path-module-object
Open

sashaosman wants to merge 1 commit into
modelcontextprotocol:mainfrom
sashaosman:fix/parse-file-path-module-object

Conversation

@sashaosman

Copy link
Copy Markdown

Fixes #3966.

_parse_file_path() split the file spec with rsplit(":", 1), so mcp run entry.py:pkg_srv:app was cut into a file path of entry.py:pkg_srv and the CLI exited with "File not found". The module:object handling in _import_server() could never be reached from the dev, run, or install commands.

This splits on the first colon after the file path instead (still skipping a Windows drive letter), so the rest of the spec can carry module:object through to _import_server().

Tested: added a foo.py:pkg.mod:srv_obj case to the existing parametrized test in tests/cli/test_utils.py, and verified the patched function against the plain path, file:object, file:module:object, missing file, and directory cases locally (the old code exits on the module:object spec, the patched code parses it). Happy to adjust the approach if maintainers would rather resolve the path differently.

_parse_file_path split the file spec on the last colon, so a spec like
entry.py:pkg_srv:app was cut into a nonexistent file path and the CLI
exited with "File not found". That made the module:object branch in
_import_server unreachable from the dev, run, and install commands.

Split on the first colon after the file path instead (still skipping a
Windows drive letter), so the remainder can carry module:object.

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.

1 issue 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="src/mcp/cli/cli.py">

<violation number="1" location="src/mcp/cli/cli.py:106">
P2: This treats the first colon in the argument as the separator, so a valid POSIX spec such as `/tmp/foo:bar.py:app` is parsed as file `/tmp/foo` and object `bar.py:app`. Select the separator based on an existing file prefix or use an unambiguous syntax so colon-containing filenames remain usable.</violation>
</file>

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

Re-trigger cubic

Comment thread src/mcp/cli/cli.py
# into the object spec and leave a file path that does not exist.
rest = file_spec[2:] if has_windows_drive else file_spec
if ":" in rest:
split_at = rest.index(":") + (2 if has_windows_drive else 0)

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 treats the first colon in the argument as the separator, so a valid POSIX spec such as /tmp/foo:bar.py:app is parsed as file /tmp/foo and object bar.py:app. Select the separator based on an existing file prefix or use an unambiguous syntax so colon-containing filenames remain usable.

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 106:

<comment>This treats the first colon in the argument as the separator, so a valid POSIX spec such as `/tmp/foo:bar.py:app` is parsed as file `/tmp/foo` and object `bar.py:app`. Select the separator based on an existing file prefix or use an unambiguous syntax so colon-containing filenames remain usable.</comment>

<file context>
@@ -97,10 +97,14 @@ def _parse_file_path(file_spec: str) -> tuple[Path, str | None]:
+    # into the object spec and leave a file path that does not exist.
+    rest = file_spec[2:] if has_windows_drive else file_spec
+    if ":" in rest:
+        split_at = rest.index(":") + (2 if has_windows_drive else 0)
+        file_str, server_object = file_spec[:split_at], file_spec[split_at + 1 :]
     else:
</file context>

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