MCPcopy
hub / github.com/giancarloerra/SocratiCode / handleGraphTool

Function handleGraphTool

src/tools/graph-tools.ts:22–479  ·  view source on GitHub ↗
(
  name: string,
  args: Record<string, unknown>,
)

Source from the content-addressed store, hash-verified

20import { ensureWatcherStarted } from "../services/watcher.js";
21
22export async function handleGraphTool(
23 name: string,
24 args: Record<string, unknown>,
25): Promise<string> {
26 const projectPath = path.resolve((args.projectPath as string) || process.cwd());
27
28 // Auto-start watcher on any graph interaction (fire-and-forget)
29 ensureWatcherStarted(projectPath);
30
31 switch (name) {
32 case "codebase_graph_build": {
33 const resolved = path.resolve(projectPath);
34
35 // Concurrency guard: if already building, show progress
36 if (isGraphBuildInProgress(resolved)) {
37 const progress = getGraphBuildProgress(resolved);
38 const lines = [
39 `⚠ Graph build already in progress for: ${resolved}`,
40 ];
41 if (progress) {
42 const elapsed = ((Date.now() - progress.startedAt) / 1000).toFixed(0);
43 const pct = progress.filesTotal > 0
44 ? ` (${Math.round((progress.filesProcessed / progress.filesTotal) * 100)}%)`
45 : "";
46 lines.push(`Phase: ${progress.phase}`);
47 lines.push(`Progress: ${progress.filesProcessed}/${progress.filesTotal} files${pct}`);
48 lines.push(`Elapsed: ${elapsed}s`);
49 }
50 lines.push("", "Call codebase_graph_status to check progress.");
51 return lines.join("\n");
52 }
53
54 // Fire-and-forget: start graph build in the background
55 const extraExts = mergeExtraExtensions(args.extraExtensions as string | undefined);
56 rebuildGraph(resolved, extraExts.size > 0 ? extraExts : undefined)
57 .then((graph) => {
58 logger.info("Background graph build completed", {
59 projectPath: resolved,
60 nodes: graph.nodes.length,
61 edges: graph.edges.length,
62 });
63 })
64 .catch((err) => {
65 const message = err instanceof Error ? err.message : String(err);
66 logger.error("Background graph build failed", { projectPath: resolved, error: message });
67 });
68
69 return [
70 `Graph build started in the background for: ${resolved}`,
71 "",
72 "IMPORTANT: The graph is now building asynchronously.",
73 "Call codebase_graph_status to check progress. Keep calling it periodically until the build completes.",
74 "Once complete, you can use codebase_graph_query, codebase_graph_stats, etc. to explore the graph.",
75 ].join("\n");
76 }
77
78 case "codebase_graph_query": {
79 const filePath = args.filePath as string;

Callers 4

index.tsFile · 0.85
tools.test.tsFile · 0.85

Calls 15

ensureWatcherStartedFunction · 0.85
isGraphBuildInProgressFunction · 0.85
getGraphBuildProgressFunction · 0.85
mergeExtraExtensionsFunction · 0.85
rebuildGraphFunction · 0.85
getOrBuildGraphFunction · 0.85
getFileDependenciesFunction · 0.85
getGraphStatsFunction · 0.85
findCircularDependenciesFunction · 0.85
projectIdFromPathFunction · 0.85

Tested by

no test coverage detected