feat(daemon+mcp): daemon required for in-Claude-Code use; thin MCP shim
The architectural convergence v0.9.0 was building toward. CLI keeps working without a daemon (claudemesh send/peer/inbox/...), but the MCP push-pipe — which Claude Code uses for mid-turn channel emits, slash commands, and resources — now requires the daemon. There is no fallback. Daemon (additive): - /v1/skills (list) and /v1/skills/:name (get) IPC endpoints, so the MCP shim can surface mesh skills without holding its own broker WS. - listSkills() / getSkill() on DaemonBrokerClient. - SSE 'message' event now carries plaintext body, sender_member_pubkey, priority, and subtype — full payload the MCP shim needs to render a channel notification. MCP server: 979 → 469 LoC (470 of the remaining 469 is the unrelated mesh-service proxy mode; the push-pipe path is ~200 LoC including boilerplate). - Probes ~/.claudemesh/daemon/daemon.sock at boot. Bails loudly with actionable instructions if missing. - Subscribes to /v1/events SSE and translates each event into a notifications/claude/channel emit. - Fetches mesh skills from the daemon for ListPrompts/GetPrompt and ListResources/ReadResource. ListTools returns []; the CLI is the API. - No broker WS, no decryption, no reconnect logic. Daemon owns all of it. claudemesh install: auto-installs and starts the daemon service for the user's primary mesh (launchd / systemd-user). Pass --no-service to skip. claudemesh launch: probes the daemon socket; if absent, spawns 'claudemesh daemon up --mesh <slug>' detached and waits up to 10s for the socket. Surfaces a clear warning on timeout but doesn't block — Claude Code's MCP shim will print the same error if the daemon really isn't there. Bundle: dist/entrypoints/mcp.js drops from 154KB → 104KB (gzipped 34KB → 19KB). Test: MCP boots cleanly via stdio, declares correct capabilities, talks JSON-RPC; daemon /v1/skills returns the empty list as expected on a mesh with no skills. Released as 1.24.0 on npm. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -44,6 +44,55 @@ export interface LaunchFlags {
|
||||
|
||||
// --- Interactive mesh picker ---
|
||||
|
||||
/**
|
||||
* Ensure the per-user daemon is running before we hand off to Claude Code.
|
||||
*
|
||||
* As of 1.24.0 the daemon owns the broker WS and feeds the MCP push-pipe
|
||||
* over IPC SSE. If the socket is absent when Claude boots its MCP shim,
|
||||
* the shim bails (no fallback). So we probe for the socket here and, if
|
||||
* missing, spawn `claudemesh daemon up --mesh <slug>` in the background,
|
||||
* waiting briefly for the socket to appear.
|
||||
*
|
||||
* Best-effort: if the daemon spawn fails, we surface the error and let
|
||||
* the launch proceed — Claude Code will print the same "daemon not
|
||||
* running" message and the user can fix it manually.
|
||||
*/
|
||||
async function ensureDaemonRunning(meshSlug: string, quiet: boolean): Promise<void> {
|
||||
const { DAEMON_PATHS } = await import("~/daemon/paths.js");
|
||||
if (existsSync(DAEMON_PATHS.SOCK_FILE)) return;
|
||||
|
||||
if (!quiet) render.info("starting claudemesh daemon…");
|
||||
const { spawn } = await import("node:child_process");
|
||||
const argv0 = process.argv[1] ?? "claudemesh";
|
||||
let binary = argv0;
|
||||
if (/\.ts$/.test(binary) || /node_modules|src\/entrypoints/.test(binary)) {
|
||||
try {
|
||||
const { execSync } = await import("node:child_process");
|
||||
binary = execSync("which claudemesh", { encoding: "utf8" }).trim();
|
||||
} catch { binary = "claudemesh"; }
|
||||
}
|
||||
const child = spawn(binary, ["daemon", "up", "--mesh", meshSlug], {
|
||||
detached: true,
|
||||
stdio: "ignore",
|
||||
});
|
||||
child.unref();
|
||||
|
||||
// Wait for the socket to appear. 10 s budget — covers cold node start +
|
||||
// broker hello round-trip on slow links.
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < 10_000) {
|
||||
if (existsSync(DAEMON_PATHS.SOCK_FILE)) {
|
||||
if (!quiet) render.ok("daemon ready");
|
||||
return;
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 200));
|
||||
}
|
||||
render.warn(
|
||||
"daemon failed to start within 10s",
|
||||
"Run `claudemesh daemon up --mesh " + meshSlug + "` manually, then re-launch.",
|
||||
);
|
||||
}
|
||||
|
||||
async function pickMesh(meshes: JoinedMesh[]): Promise<JoinedMesh> {
|
||||
if (meshes.length === 1) return meshes[0]!;
|
||||
|
||||
@@ -550,6 +599,12 @@ export async function runLaunch(flags: LaunchFlags, rawArgs: string[]): Promise<
|
||||
}
|
||||
} catch { /* best effort */ }
|
||||
|
||||
// Ensure the daemon is running before we spawn Claude. The MCP shim
|
||||
// (loaded by --dangerously-load-development-channels server:claudemesh)
|
||||
// requires the daemon's UDS to be reachable at boot — if it isn't,
|
||||
// channel push, slash commands, and resources fail.
|
||||
await ensureDaemonRunning(mesh.slug, args.quiet);
|
||||
|
||||
// Clean up stale mesh MCP entries from crashed sessions
|
||||
try {
|
||||
const claudeConfigPath = join(homedir(), ".claude.json");
|
||||
|
||||
Reference in New Issue
Block a user