Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix server argument passing in CLI run command (#1293)

Tapan Chugh770585826640
diff --git a/src/fastmcp/cli/cli.py b/src/fastmcp/cli/cli.py
index 11730d6b1..99e37107a 100644
--- a/src/fastmcp/cli/cli.py
+++ b/src/fastmcp/cli/cli.py
@@ -285,7 +285,7 @@ def dev(
@app.command
def run(
server_spec: str,
- *,
+ *server_args: str,
transport: Annotated[
run_module.TransportType | None,
cyclopts.Parameter(
@@ -373,9 +373,6 @@ def run(
Args:
server_spec: Python file, object specification (file:obj), MCPConfig file, or URL
"""
- # TODO: Handle server_args from extra context
- server_args = [] # Will need to handle this with Cyclopts context
-
logger.debug(
"Running server or client",
extra={
@@ -385,7 +382,7 @@ def run(
"port": port,
"path": path,
"log_level": log_level,
- "server_args": server_args,
+ "server_args": list(server_args),
},
)
@@ -424,7 +421,7 @@ def run(
port=port,
path=path,
log_level=log_level,
- server_args=server_args,
+ server_args=list(server_args),
show_banner=not no_banner,
)
except Exception as e:
diff --git a/tests/cli/test_cli.py b/tests/cli/test_cli.py
index 13b304446..4653c189c 100644
--- a/tests/cli/test_cli.py
+++ b/tests/cli/test_cli.py
@@ -399,6 +399,79 @@ class TestRunCommand:
assert command is not None
assert bound.arguments["transport"] == "streamable-http"
+ def test_run_command_parsing_with_server_args(self):
+ """Test run command parsing with server arguments after --."""
+ command, bound, _ = app.parse_args(
+ [
+ "run",
+ "server.py",
+ "--",
+ "--config",
+ "test.json",
+ "--debug",
+ ]
+ )
+
+ assert command is not None
+ assert bound.arguments["server_spec"] == "server.py"
+ # Server args after -- are captured as positional arguments in bound.args
+ assert bound.args == ("server.py", "--config", "test.json", "--debug")
+
+ def test_run_command_parsing_with_mixed_args(self):
+ """Test run command parsing with both FastMCP options and server args."""
+ command, bound, _ = app.parse_args(
+ [
+ "run",
+ "server.py",
+ "--transport",
+ "http",
+ "--port",
+ "8080",
+ "--",
+ "--server-port",
+ "9090",
+ "--debug",
+ ]
+ )
+
+ assert command is not None
+ assert bound.arguments["server_spec"] == "server.py"
+ assert bound.arguments["transport"] == "http"
+ assert bound.arguments["port"] == 8080
+ # Server args after -- are captured separately from FastMCP options
+ assert bound.args == ("server.py", "--server-port", "9090", "--debug")
+
+ def test_run_command_parsing_with_positional_server_args(self):
+ """Test run command parsing with positional server arguments."""
+ command, bound, _ = app.parse_args(
+ [
+ "run",
+ "server.py",
+ "--",
+ "arg1",
+ "arg2",
+ "--flag",
+ ]
+ )
+
+ assert command is not None
+ assert bound.arguments["server_spec"] == "server.py"
+ # Positional args and flags after -- are all captured
+ assert bound.args == ("server.py", "arg1", "arg2", "--flag")
+
+ def test_run_command_parsing_server_args_require_delimiter(self):
+ """Test that server args without -- delimiter are rejected."""
+ # Should fail because --config is not a recognized FastMCP option
+ with pytest.raises(SystemExit):
+ app.parse_args(
+ [
+ "run",
+ "server.py",
+ "--config",
+ "test.json",
+ ]
+ )
+
class TestWindowsSpecific:
"""Test Windows-specific functionality."""