Skip to content

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

Match Claude Code project keys by path on Windows (#6240)

nate nowack1605a5c80edf
diff --git a/fastmcp_slim/fastmcp/cli/discovery.py b/fastmcp_slim/fastmcp/cli/discovery.py
index 806262fee..fc188369f 100644
--- a/fastmcp_slim/fastmcp/cli/discovery.py
+++ b/fastmcp_slim/fastmcp/cli/discovery.py
@@ -192,11 +192,26 @@ def _scan_claude_code(start_dir: Path) -> list[DiscoveredServer]:
)
)
- # Project-scoped servers matching start_dir
- resolved_dir = str(start_dir.resolve())
+ # Project-scoped servers matching start_dir. Claude Code keys
+ # ``projects`` by a raw path string, and on Windows it writes forward
+ # slashes where ``Path.resolve()`` produces backslashes, so fall back to
+ # comparing paths when the exact key is missing. Only one entry is used so
+ # stale duplicate spellings of the same directory don't collide.
+ resolved_dir = start_dir.resolve()
projects = data.get("projects", {})
if isinstance(projects, dict):
- project_data = projects.get(resolved_dir, {})
+ project_data = projects.get(str(resolved_dir))
+ if project_data is None:
+ project_data = next(
+ (
+ candidate
+ for key, candidate in projects.items()
+ if isinstance(key, str)
+ and isinstance(candidate, dict)
+ and _is_same_project_dir(key, resolved_dir)
+ ),
+ None,
+ )
if isinstance(project_data, dict):
if project_servers := project_data.get("mcpServers"):
if isinstance(project_servers, dict):
@@ -217,6 +232,14 @@ def _scan_claude_code(start_dir: Path) -> list[DiscoveredServer]:
] + project_results
+def _is_same_project_dir(stored: str, resolved_start: Path) -> bool:
+ """Return whether a Claude Code ``projects`` key names *resolved_start*."""
+ try:
+ return Path(stored).expanduser().resolve() == resolved_start
+ except (OSError, RuntimeError, ValueError):
+ return False
+
+
def _scan_cursor_workspace(start_dir: Path) -> list[DiscoveredServer]:
"""Walk up from *start_dir* looking for ``.cursor/mcp.json``."""
current = start_dir.resolve()
diff --git a/tests/cli/test_discovery.py b/tests/cli/test_discovery.py
index 0a275a0bd..b49127bd7 100644
--- a/tests/cli/test_discovery.py
+++ b/tests/cli/test_discovery.py
@@ -322,6 +322,50 @@ class TestScanClaudeCode:
assert len(servers) == 1
assert servers[0].name == "api"
+ def test_project_servers_match_posix_path_key(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ):
+ """Claude Code on Windows stores project keys with forward slashes."""
+ monkeypatch.setattr("fastmcp.cli.discovery.Path.home", lambda: tmp_path)
+ project_dir = tmp_path / "my-project"
+ project_dir.mkdir()
+ config_path = tmp_path / ".claude.json"
+ _write_config(
+ config_path,
+ _claude_code_config(
+ project_path=project_dir.resolve().as_posix(),
+ project_servers={"api": {"url": "http://localhost:8000/mcp"}},
+ ),
+ )
+ servers = _scan_claude_code(project_dir)
+ assert len(servers) == 1
+ assert servers[0].name == "api"
+
+ def test_duplicate_project_key_spellings_use_one_entry(
+ self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+ ):
+ monkeypatch.setattr("fastmcp.cli.discovery.Path.home", lambda: tmp_path)
+ project_dir = tmp_path / "my-project"
+ project_dir.mkdir()
+ posix_dir = project_dir.resolve().as_posix()
+ config_path = tmp_path / ".claude.json"
+ _write_config(
+ config_path,
+ {
+ "projects": {
+ f"{posix_dir}/": {
+ "mcpServers": {"api": {"url": "http://localhost:8000/mcp"}}
+ },
+ posix_dir.replace("/my-project", "/./my-project"): {
+ "mcpServers": {"api": {"url": "http://localhost:9000/mcp"}}
+ },
+ }
+ },
+ )
+ servers = _scan_claude_code(project_dir)
+ assert len(servers) == 1
+ assert servers[0].name == "api"
+
def test_global_and_project_combined(
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
):