/ 目錄 / 演練場 / SonarQube MCP
● 官方 SonarSource 🔑 需要你的金鑰

SonarQube MCP

作者 SonarSource · SonarSource/sonarqube-mcp-server

Sonar 靜態分析結果交到 Claude 手中——依檔案和規則修正問題,而非瀏覽儀表板。

sonarqube-mcp-server 是 SonarQube 和 SonarCloud 的官方 MCP。它將議題、安全熱點、品質閘門、度量指標和程式碼品質歷史紀錄作為 MCP 工具暴露出來。Claude 讀取議題、查閱規則,並根據規則的建議撰寫修正方案——而非泛泛的猜測。

為什麼要用

核心特性

即時演示

實際使用效果

sonarqube-mcp.replay ▶ 就緒
0/0

安裝

選擇你的客戶端

~/Library/Application Support/Claude/claude_desktop_config.json  · Windows: %APPDATA%\Claude\claude_desktop_config.json
{
  "mcpServers": {
    "sonarqube-mcp": {
      "command": "docker",
      "args": [
        "run",
        "--init",
        "--pull=always",
        "-i",
        "--rm",
        "-e",
        "SONARQUBE_TOKEN",
        "-e",
        "SONARQUBE_ORG",
        "mcp/sonarqube"
      ]
    }
  }
}

開啟 Claude Desktop → Settings → Developer → Edit Config。儲存後重啟應用。

~/.cursor/mcp.json · .cursor/mcp.json
{
  "mcpServers": {
    "sonarqube-mcp": {
      "command": "docker",
      "args": [
        "run",
        "--init",
        "--pull=always",
        "-i",
        "--rm",
        "-e",
        "SONARQUBE_TOKEN",
        "-e",
        "SONARQUBE_ORG",
        "mcp/sonarqube"
      ]
    }
  }
}

Cursor 使用與 Claude Desktop 相同的 mcpServers 格式。專案級設定優先於全域。

VS Code → Cline → MCP Servers → Edit
{
  "mcpServers": {
    "sonarqube-mcp": {
      "command": "docker",
      "args": [
        "run",
        "--init",
        "--pull=always",
        "-i",
        "--rm",
        "-e",
        "SONARQUBE_TOKEN",
        "-e",
        "SONARQUBE_ORG",
        "mcp/sonarqube"
      ]
    }
  }
}

點擊 Cline 側欄中的 MCP Servers 圖示,然後選 "Edit Configuration"。

~/.codeium/windsurf/mcp_config.json
{
  "mcpServers": {
    "sonarqube-mcp": {
      "command": "docker",
      "args": [
        "run",
        "--init",
        "--pull=always",
        "-i",
        "--rm",
        "-e",
        "SONARQUBE_TOKEN",
        "-e",
        "SONARQUBE_ORG",
        "mcp/sonarqube"
      ]
    }
  }
}

格式與 Claude Desktop 相同。重啟 Windsurf 生效。

~/.continue/config.json
{
  "mcpServers": [
    {
      "name": "sonarqube-mcp",
      "command": "docker",
      "args": [
        "run",
        "--init",
        "--pull=always",
        "-i",
        "--rm",
        "-e",
        "SONARQUBE_TOKEN",
        "-e",
        "SONARQUBE_ORG",
        "mcp/sonarqube"
      ]
    }
  ]
}

Continue 使用伺服器物件陣列,而非映射。

~/.config/zed/settings.json
{
  "context_servers": {
    "sonarqube-mcp": {
      "command": {
        "path": "docker",
        "args": [
          "run",
          "--init",
          "--pull=always",
          "-i",
          "--rm",
          "-e",
          "SONARQUBE_TOKEN",
          "-e",
          "SONARQUBE_ORG",
          "mcp/sonarqube"
        ]
      }
    }
  }
}

加入 context_servers。Zed 儲存後熱重載。

claude mcp add sonarqube-mcp -- docker run --init --pull=always -i --rm -e SONARQUBE_TOKEN -e SONARQUBE_ORG mcp/sonarqube

一行命令搞定。用 claude mcp list 驗證,claude mcp remove 移除。

使用場景

實戰用法: SonarQube MCP

批次修正程式碼庫中某一規則的所有議題

👤 正在償還技術債的開發者 ⏱ ~45 min intermediate

何時使用: Sonar 標出同一條規則的 47 個實例,你希望在一個 PR 中全部修完。

前置條件
  • Sonar token — 使用者 → 安全性 → 生成 Token(盡可能限定至專案範圍)
  • Docker 可用 — 標準 docker 設定;首次執行時會拉取映像
步驟
  1. 找出規則
    Use sonarqube-mcp. Top 5 rules by open-issue count in project 'foo'.✓ 已複製
    → 規則 key + 數量 + 嚴重性
  2. 拉取該規則的所有實例
    List every open issue for rule java:S2293 (Use Diamond Operator) — file, line, snippet.✓ 已複製
    → 47 個議題,含 file:line 和周邊程式碼
  3. 套用修正
    For each, apply the rule's recommended fix. Keep changes minimal; don't reformat.✓ 已複製
    → 約 20 個檔案中的 47 個小幅修改
  4. 重新掃描驗證
    After my CI runs the next analysis, recheck the issue count for that rule.✓ 已複製
    → 數量降至 0

