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

Function runProxy

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

Source from the content-addressed store, hash-verified

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