fix: detect claude CLI in common install paths

command -v misses claude when not in PATH (e.g. brew install without
shell init). Now also checks /opt/homebrew/bin/claude and
/usr/local/bin/claude. Uses $CLAUDE_BIN variable for all invocations.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-15 13:17:14 +10:30
parent c303b534fc
commit 185c4d0681

18
init.sh
View File

@@ -57,8 +57,16 @@ if [ "$MODE" = "sse" ]; then
fi
# ── Detect claude CLI ────────────────────────────────────────────────
HAS_CLAUDE=false
CLAUDE_BIN=""
if command -v claude &>/dev/null; then
CLAUDE_BIN="claude"
elif [ -x "/opt/homebrew/bin/claude" ]; then
CLAUDE_BIN="/opt/homebrew/bin/claude"
elif [ -x "/usr/local/bin/claude" ]; then
CLAUDE_BIN="/usr/local/bin/claude"
fi
HAS_CLAUDE=false
if [ -n "$CLAUDE_BIN" ]; then
HAS_CLAUDE=true
fi
@@ -111,8 +119,8 @@ if [ "$MODE" = "remote" ]; then
if $HAS_CLAUDE; then
# 新版 Claude Code: 用 claude mcp add 注册transport=http即 Streamable HTTP
# 先移除旧配置(忽略错误)
claude mcp remove -s user ai-proj 2>/dev/null || true
claude mcp add ai-proj "$SSE_URL" -t http -s user -H "X-API-Key: $TOKEN" 2>&1
"$CLAUDE_BIN" mcp remove -s user ai-proj 2>/dev/null || true
"$CLAUDE_BIN" mcp add ai-proj "$SSE_URL" -t http -s user -H "X-API-Key: $TOKEN" 2>&1
echo "✅ 已通过 claude mcp add 注册 (Streamable HTTP)"
else
# 旧版或无 CLI: 回退写 .mcp.json使用 http transport
@@ -150,8 +158,8 @@ elif [ "$MODE" = "stdio" ]; then
BRIDGE_ENTRY="$MCP_BRIDGE_DIR/dist/index.js"
if $HAS_CLAUDE; then
claude mcp remove -s user ai-proj 2>/dev/null || true
claude mcp add ai-proj -s user -e "NODE_ENV=production" -e "TASK_API_BASE=$API_BASE" -e "TASK_API_TOKEN=$TOKEN" -e "MCP_SERVER_NAME=ai-proj" -- node "$BRIDGE_ENTRY" 2>&1
"$CLAUDE_BIN" mcp remove -s user ai-proj 2>/dev/null || true
"$CLAUDE_BIN" mcp add ai-proj -s user -e "NODE_ENV=production" -e "TASK_API_BASE=$API_BASE" -e "TASK_API_TOKEN=$TOKEN" -e "MCP_SERVER_NAME=ai-proj" -- node "$BRIDGE_ENTRY" 2>&1
echo "✅ 已通过 claude mcp add 注册 (stdio)"
else
mkdir -p "$(dirname "$MCP_CONFIG")"