Labsco
nicobailon logo

Code Summarizer

โ˜… 6

from nicobailon

A command-line tool that summarizes code files in a directory using Gemini Flash 2.0.

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅโœ“ VerifiedAccount requiredAdvanced setup

Code Summarizer

A command-line tool that summarizes code files in a given directory using Gemini Flash 2.0. Now with MCP server support for integration with LLM tools!

Features

  • Recursively processes code files in a directory
  • Respects .gitignore rules
  • Skips irrelevant directories like node_modules, dist, etc.
  • Summarizes code files using Gemini Flash 2.0
  • Outputs summaries to a text file
  • Configurable detail level and summary length
  • MCP server for integration with Claude Desktop and other LLM tools
  • Modular design for easy integration into other applications
  • Secure API key management
  • Authentication for MCP server endpoints
  • Retry mechanism with exponential backoff for LLM calls
  • Rate limiting to prevent abuse

MCP Server Integration Details

MCP Resources

  • code://file/* - Access individual code files
  • code://directory/* - List code files in a directory
  • summary://file/* - Get summary for a specific file
  • summary://batch/* - Get summaries for multiple files

MCP Tools

  • summarize_file - Summarize a single file with options
  • summarize_directory - Summarize a directory with options
  • set_config - Update configuration options

MCP Prompts

  • code_summary - Prompt template for summarizing code
  • directory_summary - Prompt template for summarizing entire directories

Options

  • --detail, -d: Set the level of detail for summaries. Options are 'low', 'medium', or 'high'. Default is 'medium'.
  • --max-length, -l: Maximum length of each summary in characters. Default is 500.

Security Features

API Key Management

  • API keys are stored securely and prioritize environment variables over configuration files
  • Keys are validated for proper format before use
  • API keys are never exposed in logs or error messages
  • Configuration file doesn't store API keys when they're provided via environment variables

Authentication

  • All MCP server endpoints (except health check) require authentication via API key
  • Authentication uses the x-api-key header for secure transmission
  • Failed authentication attempts are logged for security monitoring

Rate Limiting

  • Built-in rate limiting prevents abuse of the service
  • Default: 60 requests per minute per IP address
  • Configurable through server settings

Error Handling

  • Structured error system with categorization
  • Sensitive information is never exposed in error messages
  • Proper error codes are returned for different failure scenarios

LLM Call Resilience

  • Automatic retry with exponential backoff for transient failures
  • Configurable retry settings including max retries, delays, and backoff factor
  • Jitter added to retry timing to prevent thundering herd problems
  • Request ID tracking for tracing issues across the system

Supported File Types

  • TypeScript (.ts, .tsx)
  • JavaScript (.js, .jsx)
  • Python (.py)
  • Java (.java)
  • C++ (.cpp)
  • C (.c)
  • Go (.go)
  • Ruby (.rb)
  • PHP (.php)
  • C# (.cs)
  • Swift (.swift)
  • Rust (.rs)
  • Kotlin (.kt)
  • Scala (.scala)
  • Vue (.vue)
  • HTML (.html)
  • CSS (.css, .scss, .less)

How It Works

  1. The tool scans the specified directory recursively, respecting .gitignore rules.
  2. It filters files based on supported extensions.
  3. For each supported file, it reads the content and determines the programming language.
  4. It sends the code to Gemini Flash 2.0 with a prompt to summarize, including detail level and length constraints.
  5. The summaries are collected and written to the specified output file.

Output Format

The output file will have the following format:

relative/path/to/file
Summary text here

relative/path/to/next/file
Next summary text here

Project Structure

  • index.ts: Main CLI implementation
  • src/: Source code directory
    • summarizer/: Core summarization functionality
    • mcp/: MCP server implementation
    • config/: Configuration management
  • bin/: CLI entrypoint
  • config.json: Default configuration file
  • tsconfig.json: TypeScript configuration
  • package.json: Project dependencies and scripts
  • .env.example: Template for setting up environment variables
  • .gitignore: Files and directories to ignore in Git
  • __tests__: Unit and integration tests
  • __mocks__/mock-codebase: Mock codebase for testing

Environment Variables

The following environment variables can be used to configure the application:

VariableDescriptionDefault
GOOGLE_API_KEYYour Google Gemini API keyNone (required)
PORTPort for the MCP server24312
ALLOWED_ORIGINSComma-separated list of allowed CORS originshttp://localhost:3000
LOG_LEVELLogging level (error, warn, info, debug)info

See .env.example for a template.

Development

Running Tests

# Run all tests
npm test

# Run tests with coverage
npm test -- --coverage

# Test MCP server setup
npm run test:setup

Future Improvements

  • Support for more file types
  • Support for alternative LLM providers
  • Integration with an Electron app for a GUI interface
  • Enhanced MCP server capabilities
  • Advanced token usage tracking
  • OpenTelemetry-based observability
  • Enhanced audit logging capabilities
  • Secret scanning integration