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

Function runLocalHandshakeProxy

src/mcp/proxy.ts:265–623  ·  view source on GitHub ↗
(deps: LocalHandshakeDeps)

Source from the content-addressed store, hash-verified

263 * and proxies again once one answers.
264 */
265export async function runLocalHandshakeProxy(deps: LocalHandshakeDeps): Promise<void> {
266 // The proxy is long-lived and can serve fallback tool calls in-process. Match
267 // direct/daemon mode by killing this launcher if its main thread wedges, so an
268 // MCP host retry cannot accumulate abandoned `serve --mcp` wrapper processes.
269 const livenessWatchdog: WatchdogHandle | null = installMainThreadWatchdog();
270 let daemonStatus: 'connecting' | 'ready' | 'failed' = 'connecting';
271 let daemonSocket: net.Socket | null = null;
272 let clientInitId: unknown = undefined; // suppress the daemon's reply to the forwarded initialize
273 let clientInitLine: string | undefined; // replayed to a daemon that comes up mid-session (#2277)
274 // The host's clientInfo from initialize, kept as MCPSession keeps it. It picks
275 // the tool names the local handshake says (#1267), and attributes telemetry
276 // for the in-process fallback only — calls routed to the daemon are counted
277 // by the daemon's own session (which receives the forwarded initialize,
278 // clientInfo included), never double-counted here.
279 let clientInfo: ClientInfo | undefined;
280 const pending: string[] = []; // client lines buffered until the daemon resolves
281 let engine: MCPEngine | null = null;
282 let engineReady: Promise<void> | null = null;
283 // Calls being served in-process; the engine is only ever stopped after they finish.
284 const localCalls = new Set<Promise<void>>();
285 // Engines retired but not yet stopped; shutdown() waits for them too.
286 const retiring = new Set<MCPEngine>();
287 let shuttingDown = false;
288 const retryBaseMs = parseDelayMs(process.env.CODEGRAPH_DAEMON_RETRY_MS, DEFAULT_DAEMON_RETRY_MS);
289 const retryMaxMs = Math.max(retryBaseMs, parseDelayMs(process.env.CODEGRAPH_DAEMON_RETRY_MAX_MS, DEFAULT_DAEMON_RETRY_MAX_MS));
290 let retryDelayMs = retryBaseMs;
291 let retryTimer: NodeJS.Timeout | null = null;
292 let attachedAt = 0;
293 // Requests forwarded to the daemon and not yet answered, keyed by JSON-RPC id.
294 // If the daemon dies mid-session (#662 — e.g. an MCP host SIGTERM's it when a
295 // new session starts), these would otherwise hang forever; we re-serve them
296 // in-process so the host always gets a reply.
297 const inflight = new Map<unknown, string>();
298 // Explore call history for the ONE host connection this proxy serves (CG-17).
299 // Only the daemon-unavailable fallback below uses it; when the daemon is up,
300 // the tracking happens on the daemon's own MCPSession.
301 const exploreSession = new ExploreSessionState();
302 // The fallback is still one MCP client session. Keep its one-time warnings
303 // local just as MCPSession does when the shared daemon is available.
304 const indexVersionWarnings = new IndexVersionWarningState();
305 const recordDaemonWarning = (requestLine: string | undefined, response: JsonRpc): void => {
306 if (!requestLine) return;
307 const result = response.result as { content?: Array<{ type?: unknown; text?: unknown }> } | undefined;
308 const first = result?.content?.[0];
309 if (first?.type !== 'text' || typeof first.text !== 'string') return;
310 if (!first.text.startsWith(INDEX_VERSION_WARNING_PREFIX)) return;
311
312 let request: JsonRpc;
313 try { request = JSON.parse(requestLine) as JsonRpc; } catch { return; }
314 const params = request.params as {
315 arguments?: { projectPath?: unknown };
316 } | undefined;
317 const requestedPath = params?.arguments?.projectPath;
318 let root = deps.root;
319 if (typeof requestedPath === 'string') {
320 const resolved = findNearestCodeGraphRoot(requestedPath);
321 if (!resolved) return;
322 root = resolved;

Callers 1

Calls 15

instructionsForClientFunction · 0.90
toolsForClientFunction · 0.90
getStaticToolsFunction · 0.90
parseDelayMsFunction · 0.85
routeToDaemonFunction · 0.85
writeClientFunction · 0.85
connectDaemonFunction · 0.85

Tested by

no test coverage detected