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

Method getCodeGraph

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

1560 * similar to how git finds .git/ directories.
1561 */
1562 private getCodeGraph(projectPath?: string): CodeGraph {
1563 if (!projectPath) {
1564 if (!this.cg) {
1565 const searched = this.defaultProjectHint ?? process.cwd();
1566 throw new NotIndexedError(
1567 'No CodeGraph project is loaded for this session.\n' +
1568 `Searched for a .codegraph/ directory starting from: ${searched}\n` +
1569 this.formatKnownSubprojects() +
1570 'Either the server root has no index of its own (e.g. a monorepo where only ' +
1571 "sub-projects are indexed), or the MCP client launched the server outside your " +
1572 'project without reporting the workspace root. Either way, target the project ' +
1573 'explicitly:\n' +
1574 ' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project" ' +
1575 '(any project that has a .codegraph/ — including a sub-project of a monorepo)\n' +
1576 ' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]\n' +
1577 'If a project simply has no index, use your built-in tools (Read/Grep/Glob) for THAT ' +
1578 "project (the user can run 'codegraph init' there to enable it) — you can still query " +
1579 'other indexed projects by projectPath in the same session.'
1580 );
1581 }
1582 return this.freshen(this.cg);
1583 }
1584
1585 // Reject sensitive system directories before opening. Only validate a
1586 // path that actually exists — a nested or not-yet-created sub-path of a
1587 // real project must still be allowed to resolve UP to its .codegraph/
1588 // root below (issue #238), so we don't run the existence-checking
1589 // validator on paths that are meant to walk up.
1590 if (existsSync(projectPath)) {
1591 const pathError = validateProjectPath(projectPath);
1592 if (pathError) {
1593 throw new PathRefusalError(pathError);
1594 }
1595 }
1596
1597 // Always RE-RESOLVE the nearest .codegraph/ from the input path. The walk
1598 // is cheap (a few existsSync up the tree) and is the only thing that
1599 // notices a path whose index root CHANGED since it was first seen — most
1600 // importantly a git worktree that gained its own .codegraph/ after the
1601 // (long-lived) server first resolved it up to the parent checkout. We used
1602 // to short-circuit on a `projectCache[projectPath]` entry before resolving,
1603 // which pinned that first resolution for the server's whole lifetime, so a
1604 // worktree kept being served the parent checkout's index until restart
1605 // (#926). The DB connection itself is still cached (by resolved root,
1606 // below), so re-resolving costs only the stat walk, never a reopen.
1607 const resolvedRoot = findNearestCodeGraphRoot(projectPath);
1608
1609 if (!resolvedRoot) {
1610 throw new NotIndexedError(
1611 `The project at ${projectPath} isn't indexed with codegraph (no .codegraph/ directory found ` +
1612 'walking up from it), so codegraph cannot query it. Use your built-in tools (Read/Grep/Glob) ' +
1613 "for that codebase instead, and don't call codegraph for it again this session. " +
1614 "Indexing is the user's decision — they can run 'codegraph init' in that project to enable it."
1615 );
1616 }
1617
1618 // If the path resolves to the default project, reuse the already-open
1619 // default instance rather than opening a SECOND connection to the same DB.

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 9

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