From a60bc92ec67e7e3c65002270d3c62d9863d0981b Mon Sep 17 00:00:00 2001 From: JeremyDev87 Date: Sat, 4 Apr 2026 02:20:28 +0900 Subject: [PATCH] fix(plugin): guard MCP auto-registration with binary check (#1237) Guard _ensure_mcp_json() with shutil.which('codingbuddy') so plugin-only users without the codingbuddy binary don't get broken MCP config. --- .../claude-code-plugin/hooks/session-start.py | 4 ++- .../hooks/test_session_start.py | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/claude-code-plugin/hooks/session-start.py b/packages/claude-code-plugin/hooks/session-start.py index 6c3de1fc..baa3b122 100644 --- a/packages/claude-code-plugin/hooks/session-start.py +++ b/packages/claude-code-plugin/hooks/session-start.py @@ -733,7 +733,9 @@ def main(): # Step 2.6: Ensure ~/.claude/mcp.json has codingbuddy entry (#1100) try: - _ensure_mcp_json(home / ".claude" / "mcp.json") + import shutil as _shutil + if _shutil.which("codingbuddy"): + _ensure_mcp_json(home / ".claude" / "mcp.json") except Exception: pass # Never block session start diff --git a/packages/claude-code-plugin/hooks/test_session_start.py b/packages/claude-code-plugin/hooks/test_session_start.py index 2c8392f5..4b437135 100644 --- a/packages/claude-code-plugin/hooks/test_session_start.py +++ b/packages/claude-code-plugin/hooks/test_session_start.py @@ -368,6 +368,34 @@ def test_handles_corrupted_mcp_json(self): data = json.loads(mcp_path.read_text()) assert "codingbuddy" in data["mcpServers"] + def test_skips_mcp_json_when_binary_not_installed(self): + """Test that mcp.json is NOT created when codingbuddy binary is not on PATH (#1237).""" + with tempfile.TemporaryDirectory() as tmpdir: + mcp_path = Path(tmpdir) / ".claude" / "mcp.json" + + with patch("shutil.which", return_value=None): + # Simulate the guarded call from session-start main flow + import shutil as _shutil + if _shutil.which("codingbuddy"): + session_hook._ensure_mcp_json(mcp_path) + + assert not mcp_path.exists() + + def test_creates_mcp_json_when_binary_installed(self): + """Test that mcp.json IS created when codingbuddy binary is on PATH (#1237).""" + with tempfile.TemporaryDirectory() as tmpdir: + mcp_path = Path(tmpdir) / ".claude" / "mcp.json" + + with patch("shutil.which", return_value="/usr/local/bin/codingbuddy"): + import shutil as _shutil + if _shutil.which("codingbuddy"): + session_hook._ensure_mcp_json(mcp_path) + + assert mcp_path.exists() + data = json.loads(mcp_path.read_text()) + assert "codingbuddy" in data["mcpServers"] + assert data["mcpServers"]["codingbuddy"]["command"] == "codingbuddy" + class TestHookLibCopy: """Tests for lib/ directory copying alongside hook file (#1102)."""