MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / runProxyWithLocalHandshake

Method runProxyWithLocalHandshake

src/mcp/index.ts:463–498  ·  view source on GitHub ↗

* Proxy mode (the common case). Serve the MCP handshake LOCALLY for instant * tool registration, forwarding tool calls to the shared daemon — which is * connected in the background (probed, then spawned + polled if absent) so the * handshake never waits ~600ms on it. Runs until the host dis

(root: string)

Source from the content-addressed store, hash-verified

461 * never wedges a session.
462 */
463 private async runProxyWithLocalHandshake(root: string): Promise<void> {
464 // The daemon may relocate its socket past an in-project filesystem that can't
465 // host one (ExFAT/FAT volumes, WSL2 DrvFs; #997) to the deterministic tmpdir
466 // fallback. We don't read the bound path from the lockfile — both sides walk
467 // the SAME ordered candidate list, so we converge on whichever the daemon
468 // bound with zero coordination. The in-project candidate is tried first, so a
469 // normal repo pays nothing extra (it connects on the very first probe).
470 const candidates = getDaemonSocketCandidates(root);
471 const connectAnyCandidate = async (): Promise<Awaited<ReturnType<typeof connectWithHello>>> => {
472 for (const candidate of candidates) {
473 const s = await connectWithHello(candidate);
474 // A wrong-version daemon IS up — definitive; propagate so the caller
475 // serves in-process instead of spawning + polling for 6s. Don't keep
476 // probing fallbacks past it.
477 if (s === 'version-mismatch') return s;
478 if (s) return s;
479 }
480 return null;
481 };
482 const getDaemonSocket = async () => {
483 // Fast path: a daemon may already be listening (on either candidate).
484 const probe = await connectAnyCandidate();
485 if (probe === 'version-mismatch') return null; // definitive — serve in-process, don't poll for 6s
486 if (probe) return probe;
487 // None reachable — spawn one (detached) and poll for its bind.
488 spawnDetachedDaemon(root);
489 for (let attempt = 0; attempt < DAEMON_CONNECT_MAX_RETRIES; attempt++) {
490 await sleep(DAEMON_CONNECT_RETRY_DELAY_MS);
491 const s = await connectAnyCandidate();
492 if (s === 'version-mismatch') return null;
493 if (s) return s;
494 }
495 return null; // never bound — the proxy serves this session in-process
496 };
497 await runLocalHandshakeProxy({ getDaemonSocket, makeEngine: () => new MCPEngine(), root });
498 }
499
500 /** Standard SIGINT/SIGTERM handlers that route to our `stop()` (direct mode). */
501 private installSignalHandlers(): void {

Callers 1

startMethod · 0.95

Calls 2

runLocalHandshakeProxyFunction · 0.90

Tested by

no test coverage detected