feat(mcp): share one serve --mcp per project across MCP clients (#411)
One shared, detached daemon per project root: every `codegraph serve --mcp` is a thin stdio<->socket proxy (Unix socket / Windows named pipe) to it, so N agents in one repo share a single file watcher, SQLite connection, and tree-sitter warm-up instead of N copies. The daemon outlives any single session and reaps via client-refcount + idle timeout; `CODEGRAPH_NO_DAEMON=1` opts out. Hardened during review: detached-process lifecycle (preserves the #277 watchdog via the proxy; the daemon no longer orphans on host SIGKILL), atomic lockfile + pid-verified stale-clear (no double-daemon on concurrent startup), realpath root canonicalization. Validated on macOS, Linux (Docker - 3x fewer inotify watches for 3 agents), and Windows (named pipes); A/B confirms byte-identical tool output vs direct mode. Closes #411. Co-Authored-By: Colby McHenry <me@colbymchenry.com> Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Colby McHenry
Claude Opus 4.7
parent
2721165604
commit
995da54430
@@ -0,0 +1,232 @@
|
||||
/**
|
||||
* MCP shared engine — the heavyweight, *shared* state for an MCP server:
|
||||
* the project's {@link CodeGraph} instance, file watcher, and the
|
||||
* {@link ToolHandler} cache for cross-project queries.
|
||||
*
|
||||
* One engine, many sessions:
|
||||
* - direct mode (single stdio session) instantiates one engine + one session;
|
||||
* - daemon mode instantiates one engine and a new session per socket
|
||||
* connection. Every session reads from the same SQLite WAL and the same
|
||||
* inotify watch set — that's the entire point of issue #411.
|
||||
*/
|
||||
|
||||
import CodeGraph, { findNearestCodeGraphRoot } from '../index';
|
||||
import { watchDisabledReason } from '../sync';
|
||||
import { ToolHandler } from './tools';
|
||||
|
||||
export interface MCPEngineOptions {
|
||||
/**
|
||||
* Whether to start the file watcher when initializing. Daemon and direct
|
||||
* modes both want this true; tests may set it false to keep the engine
|
||||
* cheap. Honors {@link watchDisabledReason} regardless.
|
||||
*/
|
||||
watch?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared MCP engine. Thread-safe in the sense that multiple sessions can
|
||||
* call its methods concurrently — internally it serializes initialization
|
||||
* through a single promise so multiple sessions racing each other on first
|
||||
* connect never double-open the SQLite file.
|
||||
*/
|
||||
export class MCPEngine {
|
||||
private cg: CodeGraph | null = null;
|
||||
private toolHandler: ToolHandler;
|
||||
// Project root we resolved to. Null until `ensureInitialized` succeeds
|
||||
// (or null forever if no .codegraph/ ever turned up — that's a valid
|
||||
// state for the engine, since cross-project queries still work).
|
||||
private projectPath: string | null = null;
|
||||
// Set on first `ensureInitialized` so subsequent sessions don't redo work.
|
||||
private initPromise: Promise<void> | null = null;
|
||||
private watcherStarted = false;
|
||||
private opts: Required<MCPEngineOptions>;
|
||||
private closed = false;
|
||||
|
||||
constructor(opts: MCPEngineOptions = {}) {
|
||||
this.opts = { watch: opts.watch ?? true };
|
||||
this.toolHandler = new ToolHandler(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience for {@link MCPServer} compatibility: pre-seed an explicit
|
||||
* project path (from the `--path` CLI flag) without yet opening it. This
|
||||
* keeps the synchronous constructor cheap; the actual open happens on the
|
||||
* first `ensureInitialized` call.
|
||||
*/
|
||||
setProjectPathHint(projectPath: string): void {
|
||||
this.projectPath = projectPath;
|
||||
this.toolHandler.setDefaultProjectHint(projectPath);
|
||||
}
|
||||
|
||||
/** Project root that the engine resolved on first init (null if none). */
|
||||
getProjectPath(): string | null {
|
||||
return this.projectPath;
|
||||
}
|
||||
|
||||
/** Shared ToolHandler — sessions delegate tool dispatch through this. */
|
||||
getToolHandler(): ToolHandler {
|
||||
return this.toolHandler;
|
||||
}
|
||||
|
||||
/** Whether the default project's CodeGraph is open. */
|
||||
hasDefaultCodeGraph(): boolean {
|
||||
return this.toolHandler.hasDefaultCodeGraph();
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk up from `searchFrom` to find the nearest `.codegraph/` and open it.
|
||||
* Idempotent: concurrent callers share one in-flight init; subsequent
|
||||
* callers after success are no-ops.
|
||||
*
|
||||
* The original `MCPServer.tryInitializeDefault` carried the same retry-on-
|
||||
* subsequent-tool-call semantics; we preserve them by NOT throwing when the
|
||||
* search misses (just leaves `cg` null so the next call can retry).
|
||||
*/
|
||||
async ensureInitialized(searchFrom: string): Promise<void> {
|
||||
if (this.closed) return;
|
||||
if (this.toolHandler.hasDefaultCodeGraph()) return;
|
||||
if (this.initPromise) {
|
||||
try { await this.initPromise; } catch { /* let caller retry */ }
|
||||
return;
|
||||
}
|
||||
|
||||
this.initPromise = this.doInitialize(searchFrom).finally(() => {
|
||||
this.initPromise = null;
|
||||
});
|
||||
try {
|
||||
await this.initPromise;
|
||||
} catch {
|
||||
// Init errors are logged inside `doInitialize`; falling through here
|
||||
// matches MCPServer's previous "retry on next tool call" behavior.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronous last-resort init used by the per-session retry loop when the
|
||||
* background `ensureInitialized` already finished (or failed) and we need
|
||||
* to pick up a project that appeared *after* the engine started.
|
||||
*/
|
||||
retryInitializeSync(searchFrom: string): void {
|
||||
if (this.closed) return;
|
||||
if (this.toolHandler.hasDefaultCodeGraph()) return;
|
||||
this.toolHandler.setDefaultProjectHint(searchFrom);
|
||||
const resolvedRoot = findNearestCodeGraphRoot(searchFrom);
|
||||
if (!resolvedRoot) return;
|
||||
try {
|
||||
// Close any previously failed instance to avoid leaking resources.
|
||||
if (this.cg) {
|
||||
try { this.cg.close(); } catch { /* ignore */ }
|
||||
this.cg = null;
|
||||
}
|
||||
this.cg = CodeGraph.openSync(resolvedRoot);
|
||||
this.projectPath = resolvedRoot;
|
||||
this.toolHandler.setDefaultCodeGraph(this.cg);
|
||||
this.startWatching();
|
||||
this.catchUpSync();
|
||||
} catch {
|
||||
// Still failing — caller will try again on the next tool call.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Close everything. Used on graceful daemon shutdown (SIGTERM/idle timeout)
|
||||
* and on direct-mode stop. Idempotent.
|
||||
*/
|
||||
stop(): void {
|
||||
if (this.closed) return;
|
||||
this.closed = true;
|
||||
this.toolHandler.closeAll();
|
||||
if (this.cg) {
|
||||
try { this.cg.close(); } catch { /* ignore */ }
|
||||
this.cg = null;
|
||||
}
|
||||
}
|
||||
|
||||
private async doInitialize(searchFrom: string): Promise<void> {
|
||||
this.toolHandler.setDefaultProjectHint(searchFrom);
|
||||
|
||||
const resolvedRoot = findNearestCodeGraphRoot(searchFrom);
|
||||
if (!resolvedRoot) {
|
||||
// No .codegraph/ above searchFrom — that's not an error, sessions may
|
||||
// still discover one later via roots/list.
|
||||
this.projectPath = searchFrom;
|
||||
return;
|
||||
}
|
||||
|
||||
this.projectPath = resolvedRoot;
|
||||
try {
|
||||
this.cg = await CodeGraph.open(resolvedRoot);
|
||||
this.toolHandler.setDefaultCodeGraph(this.cg);
|
||||
this.startWatching();
|
||||
this.catchUpSync();
|
||||
} catch (err) {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
process.stderr.write(`[CodeGraph MCP] Failed to open project at ${resolvedRoot}: ${msg}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start file watching on the active CodeGraph instance. Idempotent — the
|
||||
* watcher is per-engine, not per-session, which is why the daemon path
|
||||
* collapses N inotify sets to one. The wording of the disabled-reason log
|
||||
* exactly matches the prior in-tree implementation so log-driven dashboards
|
||||
* keep working.
|
||||
*/
|
||||
private startWatching(): void {
|
||||
if (!this.cg || this.watcherStarted || !this.opts.watch) return;
|
||||
|
||||
const disabledReason = watchDisabledReason(this.projectPath ?? process.cwd());
|
||||
if (disabledReason) {
|
||||
process.stderr.write(
|
||||
`[CodeGraph MCP] File watcher disabled — ${disabledReason}. ` +
|
||||
`The graph will not auto-update; run \`codegraph sync\` (or install the git sync hooks via \`codegraph init\`) to refresh.\n`
|
||||
);
|
||||
this.watcherStarted = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const started = this.cg.watch({
|
||||
onSyncComplete: (result) => {
|
||||
if (result.filesChanged > 0) {
|
||||
process.stderr.write(
|
||||
`[CodeGraph MCP] Auto-synced ${result.filesChanged} file(s) in ${result.durationMs}ms\n`
|
||||
);
|
||||
}
|
||||
},
|
||||
onSyncError: (err) => {
|
||||
process.stderr.write(`[CodeGraph MCP] Auto-sync error: ${err.message}\n`);
|
||||
},
|
||||
});
|
||||
|
||||
this.watcherStarted = true;
|
||||
if (started) {
|
||||
process.stderr.write('[CodeGraph MCP] File watcher active — graph will auto-sync on changes\n');
|
||||
} else {
|
||||
process.stderr.write(
|
||||
'[CodeGraph MCP] File watcher unavailable on this platform — run `codegraph sync` to refresh the graph after changes.\n'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconcile the index with the current filesystem once, right after open —
|
||||
* catches edits, adds, deletes, and `git pull`/`checkout` changes made while
|
||||
* no watcher was running. Background, never awaited.
|
||||
*/
|
||||
private catchUpSync(): void {
|
||||
const cg = this.cg;
|
||||
if (!cg) return;
|
||||
void cg
|
||||
.sync()
|
||||
.then((result) => {
|
||||
const changed = result.filesAdded + result.filesModified + result.filesRemoved;
|
||||
if (changed > 0) {
|
||||
process.stderr.write(`[CodeGraph MCP] Caught up ${changed} file(s) changed since last run\n`);
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const msg = err instanceof Error ? err.message : String(err);
|
||||
process.stderr.write(`[CodeGraph MCP] Catch-up sync failed: ${msg}\n`);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user