Skip to content

Commit 651d504

Browse files
committed
Handle multiline YAML commands with backslashes
1 parent 729c0db commit 651d504

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

src/mcp/client/config/mcp_servers_config.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ class StdioServerConfig(MCPServerConfig):
2929
env: dict[str, str] | None = None
3030

3131
def _parse_command(self) -> list[str]:
32-
"""Parse the command string into parts, handling quotes properly."""
33-
return shlex.split(self.command)
32+
"""Parse the command string into parts, handling quotes properly.
33+
34+
Strips unnecessary whitespace and newlines to handle YAML multi-line strings.
35+
Treats backslashes followed by newlines as line continuations.
36+
"""
37+
# Handle backslash line continuations by removing them and the following newline
38+
cleaned_command = self.command.replace("\\\n", " ")
39+
# Then normalize all whitespace (including remaining newlines) to single spaces
40+
cleaned_command = " ".join(cleaned_command.split())
41+
return shlex.split(cleaned_command)
3442

3543
@property
3644
def effective_command(self) -> str:

tests/client/config/mcp.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ mcpServers:
88
filesystem:
99
command: npx -y @modelcontextprotocol/server-filesystem /Users/username/Desktop /path/to/other/allowed/dir
1010

11+
filesystem_multi_line:
12+
command: >
13+
npx -y @modelcontextprotocol/server-filesystem \
14+
/Users/username/Desktop \
15+
/path/to/other/allowed/dir
16+
1117
# Stdio server with full command string
1218
# The library will automatically parse this.
1319
# The effective_command will be "python" and effective_args will be ["-m", "my_server"]

tests/client/config/test_yaml_functionality.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ def test_npx_filesystem_server(mcp_yaml_config_file: Path):
9898
assert filesystem_server.args is None # No explicit args
9999
assert filesystem_server.env is None # No environment variables
100100

101-
# Test the effective command parsing
101+
# Test the effective command and args parsing
102102
assert filesystem_server.effective_command == "npx"
103-
expected_args = [
103+
assert filesystem_server.effective_args == [
104104
"-y",
105105
"@modelcontextprotocol/server-filesystem",
106106
"/Users/username/Desktop",
107107
"/path/to/other/allowed/dir"
108108
]
109-
assert filesystem_server.effective_args == expected_args

0 commit comments

Comments
 (0)