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

Method handleConnection

src/mcp/daemon.ts:435–469  ·  view source on GitHub ↗
(socket: net.Socket)

Source from the content-addressed store, hash-verified

433 }
434
435 private handleConnection(socket: net.Socket): void {
436 if (this.stopping) { socket.destroy(); return; }
437 this.acceptedSockets.add(socket);
438 socket.once('close', () => this.acceptedSockets.delete(socket));
439 // Hello first so the proxy can verify versions before piping any
440 // application bytes. The proxy reads exactly one line, then forwards.
441 const hello: DaemonHello = {
442 codegraph: CodeGraphPackageVersion,
443 pid: process.pid,
444 socketPath: this.socketPath,
445 protocol: 1,
446 };
447 socket.write(JSON.stringify(hello) + '\n');
448
449 // Read the optional client-hello (proxy → daemon) to learn the client's
450 // peer pids, then hand the socket to the session. Fail-safe: any problem —
451 // timeout, a non-hello first line, an early close — yields null pids and we
452 // fall back to the socket-close lifecycle exactly as before (#692).
453 void readClientHello(socket).then((peers) => {
454 if (this.stopping || socket.destroyed) { socket.destroy(); return; }
455 const transport = new SocketTransport(socket);
456 const session = new MCPSession(transport, this.engine, {
457 explicitProjectPath: this.projectRoot,
458 });
459 transport.onClose(() => this.dropClient(session));
460 this.clients.add(session);
461 this.clientPeers.set(session, peers);
462 this.disarmIdleTimer();
463 session.start();
464 // Observe inbound bytes purely to feed the inactivity backstop — a second
465 // 'data' listener that reads nothing, added AFTER the transport's so the
466 // unshifted client-hello tail reaches the transport intact.
467 socket.on('data', () => { this.lastActivityAt = Date.now(); });
468 });
469 }
470
471 private dropClient(session: MCPSession): void {
472 if (!this.clients.delete(session)) return;

Callers 1

listenMethod · 0.95

Calls 11

onCloseMethod · 0.95
dropClientMethod · 0.95
disarmIdleTimerMethod · 0.95
startMethod · 0.95
readClientHelloFunction · 0.85
deleteMethod · 0.65
writeMethod · 0.65
onMethod · 0.65
destroyMethod · 0.45
addMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected