Skip to main content
Glama

analyze_code

Analyze code files to extract statistics like line counts and function totals, supporting language-specific filtering for targeted insights.

Instructions

Analyzes code files and provides statistics.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesThe target file or directory path.
languageNoOptional language filter.
countLinesNoWhether to count lines of code.
countFunctionsNoWhether to count functions.

Implementation Reference

  • The primary handler function `handleRequest` that implements the core logic of the `analyze_code` tool: resolves paths, lists files recursively if directory, filters by language, reads contents, counts lines and functions per language heuristics, and returns aggregated statistics.
    async function handleRequest(parameters) {
        console.error('analyze_code: Starting execution');
        const startTime = Date.now();
    
        const { path: targetPath, language, countLines = true, countFunctions = true } = parameters;
        if (!targetPath) {
            throw new Error("Missing required parameter: 'path'.");
        }
    
        // Resolve to absolute path
        const absolutePath = path.resolve(targetPath);
        console.error(`analyze_code: Resolved path to ${absolutePath}`);
    
        let rootPath = absolutePath; // Assume path is directory initially
        let filesToProcess = [];
    
        // Validate path existence and determine if it's a file or directory
        try {
            const stats = await fs.stat(absolutePath);
            console.error(`analyze_code: Path exists, checking type`);
    
            if (stats.isDirectory()) {
                // Path is a directory, list files within it
                console.error(`analyze_code: Path is a directory, listing files`);
                rootPath = absolutePath; // Keep rootPath as the directory itself
    
                // Get all files in the directory recursively
                filesToProcess = await listFilesRecursive(absolutePath);
                
                // Filter by language if specified
                if (language) {
                    const extensions = getExtensionsForLanguage(language);
                    filesToProcess = filesToProcess.filter(file => {
                        const ext = path.extname(file).toLowerCase();
                        return extensions.includes(ext);
                    });
                }
                
                console.error(`analyze_code: Found ${filesToProcess.length} files to analyze`);
    
            } else if (stats.isFile()) {
                // Path is a single file
                console.error(`analyze_code: Path is a file`);
                rootPath = path.dirname(absolutePath); // Set rootPath to the parent directory
                const relativeFilePath = path.basename(absolutePath);
    
                // Check if language filter applies
                if (language) {
                    const extensions = getExtensionsForLanguage(language);
                    const ext = path.extname(relativeFilePath).toLowerCase();
                    if (!extensions.includes(ext)) {
                        console.error(`analyze_code: File ${relativeFilePath} does not match language filter ${language}`);
                        filesToProcess = []; // Skip if language doesn't match
                    } else {
                        filesToProcess.push(relativeFilePath);
                    }
                } else {
                    filesToProcess.push(relativeFilePath);
                }
                
                console.error(`analyze_code: Added single file to process: ${relativeFilePath}`);
            } else {
                // Path exists but is not a file or directory (e.g., socket, fifo)
                throw new Error(`Path '${targetPath}' is not a file or directory.`);
            }
        } catch (error) {
            if (error.code === 'ENOENT') {
                throw new Error(`Path '${targetPath}' not found.`);
            }
            throw new Error(`Error accessing path '${targetPath}': ${error.message}`);
        }
    
        if (filesToProcess.length === 0) {
            // If no files are left after filtering
            console.error(`analyze_code: No files to process, returning empty analysis`);
            return { 
                analysis: {
                    totalFiles: 0,
                    totalLines: 0,
                    totalFunctions: 0,
                    fileBreakdown: []
                }
            };
        }
    
        // Read the content of the files
        console.error(`analyze_code: Reading content of ${filesToProcess.length} files`);
        const fileContentsMap = await readFiles(filesToProcess, rootPath);
    
        // Analyze the files
        console.error(`analyze_code: Analyzing files`);
        const analysis = {
            totalFiles: filesToProcess.length,
            totalLines: 0,
            totalFunctions: 0,
            fileBreakdown: []
        };
    
        // Sort file paths before analyzing for deterministic output
        const sortedRelativePaths = [...fileContentsMap.keys()].sort((a, b) => a.localeCompare(b));
    
        for (const relativeFilePath of sortedRelativePaths) {
            const content = fileContentsMap.get(relativeFilePath);
            if (content !== undefined) { // Check if file read was successful
                const fileAnalysis = {
                    file: relativeFilePath,
                    lines: 0,
                    functions: 0
                };
    
                // Count lines if requested
                if (countLines) {
                    fileAnalysis.lines = content.split('\n').length;
                    analysis.totalLines += fileAnalysis.lines;
                }
    
                // Count functions if requested
                if (countFunctions) {
                    fileAnalysis.functions = countFunctionsInCode(content, path.extname(relativeFilePath));
                    analysis.totalFunctions += fileAnalysis.functions;
                }
    
                analysis.fileBreakdown.push(fileAnalysis);
            }
        }
    
        const executionTime = Date.now() - startTime;
        console.error(`analyze_code: Execution completed in ${executionTime}ms`);
    
        return {
            analysis
        };
    }
  • Registration of the `analyze_code` tool using `server.tool()`, including the tool name, description, Zod input schema, and wrapper that calls the handler and adapts the result.
    // Register the analyze_code tool
    if (analyzeCodeHandler) {
        server.tool(
            'analyze_code',
            'Analyzes code files and provides statistics.',
            {
                path: z.string().describe('The target file or directory path.'),
                language: z.string().optional().describe('Optional language filter.'),
                countLines: z.boolean().optional().describe('Whether to count lines of code.'),
                countFunctions: z.boolean().optional().describe('Whether to count functions.')
            },
            async (params) => {
                logInfo(`Executing analyze_code tool with params: ${JSON.stringify(params)}`);
                try {
                    const startTime = Date.now();
                    const result = await analyzeCodeHandler(params);
                    const executionTime = Date.now() - startTime;
                    logDebug(`analyze_code completed in ${executionTime}ms`);
                    return adaptToolResult(result);
                } catch (error) {
                    logError('Error in analyze_code tool:', error);
                    throw error;
                }
            }
        );
    }
  • Zod schema defining the input parameters for the `analyze_code` tool: path (required), language (optional), countLines and countFunctions (optional booleans).
    {
        path: z.string().describe('The target file or directory path.'),
        language: z.string().optional().describe('Optional language filter.'),
        countLines: z.boolean().optional().describe('Whether to count lines of code.'),
        countFunctions: z.boolean().optional().describe('Whether to count functions.')
    },
  • `countFunctionsInCode` helper: language-specific regex-based function counting (JS/TS arrow funcs/methods, Python def, Java methods).
    function countFunctionsInCode(code, fileExtension) {
        // This is a very simple implementation and won't catch all functions
        // A real implementation would use language-specific parsers
        let count = 0;
        
        // JavaScript/TypeScript
        if (['.js', '.jsx', '.ts', '.tsx', '.mjs'].includes(fileExtension.toLowerCase())) {
            // Count function declarations
            const functionMatches = code.match(/function\s+\w+\s*\(/g) || [];
            count += functionMatches.length;
            
            // Count arrow functions
            const arrowMatches = code.match(/\w+\s*=\s*\([^)]*\)\s*=>/g) || [];
            count += arrowMatches.length;
            
            // Count method definitions
            const methodMatches = code.match(/\w+\s*\([^)]*\)\s*{/g) || [];
            count += methodMatches.length;
        }
        // Python
        else if (['.py', '.pyw'].includes(fileExtension.toLowerCase())) {
            const functionMatches = code.match(/def\s+\w+\s*\(/g) || [];
            count += functionMatches.length;
        }
        // Java
        else if (['.java'].includes(fileExtension.toLowerCase())) {
            // This is a very simplified approach
            const methodMatches = code.match(/\w+\s+\w+\s*\([^)]*\)\s*{/g) || [];
            count += methodMatches.length;
        }
        
        return count;
    }
  • `listFilesRecursive` helper: recursively lists all files in a directory, returning relative paths.
    async function listFilesRecursive(dir, baseDir = dir, result = []) {
        const entries = await fs.readdir(dir, { withFileTypes: true });
        
        for (const entry of entries) {
            const fullPath = path.join(dir, entry.name);
            const relativePath = path.relative(baseDir, fullPath);
            
            if (entry.isDirectory()) {
                await listFilesRecursive(fullPath, baseDir, result);
            } else if (entry.isFile()) {
                result.push(relativePath);
            }
        }
        
        return result;
    }
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden of behavioral disclosure. It states the tool 'analyzes code files and provides statistics,' implying a read-only operation, but doesn't specify what types of statistics, whether it's resource-intensive, if it handles errors gracefully, or what the output format looks like. This leaves significant gaps in understanding the tool's behavior beyond the basic purpose.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence: 'Analyzes code files and provides statistics.' It is front-loaded with the core purpose and contains no unnecessary words or redundancy, making it highly concise and well-structured.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the complexity of a code analysis tool with 4 parameters and no annotations or output schema, the description is incomplete. It lacks details on behavioral traits, output format, error handling, and usage guidelines. While the schema covers parameters, the overall context for effective tool invocation is insufficient, especially for a tool that likely produces varied statistical results.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all four parameters (path, language, countLines, countFunctions) with clear descriptions. The description adds no additional meaning or context about the parameters beyond what's in the schema, such as examples or usage notes. This meets the baseline for high schema coverage.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Analyzes code files and provides statistics.' This specifies the verb ('analyzes') and resource ('code files') with the outcome ('provides statistics'). It distinguishes from sibling tools like 'get_file_tree' (which lists files) and 'merge_content' (which combines content), but doesn't explicitly differentiate beyond the general domain of code analysis.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. It doesn't mention sibling tools, prerequisites, or specific contexts for usage. The agent must infer usage from the purpose alone, which is insufficient for optimal tool selection.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

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/yy1588133/code-merge-mcp'

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