diff --git a/scripts/install_mcp.py b/scripts/install_mcp.py index efdc435c8..19485e28a 100644 --- a/scripts/install_mcp.py +++ b/scripts/install_mcp.py @@ -209,8 +209,18 @@ def _codex_config_path(config_root: Path) -> Path: ) / "config.toml" +def _codex_cli_path() -> Optional[str]: + """Resolve the usable Codex CLI, including the Desktop-provided path.""" + desktop_cli = os.environ.get("CODEX_CLI_PATH") + if desktop_cli: + candidate = Path(desktop_cli).expanduser() + if candidate.is_file(): + return str(candidate) + return shutil.which("codex") + + def _install_codex(config_root: Path) -> None: - codex = shutil.which("codex") + codex = _codex_cli_path() if codex is None: raise InstallError("Codex CLI is not on PATH.") _run_quiet([codex, "mcp", "remove", SERVER_NAME]) diff --git a/test/test_install_mcp_script.py b/test/test_install_mcp_script.py index f4bec85e0..f63a0e0d5 100644 --- a/test/test_install_mcp_script.py +++ b/test/test_install_mcp_script.py @@ -149,3 +149,28 @@ def test_codex_config_path_honors_codex_home(monkeypatch, tmp_path): monkeypatch.setenv("CODEX_HOME", str(custom_home)) assert installer._codex_config_path(tmp_path) == custom_home / "config.toml" + + +def test_codex_cli_path_prefers_desktop_environment(monkeypatch, tmp_path): + installer = _load_installer() + desktop_cli = tmp_path / "codex.exe" + desktop_cli.touch() + monkeypatch.setenv("CODEX_CLI_PATH", str(desktop_cli)) + monkeypatch.setattr( + installer.shutil, + "which", + lambda command: "C:/Program Files/WindowsApps/codex.exe", + ) + + assert installer._codex_cli_path() == str(desktop_cli) + + +def test_codex_cli_path_falls_back_to_path(monkeypatch, tmp_path): + installer = _load_installer() + monkeypatch.setenv( + "CODEX_CLI_PATH", + str(tmp_path / "missing-codex.exe"), + ) + monkeypatch.setattr(installer.shutil, "which", lambda command: "codex") + + assert installer._codex_cli_path() == "codex"