MCP tools failed with "CodeGraph not initialized" when a client launched the server outside the project and sent no rootUri/workspaceFolders — the server fell back to its own cwd, missed the project's .codegraph/, and returned a misleading "run codegraph init" error on every call. The only workaround was passing projectPath by hand to each tool. When no explicit path is given, the server now asks the client for its workspace root via the standard MCP roots/list request (gated on the client advertising the roots capability) before falling back to cwd. This required teaching the stdio transport to send server->client requests and match their responses by id (previously responses were dropped as invalid). When a project still can't be resolved, the error now names the directory it searched and tells the user to pass projectPath or add --path to the MCP config, instead of pointing at a re-init they don't need. Reported-by: @zhangyu1197 Closes #196 Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
cf7db7cb98
commit
1cd162a66d
+21
-1
@@ -440,6 +440,9 @@ export const tools: ToolDefinition[] = [
|
||||
export class ToolHandler {
|
||||
// Cache of opened CodeGraph instances for cross-project queries
|
||||
private projectCache: Map<string, CodeGraph> = new Map();
|
||||
// The directory the server last searched for a default project. Surfaced in
|
||||
// the "not initialized" error so users can see why detection missed.
|
||||
private defaultProjectHint: string | null = null;
|
||||
|
||||
constructor(private cg: CodeGraph | null) {}
|
||||
|
||||
@@ -450,6 +453,14 @@ export class ToolHandler {
|
||||
this.cg = cg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record the directory the server tried to resolve the default project from.
|
||||
* Used only to make the "no default project" error actionable.
|
||||
*/
|
||||
setDefaultProjectHint(searchedPath: string): void {
|
||||
this.defaultProjectHint = searchedPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a default CodeGraph instance is available
|
||||
*/
|
||||
@@ -495,7 +506,16 @@ export class ToolHandler {
|
||||
private getCodeGraph(projectPath?: string): CodeGraph {
|
||||
if (!projectPath) {
|
||||
if (!this.cg) {
|
||||
throw new Error('CodeGraph not initialized for this project. Run \'codegraph init\' first.');
|
||||
const searched = this.defaultProjectHint ?? process.cwd();
|
||||
throw new Error(
|
||||
'No CodeGraph project is loaded for this session.\n' +
|
||||
`Searched for a .codegraph/ directory starting from: ${searched}\n` +
|
||||
'The index is likely fine — this is a working-directory detection issue: ' +
|
||||
"the MCP client launched the server outside your project and didn't report the " +
|
||||
'workspace root. Fix it either way:\n' +
|
||||
' • Pass projectPath to the tool call, e.g. projectPath: "/absolute/path/to/your/project"\n' +
|
||||
' • Or add --path to the server\'s MCP config args: ["serve", "--mcp", "--path", "/absolute/path/to/your/project"]'
|
||||
);
|
||||
}
|
||||
return this.cg;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user