Code Explainer MCP

remote-capable server

The server can be hosted and run remotely because it primarily relies on remote services or has no dependency on the local environment.

Integrations

  • Provides an example client implementation for making API requests to the code explainer from Node.js applications

  • Serves as the deployment platform for the code explainer MCP, enabling serverless execution of the code analysis functionality

  • Offers command-line example for testing the code explanation endpoint during local development

コード解説者MCP

コード解説のためのMCP(Model Context Protocol)サーバーとして機能するCloudflare Worker。コードの構造と機能を包括的に分析・解説します。

特徴

  • アーキテクチャ ダイアグラム: 全体的な構造、コンポーネント間の関係、およびデータ フローを示す ASCII ダイアグラムを生成します。
  • コア機能分析: パターン認識に基づいてコードの主な目的を識別し、説明します。
  • コンポーネントの内訳: すべての主要なクラスと関数を、その役割の簡単な説明とともに一覧表示します。
  • 多言語サポート: JavaScript、TypeScript、Python、Java、C# など、さまざまなプログラミング言語のコードを分析します。
  • JSDoc/Docstring 認識: コード内の既存のドキュメントを抽出して利用します。
  • セキュア API : エンドポイントを保護するためのベアラー トークン認証。

仕組み

Code Explainer は、次のような技術を組み合わせてソース コードを分析します。

  1. パターン認識: コード構造と共通パターンを識別します
  2. 関係分析: コンポーネント間の依存関係をマッピングします
  3. ドキュメント抽出: 既存のドキュメントコメントを優先します
  4. アーキテクチャの視覚化: コード構造のASCIIダイアグラムを作成します
  5. コンポーネントの説明: 関数とクラスの意味的な説明を提供します

すべての処理は外部依存なしに Cloudflare Worker 内で行われます。

インストール

前提条件

  • Node.js (バージョン 12 以上)
  • Wrangler (Cloudflare Workers CLI)
  • Cloudflareアカウント

設定

  1. このリポジトリをクローンします:
    git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp
  2. 依存関係をインストールします:
    npm install
  3. 秘密鍵を設定します:
    • wrangler.jsoncを編集し、 YOUR_SECRET_KEY_HEREを選択した秘密鍵に置き換えるか、
    • Cloudflare シークレットを使用する (本番環境では推奨):
      wrangler secret put SHARED_SECRET
  4. Cloudflare Workers にデプロイする:
    npm run deploy

使用法

APIエンドポイント

次の JSON 本文を含む POST リクエストをワーカー URL に送信します。

{ "method": "explainCode", "params": ["your code here", "programming language"] }

秘密キーを含む Authorization ヘッダーを追加します。

Authorization: Bearer YOUR_SECRET_KEY_HERE

応答フォーマット

応答は、コード分析を含むresultフィールドを持つ JSON オブジェクトになります。

{ "result": "# Code Analysis for JavaScript Code\n\n## Architecture Diagram\n...\n\n## Core Functionality\n..." }

使用例

JavaScript(ブラウザ)

async function explainCode(code, language) { const response = await fetch('https://your-worker-url.workers.dev', { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_SECRET_KEY_HERE', }, body: JSON.stringify({ method: "explainCode", params: [code, language] }), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const data = await response.json(); return data.result; } // Example usage const jsCode = `function add(a, b) { return a + b; }`; explainCode(jsCode, "javascript") .then(explanation => console.log(explanation)) .catch(error => console.error('Error:', error));

Python(リクエスト)

import requests import json def explain_code(code, language, api_url, secret_key): headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {secret_key}' } payload = { 'method': 'explainCode', 'params': [code, language] } response = requests.post(api_url, headers=headers, json=payload) response.raise_for_status() return response.json()['result'] # Example usage code = "def hello(): print('Hello, world!')" explanation = explain_code(code, "python", "https://your-worker-url.workers.dev", "YOUR_SECRET_KEY_HERE") print(explanation)

Node.js(アクシオス)

const axios = require('axios'); async function explainCode(code, language) { try { const response = await axios.post('https://your-worker-url.workers.dev', { method: 'explainCode', params: [code, language] }, { headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer YOUR_SECRET_KEY_HERE' } }); return response.data.result; } catch (error) { console.error('Error:', error.response ? error.response.data : error.message); throw error; } } // Example usage const codeToAnalyze = ` class Person { constructor(name) { this.name = name; } sayHello() { return \`Hello, my name is \${this.name}\`; } } `; explainCode(codeToAnalyze, 'javascript') .then(explanation => console.log(explanation)) .catch(err => console.error('Failed to explain code:', err));

地域開発

  1. リポジトリをクローンし、依存関係をインストールします。
    git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp npm install
  2. 開発サーバーを実行します。
    wrangler dev
  3. エンドポイントをローカルでテストします。
    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"]}'

開発ガイドライン

  • TypeScriptのベストプラクティスに従う
  • 複雑なロジックにコメントを追加する
  • 公開APIの変更に関するドキュメントの更新
  • 新機能のテストを追加する

安全

  • APIはベアラートークン認証で保護されています
  • 本番環境で共有シークレットを保存するには環境シークレットを使用します
  • 実際の秘密鍵をバージョン管理にコミットしないでください
  • 本番環境での導入にはレート制限が推奨されます

ライセンス

このプロジェクトは、Apache License 2.0 に基づいてライセンスされています。詳細については、 LICENSEファイルを参照してください。

-
security - not tested
A
license - permissive license
-
quality - not tested

ソースコードを分析して、アーキテクチャ図、コア機能分析、複数のプログラミング言語にわたるコンポーネントの内訳などの包括的な説明を提供する Cloudflare Worker。

  1. Features
    1. How It Works
      1. Installation
        1. Prerequisites
        2. Setup
      2. Usage
        1. API Endpoint
        2. Response Format
        3. Example Usage
      3. Local Development
        1. Development Guidelines
      4. Security
        1. License
          ID: n61qu4bdbv