Skip to content

Commit 9d97390

Browse files
authored
fix gemini mcp registration to write to correct settings.json (#155)
1 parent 0d4dc1b commit 9d97390

2 files changed

Lines changed: 33 additions & 23 deletions

File tree

src/ucode/mcp.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
import json
6+
import os
67
import shutil
78
import string
89
import subprocess
@@ -23,7 +24,7 @@
2324
from questionary.question import Question
2425
from questionary.styles import merge_styles_default
2526

26-
from ucode.agents import copilot, opencode
27+
from ucode.agents import copilot, gemini, opencode
2728
from ucode.config_io import restore_file
2829
from ucode.databricks import (
2930
build_mcp_service_url,
@@ -182,6 +183,13 @@ def remove_codex_mcp_server(name: str) -> bool:
182183
return True
183184

184185

186+
def _gemini_cli_env() -> dict[str, str]:
187+
# Pin GEMINI_CLI_HOME to the same directory the launcher.
188+
env = os.environ.copy()
189+
env["GEMINI_CLI_HOME"] = str(gemini.GEMINI_HOME_DIR)
190+
return env
191+
192+
185193
def add_gemini_mcp_server(name: str, url: str) -> None:
186194
try:
187195
subprocess.run(
@@ -202,6 +210,7 @@ def add_gemini_mcp_server(name: str, url: str) -> None:
202210
capture_output=True,
203211
text=True,
204212
timeout=30,
213+
env=_gemini_cli_env(),
205214
)
206215
except subprocess.CalledProcessError as exc:
207216
raise RuntimeError(f"Failed to add MCP server '{name}' via gemini CLI.") from exc
@@ -215,6 +224,7 @@ def remove_gemini_mcp_server(name: str) -> bool:
215224
capture_output=True,
216225
text=True,
217226
timeout=30,
227+
env=_gemini_cli_env(),
218228
)
219229
except subprocess.TimeoutExpired as exc:
220230
raise RuntimeError(f"Timed out removing MCP server '{name}' via gemini CLI.") from exc

tests/test_mcp.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,29 +94,29 @@ def fake_run(args, **kwargs):
9494

9595
mcp.add_gemini_mcp_server("github", f"{WS}/api/2.0/mcp/external/github")
9696

97-
assert calls == [
98-
{
99-
"args": [
100-
"gemini",
101-
"mcp",
102-
"add",
103-
"github",
104-
f"{WS}/api/2.0/mcp/external/github",
105-
"--type",
106-
"http",
107-
"--scope",
108-
"user",
109-
"--header",
110-
"Authorization: Bearer ${OAUTH_TOKEN}",
111-
],
112-
"kwargs": {
113-
"check": True,
114-
"capture_output": True,
115-
"text": True,
116-
"timeout": 30,
117-
},
118-
}
97+
assert len(calls) == 1
98+
call = calls[0]
99+
assert call["args"] == [
100+
"gemini",
101+
"mcp",
102+
"add",
103+
"github",
104+
f"{WS}/api/2.0/mcp/external/github",
105+
"--type",
106+
"http",
107+
"--scope",
108+
"user",
109+
"--header",
110+
"Authorization: Bearer ${OAUTH_TOKEN}",
119111
]
112+
kwargs = call["kwargs"]
113+
assert kwargs["check"] is True
114+
assert kwargs["capture_output"] is True
115+
assert kwargs["text"] is True
116+
assert kwargs["timeout"] == 30
117+
# GEMINI_CLI_HOME must point at the launcher's home so `gemini mcp
118+
# add` writes the same settings.json the ucode session reads from.
119+
assert kwargs["env"]["GEMINI_CLI_HOME"] == str(mcp.gemini.GEMINI_HOME_DIR)
120120

121121

122122
class TestRemoveClaudeMcpServer:

0 commit comments

Comments
 (0)