π The fast, Pythonic way to build MCP servers and clients.
fix: forward script arguments in FastMCPStdioTransport (#6119)
Kashif Manzerbaf255c0a332
diff --git a/fastmcp_slim/fastmcp/client/transports/stdio.py b/fastmcp_slim/fastmcp/client/transports/stdio.pyindex daf794854..ef9e669c8 100644--- a/fastmcp_slim/fastmcp/client/transports/stdio.py+++ b/fastmcp_slim/fastmcp/client/transports/stdio.py@@ -352,7 +352,10 @@ class PythonStdioTransport(StdioTransport):class FastMCPStdioTransport(StdioTransport):- """Transport for running FastMCP servers using the FastMCP CLI."""+ """Transport for running FastMCP servers using the FastMCP CLI.++ `args` are passed to the server script, not interpreted as FastMCP CLI options.+ """def __init__(self,@@ -369,13 +372,17 @@ class FastMCPStdioTransport(StdioTransport):if not str(script_path).endswith(".py"):raise ValueError(f"Not a Python script: {script_path}")+ full_args = ["run", str(script_path)]+ if args:+ full_args.extend(["--", *args])+# Same UTF-8 default as PythonStdioTransport: the `fastmcp` command# is a Python entry point with the same sanitized child environment.env = {"PYTHONUTF8": "1", **(env or {})}super().__init__(command="fastmcp",- args=["run", str(script_path)],+ args=full_args,env=env,cwd=cwd,keep_alive=keep_alive,diff --git a/tests/client/test_stdio.py b/tests/client/test_stdio.pyindex 6d627fd28..6a29834da 100644--- a/tests/client/test_stdio.py+++ b/tests/client/test_stdio.py@@ -894,3 +894,72 @@ class TestPythonTransportEnvironment:script_path=script, env={"PYTHONUTF8": "0", "EXTRA": "kept"})assert custom.env == {"PYTHONUTF8": "0", "EXTRA": "kept"}+++class TestFastMCPTransportArguments:+ @pytest.mark.parametrize(+ ("args", "expected_tail"),+ [(None, []), ([], []), (["hello"], ["--", "hello"]), ([""], ["--", ""])],+ )+ def test_builds_script_arguments(+ self, tmp_path: Path, args: list[str] | None, expected_tail: list[str]+ ) -> None:+ script = tmp_path / "server.py"+ script.write_text("", encoding="utf-8")+ original_args = args.copy() if args is not None else None++ transport = FastMCPStdioTransport(script, args=args)++ assert transport.command == "fastmcp"+ assert transport.args == ["run", str(script), *expected_tail]+ assert args == original_args++ def test_omitted_arguments_keep_existing_command(self, tmp_path: Path) -> None:+ script = tmp_path / "server.py"+ script.write_text("", encoding="utf-8")++ transport = FastMCPStdioTransport(str(script))++ assert transport.args == ["run", str(script)]++ @pytest.mark.client_process+ @pytest.mark.parametrize(+ "args",+ [+ ["hello"],+ [""],+ ["--transport", "http", "two words", "", "ιͺ", "--", "--literal"],+ ],+ )+ async def test_script_receives_arguments(+ self, tmp_path: Path, args: list[str]+ ) -> None:+ script = tmp_path / "server with spaces.py"+ script.write_text(+ inspect.cleandoc("""+ import sys+ from fastmcp import FastMCP++ arguments = sys.argv[1:]+ mcp = FastMCP()++ @mcp.tool+ def get_arguments() -> list[str]:+ return arguments+ """),+ encoding="utf-8",+ )+ transport = FastMCPStdioTransport(+ script,+ args=args,+ env={+ "FASTMCP_SHOW_SERVER_BANNER": "false",+ "FASTMCP_CHECK_FOR_UPDATES": "off",+ },+ keep_alive=False,+ )++ async with Client(transport) as client:+ result = await client.call_tool("get_arguments")++ assert result.data == args