Repository Public
1#!/usr/bin/env python32"""sync_mcp.py <nœud> <dossier-état> — enregistre sur le nœud les serveurs MCP du laptop (~/.claude.json + ~/.mcp.json), pour3Claude Code (`claude mcp add-json -s user`) et pour Codex (`codex mcp add`). Les serveurs dont le programme n'existe pas sur le4laptop, ou listés dans SKIP (services retirés), sont ignorés. N'agit que si la définition a changé ou manque sur le nœud."""5import hashlib, json, os, shlex, subprocess, sys6node, state = sys.argv[1], sys.argv[2]7SKIP = {"hfmarketdata", "akos"} # sites retirés (2026-10-02, 2026-11-09)8SSH = ["ssh", "-o", "BatchMode=yes", "-o", "ConnectTimeout=10", node]9home = os.path.expanduser("~")10defs = dict(json.load(open(home + "/.claude.json")).get("mcpServers", {}))11if os.path.exists(home + "/.mcp.json"): defs.update(json.load(open(home + "/.mcp.json")).get("mcpServers", {}))12want = {}13for k, v in defs.items():14 if k in SKIP or v.get("url"): continue15 paths = [a for a in [v.get("command", "")] + list(v.get("args", [])) if a.startswith(home + "/") and not a.startswith(home + "/Desktop/")]16 if any(not os.path.exists(p) for p in paths): continue # serveur déjà cassé sur le laptop17 node_path = lambda a: a.replace(home + "/Desktop/Cluster/apple/cluster-mcp", home + "/.maclustr-operator/cluster-mcp").replace(home + "/.local/bin/uv", "/opt/homebrew/bin/uv")18 want[k] = {"type": "stdio", "command": node_path(v["command"]), "args": [node_path(a) for a in v.get("args", [])], "env": dict(v.get("env", {}))}19r = subprocess.run(SSH + ["python3 -c \"import json,os;print(json.dumps(sorted(json.load(open(os.path.expanduser('~/.claude.json'))).get('mcpServers',{}))))\"; grep -o '^\\[mcp_servers\\.[A-Za-z0-9_-]*\\]' ~/.codex/config.toml 2>/dev/null"], capture_output=True, text=True)20if r.returncode not in (0, 1): print(" MCP : nœud injoignable"); sys.exit(1)21lines = r.stdout.strip().split("\n"); have_c = set(json.loads(lines[0])); have_x = {l[13:-1] for l in lines[1:] if l.startswith("[mcp_servers.")}22hf = os.path.join(state, "mcp.hashes"); old = json.load(open(hf)) if os.path.exists(hf) else {}23new = {k: hashlib.sha256(json.dumps(v, sort_keys=True).encode()).hexdigest() for k, v in want.items()}24script = ["export PATH=/opt/homebrew/bin:$PATH"]; done = []25for k, v in want.items():26 changed = old.get(k) != new[k]27 if changed or k not in have_c:28 script.append("claude mcp remove -s user %s >/dev/null 2>&1; claude mcp add-json -s user %s %s >/dev/null && echo claude:%s" % (k, k, shlex.quote(json.dumps(v)), k))29 if changed or k not in have_x:30 env = " ".join("--env %s" % shlex.quote("%s=%s" % kv) for kv in v["env"].items())31 script.append("codex mcp remove %s >/dev/null 2>&1; codex mcp add %s %s -- %s >/dev/null && echo codex:%s" % (k, k, env, " ".join(shlex.quote(a) for a in [v["command"]] + v["args"]), k))32if len(script) > 1:33 r = subprocess.run(SSH + ["bash -s"], input="\n".join(script), capture_output=True, text=True); done = r.stdout.split()34 json.dump(new, open(hf, "w"))35print(" MCP : %d serveurs voulus (%s) ; enregistrés ce tour : %s" % (len(want), ", ".join(sorted(want)), ", ".join(done) or "aucun (déjà à jour)"))36