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

Method getCodeGraph

src/mcp/tools.ts:2441–2533  ·  view source on GitHub ↗

* Get CodeGraph instance for a project * * If projectPath is provided, opens that project's CodeGraph (cached). * Otherwise returns the default CodeGraph instance. * * Walks up parent directories to find the nearest .codegraph/ folder, * similar to how git finds .git/ directories.

(projectPath?: string)

Source from the content-addressed store, hash-verified

2439 * similar to how git finds .git/ directories.
2440 */
2441 private getCodeGraph(projectPath?: string): CodeGraph {
2442 if (!projectPath) {
2443 if (!this.cg) {
2444 if (this.defaultOpenFailure) throw this.defaultOpenFailure;
2445 const searched = this.defaultProjectHint ?? process.cwd();
2446 // A refused default is the same "pass projectPath" state, success-shaped
2447 // like the rest: the agent can still query every other project.
2448 const why = this.defaultRootRefusal
2449 ? `${this.defaultRootRefusal}. CodeGraph never uses a credential, config or system folder ` +
2450 'as a project, so this session has no default project. To query a project outside such ' +
2451 'folders, target it explicitly:\n'
2452 : `Searched for a .codegraph/ directory starting from: ${searched}\n` +
2453 this.formatKnownSubprojects() +
2454 'Either the server root has no index of its own (e.g. a monorepo where only ' +
2455 "sub-projects are indexed), or the MCP client launched the server outside your " +
2456 'project without reporting the workspace root. Either way, target the project ' +
2457 'explicitly:\n';
2458 throw new NotIndexedError(
2459 'No CodeGraph project is loaded for this session.\n' +
2460 why +
2461 ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' +
2462 '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' +
2463 ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
2464 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' +
2465 "project (the user can run 'codegraph init' there to enable it) — you can still query " +
2466 'other indexed projects by projectPath in the same session.'
2467 );
2468 }
2469 return this.freshen(this.cg);
2470 }
2471
2472 // Reject sensitive system directories before opening. Only validate a
2473 // path that actually exists — a nested or not-yet-created sub-path of a
2474 // real project must still be allowed to resolve UP to its .codegraph/
2475 // root below (issue #238), so we don't run the existence-checking
2476 // validator on paths that are meant to walk up.
2477 if (existsSync(projectPath)) {
2478 const pathError = validateProjectPath(projectPath);
2479 if (pathError) {
2480 throw new PathRefusalError(pathError);
2481 }
2482 }
2483
2484 // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk
2485 // is cheap (a few existsSync up the tree) and is the only thing that
2486 // notices a path whose index root CHANGED since it was first seen — most
2487 // importantly a git worktree that gained its own .codegraph/ after the
2488 // (long-lived) server first resolved it up to the parent checkout. We used
2489 // to short-circuit on a `projectCache[projectPath]` entry before resolving,
2490 // which pinned that first resolution for the server's whole lifetime, so a
2491 // worktree kept being served the parent checkout's index until restart
2492 // (#926). The DB connection itself is still cached (by resolved root,
2493 // below), so re-resolving costs only the stat walk, never a reopen.
2494 const resolvedRoot = findNearestCodeGraphRoot(projectPath);
2495 // Two spellings of one root (a symlinked checkout, `/tmp` vs
2496 // `/private/tmp`) must share one connection and one watcher (#1835).
2497 const canonicalRoot = resolvedRoot ? canonicalPath(resolvedRoot) : null;
2498

Callers 15

awaitProjectGateMethod · 0.95
worktreeMismatchForMethod · 0.95
withStalenessNoticeMethod · 0.95
executeMethod · 0.95
handleSearchMethod · 0.95
handleCallersMethod · 0.95
handleCalleesMethod · 0.95
handleImpactMethod · 0.95
handleExploreMethod · 0.95
handleNodeMethod · 0.95
handleStatusMethod · 0.95

Calls 7

freshenMethod · 0.95
openProjectRootMethod · 0.95
uncoveredNestedRepoMethod · 0.95
validateProjectPathFunction · 0.90
findNearestCodeGraphRootFunction · 0.90
canonicalPathFunction · 0.85

Tested by 1

openFunction · 0.64