code explainer.com
code explainer.com logo

Code Explainer

Integrates with Cloudflare Workers to provide code explanation services, including architecture diagram generation, core...

Created byApr 23, 2025

Code Explainer MCP

A Cloudflare Worker that serves as an MCP (Model Context Protocol) server for code explanation. It analyzes and explains code with a comprehensive breakdown of structure and functionality.
![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)

Features

  • **Architecture Diagram**: Generates an ASCII diagram showing the overall structure, relationships between components, and data flow.
  • **Core Functionality Analysis**: Identifies and explains the primary purpose of the code based on pattern recognition.
  • **Component Breakdown**: Lists all main classes and functions with brief descriptions of their roles.
  • **Multi-language Support**: Analyzes code in various programming languages including JavaScript, TypeScript, Python, Java, C#, and more.
  • **JSDoc/Docstring Recognition**: Extracts and utilizes existing documentation in the code.
  • **Secure API**: Bearer token authentication to secure your endpoints.

How It Works

The Code Explainer analyzes source code using a combination of techniques:
  1. **Pattern Recognition**: Identifies code structures and common patterns
  1. **Relationship Analysis**: Maps dependencies between components
  1. **Documentation Extraction**: Prioritizes existing documentation comments
  1. **Architecture Visualization**: Creates ASCII diagrams of the code structure
  1. **Component Description**: Provides semantic descriptions of functions and classes
All processing happens within the Cloudflare Worker with no external dependencies.

Installation

Prerequisites

  • [Node.js](https://nodejs.org/) (version 12 or higher)
  • [Wrangler](https://developers.cloudflare.com/workers/wrangler/get-started/) (Cloudflare Workers CLI)
  • A Cloudflare account

Setup

  1. Clone this repository: ```bash git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp ```
  1. Install dependencies: ```bash npm install ```
  1. Configure your secret key: - Edit `wrangler.jsonc` and replace `YOUR_SECRET_KEY_HERE` with your chosen secret key, or - Use Cloudflare secrets (recommended for production): ```bash wrangler secret put SHARED_SECRET ```
  1. Deploy to Cloudflare Workers: ```bash npm run deploy ```

Usage

API Endpoint

Send a POST request to your worker URL with the following JSON body:
Include the Authorization header with your secret key:

Response Format

The response will be a JSON object with a `result` field containing the code analysis:

Example Usage

JavaScript (Browser)

Python (Requests)

Node.js (Axios)

Local Development

  1. Clone the repository and install dependencies: ```bash git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp npm install ```
  1. Run the development server: ```bash wrangler dev ```
  1. Test the endpoint locally: ```bash curl -X POST http://localhost:8787 \ -H "Content-Type: application/json" \ -H "Authorization: Bearer YOUR_SECRET_KEY_HERE" \ -d '{"method":"explainCode","params":["function hello() { return \"Hello World\"; }","javascript"]}' ```

Development Guidelines

  • Follow TypeScript best practices
  • Add comments for complex logic
  • Update documentation for public API changes
  • Add tests for new features

Security

  • The API is secured with Bearer token authentication
  • Use environment secrets for storing the shared secret in production
  • Do not commit your actual secret key to version control
  • Rate limiting is recommended for production deployments

License

This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.