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

Method getCodeGraph

src/mcp/tools.ts:2600–2692  ·  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

2598 * similar to how git finds .git/ directories.
2599 */
2600 private getCodeGraph(projectPath?: string): CodeGraph {
2601 if (!projectPath) {
2602 if (!this.cg) {
2603 if (this.defaultOpenFailure) throw this.defaultOpenFailure;
2604 const searched = this.defaultProjectHint ?? process.cwd();
2605 // A refused default is the same "pass projectPath" state, success-shaped
2606 // like the rest: the agent can still query every other project.
2607 const why = this.defaultRootRefusal
2608 ? `${this.defaultRootRefusal}. CodeGraph never uses a credential, config or system folder ` +
2609 'as a project, so this session has no default project. To query a project outside such ' +
2610 'folders, target it explicitly:\n'
2611 : `Searched for a .codegraph/ directory starting from: ${searched}\n` +
2612 this.formatKnownSubprojects() +
2613 'Either the server root has no index of its own (e.g. a monorepo where only ' +
2614 "sub-projects are indexed), or the MCP client launched the server outside your " +
2615 'project without reporting the workspace root. Either way, target the project ' +
2616 'explicitly:\n';
2617 throw new NotIndexedError(
2618 'No CodeGraph project is loaded for this session.\n' +
2619 why +
2620 ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' +
2621 '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' +
2622 ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
2623 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' +
2624 "project (the user can run 'codegraph init' there to enable it) — you can still query " +
2625 'other indexed projects by projectPath in the same session.'
2626 );
2627 }
2628 return this.freshen(this.cg);
2629 }
2630
2631 // Reject sensitive system directories before opening. Only validate a
2632 // path that actually exists — a nested or not-yet-created sub-path of a
2633 // real project must still be allowed to resolve UP to its .codegraph/
2634 // root below (issue #238), so we don't run the existence-checking
2635 // validator on paths that are meant to walk up.
2636 if (existsSync(projectPath)) {
2637 const pathError = validateProjectPath(projectPath);
2638 if (pathError) {
2639 throw new PathRefusalError(pathError);
2640 }
2641 }
2642
2643 // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk
2644 // is cheap (a few existsSync up the tree) and is the only thing that
2645 // notices a path whose index root CHANGED since it was first seen — most
2646 // importantly a git worktree that gained its own .codegraph/ after the
2647 // (long-lived) server first resolved it up to the parent checkout. We used
2648 // to short-circuit on a `projectCache[projectPath]` entry before resolving,
2649 // which pinned that first resolution for the server's whole lifetime, so a
2650 // worktree kept being served the parent checkout's index until restart
2651 // (#926). The DB connection itself is still cached (by resolved root,
2652 // below), so re-resolving costs only the stat walk, never a reopen.
2653 const resolvedRoot = findNearestCodeGraphRoot(projectPath);
2654 // Two spellings of one root (a symlinked checkout, `/tmp` vs
2655 // `/private/tmp`) must share one connection and one watcher (#1835).
2656 const canonicalRoot = resolvedRoot ? canonicalPath(resolvedRoot) : null;
2657

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