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

Method startDirect

src/mcp/index.ts:588–643  ·  view source on GitHub ↗

Single-process stdio MCP session — the pre-issue-#411 code path.

(reason: string)

Source from the content-addressed store, hash-verified

586
587 /** Single-process stdio MCP session — the pre-issue-#411 code path. */
588 private async startDirect(reason: string): Promise<void> {
589 if (reason && process.env.CODEGRAPH_MCP_DEBUG) {
590 process.stderr.write(`[CodeGraph MCP] Direct mode: ${reason}.\n`);
591 }
592
593 // #1740: refuse a second direct writer on an initialized project. Daemon
594 // mode multiplexes clients; direct mode is single-writer-per-project.
595 const writerRoot = resolveDaemonRoot(this.projectPath);
596 if (writerRoot) {
597 assertNoRebuild(writerRoot);
598 const writer = tryAcquireWriterLock(writerRoot, 'direct');
599 if (writer.kind === 'taken') {
600 const msg = writerLockHeldMessage(writer.existing, writer.pidPath);
601 process.stderr.write(`[CodeGraph MCP] ${msg}\n`);
602 process.exit(1);
603 }
604 this.writerLockRoot = writerRoot;
605 }
606
607 this.engine = new MCPEngine({ queryPool: true, queryPoolDefaultMax: DIRECT_QUERY_POOL_MAX });
608 const transport = new StdioTransport({ exitOnClose: false, onClose: () => { void this.stop(); } });
609 this.session = new MCPSession(transport, this.engine, {
610 explicitProjectPath: this.projectPath,
611 });
612
613 if (this.projectPath) {
614 // Background init so the initialize response stays fast (#172).
615 void this.engine.ensureInitialized(this.projectPath);
616 }
617
618 this.session.start();
619
620 // Detect parent-process death — same logic as pre-refactor. When stdin
621 // closes the transport drains the engine through stop(), but
622 // SIGKILL of the parent doesn't reliably close stdin on Linux (#277).
623 // Also treat a stdin `'error'` (a socket-backed stdin can fail with
624 // ECONNRESET/hangup instead of a clean close) as shutdown, and destroy the
625 // stream so a hung fd can't busy-spin the event loop (#799).
626 treatStdinFailureAsShutdown(() => this.stop());
627 // Backstop for a launch abandoned during startup (#1185): launcher killed
628 // before EARLY_PPID could see it + host holding our pipes open. A server
629 // that never receives a byte of MCP traffic isn't serving anyone. Armed
630 // after session.start() attached the real stdin consumer.
631 armStartupHandshakeTimeout(() => {
632 process.stderr.write(
633 '[CodeGraph MCP] No MCP traffic since startup; assuming an abandoned launch and shutting down (#1185). ' +
634 'Tune with CODEGRAPH_STARTUP_HANDSHAKE_TIMEOUT_MS (0 disables).\n'
635 );
636 this.stop();
637 });
638
639 this.mode = 'direct';
640 this.installSignalHandlers();
641 this.installPpidWatchdog();
642 this.livenessWatchdog = installMainThreadWatchdog(watchdogProgressPaths(resolveDaemonRoot(this.projectPath)));
643 }
644
645 /**

Callers 1

startMethod · 0.95

Calls 15

stopMethod · 0.95
installSignalHandlersMethod · 0.95
installPpidWatchdogMethod · 0.95
assertNoRebuildFunction · 0.90
tryAcquireWriterLockFunction · 0.90
writerLockHeldMessageFunction · 0.90
resolveDaemonRootFunction · 0.85
watchdogProgressPathsFunction · 0.85
exitMethod · 0.80

Tested by

no test coverage detected