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

Method runProxyWithLocalHandshake

src/mcp/index.ts:763–801  ·  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; one of * an older release is stopped first, #2335) so the * hands

(root: string)

Source from the content-addressed store, hash-verified

761 * never wedges a session.
762 */
763 private async runProxyWithLocalHandshake(root: string): Promise<void> {
764 // The daemon may relocate its socket past an in-project filesystem that can't
765 // host one (ExFAT/FAT volumes, WSL2 DrvFs; #997) to the deterministic tmpdir
766 // fallback. We don't read the bound path from the lockfile — both sides walk
767 // the SAME ordered candidate list, so we converge on whichever the daemon
768 // bound with zero coordination. The in-project candidate is tried first, so a
769 // normal repo pays nothing extra (it connects on the very first probe).
770 const candidates = getDaemonSocketCandidates(root);
771 const connectAnyCandidate = (deadline?: number) => connectAnyDaemonCandidate(candidates, deadline);
772 const getDaemonSocket = async () => {
773 // Fast path: a daemon may already be listening (on either candidate).
774 const probe = await connectAnyCandidate();
775 if (probe === 'version-mismatch') return null; // definitive — serve in-process, don't poll for 6s
776 // Something of this version answers where the daemon listens but can't
777 // prove it runs as this user: no session goes through it, and none is
778 // spawned to fight it for the socket. Serve in-process.
779 if (probe === 'unproven') return null;
780 if (probe && probe !== 'older-version') return probe;
781 // A daemon of an older release answered — or none did, while the project
782 // lock may still name one on a socket we don't probe (on Windows, daemons
783 // before #2278 named their pipe after the root as typed). Either way it
784 // makes way for one from this install (#2335).
785 const answered = probe === 'older-version';
786 const older = await replaceOlderDaemon(root, answered);
787 if (older === 'kept' && answered) return null;
788 // None reachable — spawn one (detached) and poll for its bind. After a
789 // replacement this launcher holds the writer slot for it meanwhile.
790 try {
791 spawnDetachedDaemon(root, older === 'stopped');
792 // Null when nothing answered in time — never bound, or a holder that
793 // never says hello: the proxy serves this session in-process.
794 return await pollForDaemon(connectAnyCandidate);
795 } finally {
796 // The new daemon has taken the slot over, or never came; a no-op unless still ours.
797 if (older === 'stopped') releaseWriterLock(root);
798 }
799 };
800 await runLocalHandshakeProxy({ getDaemonSocket, makeEngine: () => makeFallbackEngine(root), root });
801 }
802
803 /** Standard SIGINT/SIGTERM handlers that route to our `stop()` (direct mode). */
804 private installSignalHandlers(): void {

Callers 1

startMethod · 0.95

Calls 3

runLocalHandshakeProxyFunction · 0.90
makeFallbackEngineFunction · 0.85

Tested by

no test coverage detected