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