codegraph icon indicating copy to clipboard operation
codegraph copied to clipboard

CodeGraph MCP tools fail with "CodeGraph not initialized" when projectPath is omitted, even though index exists

Open zhangyu1197 opened this issue 6 months ago • 3 comments

Problem

When using CodeGraph MCP tools without specifying the projectPath parameter, the tools fail with the error: Error: Tool execution failed: CodeGraph not initialized for this project. Run 'codegraph init' first. However, the CodeGraph index does exist in the project directory at .codegraph/codegraph.db (49.46 MB), and codegraph status command works correctly.

Environment

  • Project Directory: /Users/20250310/IdeaProjects/test1
  • Working Directory: Same as project directory
  • CodeGraph Index: Exists at .codegraph/codegraph.db
  • Index Status: Valid (1,152 files, 15,420 nodes, 26,260 edges)

zhangyu1197 avatar May 20 '26 04:05 zhangyu1197

Please try installing bettersqlite3 npm install better-sqlite3 and try again , sometimes sqlite db is locked

shubhamshnd avatar May 20 '26 06:05 shubhamshnd

execute npm install better-sqlite3 and try again, it was still failed.After consulting AI,I wrote a rule,when call mcp, add projectPath, for example:mcp_codegraph_codegraph_status({ "projectPath": "/Users/20250310/IdeaProjects/tes1" }),then it was successful

zhangyu1197 avatar May 20 '26 08:05 zhangyu1197

Fixed in #214 (merged to main) — thanks @zhangyu1197 for the clear report, the diagnosis, and the projectPath workaround that pinpointed it. 🙏

Root cause: when a client launches the MCP server outside the project and doesn't report a workspace root in initialize, the server was falling back to its own working directory and missing your .codegraph/. It now asks the client for its workspace root via the standard MCP roots/list request before falling back — so detection works without you having to pass projectPath by hand. And if it still can't resolve a project, the error now tells you exactly how to fix it (pass projectPath, or add --path /abs/project to the server's MCP config args) instead of the misleading "run codegraph init".

This will go out in the next npm release. If your client doesn't implement the MCP roots capability, the --path flag in your MCP config is the reliable fallback.

colbymchenry avatar May 20 '26 16:05 colbymchenry