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

Method doInitialize

src/mcp/engine.ts:398–450  ·  view source on GitHub ↗
(searchFrom: string)

Source from the content-addressed store, hash-verified

396 }
397
398 private async doInitialize(searchFrom: string): Promise<void> {
399 this.toolHandler.setDefaultProjectHint(searchFrom);
400
401 // Up-walk first; when nothing is indexed at or above searchFrom, a bounded
402 // down-scan may adopt a SINGLE indexed sub-project as the default (#1606 —
403 // the workspace-container shape where only children are indexed). Zero or
404 // several candidates → no default project, but SAY so (#1607): the silent
405 // variant of this state read as "CodeGraph is broken" and was diagnosable
406 // only by knowing to look for a missing ~/.codegraph/daemons/ entry.
407 const res = resolveServerRoot(searchFrom);
408 const resolvedRoot = res.root;
409 this.noteRootRefusal(res.refusal);
410 if (!resolvedRoot) {
411 // Sessions may still discover a project later via roots/list, and the
412 // per-call retry re-resolves — this state is recoverable, hence stderr
413 // (not a failure) + candidates surfaced through the tool-call error.
414 this.projectPath = searchFrom;
415 this.toolHandler.setKnownSubprojects(res.candidates, searchFrom);
416 if (!res.refusal) {
417 process.stderr.write(
418 `[CodeGraph MCP] No .codegraph/ at or above ${searchFrom}: no default project, live sync disabled until an indexed project is accessed via projectPath.\n`
419 );
420 }
421 if (res.candidates.length > 0) {
422 const rels = res.candidates.map((c) => path.relative(searchFrom, c) || '.');
423 process.stderr.write(
424 `[CodeGraph MCP] Indexed sub-projects found: ${rels.join(', ')}. Pass \`projectPath\` per call, or launch with --path.\n`
425 );
426 }
427 this.maybeStartPool(null);
428 return;
429 }
430 if (res.viaSubScan) this.logSubprojectAdoption(searchFrom, resolvedRoot);
431
432 this.projectPath = resolvedRoot;
433 // Opening a project, and the catch-up after it, is work (#3250).
434 this.workBegins();
435 try {
436 assertNoRebuild(resolvedRoot);
437 const opened = await loadCodeGraph().open(resolvedRoot, { readOnly: this.opts.readOnly });
438 if (this.closed) { opened.close(); return; }
439 this.cg = opened;
440 this.toolHandler.setDefaultCodeGraph(this.cg);
441 this.startWatching();
442 this.catchUpSync();
443 this.maybeStartPool(resolvedRoot);
444 } catch (err) {
445 const msg = err instanceof Error ? err.message : String(err);
446 process.stderr.write(`[CodeGraph MCP] Failed to open project at ${resolvedRoot}: ${msg}\n`);
447 // The agent otherwise hears only "no project loaded" (#995).
448 this.toolHandler.setDefaultOpenFailure(reportableOpenFailure(err));
449 }
450 }
451
452 /**
453 * Record why the default project is refused (a sensitive folder, #2929's

Callers 1

ensureInitializedMethod · 0.95

Calls 15

noteRootRefusalMethod · 0.95
maybeStartPoolMethod · 0.95
logSubprojectAdoptionMethod · 0.95
workBeginsMethod · 0.95
startWatchingMethod · 0.95
catchUpSyncMethod · 0.95
resolveServerRootFunction · 0.90
assertNoRebuildFunction · 0.90
reportableOpenFailureFunction · 0.85
setDefaultProjectHintMethod · 0.80
setKnownSubprojectsMethod · 0.80
mapMethod · 0.80

Tested by

no test coverage detected