-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·264 lines (236 loc) · 8.02 KB
/
Copy pathinstall.sh
File metadata and controls
executable file
·264 lines (236 loc) · 8.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env bash
# Install Agent Brain Claude Code plugin (hosted SSE + hooks).
#
# Usage:
# ./plugins/claude-code/install.sh \
# --url https://agent-memory.nighthawklabs.org/mcp \
# --agent-id claude-you \
# --api-key YOUR_KEY
#
# Remote (no clone needed):
# curl -fsSL https://raw.githubusercontent.com/realnighthawk/agent-plugins/main/plugins/claude-code/install.sh | bash -s -- --api-key ...
set -euo pipefail
PLUGIN_GITHUB_REPO="${AGENT_PLUGINS_GITHUB_REPO:-realnighthawk/agent-plugins}"
# Detect if running from a local checkout (dev workflow — enables go build fallback for mcp-call).
_script_path="${BASH_SOURCE[0]:-}"
LOCAL_CHECKOUT=""
if [[ -n "${_script_path}" ]]; then
_lib="$(cd "$(dirname "${_script_path}")/../.." 2>/dev/null && pwd)/scripts/lib/install-repo.sh"
if [[ -f "${_lib}" ]]; then
# shellcheck source=../../scripts/lib/install-repo.sh
. "${_lib}"
LOCAL_CHECKOUT="$(plugins_repo_root "${_script_path}")"
fi
fi
MCP_URL="https://agent-memory.nighthawklabs.org/mcp"
AGENT_ID=""
API_KEY="${NIGHTHAWK_API_KEY:-}"
JWT="${NIGHTHAWK_JWT:-}"
USER_TAG="${USER:-you}"
SCOPE="user"
MCP_CALL_VERSION="${MCP_CALL_VERSION:-latest}"
SKIP_PLUGIN_ADD=0
usage() {
sed -n '2,11p' "${BASH_SOURCE[0]:-$0}" | sed 's/^# \{0,1\}//'
exit "${1:-0}"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--url) MCP_URL="$2"; shift 2 ;;
--agent-id) AGENT_ID="$2"; shift 2 ;;
--api-key) API_KEY="$2"; shift 2 ;;
--jwt) JWT="$2"; shift 2 ;;
--user) USER_TAG="$2"; shift 2 ;;
--scope) SCOPE="$2"; shift 2 ;;
--version) MCP_CALL_VERSION="$2"; shift 2 ;;
--skip-plugin-add) SKIP_PLUGIN_ADD=1; shift ;;
-h|--help) usage 0 ;;
*) echo "unknown arg: $1" >&2; usage 1 ;;
esac
done
if [[ -z "$AGENT_ID" ]]; then
if [[ "$SCOPE" == "project" ]]; then
AGENT_ID="claude-${USER_TAG}-$(basename "$PWD")"
else
AGENT_ID="claude-${USER_TAG}"
fi
fi
if [[ -z "$API_KEY" && -z "$JWT" ]]; then
echo "Provide --api-key or --jwt (or set NIGHTHAWK_API_KEY / NIGHTHAWK_JWT)." >&2
exit 1
fi
ENV_DIR="${HOME}/.config/agent-brain"
ENV_FILE="${ENV_DIR}/claude.env"
mkdir -p "$ENV_DIR"
cat >"$ENV_FILE" <<EOF
# Agent Brain Claude Code — generated by install.sh
export NIGHTHAWK_MCP_URL=${MCP_URL}
export NIGHTHAWK_AGENT_ID=${AGENT_ID}
EOF
chmod 600 "$ENV_FILE"
if [[ -n "$JWT" ]]; then
echo "export NIGHTHAWK_JWT=${JWT}" >>"$ENV_FILE"
else
echo "export NIGHTHAWK_API_KEY=${API_KEY}" >>"$ENV_FILE"
fi
configure_claude_settings() {
local settings="${HOME}/.claude/settings.json"
mkdir -p "$(dirname "$settings")"
[[ -f "$settings" ]] || echo '{}' >"$settings"
python3 - "$settings" "$MCP_URL" "$AGENT_ID" "$API_KEY" "$JWT" <<'PY'
import json, sys
path, url, agent, api_key, jwt = sys.argv[1:6]
with open(path) as f:
data = json.load(f)
env = data.setdefault("env", {})
env["NIGHTHAWK_MCP_URL"] = url
env["NIGHTHAWK_AGENT_ID"] = agent
if jwt:
env["NIGHTHAWK_JWT"] = jwt
env.pop("NIGHTHAWK_API_KEY", None)
else:
env["NIGHTHAWK_API_KEY"] = api_key
env.pop("NIGHTHAWK_JWT", None)
# Agent-brain plugin hooks replace legacy memory-* user hooks.
hooks = data.get("hooks", {})
for event in ("SessionStart", "Stop"):
entries = hooks.get(event, [])
filtered = []
for entry in entries:
inner = []
for h in entry.get("hooks", []):
cmd = h.get("command", "")
if "memory-session-start.sh" in cmd or "memory-stop.sh" in cmd:
continue
inner.append(h)
if inner:
entry = dict(entry)
entry["hooks"] = inner
filtered.append(entry)
if filtered:
hooks[event] = filtered
elif event in hooks:
del hooks[event]
if hooks:
data["hooks"] = hooks
elif "hooks" in data:
del data["hooks"]
with open(path, "w") as f:
json.dump(data, f, indent=2)
f.write("\n")
PY
echo "Updated Claude settings: ${settings} (NIGHTHAWK_* env; removed legacy memory-* hooks)"
}
configure_claude_settings
# Return the installPath of the installed agent-brain plugin.
plugin_cache_dir() {
python3 - "${HOME}/.claude/plugins/installed_plugins.json" <<'PY'
import json, sys, os
path = sys.argv[1]
if not os.path.exists(path):
print(f"error: {path} not found — was the plugin installed?", file=sys.stderr)
sys.exit(1)
with open(path) as f:
d = json.load(f)
entries = d.get("plugins", {}).get("agent-brain@agent-plugins", [])
if not entries:
print("error: agent-brain@agent-plugins not in installed_plugins.json", file=sys.stderr)
sys.exit(1)
print(entries[0]["installPath"])
PY
}
register_claude_plugin() {
echo "Refreshing marketplace with Claude Code..."
# Remove first to force re-fetch of marketplace.json (picks up version bumps).
claude plugin marketplace remove agent-plugins --scope user 2>&1 || true
claude plugin marketplace add "${PLUGIN_GITHUB_REPO}" --scope user
echo "Installing agent-brain plugin..."
claude plugin install agent-brain@agent-plugins --scope user
}
install_mcp_call() {
local dest="$1"
mkdir -p "$(dirname "$dest")"
if [[ -n "${LOCAL_CHECKOUT}" && -f "${LOCAL_CHECKOUT}/scripts/fetch-mcp-call.sh" ]]; then
export MCP_CALL_VERSION
"${LOCAL_CHECKOUT}/scripts/fetch-mcp-call.sh" "$dest"
return
fi
local os_name arch asset repo ver url tmp
case "$(uname -s)" in
Darwin) os_name=darwin ;;
Linux) os_name=linux ;;
*) echo "unsupported OS: $(uname -s)" >&2; exit 1 ;;
esac
case "$(uname -m)" in
x86_64|amd64) arch=amd64 ;;
arm64|aarch64) arch=arm64 ;;
*) echo "unsupported arch: $(uname -m)" >&2; exit 1 ;;
esac
asset="mcp-call-${os_name}-${arch}"
repo="${NIGHTHAWK_MCP_CALL_REPO:-realnighthawk/agent-plugins}"
ver="${MCP_CALL_VERSION:-latest}"
if [[ -z "$ver" || "$ver" == "latest" ]]; then
url="https://github.com/${repo}/releases/latest/download/${asset}"
else
url="https://github.com/${repo}/releases/download/v${ver#v}/${asset}"
fi
tmp="${dest}.$$.tmp"
echo "Downloading mcp-call (${asset})..."
curl -fsSL "$url" -o "$tmp"
chmod +x "$tmp"
mv -f "$tmp" "$dest"
echo "Installed ${dest} from release"
}
if [[ -n "${LOCAL_CHECKOUT}" ]]; then
plugins_sync_memory_skills "${LOCAL_CHECKOUT}"
fi
if [[ "$SKIP_PLUGIN_ADD" -eq 0 ]]; then
register_claude_plugin
fi
# For local dev: use the checkout. For remote installs: use Claude Code's plugin cache.
if [[ -n "${LOCAL_CHECKOUT}" ]]; then
PLUGIN_DIR="${LOCAL_CHECKOUT}/plugins/claude-code"
else
PLUGIN_DIR="$(plugin_cache_dir)"
fi
echo "Installing mcp-call..."
install_mcp_call "${PLUGIN_DIR}/bin/mcp-call"
# Memory protocol skills (memory-write, memory-policy, etc.) are server-side — retrieved via MCP at session start.
write_mcp_config() {
local mcp_file="${PLUGIN_DIR}/.mcp.json"
if [[ -n "$JWT" ]]; then
jq -n \
--arg url "$MCP_URL" \
--arg jwt "$JWT" \
--arg agent_id "$AGENT_ID" \
'{"mcpServers":{"agent-brain":{"type":"http","url":$url,"headers":{"Authorization":("Bearer "+$jwt),"X-Agent-ID":$agent_id}}}}' \
> "$mcp_file"
else
jq -n \
--arg url "$MCP_URL" \
--arg api_key "$API_KEY" \
--arg agent_id "$AGENT_ID" \
'{"mcpServers":{"agent-brain":{"type":"http","url":$url,"headers":{"X-API-Key":$api_key,"X-Agent-ID":$agent_id}}}}' \
> "$mcp_file"
fi
chmod 600 "$mcp_file"
echo "Wrote MCP config (with credentials) to ${mcp_file}"
}
write_mcp_config
echo ""
echo "Installed Agent Brain for Claude Code"
echo " Plugin dir: ${PLUGIN_DIR}"
echo " MCP URL: ${MCP_URL}"
echo " Agent ID: ${AGENT_ID}"
echo " Env file: ${ENV_FILE}"
echo " Claude env: ~/.claude/settings.json (NIGHTHAWK_* injected for hooks)"
echo " MCP config: ${PLUGIN_DIR}/.mcp.json (credentials written directly)"
echo ""
echo "Optional: source ${ENV_FILE} in your shell profile for CLI mcp-call usage."
echo ""
echo "Server checklist:"
echo " 1. Memory Explorer → Settings: register agent \"${AGENT_ID}\" with write policy for user-stated facts"
echo " 2. Restart Claude Code or run /reload-plugins"
echo ""
echo "Smoke test:"
echo " source ${ENV_FILE} && ${PLUGIN_DIR}/bin/mcp-call memory_system_status '{}'"