結果: 一個專注的 PR 關閉整條規則,而非 47 個分散的修正提交。

注意事項
  • 規則自動修正破壞了自動生成的程式碼 — 依目錄篩選;跳過生成的路徑或手動稽核
搭配使用: github · filesystem

偵錯 PR 品質閘門失敗的原因

👤 PR 被封鎖的開發者 ⏱ ~30 min intermediate

何時使用: Sonar 讓你的 PR 失敗,而儀表板令人不知所措。

步驟
  1. 取得閘門狀態
    Use sonarqube-mcp. Show quality gate status for PR 1234 — which conditions failed?✓ 已複製
    → 閘門細分:例如「新覆蓋率 67% < 80% 閾值」
  2. 深入失敗條件
    For 'New Coverage' below threshold — list new lines added in this PR with no test coverage.✓ 已複製
    → 各檔案未覆蓋的新增行清單
  3. 生成測試
    Write tests covering those uncovered lines.✓ 已複製
    → 新增測試檔案;各案例的思路說明

結果: PR 以有針對性的測試解除封鎖,而非地毯式的覆蓋率轟炸。

注意事項
  • 測試是為了覆蓋率而寫,而非為了行為 — 要求 Claude 說明每個測試在斷言什麼;拒絕沒有真正斷言的測試
搭配使用: github

審查並解決安全熱點

👤 注重安全的工程師 ⏱ ~40 min advanced

何時使用: 熱點已積累,需要分類為「已審查安全」或「需要修正」。

步驟
  1. 列出未解決的熱點
    Use sonarqube-mcp. List all unreviewed security hotspots in project 'foo' grouped by category.✓ 已複製
    → 依類別分組的熱點(例如「弱加密」、「身份驗證」)
  2. 說明風險
    For each, summarize the risk in 1 line. Mark which are clearly safe-to-acknowledge vs need-fix.✓ 已複製
    → 附有標記的分類清單
  3. 修正或確認
    For need-fix ones, apply the rule's safe pattern. For safe ones, give me the comment to leave on the hotspot review.✓ 已複製
    → 修正已套用 + 審查留言草稿

結果: 熱點附有稽核軌跡地被清除,而非靜默忽視。

注意事項
  • 不假思索地標記為「安全」 — 要求 Claude 在留言中說明*為什麼*安全——這會建立可稽核的軌跡
搭配使用: github

組合

與其他 MCP 搭配,撬動十倍槓桿

sonarqube-mcp + github

在 PR 上標注 Sonar 發現,或自動為新違規建立議題

On every PR, comment Sonar's gate result and any new issues it added.✓ 已複製
sonarqube-mcp + filesystem

在本機套用規則修正並提交

Fix all java:S2293 instances; write the diffs locally for review before commit.✓ 已複製

工具

此 MCP 暴露的能力

工具輸入參數何時呼叫成本
search_issues project, rules?, severities?, types?, branch? 依規則/嚴重性/類型篩選議題 1 次 SonarQube API 呼叫
get_rule rule_key 修正前先讀取官方指引 1 次 API 呼叫
get_quality_gate project, branch_or_pr 診斷 PR/分支閘門失敗 1 次 API 呼叫
search_hotspots project, status? 安全審查 1 次 API 呼叫
get_metrics project, metric_keys[] 趨勢/漂移檢查(覆蓋率、重複程式碼) 1 次 API 呼叫

成本與限制

運行它的成本

API 配額
Sonar API 速率限制依版本而異——批次迴圈請保守處理
每次呼叫 Token 數
議題以每頁 100 個分頁回傳;每個議題約 200 token
費用
SonarCloud 公開專案免費;SonarQube 需要 Enterprise 授權
提示
在 search_issues 時積極篩選——翻遍所有頁面是浪費

安全

權限、密鑰、影響範圍

最小權限: Browse See Source Code
憑證儲存: SONARQUBE_TOKEN 環境變數,限定至專案範圍
資料出站: 連至你的 Sonar 實例(sonarcloud.io 或自架版本)
切勿授予: 對聊天驅動的 agent 授予管理權限

故障排查

常見錯誤與修復

401 Unauthorized

重新生成 token;確認 token 不是只有使用者範圍而需要專案範圍

驗證: curl -u $SONARQUBE_TOKEN: $SONAR_HOST/api/projects/search
組織錯誤

SONARQUBE_ORG 環境變數必須完全符合你的 Sonar 組織 slug(小寫)

Docker 容器無法存取自架 SonarQube

傳入 --network host,或將 SonarQube 暴露在可存取的主機名稱;在容器內設定 SONAR_HOST_URL

驗證: docker run ... curl $SONAR_HOST_URL/api/system/status
議題清單看起來是舊的

Sonar 反映的是最後一次完成的分析——重新執行分析或查看專案的最後分析時間戳記

替代方案

SonarQube MCP 對比其他方案

替代方案何時用它替代權衡
CodeQL via GitHub MCP你已使用 GitHub Advanced Security,且希望在同一介面看到發現不同的規則集;沒有品質閘門概念
Semgrep MCP你想要不需要 Sonar 基礎架構的輕量級模式掃描對程式碼異味/可維護性度量指標的覆蓋較少

更多

資源

📖 閱讀 GitHub 上的官方 README

🐙 查看未解決的 issue

🔍 瀏覽全部 400+ MCP 伺服器和 Skills