Code Explainer MCP
代码解释器 MCP
Cloudflare Worker 用作代码解释的 MCP(模型上下文协议)服务器。它通过对代码结构和功能的全面细分来分析和解释代码。
特征
架构图:生成一个 ASCII 图表,显示整体结构、组件之间的关系和数据流。
核心功能分析:根据模式识别识别并解释代码的主要目的。
组件细分:列出所有主要类和功能以及它们的作用的简要说明。
多语言支持:分析各种编程语言的代码,包括 JavaScript、TypeScript、Python、Java、C# 等。
JSDoc/Docstring 识别:提取并利用代码中的现有文档。
安全 API :承载令牌认证以保护您的端点。
Related MCP server: Smart Code Reviewer
工作原理
代码解释器使用多种技术来分析源代码:
模式识别:识别代码结构和常见模式
关系分析:映射组件之间的依赖关系
文档提取:优先考虑现有文档注释
架构可视化:创建代码结构的 ASCII 图表
组件描述:提供函数和类的语义描述
所有处理都在 Cloudflare Worker 内部进行,无需任何外部依赖。
安装
先决条件
设置
克隆此存储库:
git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp安装依赖项:
npm install配置你的密钥:
编辑
wrangler.jsonc并将YOUR_SECRET_KEY_HERE替换为你选择的密钥,或者使用 Cloudflare 机密(推荐用于生产):
wrangler secret put SHARED_SECRET
部署到 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));本地开发
克隆存储库并安装依赖项:
git clone https://github.com/BillDuke13/code-explainer-mcp.git cd code-explainer-mcp npm install运行开发服务器:
wrangler dev在本地测试端点:
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文件。
This server cannot be deployed
Maintenance
Related MCP Connectors
Architecture diagrams, explanations and Mermaid source for public GitHub repositories.
Code intelligence platform for AI agents. 20 tools for architecture, security & impact analysis.
AI-powered codebase analysis — call graphs, security, dead code, complexity. 150+ tools.
Code intelligence for LLMs. Analyze, search, and retrieve code from any public git repository.
Related MCP Servers
- AlicenseNot gradedqualityBmaintenanceConverts code into UML diagrams and flowcharts through static analysis, enabling visualization of code structure and explanation of functionality.39MIT
- AlicenseAqualityDmaintenanceEnables comprehensive code analysis including quality assessment, security vulnerability detection, refactoring suggestions, complexity calculations, and automatic documentation generation for multiple programming languages.56 npmMIT
- AlicenseNot gradedqualityDmaintenanceAnalyzes codebases to automatically generate README, API docs, architecture diagrams, and CHANGELOG.4 npmMIT
- FlicenseAqualityDmaintenanceAnalyzes any codebase to detect project type, languages, frameworks, entry points, API routes, controllers, services, components, tests, and more.5-