Skip to main content
Glama

analyze-metrics

Analyze code metrics by specifying a repository path, enabling file-level or function-level insights with the CodeAnalysis MCP Server.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
includeFilesNoInclude file-level metrics in the output
includeFunctionsNoInclude function-level metrics in the output
repositoryPathYesPath to the repository to analyze

Implementation Reference

  • MCP tool handler for 'analyze-metrics': calls analyzeCodeMetrics, applies output filtering based on options, and returns JSON metrics or error.
    async ({ repositoryPath, includeFiles, includeFunctions }) => { try { console.log(`Analyzing code metrics in: ${repositoryPath}`); // Perform the analysis const metrics = await analyzeCodeMetrics(repositoryPath); // Filter the output based on options if (!includeFiles) { delete metrics.files; } else if (!includeFunctions) { // Remove function details from file metrics for (const file of metrics?.files ?? []) { delete file.functions; } } return { content: [{ type: "text", text: JSON.stringify(metrics, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Error analyzing code metrics: ${(error as Error).message}` }], isError: true }; }
  • Zod input schema defining parameters for the analyze-metrics tool.
    repositoryPath: z.string().describe("Path to the repository to analyze"), includeFiles: z.boolean().optional().default(true).describe("Include file-level metrics in the output"), includeFunctions: z.boolean().optional().default(false).describe("Include function-level metrics in the output") },
  • Registers the 'analyze-metrics' tool on the MCP server with schema and handler.
    export function registerCodeMetricsTools(server: McpServer) { server.tool( "analyze-metrics", { repositoryPath: z.string().describe("Path to the repository to analyze"), includeFiles: z.boolean().optional().default(true).describe("Include file-level metrics in the output"), includeFunctions: z.boolean().optional().default(false).describe("Include function-level metrics in the output") }, async ({ repositoryPath, includeFiles, includeFunctions }) => { try { console.log(`Analyzing code metrics in: ${repositoryPath}`); // Perform the analysis const metrics = await analyzeCodeMetrics(repositoryPath); // Filter the output based on options if (!includeFiles) { delete metrics.files; } else if (!includeFunctions) { // Remove function details from file metrics for (const file of metrics?.files ?? []) { delete file.functions; } } return { content: [{ type: "text", text: JSON.stringify(metrics, null, 2) }] }; } catch (error) { return { content: [{ type: "text", text: `Error analyzing code metrics: ${(error as Error).message}` }], isError: true }; } } );
  • Core helper function that performs the actual code metrics analysis: finds code files, analyzes each for lines/comments/complexity/functions, aggregates project metrics.
    export async function analyzeCodeMetrics(repositoryPath: string): Promise<ProjectMetrics> { // Find all code files const files = await findCodeFiles(repositoryPath); // Analyze each file const fileMetrics: FileMetrics[] = []; let totalComplexity = 0; for (const file of files) { const filePath = path.join(repositoryPath, file); const language = detectLanguage(file); try { const content = await fs.readFile(filePath, 'utf8'); const metrics = analyzeFileMetrics(content, language); fileMetrics.push({ filePath: file, language, ...metrics }); totalComplexity += metrics.cyclomaticComplexity; } catch (error) { console.error(`Error analyzing file ${file}:`, error); } } // Compile project-level metrics const totalLines = fileMetrics.reduce((sum, file) => sum + file.lineCount, 0); const totalCodeLines = fileMetrics.reduce((sum, file) => sum + file.codeLines, 0); const totalCommentLines = fileMetrics.reduce((sum, file) => sum + file.commentLines, 0); // Count files by language const filesByLanguage: Record<string, number> = {}; for (const file of fileMetrics) { filesByLanguage[file.language] = (filesByLanguage[file.language] || 0) + 1; } return { totalFiles: fileMetrics.length, totalLines, totalCodeLines, totalCommentLines, averageComplexity: fileMetrics.length > 0 ? totalComplexity / fileMetrics.length : 0, filesByLanguage, files: fileMetrics }; }
  • src/server.ts:44-46 (registration)
    Calls registerCodeMetricsTools to register code metrics tools including analyze-metrics during server startup.
    console.log("• Registering code metrics features..."); registerToolsOnce(registerCodeMetricsTools); console.log("✓");

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