visualize-dependencies
Generate visual dependency graphs from code repositories to analyze and understand relationships between files and modules using mermaid, ascii, or dot formats.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| fileContent | No | ||
| filePath | No | ||
| format | No | mermaid | |
| path | No | ||
| repositoryUrl | No |
Implementation Reference
- src/features/visualization/index.ts:10-47 (registration)Registers the 'visualize-dependencies' tool on the MCP server, including Zod input schema and async handler function that invokes the visualization logic.server.tool( "visualize-dependencies", { repositoryUrl: z.string().optional(), filePath: z.string().optional(), path: z.string().optional(), fileContent: z.string().optional(), format: z.enum(["ascii", "mermaid", "dot"]).default("mermaid") }, async ({ repositoryUrl, filePath, path, fileContent, format }) => { try { const targetPath = path || repositoryUrl || filePath; const visualization = await generateDependencyGraph({ repositoryUrl: targetPath, filePath, fileContent, format }); return { content: [{ type: "text", text: visualization, _metadata: format === "mermaid" ? { format: "mermaid" } : undefined }] }; } catch (error) { return { content: [{ type: "text", text: `Error generating visualization: ${(error as Error).message}` }], isError: true }; } } );
- The execution handler for the tool, which normalizes input parameters, calls generateDependencyGraph, and returns formatted content or error.async ({ repositoryUrl, filePath, path, fileContent, format }) => { try { const targetPath = path || repositoryUrl || filePath; const visualization = await generateDependencyGraph({ repositoryUrl: targetPath, filePath, fileContent, format }); return { content: [{ type: "text", text: visualization, _metadata: format === "mermaid" ? { format: "mermaid" } : undefined }] }; } catch (error) { return { content: [{ type: "text", text: `Error generating visualization: ${(error as Error).message}` }], isError: true }; } }
- Core helper function that gathers import dependencies from repository, file, or code snippet and generates the visualization string in the specified format.export async function generateDependencyGraph(options: { repositoryUrl?: string; filePath?: string; fileContent?: string; format: string; }): Promise<string> { const { repositoryUrl, filePath, fileContent, format } = options; // Gather the dependency data let dependencies: Record<string, string[]> = {}; if (repositoryUrl) { const repoPath = await getRepository(repositoryUrl); if (filePath) { // Analyze specific file const fullPath = path.join(repoPath, filePath); const code = fs.readFileSync(fullPath, 'utf8'); const fileLanguage = path.extname(filePath).slice(1); const analysis = analyzeCode(code, fileLanguage); dependencies[filePath] = analysis.imports; } else { // Analyze all files in repository const files = listFiles(repoPath); dependencies = gatherDependencies(repoPath, files); } } else if (fileContent) { // Analyze provided code snippet const analysis = analyzeCode(fileContent); dependencies['snippet'] = analysis.imports; } else { throw new Error("Either repositoryUrl, filePath, or fileContent must be provided"); } // Generate the visualization in the requested format switch (format) { case "mermaid": return generateMermaidDependencyGraph(dependencies); case "dot": return generateDotDependencyGraph(dependencies); case "ascii": return generateAsciiDependencyGraph(dependencies); default: throw new Error(`Unsupported format: ${format}`); } }
- Supporting function to scan all files in a repository and collect their import dependencies.function gatherDependencies(repoPath: string, files: string[]): Record<string, string[]> { const dependencies: Record<string, string[]> = {}; for (const file of files) { try { const fullPath = path.join(repoPath, file); const code = fs.readFileSync(fullPath, 'utf8'); const fileLanguage = path.extname(file).slice(1); const analysis = analyzeCode(code, fileLanguage); dependencies[file] = analysis.imports; } catch (error) { console.warn(`Error analyzing ${file}: ${(error as Error).message}`); } } return dependencies; }
- Generates a Mermaid diagram string from the collected dependencies (default format).function generateMermaidDependencyGraph(dependencies: Record<string, string[]>): string { let mermaid = "graph TD;\n"; for (const [file, imports] of Object.entries(dependencies)) { const safeFile = sanitizeId(file); // Add the file node mermaid += ` ${safeFile}["${file}"];\n`; // Add dependencies for (const importItem of imports) { const safeImport = sanitizeId(importItem); mermaid += ` ${safeFile} --> ${safeImport}["${importItem}"];\n`; } } return mermaid; }