Skip to content

Commit c92c12f

Browse files
committed
Add effective_command and effective_args
Add the ability to parse a multi-word command and extract the command and args into `effective_command` and `effective_args` attributes.
1 parent 7d20254 commit c92c12f

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/mcp/client/config/mcp_servers_config.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ class StdioServerConfig(MCPServerConfig):
2323
args: list[str] | None = None
2424
env: dict[str, str] | None = None
2525

26+
@property
27+
def effective_command(self) -> str:
28+
"""Get the effective command (first part of the command string)."""
29+
return self.command.split()[0]
30+
31+
@property
32+
def effective_args(self) -> list[str]:
33+
"""Get the effective arguments (parsed from command plus explicit args)."""
34+
command_parts = self.command.split()
35+
parsed_args = command_parts[1:] if len(command_parts) > 1 else []
36+
explicit_args = self.args or []
37+
return parsed_args + explicit_args
38+
2639

2740
class StreamableHttpConfig(MCPServerConfig):
2841
"""Configuration for StreamableHTTP-based MCP servers."""

tests/client/config/test_mcp_servers_config.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ def mcp_config_file(tmp_path: Path) -> Path:
2424
"args": ["-m", "my_server"],
2525
"env": {"DEBUG": "true"},
2626
},
27+
"stdio_server_with_full_command": {
28+
"command": "python -m my_server",
29+
},
30+
"stdio_server_with_full_command_and_explicit_args": {
31+
"command": "python -m my_server", # Two args here: -m and my_server
32+
"args": ["--debug"], # One explicit arg here: --debug
33+
},
2734
"http_streamable": {
2835
"url": "https://api.example.com/mcp",
2936
"headers": {"Authorization": "Bearer token123"},
@@ -49,7 +56,45 @@ def test_stdio_server(mcp_config_file: Path):
4956
assert stdio_server.args == ["-m", "my_server"]
5057
assert stdio_server.env == {"DEBUG": "true"}
5158
assert stdio_server.type == "stdio" # Should be automatically inferred
52-
59+
60+
# In this case, effective_command and effective_args are the same as command
61+
# and args.
62+
# But later on, we will see a test where the command is specified as a
63+
# single string, and we expect the command to be split into command and args
64+
assert stdio_server.effective_command == "python"
65+
assert stdio_server.effective_args == ["-m", "my_server"]
66+
67+
68+
def test_stdio_server_with_full_command_should_be_split(mcp_config_file: Path):
69+
"""This test should fail - it expects the command to be split into command and args."""
70+
config = MCPServersConfig.from_file(mcp_config_file)
71+
72+
stdio_server = config.servers["stdio_server_with_full_command"]
73+
assert isinstance(stdio_server, StdioServerConfig)
74+
75+
# This is how the command was specified
76+
assert stdio_server.command == "python -m my_server"
77+
78+
# This is how the command is split into command and args
79+
assert stdio_server.effective_command == "python"
80+
assert stdio_server.effective_args == ["-m", "my_server"]
81+
82+
83+
def test_stdio_server_with_full_command_and_explicit_args(mcp_config_file: Path):
84+
"""Test that effective_args combines parsed command args with explicit args."""
85+
config = MCPServersConfig.from_file(mcp_config_file)
86+
87+
stdio_server = config.servers["stdio_server_with_full_command_and_explicit_args"]
88+
assert isinstance(stdio_server, StdioServerConfig)
89+
90+
# Test original values
91+
assert stdio_server.command == "python -m my_server"
92+
assert stdio_server.args == ["--debug"]
93+
94+
# Test effective values - should combine parsed command args with explicit args
95+
assert stdio_server.effective_command == "python"
96+
assert stdio_server.effective_args == ["-m", "my_server", "--debug"]
97+
5398

5499
def test_streamable_http_server(mcp_config_file: Path):
55100
config = MCPServersConfig.from_file(mcp_config_file)

0 commit comments

Comments
 (0)