一个作为MCP(模型上下文协议)服务器的Cloudflare Worker,用于代码解释。它分析并解释代码,提供全面的结构和功能分解。
Code Explainer 使用以下组合技术分析源代码:
所有处理都在Cloudflare Worker中进行,没有外部依赖。
克隆此仓库:
git clone https://github.com/BillDuke13/code-explainer-mcp.git
cd code-explainer-mcp
安装依赖项:
npm install
配置您的密钥:
wrangler.jsonc 并将 YOUR_SECRET_KEY_HERE 替换为您选择的密钥,或者wrangler secret put SHARED_SECRET
部署到Cloudflare Workers:
npm run deploy
向您的worker URL发送POST请求,请求体如下所示的JSON:
{
"method": "explainCode",
"params": ["your code here", "programming language"]
}
在请求头中包含授权信息,使用您的密钥:
Authorization: Bearer YOUR_SECRET_KEY_HERE
响应将是一个包含代码分析结果的JSON对象:
{
"result": "# 代码分析:JavaScript代码\n\n## 架构图\n...\n\n## 核心功能\n..."
}
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错误!状态码:${response.status}`);
}
const data = await response.json();
return data.result;
}
// 示例用法
const jsCode = `function add(a, b) { return a + b; }`;
explainCode(jsCode, "javascript")
.then(explanation => console.log(explanation))
.catch(error => console.error('错误:', error));
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']
# 示例用法
code = "def hello(): print('Hello, world!')"
explanation = explain_code(code, "python", "https://your-worker-url.workers.dev", "YOUR_SECRET_KEY_HERE")
print(explanation)
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.response ? error.response.data : error.message);
throw error;
}
}
// 示例用法
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('无法解释代码:', 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"]}'
本项目根据Apache许可证2.0版发布 - 详情见LICENSE文件。