🚀 Code Lens MCP Server
Code Lens MCP Server 是一個由 Google Gemini 驅動的 MCP 服務器,可用於自動化代碼審查、分析和文檔生成,為開發者提供高效、準確的代碼處理解決方案。
🚀 快速開始
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
Docker
docker run -i --rm -e GEMINI_API_KEY="your-api-key" ghcr.io/j0hanz/code-lens
或者使用 Docker Compose:
GEMINI_API_KEY=your-api-key docker compose up
✨ 主要特性
- PR 審查流程:生成差異、評估影響、檢測 API 重大變更,並生成帶有合併建議的審查摘要。
- 文件分析:加載任何源文件以獲取重構建議、檢測代碼異味、生成文檔,並支持自然語言問答。
- 邏輯驗證:使用 Gemini 的代碼執行沙箱驗證算法。
- 結構化輸出:所有工具通過 Zod v4 輸出模式返回經過驗證的 JSON。
- 網絡搜索:使用 Google Search with Grounding 進行最新信息檢索。
- 任務生命週期支持:除
load_file 外,所有工具都可以通過 MCP 任務運行,並支持輪詢、取消和進度更新。
📦 安裝指南
環境要求
各客戶端安裝配置
在 VS Code 中安裝

添加到 .vscode/mcp.json:
{
"servers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
或者通過 CLI 安裝:
code --add-mcp '{"name":"code-lens","command":"npx","args":["-y","@j0hanz/code-lens-mcp@latest"]}'
更多信息,請參閱 VS Code MCP 文檔。
在 VS Code Insiders 中安裝

添加到 .vscode/mcp.json:
{
"servers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
或者通過 CLI 安裝:
code-insiders --add-mcp '{"name":"code-lens","command":"npx","args":["-y","@j0hanz/code-lens-mcp@latest"]}'
更多信息,請參閱 VS Code Insiders MCP 文檔。
在 Cursor 中安裝

添加到 ~/.cursor/mcp.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Cursor MCP 文檔。
在 Visual Studio 中安裝

添加到 mcp.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Visual Studio MCP 文檔。
在 Goose 中安裝

{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Goose MCP 文檔。
在 LM Studio 中安裝

{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 LM Studio MCP 文檔。
在 Claude Desktop 中安裝
添加到 claude_desktop_config.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Claude Desktop MCP 文檔。
在 Claude Code 中安裝
claude mcp add code-lens -- npx -y @j0hanz/code-lens-mcp@latest
或者添加到配置文件:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Claude Code MCP 文檔。
在 Windsurf 中安裝
添加到 ~/.codeium/windsurf/mcp_config.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Windsurf MCP 文檔。
在 Amp 中安裝
amp mcp add code-lens -- npx -y @j0hanz/code-lens-mcp@latest
或者添加到配置文件:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Amp MCP 文檔。
在 Cline 中安裝
添加到 cline_mcp_settings.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Cline MCP 文檔。
在 Codex CLI 中安裝
添加到 ~/.codex/config.yaml:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Codex CLI MCP 文檔。
在 GitHub Copilot 中安裝
添加到 .vscode/mcp.json:
{
"servers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 GitHub Copilot MCP 文檔。
在 Warp 中安裝
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Warp MCP 文檔。
在 Kiro 中安裝
添加到 .kiro/settings/mcp.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Kiro MCP 文檔。
在 Gemini CLI 中安裝
添加到 ~/.gemini/settings.json:
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Gemini CLI MCP 文檔。
在 Zed 中安裝
添加到 ~/.config/zed/settings.json:
{
"context_servers": {
"code-lens": {
"settings": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"]
}
}
}
}
更多信息,請參閱 Zed MCP 文檔。
在 Augment 中安裝
添加到 VS Code 的 settings.json 文件的 augment.advanced 部分:
{
"augment.advanced": {
"mcpServers": [
{
"id": "code-lens",
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
]
}
}
更多信息,請參閱 Augment MCP 文檔。
在 Roo Code 中安裝
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Roo Code MCP 文檔。
在 Kilo Code 中安裝
{
"mcpServers": {
"code-lens": {
"command": "npx",
"args": ["-y", "@j0hanz/code-lens-mcp@latest"],
"env": {
"GEMINI_API_KEY": "your-api-key"
}
}
}
}
更多信息,請參閱 Kilo Code MCP 文檔。
💻 使用示例
PR 審查流程
- 調用
generate_diff 捕獲未暫存或已暫存的更改。
- 運行
analyze_pr_impact 評估嚴重程度和重大變更。
- 運行
generate_review_summary 生成風險評級和合並建議。
- 運行
detect_api_breaking_changes 檢查公共 API 是否有重大變更。
- 運行
generate_test_plan 生成優先級測試用例。
單文件分析
- 調用
load_file 緩存源文件。
- 運行
refactor_code 獲取結構改進建議。
- 運行
detect_code_smells 檢測 Fowler 分類法中的反模式。
- 運行
generate_documentation 生成 JSDoc/TSDoc 存根。
- 使用
ask_about_code 對文件進行自然語言問答。
- 使用
verify_logic 通過代碼執行驗證算法。
性能審計
- 對性能敏感的更改調用
generate_diff。
- 運行
analyze_time_space_complexity 檢測大 O 複雜度退化。
研究
- 使用
web_search 通過 Google Search with Grounding 獲取最新的文檔或 API 參考。
📚 詳細文檔
架構
[MCP Client]
│
│ 傳輸方式: stdio
▼
[MCP Server: code-lens]
│ 入口: src/index.ts → src/server.ts
│
├── initialize / initialized (生命週期握手)
│
├── tools/call ──────────────────────────────────────────────
│ │
│ │ 基於差異的工具 (需要先調用 generate_diff):
│ ├── [generate_diff] 同步 — 捕獲 git 差異
│ ├── [analyze_pr_impact] 快速 — 嚴重程度和影響評估
│ ├── [generate_review_summary] 快速 — 風險和合並建議
│ ├── [generate_test_plan] 快速 — 測試用例
│ ├── [analyze_time_space_complexity] 快速 — 大 O 複雜度分析
│ ├── [detect_api_breaking_changes] 快速 — API 重大變更檢測
│ │
│ │ 基於文件的工具 (需要先調用 load_file):
│ ├── [load_file] 同步 — 緩存源文件
│ ├── [refactor_code] 快速 — 重構建議
│ ├── [detect_code_smells] 快速 — 代碼異味檢測
│ ├── [generate_documentation] 快速 — 文檔存根生成
│ ├── [ask_about_code] 快速 — 問答
│ ├── [verify_logic] 快速 — 代碼執行驗證
│ │
│ │ 獨立工具:
│ └── [web_search] 快速 — Google 搜索
│
├── resources/read ──────────────────────────────────────────
│ ├── [internal://instructions] 服務器使用指南
│ ├── [internal://tool-catalog] 工具參考
│ ├── [internal://workflows] 工作流序列
│ ├── [internal://server-config] 運行時配置
│ ├── [internal://tool-info/{name}] 每個工具的詳細信息
│ ├── [internal://diff/current] 緩存的差異 (text/x-patch)
│ └── [internal://file/current] 緩存的源文件
│
├── prompts/get ─────────────────────────────────────────────
│ ├── [get-help] 完整的服務器說明
│ ├── [review-guide] 工具 + 重點區域工作流
│ ├── [select-workflow] 根據變更類型選擇的管道
│ ├── [analyze-file] 文件分析管道
│ └── [tool-chain] 工具先決條件鏈
│
└── 功能: 結構化輸出、工具註釋、通知
請求生命週期
[客戶端] -- initialize {protocolVersion, capabilities} --> [服務器]
[服務器] -- {protocolVersion, capabilities, serverInfo} --> [客戶端]
[客戶端] -- notifications/initialized --> [服務器]
[客戶端] -- tools/call {name, arguments} --> [服務器]
[服務器] -- notifications/progress {token, progress, total} --> [客戶端]
[服務器] -- {content, structuredContent, isError?} --> [客戶端]
任務生命週期
generate_diff 和 load_file 僅支持同步。所有其他工具都支持 taskSupport: optional。
- 請求者可以提供任務 TTL。服務器使用該值,最大不超過
MAX_TASK_TTL_MS,如果未提供則使用 TASK_TTL_MS。
- 取消的任務將保持為
cancelled 狀態,並且 tasks/result 返回一個取消形狀的工具結果。
MCP 表面
工具
| 工具 |
描述 |
先決條件 |
模式 |
generate_diff |
捕獲 git 差異(未暫存/已暫存)並在服務器端緩存 |
— |
同步 |
analyze_pr_impact |
評估嚴重程度、類別、重大變更和回滾複雜度 |
generate_diff |
快速 |
generate_review_summary |
PR 摘要、風險評級和合並建議 |
generate_diff |
快速 |
generate_test_plan |
優先級測試用例和覆蓋指導 |
generate_diff |
快速 |
analyze_time_space_complexity |
大 O 複雜度分析和退化檢測 |
generate_diff |
快速 |
detect_api_breaking_changes |
檢測 API/接口的重大變更 |
generate_diff |
快速 |
load_file |
緩存源文件以供分析工具使用 |
— |
同步 |
refactor_code |
提供複雜度、重複、命名和分組方面的建議 |
load_file |
快速 |
detect_code_smells |
檢測結構代碼異味(Fowler 分類法) |
load_file |
快速 |
generate_documentation |
為公共導出生成 JSDoc/TSDoc 文檔存根 |
load_file |
快速 |
ask_about_code |
對緩存文件進行自然語言問答 |
load_file |
快速 |
verify_logic |
通過 Gemini 代碼執行沙箱驗證算法 |
load_file |
快速 |
web_search |
使用 Google Search with Grounding 進行搜索 |
— |
快速 |
資源
| URI |
描述 |
MIME 類型 |
internal://instructions |
完整的服務器使用說明 |
text/markdown |
internal://tool-catalog |
工具參考:模型、參數、輸出和數據流 |
text/markdown |
internal://workflows |
推薦的工作流和工具序列 |
text/markdown |
internal://server-config |
運行時配置和限制 |
text/markdown |
internal://tool-info/{toolName} |
每個工具的詳細信息(參數化) |
text/markdown |
internal://diff/current |
最近生成的差異 |
text/x-patch |
internal://file/current |
最近加載的源文件 |
text/plain |
提示
| 提示 |
描述 |
get-help |
完整的服務器說明:功能、工具、資源和約束 |
review-guide |
特定工具和重點區域的工作流指南 |
select-workflow |
根據變更類型推薦的工具管道 |
analyze-file |
基於目標的單文件分析工具管道 |
tool-chain |
給定工具的完整先決條件鏈 |
MCP 功能
工具註釋
所有工具都提供 MCP 工具註釋:
| 註釋 |
是否使用 |
readOnlyHint |
是 |
destructiveHint |
是 |
idempotentHint |
是 |
openWorldHint |
是 |
結構化輸出
所有由 Gemini 驅動的工具都使用 Zod v4 輸出模式,除了文本 content 之外,還返回經過驗證的 structuredContent。
配置
| 變量 |
默認值 |
描述 |
GEMINI_API_KEY |
— |
必需。Gemini API 密鑰。如果未設置,將使用 GOOGLE_API_KEY。 |
GEMINI_MODEL |
gemini-3-flash-preview |
覆蓋所有工具的默認 Gemini 模型。 |
MAX_DIFF_CHARS |
120000 |
差異的最大字符數。 |
MAX_CONCURRENT_CALLS |
10 |
最大併發 Gemini API 調用數。 |
MAX_CONCURRENT_BATCH_CALLS |
2 |
最大併發批量 Gemini 調用數。 |
MAX_CONCURRENT_CALLS_WAIT_MS |
2000 |
併發信號量的等待超時時間。 |
TASK_TTL_MS |
300000 |
當請求未指定 task.ttl 時,任務結果的默認保留時間(毫秒)。 |
MAX_TASK_TTL_MS |
3600000 |
請求提供的任務 TTL 的上限。設置為 0 可移除上限。 |
GEMINI_BATCH_MODE |
off |
啟用 Gemini 批量模式。 |
GEMINI_HARM_BLOCK_THRESHOLD |
BLOCK_NONE |
安全過濾閾值(BLOCK_NONE、BLOCK_ONLY_HIGH、BLOCK_MEDIUM_AND_ABOVE、BLOCK_LOW_AND_ABOVE)。 |
GEMINI_DIFF_CACHE_ENABLED |
false |
啟用 Gemini 大差異的上下文緩存。 |
GEMINI_DIFF_CACHE_TTL_S |
3600 |
緩存的 TTL(秒)(啟用緩存時)。 |
CLI 標誌
npx @j0hanz/code-lens-mcp@latest --model gemini-2.5-flash --max-diff-chars 200000
| 標誌 |
環境變量等效項 |
--model, -m |
GEMINI_MODEL |
--max-diff-chars |
MAX_DIFF_CHARS |
安全
| 控制措施 |
狀態 |
| 輸入驗證 |
所有工具輸入使用 Zod v4 模式驗證 |
| 路徑安全 |
load_file 將路徑限制在工作區根目錄 |
| 標準輸出安全 |
日誌記錄到標準錯誤輸出;標準輸出用於 MCP 協議 |
| 非 root 容器 |
Docker 以專用 mcp 用戶身份運行 |
開發
npm install
npm run build
npm run dev
npm run dev:run
npm run start
npm run type-check
npm run lint
npm run test
npm run format
npm run inspector
npm run knip
構建和發佈
- CI:
.github/workflows/release.yml
- Docker: 使用
node:24-alpine 進行多階段構建 (Dockerfile)
- Docker Compose:
docker-compose.yml
- npm: 發佈為
@j0hanz/code-lens-mcp
故障排除
- 缺少 API 密鑰:在環境變量或客戶端配置的
env 塊中設置 GEMINI_API_KEY 或 GOOGLE_API_KEY。
- "E_NO_DIFF" 錯誤:在運行任何基於差異的審查工具之前調用
generate_diff。
- "E_NO_FILE" 錯誤:在運行任何文件分析工具之前調用
load_file。
- 大差異被截斷:增加
MAX_DIFF_CHARS(默認值:120,000 字符)。
- 標準輸出有噪音:確保沒有其他進程寫入標準輸出;服務器使用標準輸入輸出傳輸。
致謝
📄 許可證
本項目採用 MIT 許可證。有關詳細信息,請參閱 LICENSE。歡迎通過 拉取請求 進行貢獻。