Skip to main content
Glama

visualize-code-structure

Generate visual representations of code structure by analyzing repository URLs, file paths, or content. Customize outputs with methods, attributes, and formats like ASCII, Mermaid, or DOT for clear code analysis.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
fileContentNo
filePathNo
formatNomermaid
repositoryUrlNo
showAttributesNo
showMethodsNo

Implementation Reference

  • Registers the 'visualize-code-structure' MCP tool, defining its input schema and inline execution handler that delegates to generateCodeStructureVisualization
    server.tool( "visualize-code-structure", { repositoryUrl: z.string().optional(), filePath: z.string().optional(), fileContent: z.string().optional(), showMethods: z.boolean().default(true), showAttributes: z.boolean().default(true), format: z.enum(["ascii", "mermaid", "dot"]).default("mermaid") }, async ({ repositoryUrl, filePath, fileContent, showMethods, showAttributes, format }) => { try { const visualization = await generateCodeStructureVisualization({ repositoryUrl, filePath, fileContent, showMethods, showAttributes, 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 function that fetches code from repository or input, analyzes it using analyzeCode, and generates structure visualization (class/function diagram) in mermaid/dot/ascii format
    export async function generateCodeStructureVisualization(options: { repositoryUrl?: string; filePath?: string; fileContent?: string; showMethods: boolean; showAttributes: boolean; format: string; }): Promise<string> { const { repositoryUrl, filePath, fileContent, showMethods, showAttributes, format } = options; // Get the code to analyze let code: string; let language: string = ""; if (repositoryUrl) { const repoPath = await getRepository(repositoryUrl); if (!filePath) { throw new Error("filePath must be provided when repositoryUrl is specified"); } const fullPath = path.join(repoPath, filePath); code = fs.readFileSync(fullPath, 'utf8'); language = path.extname(filePath).slice(1); } else if (fileContent) { code = fileContent; } else { throw new Error("Either repositoryUrl with filePath, or fileContent must be provided"); } // Analyze the code structure const analysis = analyzeCode(code, language); // Generate the visualization in the requested format switch (format) { case "mermaid": return generateMermaidClassDiagram(analysis, { showMethods, showAttributes }); case "dot": return generateDotClassDiagram(analysis, { showMethods, showAttributes }); case "ascii": return generateAsciiClassDiagram(analysis, { showMethods, showAttributes }); default: throw new Error(`Unsupported format: ${format}`); } }
  • Helper function to generate Mermaid class diagrams from code analysis results (used for default 'mermaid' format)
    function generateMermaidClassDiagram(analysis: any, options: { showMethods: boolean, showAttributes: boolean }): string { const { classes, functions } = analysis; const { showMethods, showAttributes } = options; let mermaid = "classDiagram\n"; // Add classes for (const className of classes) { mermaid += ` class ${className} {\n`; // Add attributes and methods if available and requested if (showAttributes) { mermaid += ` +attribute: type\n`; // Placeholder - would need actual analysis } if (showMethods) { mermaid += ` +method()\n`; // Placeholder - would need actual analysis } mermaid += " }\n"; } // Add standalone functions if no classes if (classes.length === 0 && functions.length > 0) { mermaid += ` class Functions {\n`; for (const func of functions) { mermaid += ` +${func}()\n`; } mermaid += " }\n"; } return mermaid; }
  • src/server.ts:64-66 (registration)
    Main server registration call that invokes registerVisualizationFeatures to add visualization tools including 'visualize-code-structure'
    console.log("• Registering visualization features..."); registerToolsOnce(registerVisualizationFeatures); console.log("✓");
  • Zod schema defining input parameters for the 'visualize-code-structure' tool
    { repositoryUrl: z.string().optional(), filePath: z.string().optional(), fileContent: z.string().optional(), showMethods: z.boolean().default(true), showAttributes: z.boolean().default(true), format: z.enum(["ascii", "mermaid", "dot"]).default("mermaid") },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/0xjcf/MCP_CodeAnalysis'

If you have feedback or need assistance with the MCP directory API, please join our Discord server