π The fast, Pythonic way to build MCP servers and clients.
Keep fastmcp.json resolvable when deployment.cwd changes the working directory (#5700)
Asem ahmedb098311fac03
diff --git a/fastmcp_slim/fastmcp/utilities/cli.py b/fastmcp_slim/fastmcp/utilities/cli.pyindex 3e24cc14c..1f0a8d360 100644--- a/fastmcp_slim/fastmcp/utilities/cli.py+++ b/fastmcp_slim/fastmcp/utilities/cli.py@@ -86,7 +86,13 @@ def load_and_merge_config(adapter = get_cached_typeadapter(MCPServerConfig)config = adapter.validate_python(data)- # Apply deployment settings+ # Apply deployment settings. A deployment cwd changes the+ # working directory, so pin the config path first or the+ # relative spec no longer resolves afterwards. Make it+ # absolute without following a symlink, so a link keeps+ # its own name and directory.+ config_path = Path(os.path.abspath(config_path))+ resolved_spec = str(config_path)if config.deployment:config.deployment.apply_runtime_settings(config_path)diff --git a/tests/cli/test_run_config.py b/tests/cli/test_run_config.pyindex 4d1036ae5..8c0b089f8 100644--- a/tests/cli/test_run_config.py+++ b/tests/cli/test_run_config.py@@ -7,6 +7,7 @@ from pathlib import Pathimport pytestfrom fastmcp.cli.run import load_mcp_server_config+from fastmcp.utilities.cli import load_and_merge_configfrom fastmcp.utilities.mcp_server_config import (Deployment,MCPServerConfig,@@ -310,3 +311,39 @@ def test_environment_config_path_resolution(tmp_path):# Path should be resolved relative to config filereq_idx = uv_cmd.index("--with-requirements") + 1assert Path(uv_cmd[req_idx]).is_absolute() or uv_cmd[req_idx] == "requirements.txt"+++def test_relative_config_path_survives_deployment_cwd(tmp_path, monkeypatch):+ """A relative config path must still resolve after deployment.cwd changes the cwd."""+ work_dir = tmp_path / "work"+ work_dir.mkdir()+ (work_dir / "server.py").write_text("# Server")+ (tmp_path / "fastmcp.json").write_text(+ json.dumps({"source": {"path": "server.py"}, "deployment": {"cwd": "work"}})+ )+ monkeypatch.chdir(tmp_path)++ _, server_spec = load_and_merge_config("fastmcp.json")++ assert Path.cwd() == work_dir.resolve()+ # run_command re-opens the spec after the working directory changed+ config = load_mcp_server_config(Path(server_spec))+ assert config.deployment.cwd == "work"+ assert Path.cwd() == work_dir.resolve()+++def test_config_symlink_keeps_its_own_name(tmp_path, monkeypatch):+ """A symlinked config keeps the link's name, so the spec is still read as JSON."""+ target = tmp_path / "config.txt"+ target.write_text(json.dumps({"source": {"path": "server.py"}}))+ (tmp_path / "server.py").write_text("# Server")+ link = tmp_path / "fastmcp.json"+ try:+ link.symlink_to(target)+ except (OSError, NotImplementedError):+ pytest.skip("symlinks are not available")+ monkeypatch.chdir(tmp_path)++ _, server_spec = load_and_merge_config("fastmcp.json")++ assert Path(server_spec) == link