Skip to main content
Glama
BillDuke13

Code Explainer MCP

by BillDuke13

代码解释器 MCP

Cloudflare Worker 用作代码解释的 MCP(模型上下文协议)服务器。它通过对代码结构和功能的全面细分来分析和解释代码。

执照

特征

  • 架构图:生成一个 ASCII 图表,显示整体结构、组件之间的关系和数据流。

  • 核心功能分析:根据模式识别识别并解释代码的主要目的。

  • 组件细分:列出所有主要类和功能以及它们的作用的简要说明。

  • 多语言支持:分析各种编程语言的代码,包括 JavaScript、TypeScript、Python、Java、C# 等。

  • JSDoc/Docstring 识别:提取并利用代码中的现有文档。

  • 安全 API :承载令牌认证以保护您的端点。

Related MCP server: Remote MCP Server for Website Analysis

工作原理

代码解释器使用多种技术来分析源代码:

  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 主体向您的工作者 URL 发送 POST 请求:

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

将授权标头与您的密钥一起包含在内:

Authorization: Bearer YOUR_SECRET_KEY_HERE

响应格式

响应将是一个 JSON 对象,其result字段包含代码分析:

{
  "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(Axios)

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 通过 Bearer 令牌认证进行保护

  • 使用环境机密在生产中存储共享机密

  • 不要将您的实际密钥提交给版本控制

  • 建议对生产部署进行速率限制

执照

该项目根据 Apache License 2.0 获得许可 - 有关详细信息,请参阅LICENSE文件。

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

Resources

Looking for Admin?

Admins can modify the Dockerfile, update the server description, and track usage metrics. If you are the server author, to access the admin panel.

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/BillDuke13/code-explainer-mcp'

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