Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.
Python aicode-complexitycode-healthcode-intelligencecode-qualitydead-codedeveloper-toolsdocumentationgit-analyticsmcpopen-sourcepythonrefactoringstatic-analysistechnical-debt
fix(server): read the embedder key from the process env so CLI and MCP agree (#1810)
Sloecd8666459b44
diff --git a/packages/server/src/repowise/server/mcp_server/_server.py b/packages/server/src/repowise/server/mcp_server/_server.pyindex f25abd44c..2ca988694 100644--- a/packages/server/src/repowise/server/mcp_server/_server.py+++ b/packages/server/src/repowise/server/mcp_server/_server.py@@ -98,6 +98,16 @@ def _persisted_embedder_key(name: str) -> str | None:return Nonecanonical = env_vars[0]+ # The process environment is the highest-precedence source, matching the+ # CLI's resolver (cli/providers/keys.py): an exported key is an explicit+ # override. The server used to skip this tier entirely, so on a machine+ # with an exported credential it and the CLI disagreed about the same+ # repo in the same shell (issue #1711).+ for var in env_vars:+ value = os.environ.get(var)+ if value:+ return value+if _state._repo_path:try:# Reuse the reader get_answer already uses for the LLM credential,diff --git a/tests/unit/cli/test_embedder_key_resolution.py b/tests/unit/cli/test_embedder_key_resolution.pyindex 497b42859..c95406772 100644--- a/tests/unit/cli/test_embedder_key_resolution.py+++ b/tests/unit/cli/test_embedder_key_resolution.py@@ -313,6 +313,27 @@ def test_cli_and_mcp_resolve_the_same_key_for_the_same_repo(assert _server._persisted_embedder_key("openai") == expected+def test_env_export_wins_in_both_cli_and_server(+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch+) -> None:+ """Issue #1711: an exported key is an explicit override on BOTH sides.++ The server previously never consulted the process env, so on a machine with+ an exported credential it resolved a different key (or none) than the CLI,+ and the same repo in the same shell disagreed over MCP vs ``repowise+ search``. The server now reads the env tier first, matching the CLI.+ """+ from repowise.server.mcp_server import _server, _state++ _write_env(tmp_path, OPENAI_API_KEY="«redacted:sk-repo»")+ _write_global_config(embedder="openai", embedder_api_key="«redacted:sk-global»")+ monkeypatch.setenv("OPENAI_API_KEY", "sk-from-env")+ monkeypatch.setattr(_state, "_repo_path", str(tmp_path), raising=False)++ assert resolve_embedder_api_key("openai", tmp_path).key == "sk-from-env"+ assert _server._persisted_embedder_key("openai") == "sk-from-env"++def test_no_key_search_is_reported_for_a_keyless_backend(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: