Skip to content

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

Create the MCP config file on install when it is missing or empty (#5887)

Asem ahmed3c07bfae6db9
diff --git a/fastmcp_slim/fastmcp/mcp_config.py b/fastmcp_slim/fastmcp/mcp_config.py
index adf01f424..786afc448 100644
--- a/fastmcp_slim/fastmcp/mcp_config.py
+++ b/fastmcp_slim/fastmcp/mcp_config.py
@@ -389,8 +389,12 @@ def update_config_file(
"""Update an MCP configuration file from a server object, preserving existing fields.
This is used for updating the mcpServer configurations of third-party tools so we do not
- worry about transforming server objects here."""
- config = MCPConfig.from_file(file_path)
+ worry about transforming server objects here. A missing or empty file is treated as a
+ config with no servers, so it is created on write."""
+ if file_path.exists() and file_path.read_text(encoding="utf-8").strip():
+ config = MCPConfig.from_file(file_path)
+ else:
+ config = MCPConfig()
# If updating an existing server, merge with existing configuration
# to preserve any unknown fields
diff --git a/tests/cli/test_cursor.py b/tests/cli/test_cursor.py
index 3996aee34..464b6e6f8 100644
--- a/tests/cli/test_cursor.py
+++ b/tests/cli/test_cursor.py
@@ -371,6 +371,23 @@ class TestInstallCursor:
assert result is False
+ def test_install_cursor_workspace_empty_config_file(self, tmp_path):
+ """Test that an empty .cursor/mcp.json is filled in instead of rejected."""
+ config_file = tmp_path / ".cursor" / "mcp.json"
+ config_file.parent.mkdir()
+ config_file.write_text("")
+
+ result = install_cursor_workspace(
+ file=Path("/path/to/server.py"),
+ server_object=None,
+ name="test-server",
+ workspace_path=tmp_path,
+ )
+
+ assert result is True
+ saved = json.loads(config_file.read_text(encoding="utf-8"))
+ assert list(saved["mcpServers"]) == ["test-server"]
+
def test_install_cursor_deduplicate_packages(self):
"""Test that duplicate packages are deduplicated."""
with patch("fastmcp.cli.install.cursor.open_deeplink") as mock_open:
diff --git a/tests/cli/test_install.py b/tests/cli/test_install.py
index 094491f4c..96355895f 100644
--- a/tests/cli/test_install.py
+++ b/tests/cli/test_install.py
@@ -4,6 +4,7 @@ from pathlib import Path
import pytest
from fastmcp.cli.install import install_app
+from fastmcp.cli.install.claude_desktop import install_claude_desktop
from fastmcp.cli.install.mcp_json import install_mcp_json
from fastmcp.cli.install.shared import validate_server_name
from fastmcp.cli.install.stdio import install_stdio
@@ -162,6 +163,63 @@ class TestClaudeDesktopInstall:
assert bound.arguments.get("config_path") is None
+class TestClaudeDesktopConfigFile:
+ """Claude Desktop only creates its config file once the user opens Edit Config."""
+
+ @pytest.fixture
+ def server_file(self, tmp_path: Path) -> Path:
+ server_file = tmp_path / "server.py"
+ server_file.write_text(
+ "from fastmcp import FastMCP\n\nmcp = FastMCP('demo')\n", encoding="utf-8"
+ )
+ return server_file
+
+ @pytest.fixture
+ def config_dir(self, tmp_path: Path) -> Path:
+ config_dir = tmp_path / "claude"
+ config_dir.mkdir()
+ return config_dir
+
+ @pytest.mark.parametrize("existing", [None, "", " \n\t\n"])
+ def test_install_creates_missing_or_blank_config_file(
+ self, server_file: Path, config_dir: Path, existing: str | None
+ ):
+ config_file = config_dir / "claude_desktop_config.json"
+ if existing is not None:
+ config_file.write_text(existing, encoding="utf-8")
+
+ assert install_claude_desktop(
+ server_file, "mcp", "demo", config_path=config_dir
+ )
+
+ saved = json.loads(config_file.read_text(encoding="utf-8"))
+ assert list(saved["mcpServers"]) == ["demo"]
+ assert saved["mcpServers"]["demo"]["args"][-1] == f"{server_file.resolve()}:mcp"
+
+ def test_install_keeps_other_settings_in_existing_config_file(
+ self, server_file: Path, config_dir: Path
+ ):
+ config_file = config_dir / "claude_desktop_config.json"
+ config_file.write_text(
+ json.dumps(
+ {
+ "preferences": {"theme": "dark"},
+ "mcpServers": {"other": {"command": "npx", "args": ["other"]}},
+ }
+ ),
+ encoding="utf-8",
+ )
+
+ assert install_claude_desktop(
+ server_file, "mcp", "demo", config_path=config_dir
+ )
+
+ saved = json.loads(config_file.read_text(encoding="utf-8"))
+ assert saved["preferences"] == {"theme": "dark"}
+ assert saved["mcpServers"]["other"]["command"] == "npx"
+ assert saved["mcpServers"]["demo"]["args"][-1] == f"{server_file.resolve()}:mcp"
+
+
class TestInstallConfigEncoding:
"""MCP config files are UTF-8 (RFC 8259), so installers must not decode them via the locale."""