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

Function runProxy

src/mcp/proxy.ts:85–125  ·  view source on GitHub ↗
(
  socketPath: string,
  expectedVersion: string = CodeGraphPackageVersion,
)

Source from the content-addressed store, hash-verified

83 * price of never silently running a stale daemon against newer client code.)
84 */
85export async function runProxy(
86 socketPath: string,
87 expectedVersion: string = CodeGraphPackageVersion,
88): Promise<ProxyResult> {
89 // POSIX: refuse to connect to a stale socket file that points at no
90 // listening process. `fs.existsSync` is a cheap pre-check; a real
91 // ECONNREFUSED below catches the rare "exists but unbound" race.
92 if (process.platform !== 'win32' && !fs.existsSync(socketPath)) {
93 return { outcome: 'fallback-needed', reason: 'socket file missing' };
94 }
95
96 const socket = net.createConnection(socketPath);
97 socket.setEncoding('utf8');
98
99 const hello = await readHelloLine(socket).catch((err) => {
100 socket.destroy();
101 return new Error(String(err));
102 });
103 if (hello instanceof Error) {
104 return { outcome: 'fallback-needed', reason: hello.message };
105 }
106
107 if (hello.codegraph !== expectedVersion) {
108 process.stderr.write(
109 `[CodeGraph MCP] Found a daemon on ${socketPath} but version (${hello.codegraph}) ` +
110 `differs from ours (${expectedVersion}); falling back to direct mode.\n`
111 );
112 socket.destroy();
113 return { outcome: 'fallback-needed', reason: 'version mismatch' };
114 }
115
116 logAttachedDaemon(socketPath, hello);
117
118 sendClientHello(socket);
119 startPpidWatchdog(socket);
120 await pipeUntilClose(socket);
121 // Host disconnected (or the daemon went away). The proxy's only job is the
122 // pipe; exit now so we don't linger — process.stdin's 'data' listener would
123 // otherwise keep the event loop alive and leave a zombie launcher behind.
124 process.exit(0);
125}
126
127/**
128 * Connect to a daemon at `socketPath` and verify its hello (exact version match).

Callers

nothing calls this directly

Calls 7

readHelloLineFunction · 0.85
logAttachedDaemonFunction · 0.85
sendClientHelloFunction · 0.85
startPpidWatchdogFunction · 0.85
pipeUntilCloseFunction · 0.85
destroyMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected