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

Method getCodeGraph

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

1028 * similar to how git finds .git/ directories.
1029 */
1030 private getCodeGraph(projectPath?: string): CodeGraph {
1031 if (!projectPath) {
1032 if (!this.cg) {
1033 const searched = this.defaultProjectHint ?? process.cwd();
1034 throw new NotIndexedError(
1035 'No CodeGraph project is loaded for this session.\n' +
1036 `Searched for a .codegraph/ directory starting from: ${searched}\n` +
1037 'Either the server root has no index of its own (e.g. a monorepo where only ' +
1038 "sub-projects are indexed), or the MCP client launched the server outside your " +
1039 'project without reporting the workspace root. Either way, target the project ' +
1040 'explicitly:\n' +
1041 ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' +
1042 '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' +
1043 ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
1044 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' +
1045 "project (the user can run 'codegraph init' there to enable it) — you can still query " +
1046 'other indexed projects by projectPath in the same session.'
1047 );
1048 }
1049 return this.freshen(this.cg);
1050 }
1051
1052 // Reject sensitive system directories before opening. Only validate a
1053 // path that actually exists — a nested or not-yet-created sub-path of a
1054 // real project must still be allowed to resolve UP to its .codegraph/
1055 // root below (issue #238), so we don't run the existence-checking
1056 // validator on paths that are meant to walk up.
1057 if (existsSync(projectPath)) {
1058 const pathError = validateProjectPath(projectPath);
1059 if (pathError) {
1060 throw new PathRefusalError(pathError);
1061 }
1062 }
1063
1064 // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk
1065 // is cheap (a few existsSync up the tree) and is the only thing that
1066 // notices a path whose index root CHANGED since it was first seen — most
1067 // importantly a git worktree that gained its own .codegraph/ after the
1068 // (long-lived) server first resolved it up to the parent checkout. We used
1069 // to short-circuit on a `projectCache[projectPath]` entry before resolving,
1070 // which pinned that first resolution for the server's whole lifetime, so a
1071 // worktree kept being served the parent checkout's index until restart
1072 // (#926). The DB connection itself is still cached (by resolved root,
1073 // below), so re-resolving costs only the stat walk, never a reopen.
1074 const resolvedRoot = findNearestCodeGraphRoot(projectPath);
1075
1076 if (!resolvedRoot) {
1077 throw new NotIndexedError(
1078 `The project at ${projectPath} isn't indexed with codegraph (no .codegraph/ directory found ` +
1079 'walking up from it), so codegraph cannot query it. Use your built-in tools (Read/Grep/Glob) ' +
1080 "for that codebase instead, and don't call codegraph for it again this session. " +
1081 "Indexing is the user's decision — they can run 'codegraph init' in that project to enable it."
1082 );
1083 }
1084
1085 // If the path resolves to the default project, reuse the already-open
1086 // default instance rather than opening a SECOND connection to the same DB.
1087 // A duplicate connection serializes reads against the watcher's auto-sync

Callers 11

worktreeMismatchForMethod · 0.95
withStalenessNoticeMethod · 0.95
handleSearchMethod · 0.95
handleCallersMethod · 0.95
handleCalleesMethod · 0.95
handleImpactMethod · 0.95
handleExploreMethod · 0.95
handleNodeMethod · 0.95
handleStatusMethod · 0.95
handleFilesMethod · 0.95

Calls 8

freshenMethod · 0.95
validateProjectPathFunction · 0.90
findNearestCodeGraphRootFunction · 0.90
openSyncMethod · 0.80
loadCodeGraphFunction · 0.70
getProjectRootMethod · 0.65
getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected