@xmarts/genius-setup 1.8.0 → 1.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -123,16 +123,17 @@ function runSetup(token) {
123
123
  if (secret) { try { fs.chmodSync(p, 0o600); } catch (_) {} }
124
124
  }
125
125
 
126
- // 1) ~/.genius/config.json + hook
126
+ // 1) ~/.genius/config.json + hook. MERGE into any existing config so state we
127
+ // track across runs (e.g. managed_mcp_keys for MCP de-provisioning) survives.
127
128
  ensureDir(HOOKS_DIR);
128
- const cfg = {
129
- base_url: BASE,
130
- token: token,
131
- capture_endpoint: BASE + '/xma/genius/v1/capture/transcript',
132
- inject_endpoint: BASE + '/xma/genius/v1/inject',
133
- };
129
+ const cfgPath0 = path.join(GENIUS_DIR, 'config.json');
130
+ const cfg = readJson(cfgPath0);
131
+ cfg.base_url = BASE;
132
+ cfg.token = token;
133
+ cfg.capture_endpoint = BASE + '/xma/genius/v1/capture/transcript';
134
+ cfg.inject_endpoint = BASE + '/xma/genius/v1/inject';
134
135
  if (CLIENT) cfg.target_client = CLIENT;
135
- writeJson(path.join(GENIUS_DIR, 'config.json'), cfg, true); // secret: 0600
136
+ writeJson(cfgPath0, cfg, true); // secret: 0600
136
137
 
137
138
  // Copy the hooks shipped alongside this script (capture + per-turn inject).
