一款命令行工具,使用Gemini Flash 2.0对给定目录中的代码文件进行总结。现在支持与LLM工具集成的MCP服务器!
.gitignore规则node_modules、dist等克隆仓库
git clone https://github.com/nicobailon/code-summarizer.git
cd code-summarizer
安装依赖项:
npm install
创建一个包含Google API密钥的.env文件:
GOOGLE_API_KEY=your_api_key_here
构建项目:
npm run build
代码总结器包括一个模型上下文协议(MCP)服务器,允许像Claude Desktop、Cursor AI和Cline这样的LLM工具访问代码总结和文件内容。
# 启动MCP服务器
npm start -- server
默认情况下,服务器运行在24312端口上。您可以在配置中更改此设置:
# 设置自定义MCP服务器端口
npm start -- config set --port 8080
~/.claude/claude_desktop_config.json(macOS/Linux)或%USERPROFILE%\.claude\claude_desktop_config.json(Windows)创建一个文件,内容如下:{
"code-summarizer": {
"command": "npx",
"args": ["-y", "your-path-to-code-summarizer/bin/code-summarizer.js", "server"],
"env": {
"GOOGLE_API_KEY": "your_api_key_here"
}
}
}
Claude Desktop示例提示:
.cursor/mcp.json文件:{
"mcpServers": {
"code-summarizer": {
"transport": "sse",
"url": "http://localhost:22312/sse",
"headers": {
"x-api-key": "your_api_key_here"
}
}
}
}
Cursor示例提示:
/mcp add code-summarizer http://localhost:24312/sse
/mcp config code-summarizer headers.x-api-key your_api_key_here
Cline示例提示:
使用MCP集成,您可以:
MCP服务器以结构化的方式向LLM工具暴露您的代码库,使它们能够读取、导航和总结您的代码,而无需手动粘贴代码片段。
code://file/* - 访问单个代码文件code://directory/* - 列出目录中的代码文件summary://file/* - 获取特定文件的总结summary://batch/* - 获取多个文件的总结summarize_file - 使用选项总结单个文件summarize_directory - 使用选项总结整个目录set_config - 更新配置选项code_summary - 用于总结代码的提示模板directory_summary - 用于总结整个目录的提示模板连接被拒绝
npm start -- server)身份验证错误
x-api-key)传输错误
/sse)权限问题
Claude Desktop找不到MCP服务器
claude_desktop_config.json中的路径是否正确速率限制
对于其他问题,请查看服务器日志或在GitHub存储库中打开一个问题。
# 默认命令(总结)
npm start -- summarize [目录] [输出文件] [选项]
# 总结当前目录中的代码(输出到summaries.txt)
npm start -- summarize
# 使用特定详细程度和最大长度总结代码
npm start -- summarize --detail high --max-length 1000
# 显示帮助
npm start -- --help
# 设置您的API密钥
npm start -- config set --api-key "your-api-key"
# 设置默认详细程度和最大长度
npm start -- config set --detail-level high --max-length 1000
# 设置MCP服务器端口(默认:24312)
npm start -- config set --port 8080
# 显示当前配置
npm start -- config show
# 重置配置到默认值
npm start -- config reset
连接到MCP服务器时,需要在请求标头中包含您的API密钥:
x-api-key: your_api_key_here
所有端点(除了健康检查)都需要通过API密钥进行身份验证。
--detail, -d:设置总结的详细程度。选项有'低'、'中'或'高'。默认是'中'。--max-length, -l:每个总结的最大长度(字符)。默认是500。x-api-key标头进行安全传输.gitignore规则。输出文件将具有以下格式:
相对路径/到/文件
总结文本在这里
相对路径/到/下一个文件
下一个总结文本在这里
index.ts:主CLI实现src/:源代码目录
summarizer/:核心总结功能mcp/:MCP服务器实现config/:配置管理bin/:CLI入口点config.json:默认配置文件tsconfig.json:TypeScript配置package.json:项目依赖项和脚本.env.example:设置环境变量的模板.gitignore:Git忽略的文件和目录__tests__:单元和集成测试__mocks__/mock-codebase:用于测试的模拟代码库可以使用以下环境变量来配置应用程序:
| 变量 | 描述 | 默认值 |
|---|---|---|
GOOGLE_API_KEY | 您的Google Gemini API密钥 | 无(必需) |
PORT | MCP服务器端口 | 24312 |
ALLOWED_ORIGINS | 允许的CORS来源的逗号分隔列表 | http://localhost:3000 |
LOG_LEVEL | 日志级别(error, warn, info, debug) | info |
参见.env.example以获取模板。
# 运行所有测试
npm test
# 运行带覆盖率的测试
npm test -- --coverage
# 测试MCP服务器设置
npm run test:setup