Repository navigation
Expand file tree
/
Copy pathinit-mcp.sh
More file actions
executable file
·319 lines (279 loc) · 7.97 KB
/
Copy pathinit-mcp.sh
File metadata and controls
executable file
·319 lines (279 loc) · 7.97 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env bash
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$ROOT_DIR"
SERVER_NAME="sisyphus"
ENABLE_CODEX=1
ENABLE_CLAUDE=1
DRY_RUN=0
MCP_DEBUG_LOG="/tmp/sisyphus-mcp-debug.log"
usage() {
cat <<'EOF'
Initialize Sisyphus MCP registrations for Codex and Claude.
This script:
- registers a global stdio MCP server in Codex
- writes a project-local .mcp.json for Claude Code
Usage:
./init-mcp.sh [options]
Options:
--repo PATH Repository root that Sisyphus should manage. Default: this repo.
--name NAME MCP server name to register. Default: sisyphus
--codex-only Register only in Codex
--claude-only Register only in Claude
--dry-run Print commands without executing them
-h, --help Show this help
Environment overrides:
CODEX_BIN Codex CLI path or command name
EOF
}
require_option_value() {
local option_name="$1"
local option_value="${2:-}"
if [[ -z "$option_value" || "$option_value" == -* ]]; then
echo "Missing value for $option_name" >&2
exit 1
fi
}
print_command() {
local quoted=()
local arg
for arg in "$@"; do
quoted+=("$(printf '%q' "$arg")")
done
printf '%s\n' "${quoted[*]}"
}
repo_pythonpath() {
local inherited_pythonpath="${PYTHONPATH:-}"
local repo_src="$REPO_ROOT/src"
if [[ -n "$inherited_pythonpath" ]]; then
printf '%s:%s' "$repo_src" "$inherited_pythonpath"
return 0
fi
printf '%s' "$repo_src"
}
repo_bootstrap_script() {
printf '%s' "$REPO_ROOT/scripts/run_sisyphus_mcp_server.py"
}
resolve_repo_root() {
if [[ ! -d "$REPO_ROOT" ]]; then
echo "Repository root does not exist: $REPO_ROOT" >&2
exit 1
fi
REPO_ROOT="$(cd "$REPO_ROOT" && pwd)"
}
resolve_python_bin() {
local candidate="$ROOT_DIR/.venv/bin/python"
if [[ ! -x "$candidate" ]]; then
echo "Missing Python at $candidate. Run 'uv sync' from $ROOT_DIR first." >&2
exit 1
fi
printf '%s' "$candidate"
}
validate_python_bin() {
local python_bin="$1"
local bootstrap_script
bootstrap_script="$(repo_bootstrap_script)"
SISYPHUS_REPO_ROOT="$REPO_ROOT" SISYPHUS_MCP_BOOTSTRAP_INSPECT=1 "$python_bin" "$bootstrap_script" >/dev/null 2>&1
}
repair_repo_python() {
local uv_bin
local backup_dir
uv_bin="${UV_BIN:-/opt/homebrew/bin/uv}"
if [[ ! -x "$uv_bin" ]] && ! command -v uv >/dev/null 2>&1; then
echo "Unable to repair MCP runtime automatically: uv is not available." >&2
exit 1
fi
if [[ ! -x "$uv_bin" ]]; then
uv_bin="$(command -v uv)"
fi
echo "Detected an unhealthy repo virtualenv; attempting repair via uv sync..." >&2
if UV_PROJECT_ENVIRONMENT="$ROOT_DIR/.venv" "$uv_bin" sync; then
return 0
fi
backup_dir="$ROOT_DIR/.venv.broken.$(date +%Y%m%d%H%M%S)"
echo "In-place uv sync failed; moving broken virtualenv to $backup_dir and rebuilding..." >&2
mv "$ROOT_DIR/.venv" "$backup_dir"
if UV_PROJECT_ENVIRONMENT="$ROOT_DIR/.venv" "$uv_bin" sync; then
echo "Rebuilt repo virtualenv; previous environment preserved at $backup_dir" >&2
return 0
fi
echo "Failed to rebuild repo virtualenv; previous environment is preserved at $backup_dir" >&2
exit 1
}
ensure_python_bin() {
local python_bin
python_bin="$(resolve_python_bin)"
if validate_python_bin "$python_bin"; then
printf '%s' "$python_bin"
return 0
fi
repair_repo_python
python_bin="$(resolve_python_bin)"
if validate_python_bin "$python_bin"; then
printf '%s' "$python_bin"
return 0
fi
echo "Python interpreter is still unable to execute the Sisyphus MCP bootstrap: $python_bin" >&2
exit 1
}
resolve_codex_bin() {
if [[ -n "${CODEX_BIN:-}" ]]; then
if command -v "$CODEX_BIN" >/dev/null 2>&1; then
command -v "$CODEX_BIN"
return 0
fi
if [[ -x "$CODEX_BIN" ]]; then
printf '%s' "$CODEX_BIN"
return 0
fi
echo "CODEX_BIN is set but not executable: $CODEX_BIN" >&2
return 1
fi
command -v codex >/dev/null 2>&1 || return 1
command -v codex
}
register_codex() {
local codex_bin="$1"
local python_bin="$2"
local pythonpath_value
local bootstrap_script
pythonpath_value="$(repo_pythonpath)"
bootstrap_script="$(repo_bootstrap_script)"
if [[ "$DRY_RUN" -eq 1 ]]; then
print_command "$codex_bin" mcp remove "$SERVER_NAME"
print_command \
"$codex_bin" mcp add "$SERVER_NAME" \
--env "SISYPHUS_REPO_ROOT=$REPO_ROOT" \
--env "SISYPHUS_MCP_DEBUG_LOG=$MCP_DEBUG_LOG" \
--env "PYTHONPATH=$pythonpath_value" \
-- \
"$python_bin" "$bootstrap_script"
return 0
fi
"$codex_bin" mcp remove "$SERVER_NAME" >/dev/null 2>&1 || true
"$codex_bin" mcp add "$SERVER_NAME" \
--env "SISYPHUS_REPO_ROOT=$REPO_ROOT" \
--env "SISYPHUS_MCP_DEBUG_LOG=$MCP_DEBUG_LOG" \
--env "PYTHONPATH=$pythonpath_value" \
-- \
"$python_bin" "$bootstrap_script"
}
print_claude_project_config() {
local python_bin="$2"
PYTHONPATH="$(repo_pythonpath)" "$python_bin" - "$1" "$SERVER_NAME" "$python_bin" "$REPO_ROOT" "$MCP_DEBUG_LOG" "${PYTHONPATH:-}" <<'PY'
import json
import pathlib
import sys
from sisyphus.mcp_launcher import build_stdio_server_config
path = pathlib.Path(sys.argv[1])
name = sys.argv[2]
command = sys.argv[3]
repo_root = sys.argv[4]
debug_log = sys.argv[5]
inherited_pythonpath = sys.argv[6] or None
server_config = build_stdio_server_config(
command,
repo_root,
debug_log,
inherited_pythonpath=inherited_pythonpath,
)
data = {}
if path.exists():
raw = path.read_text(encoding="utf-8")
if raw.strip():
data = json.loads(raw)
if not isinstance(data, dict):
raise SystemExit(f"{path} must contain a JSON object")
mcp_servers = data.get("mcpServers")
if mcp_servers is None:
mcp_servers = {}
elif not isinstance(mcp_servers, dict):
raise SystemExit(f"{path} has a non-object mcpServers value")
mcp_servers[name] = server_config
data["mcpServers"] = mcp_servers
print(json.dumps(data, indent=2))
PY
}
write_claude_project_config() {
local python_bin="$1"
local mcp_config_path="$REPO_ROOT/.mcp.json"
local config_dir tmp_config_path
config_dir="$(dirname "$mcp_config_path")"
if [[ "$DRY_RUN" -eq 1 ]]; then
printf 'Would update %s with:\n' "$mcp_config_path"
print_claude_project_config "$mcp_config_path" "$python_bin"
return 0
fi
tmp_config_path="$(mktemp "$config_dir/.mcp.json.XXXXXX")"
print_claude_project_config "$mcp_config_path" "$python_bin" >"$tmp_config_path"
mv "$tmp_config_path" "$mcp_config_path"
echo "Wrote Claude MCP config: $mcp_config_path"
}
exclude_claude_project_config() {
local exclude_path="$REPO_ROOT/.git/info/exclude"
if [[ ! -f "$exclude_path" ]]; then
return 0
fi
if rg -Fx -- ".mcp.json" "$exclude_path" >/dev/null 2>&1; then
return 0
fi
if [[ "$DRY_RUN" -eq 1 ]]; then
printf 'Would append .mcp.json to %s\n' "$exclude_path"
return 0
fi
printf '\n.mcp.json\n' >>"$exclude_path"
echo "Updated git exclude: $exclude_path"
}
while [[ $# -gt 0 ]]; do
case "$1" in
--repo)
require_option_value "$1" "${2:-}"
REPO_ROOT="$(cd "$2" && pwd)"
shift 2
;;
--name)
require_option_value "$1" "${2:-}"
SERVER_NAME="$2"
shift 2
;;
--codex-only)
ENABLE_CODEX=1
ENABLE_CLAUDE=0
shift
;;
--claude-only)
ENABLE_CODEX=0
ENABLE_CLAUDE=1
shift
;;
--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Unknown option: $1" >&2
usage >&2
exit 1
;;
esac
done
resolve_repo_root
PYTHON_BIN="$(ensure_python_bin)"
if [[ "$ENABLE_CODEX" -eq 1 ]]; then
if CODEX_BIN_RESOLVED="$(resolve_codex_bin)"; then
echo "Registering Codex MCP: $SERVER_NAME"
register_codex "$CODEX_BIN_RESOLVED" "$PYTHON_BIN"
else
echo "Skipping Codex registration: codex CLI not found" >&2
fi
fi
if [[ "$ENABLE_CLAUDE" -eq 1 ]]; then
echo "Writing Claude MCP config: $SERVER_NAME"
write_claude_project_config "$PYTHON_BIN"
exclude_claude_project_config
fi
echo "Done."