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

Function runProxy

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 8

readHelloLineFunction · 0.85
logAttachedDaemonFunction · 0.85
sendClientHelloFunction · 0.85
startPpidWatchdogFunction · 0.85
pipeUntilCloseFunction · 0.85
exitMethod · 0.80
writeMethod · 0.65
destroyMethod · 0.45

Tested by

no test coverage detected