138
139
  function copyHook(fname) {
@@ -211,6 +212,97 @@ function runSetup(token) {
211
212
  log('WARN: could not write CLAUDE.md protocol (' + e.message + ')');
212
213
  }
213
214
 
215
+ // 5) Fan out the consultant's OTHER MCP servers (asana, clickup, google, ...).
216
+ // Their non-secret shape (manifest) + per-device secret VALUES come from the
217
+ // Bearer-authed /xma/genius/v1/mcp/secrets endpoint. We write Claude Code
218
+ // always and Claude Desktop only if it is installed. Fail-open: a fetch
219
+ // error never breaks onboarding. Re-runs (self-update / epoch bump) converge.
220
+ function desktopConfigPath() {
221
+ try {
222
+ if (process.platform === 'darwin')
223
+ return path.join(HOME, 'Library', 'Application Support', 'Claude', 'claude_desktop_config.json');
224
+ if (process.platform === 'win32')
225
+ return path.join(process.env.APPDATA || path.join(HOME, 'AppData', 'Roaming'), 'Claude', 'claude_desktop_config.json');
226
+ return path.join(HOME, '.config', 'Claude', 'claude_desktop_config.json');
227
+ } catch (_) { return ''; }
228
+ }
229
+ function mcpEntry(srv, secrets, host) {
230
+ // One mcpServers entry, or null if a required secret is missing (never write a
231
+ // half-broken server) or it is not portable to this host.
232
+ const env = {};
233
+ let ok = true;
234
+ (srv.env_keys || []).forEach((k) => { if (secrets[k]) env[k] = secrets[k]; else ok = false; });
235
+ if (!ok) return null;
236
+ const isHttp = srv.transport === 'http' || srv.transport === 'sse';
237
+ if (host === 'desktop') {
238
+ if (srv.host_targets !== 'both' || srv.desktop_strategy === 'skip') return null;
239
+ if (isHttp) return { command: 'npx', args: ['-y', 'mcp-remote', srv.url] }; // stdio-only Desktop
240
+ const e = { command: srv.command, args: srv.args || [] };
241
+ if (Object.keys(env).length) e.env = env;
242
+ return e;
243
+ }
244
+ if (isHttp) return { type: srv.transport, url: srv.url };
245
+ const e = { type: 'stdio', command: srv.command, args: srv.args || [] };
246
+ if (Object.keys(env).length) e.env = env;
247
+ return e;
248
+ }
249
+ function writeHostMcps(cfgPath, manifest, secrets, host, prevKeys) {
250
+ let obj = readJson(cfgPath);
251
+ if (!obj || typeof obj !== 'object') obj = {};
252
+ backup(cfgPath);
253
+ obj.mcpServers = obj.mcpServers || {};
254
+ // De-provision: drop servers WE previously managed that are no longer granted.
255
+ (prevKeys || []).forEach((k) => {
256
+ if (obj.mcpServers[k] && !manifest.find((s) => s.key === k)) delete obj.mcpServers[k];
257
+ });
258
+ const nowKeys = [];
259
+ manifest.forEach((srv) => {
260
+ const entry = mcpEntry(srv, secrets, host);
261
+ if (entry) { obj.mcpServers[srv.key] = entry; nowKeys.push(srv.key); }
262
+ else if (obj.mcpServers[srv.key] && (prevKeys || []).includes(srv.key)) {
263
+ delete obj.mcpServers[srv.key]; // lost its secret / portability -> remove
264
+ }
265
+ });
266
+ writeJson(cfgPath, obj, true); // may carry env secrets -> 0600
267
+ return nowKeys;
268
+ }
269
+ function provisionMcps(token) {
270
+ let url;
271
+ try { url = new URL(BASE + '/xma/genius/v1/mcp/secrets'); } catch (_) { return; }
272
+ const mod = url.protocol === 'http:' ? require('http') : require('https');
273
+ const payload = JSON.stringify({ jsonrpc: '2.0', method: 'call', id: 1, params: {} });
274
+ const req = mod.request({
275
+ hostname: url.hostname, port: url.port || (url.protocol === 'http:' ? 80 : 443),
276
+ path: url.pathname, method: 'POST',
277
+ headers: {
278
+ 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(payload),
279
+ 'Authorization': 'Bearer ' + token, 'User-Agent': 'XmartsGenius-Setup/1.6.0',
280
+ },
281
+ timeout: 15000,
282
+ }, (res) => {
283
+ let data = ''; res.on('data', (c) => { data += c; });
284
+ res.on('end', () => {
285
+ try {
286
+ const parsed = JSON.parse(data); const r = parsed.result || parsed;
287
+ if (!r || r.status !== 'ok' || !Array.isArray(r.manifest)) return;
288
+ const secrets = r.secrets || {};
289
+ const cfgPath = path.join(GENIUS_DIR, 'config.json');
290
+ const prev = (readJson(cfgPath).managed_mcp_keys) || [];
291
+ const managed = writeHostMcps(path.join(HOME, '.claude.json'), r.manifest, secrets, 'code', prev);
292
+ const dp = desktopConfigPath();
293
+ if (dp && (fs.existsSync(dp) || fs.existsSync(path.dirname(dp)))) {
294
+ try { writeHostMcps(dp, r.manifest, secrets, 'desktop', prev); } catch (_) {}
295
+ }
296
+ try { const c = readJson(cfgPath); c.managed_mcp_keys = managed; writeJson(cfgPath, c, true); } catch (_) {}
297
+ if (managed.length) log(' - MCP fan-out : ' + managed.join(', '));
298
+ } catch (_) {}
299
+ });
300
+ });
301
+ req.on('error', () => {}); req.on('timeout', () => { try { req.destroy(); } catch (_) {} });
302
+ req.write(payload); req.end();
303
+ }
304
+ provisionMcps(token);
305
+
214
306
  log('');
215
307
  log(' Xmarts Genius is set up.');
216
308
  log(' - MCP server : beesmart_genius -> ' + BASE + '/mcp/consultant');
@@ -25,6 +25,7 @@ TIMEOUT = 3.5
25
25
  SELF_UPDATE_TTL = 86400 # at most once/day
26
26
  SELF_UPDATE_STAMP = os.path.join(HOME, ".genius", ".last_selfupdate")
27
27
  FORCE_STAMP = os.path.join(HOME, ".genius", ".last_forced_epoch")
28
+ MCP_EPOCH_STAMP = os.path.join(HOME, ".genius", ".last_mcp_epoch")
28
29
 
29
30
  SESSION_PRIMER = (
30
31
  "[Xmarts Genius — Brain connected]\n"
@@ -77,9 +78,10 @@ def _inject(cfg, prompt):
77
78
  result = data.get("result", data) or {}
78
79
  return ((result.get("context", "") or ""),
79
80
  result.get("hook_force_epoch"),
80
- result.get("hook_pin_version"))
81
+ result.get("hook_pin_version"),
82
+ result.get("mcp_manifest_epoch"))
81
83
  except Exception:
82
- return "", None, None
84
+ return "", None, None, None
83
85
 
84
86
 
85
87
  # M6 (Phase-0 integrity gate): NEVER resolve `@latest`. An unsigned `npx @latest`
@@ -101,7 +103,7 @@ def _pkg_spec(pin_version):
101
103
  return "@xmarts/genius-setup@%s" % PINNED_FALLBACK
102
104
 
103
105
 
104
- def _maybe_self_update(cfg, force_epoch=None, pin_version=None):
106
+ def _maybe_self_update(cfg, force_epoch=None, pin_version=None, mcp_epoch=None):
105
107
  """Keep the consultant on the SERVER-PINNED Genius package with ZERO action: at most
106
108
  once/day, silently re-run the installer in the background. New hooks, MCP
107
109
  tools, settings and the CLAUDE.md protocol then flow automatically — the
@@ -129,6 +131,18 @@ def _maybe_self_update(cfg, force_epoch=None, pin_version=None):
129
131
  forced = fe > last
130
132
  except Exception:
131
133
  forced = False
134
+ # MCP fan-out reconvergence: a newer manifest epoch forces an immediate
135
+ # re-run (re-provisions ~/.claude.json + Desktop), bypassing the daily TTL.
136
+ if not forced and mcp_epoch:
137
+ try:
138
+ me = int(mcp_epoch)
139
+ lastm = 0
140
+ if os.path.exists(MCP_EPOCH_STAMP):
141
+ with open(MCP_EPOCH_STAMP) as fh:
142
+ lastm = int((fh.read() or "0").strip() or 0)
143
+ forced = me > lastm
144
+ except Exception:
145
+ pass
132
146
  if not forced and os.path.exists(SELF_UPDATE_STAMP) and (
133
147
  time.time() - os.path.getmtime(SELF_UPDATE_STAMP)) < SELF_UPDATE_TTL:
134
148
  return
@@ -140,6 +154,9 @@ def _maybe_self_update(cfg, force_epoch=None, pin_version=None):
140
154
  if force_epoch:
141
155
  with open(FORCE_STAMP, "w") as fh:
142
156
  fh.write(str(int(force_epoch)))
157
+ if mcp_epoch:
158
+ with open(MCP_EPOCH_STAMP, "w") as fh:
159
+ fh.write(str(int(mcp_epoch)))
143
160
  except Exception:
144
161
  return
145
162
  env = dict(os.environ)
@@ -174,11 +191,12 @@ def main():
174
191
  _maybe_self_update(cfg) # TTL-only on priming
175
192
  _emit("SessionStart", SESSION_PRIMER)
176
193
  else:
177
- context, force_epoch, pin_version = _inject(cfg, event.get("prompt") or "")
194
+ context, force_epoch, pin_version, mcp_epoch = _inject(cfg, event.get("prompt") or "")
178
195
  # The inject call already round-tripped the server, so honour any
179
196
  # admin-pushed force-update epoch here (bypasses the daily TTL) and install
180
- # the exact server-pinned version (never @latest).
181
- _maybe_self_update(cfg, force_epoch, pin_version)
197
+ # the exact server-pinned version (never @latest). A newer MCP manifest
198
+ # epoch likewise forces an immediate re-provision of the fan-out servers.
199
+ _maybe_self_update(cfg, force_epoch, pin_version, mcp_epoch)
182
200
  _emit("UserPromptSubmit", context)
183
201
 
184
202
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@xmarts/genius-setup",
3
- "version": "1.8.0",
4
- "description": "One-command, self-updating onboarding for Xmarts Genius (Brain MCP + per-turn injection + automatic session/time capture + plan/committee→Brain enforcement + CLAUDE.md protocol). Secret-free onboarding via single-use setup-token exchange (no Bearer in the GET-able bootstrap). Installs once; self-updates daily so new tools/skills/features arrive with zero action.",
3
+ "version": "1.9.0",
4
+ "description": "One-command, self-updating onboarding for Xmarts Genius (Brain MCP + per-turn injection + automatic session/time capture + plan/committee→Brain enforcement + CLAUDE.md protocol + MCP fan-out: provisions the consultant's granted MCP servers into Claude Code & Claude Desktop). Secret-free onboarding via single-use setup-token exchange. Installs once; self-updates daily so new tools/skills/features arrive with zero action.",
5
5
  "bin": {
6
6
  "genius-setup": "bin/genius-setup.js"
7
7
  },