visualize-dependencies
Generate dependency diagrams for codebases to visualize relationships between files and modules, supporting multiple output formats for analysis.
Tool call template
This tool declares no required parameters. Add optional parameters from the input schema as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {},
"name": "visualize-dependencies"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repositoryUrl | No | ||
| filePath | No | ||
| path | No | ||
| fileContent | No | ||
| format | No | mermaid |
Implementation Reference
- src/features/visualization/index.ts:10-47 (registration)Registration of the visualize-dependencies MCP tool, including input schema and the handler function.
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 }; } } ); - Core handler logic for generating dependency graph visualizations from repositories, files, or code snippets by analyzing imports and producing Mermaid, DOT, or ASCII formats.
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}`); } } - Input schema using Zod for validating parameters to the visualize-dependencies tool.
{ repositoryUrl: z.string().optional(), filePath: z.string().optional(), path: z.string().optional(), fileContent: z.string().optional(), format: z.enum(["ascii", "mermaid", "dot"]).default("mermaid") - Helper function to gather dependencies from all files in a repository by analyzing their code.
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; } - Helper function to generate Mermaid format dependency graph from dependency data.
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; }