SonarQube
官方提供與 SonarQube Server 或 Cloud 的無縫整合,並能在代理程式上下文中直接分析程式碼片段
你可以用 SonarQube MCP 做什麼?
- 連線至 SonarQube Cloud 或 Server — 使用權杖、組織金鑰或伺服器 URL 設定
sonarsource/sonarqube-mcp容器,將您的助理連結至 SonarQube。 - 分析程式碼片段 — 請助理分析程式碼片段或檔案,並使用工作區掛載直接從磁碟讀取檔案。
- 搜尋及管理問題 — 透過
issues工具集尋找並更新 SonarQube 問題。 - 檢視安全熱點 — 透過
security-hotspots工具集搜尋並檢視安全熱點。 - 瀏覽專案與品質閘門 — 查詢專案金鑰、品質閘門、規則、度量指標及涵蓋率資料。
- 限制工具集並啟用唯讀模式 — 使用
SONARQUBE_TOOLSETS和SONARQUBE_READ_ONLY控制載入哪些工具,並封鎖寫入操作。
文件
SonarQube MCP Server
SonarQube MCP Server 是一個 Model Context Protocol (MCP) 伺服器,可與 SonarQube Server 或 Cloud 無縫整合,提供程式碼品質與安全性功能。 它也支援直接在代理程式情境中分析程式碼片段。
請從快速設定開始,或進一步了解 SonarQube 產品系列。
快速設定
安全性最佳實務
🔒 重要事項:您的 SonarQube 權杖是敏感憑證。請遵循以下安全性實務:
使用 CLI 命令時:
- 避免將權杖寫死在命令列引數中——它們會儲存在 shell 歷史記錄中
- 使用環境變數——執行命令前,先將權杖設為環境變數
使用設定檔時:
- 切勿將權杖提交至版本控制
- 盡可能在設定檔中使用環境變數替代
🚀 產生您的設定
最快的入門方式是使用 SonarQube MCP Server 設定產生器——這是一個互動式工具,可為您偏好的 AI 代理程式用戶端產生可直接使用的設定。
手動設定
如果您偏好自行設定,最簡單的方法是使用我們位於 sonarsource/sonarqube-mcp 的容器映像。使用 sonarsource/sonarqube-mcp 以自動更新(搭配 --pull=always),或固定使用版本標籤(例如 sonarsource/sonarqube-mcp:1.19.0.2785),以確保部署可重現。部分版本也會以其對應的 SonarQube Server 版本標記,格式為 <SQS major>.<SQS minor>.<patch>(例如 sonarsource/sonarqube-mcp:2026.5.0);每個針對該 SonarQube Server 版本的 MCP 版本都會遞增修補程式編號。若要在本機建置,請參閱下文。
**注意:**以下範例使用
docker,但任何 OCI 相容的容器執行環境都可使用(例如 Podman、nerdctl)。只要將docker替換為您偏好的工具即可。
Antigravity
SonarQube MCP Server 可在 Antigravity MCP Store 中使用。請依照以下指示操作:
- 開啟 Agent Side Panel
- 點選右上角的三個點 (...),然後選取 MCP Servers
- 搜尋
SonarQube並選取 Install - 提供必要的 SonarQube 使用者權杖。您也可以提供 SonarQube Cloud 的組織金鑰,或在連線至 SonarQube Server 時提供 SonarQube URL。
若使用 SonarQube Cloud US,請將 URL 設為 https://sonarqube.us。
或者,您也可以透過 mcp_config.json 手動設定伺服器:
- 若要連線至 SonarQube Cloud:
在 Agent Side Panel 中,點選三個點 (...) -> MCP Store -> Manage MCP Servers -> View raw config,然後新增以下內容:
{
"mcpServers": {
"sonarqube": {
"command": "docker",
"args": ["run", "--init", "--pull=always", "-i", "--rm", "-e", "SONARQUBE_TOKEN", "-e", "SONARQUBE_ORG", "sonarsource/sonarqube-mcp"],
"env": {
"SONARQUBE_TOKEN": "<YOUR_TOKEN>",
"SONARQUBE_ORG": "<YOUR_ORG>"
}
}
}
}
若使用 SonarQube Cloud US,請將 "SONARQUBE_URL": "https://sonarqube.us" 手動新增至 env 區段,並將 "-e", "SONARQUBE_URL" 新增至 args 陣列。
- 若要連線至 SonarQube Server:
{
"mcpServers": {
"sonarqube": {
"command": "docker",
"args": ["run", "--init", "--pull=always", "-i", "--rm", "-e", "SONARQUBE_TOKEN", "-e", "SONARQUBE_URL", "sonarsource/sonarqube-mcp"],
"env": {
"SONARQUBE_TOKEN": "<YOUR_USER_TOKEN>",
"SONARQUBE_URL": "<YOUR_SERVER_URL>"
}
}
}
}
Claude Code
- 若要連線至 SonarQube Cloud:
claude mcp add sonarqube \
--env SONARQUBE_TOKEN=$SONAR_TOKEN \
--env SONARQUBE_ORG=$SONAR_ORG \
-- docker run --init --pull=always -i --rm -e SONARQUBE_TOKEN -e SONARQUBE_ORG sonarsource/sonarqube-mcp
若使用 SonarQube Cloud US,請將 --env SONARQUBE_URL=https://sonarqube.us 新增至命令中。
- 若要連線至 SonarQube Server:
claude mcp add sonarqube \
--env SONARQUBE_TOKEN=$SONAR_USER_TOKEN \
--env SONARQUBE_URL=$SONAR_URL \
-- docker run --init --pull=always -i --rm -e SONARQUBE_TOKEN -e SONARQUBE_URL sonarsource/sonarqube-mcp
Codex CLI
手動編輯位於 ~/.codex/config.toml 的設定檔,並新增以下設定:
- 若要連線至 SonarQube Cloud:
[mcp_servers.sonarqube]
command = "docker"
args = ["run", "--init", "--pull=always", "--rm", "-i", "-e", "SONARQUBE_TOKEN", "-e", "SONARQUBE_ORG", "sonarsource/sonarqube-mcp"]
env = { "SONARQUBE_TOKEN" = "<YOUR_USER_TOKEN>", "SONARQUBE_ORG" = "<YOUR_ORG>" }
若使用 SonarQube Cloud US,請將 "SONARQUBE_URL" = "https://sonarqube.us" 新增至 env 區段,並將 "-e", "SONARQUBE_URL" 新增至 args 陣列。
- 若要連線至 SonarQube Server:
[mcp_servers.sonarqube]
command = "docker"
args = ["run", "--init", "--pull=always", "--rm", "-i", "-e", "SONARQUBE_TOKEN", "-e", "SONARQUBE_URL", "sonarsource/sonarqube-mcp"]
env = { "SONARQUBE_TOKEN" = "<YOUR_TOKEN>", "SONARQUBE_URL" = "<YOUR_SERVER_URL>" }
Cursor
- 若要連線至 SonarQube Cloud:
若使用 SonarQube Cloud US,請在安裝後,將 "SONARQUBE_URL": "https://sonarqube.us" 手動新增至 MCP 設定中的 env 區段。
- 若要連線至 SonarQube Server:
Gemini CLI
**注意:**Gemini CLI 擴充功能已移至 sonarqube-agent-plugins 儲存庫。請改從該處安裝。
您可以使用以下命令安裝我們的 MCP 伺服器擴充功能:
gemini extensions install https://github.com/SonarSource/sonarqube-agent-plugins
啟動 Gemini 前,您需要設定必要的環境變數:
必要的環境變數:
-
適用於 SonarQube Cloud:
SONARQUBE_TOKEN- 您的 SonarQube Cloud 權杖SONARQUBE_ORG- 您的組織金鑰SONARQUBE_URL- (選用)設為https://sonarqube.us以使用 SonarQube Cloud US
-
適用於 SonarQube Server:
SONARQUBE_TOKEN- 您的 SonarQube Server 使用者權杖SONARQUBE_URL- 您的 SonarQube Server URL
安裝後,擴充功能會安裝在 <home>/.gemini/extensions/sonarqube/gemini-extension.json。
GitHub Copilot CLI
啟動 Copilot CLI 後,執行以下命令以新增 SonarQube MCP 伺服器:
/mcp add
您需要提供 MCP 伺服器的相關資訊;您可以使用 Tab 鍵在欄位間移動。
- 若要連線至 SonarQube Cloud:
Server Name: sonarqube
Server Type: Local (Press 1)
Command: docker
Arguments: run, --init, --pull=always, --rm, -i, -e, SONARQUBE_TOKEN, -e, SONARQUBE_ORG, sonarsource/sonarqube-mcp
Environment Variables: SONARQUBE_TOKEN=<YOUR_TOKEN>,SONARQUBE_ORG=<YOUR_ORG>
Tools: *
若使用 SonarQube Cloud US,請將 -e, SONARQUBE_URL 新增至 Arguments,並將 SONARQUBE_URL=https://sonarqube.us 新增至 Environment Variables。
- 若要連線至 SonarQube Server:
Server Name: sonarqube
Server Type: Local (Press 1)
Command: docker
Arguments: run, --init, --pull=always, --rm, -i, -e, SONARQUBE_TOKEN, -e, SONARQUBE_URL, sonarsource/sonarqube-mcp
Environment Variables: SONARQUBE_TOKEN=<YOUR_USER_TOKEN>,SONARQUBE_URL=<YOUR_SERVER_URL>
Tools: *
設定檔位於 ~/.copilot/mcp-config.json。
GitHub Copilot coding agent
GitHub Copilot coding agent 可直接在您的 CI/CD 中使用 SonarQube MCP 伺服器。
若要將密碼新增至 Copilot 環境,請依照 Copilot 文件操作。只有名稱以 COPILOT_MCP_ 為前綴的密碼,才能供 MCP 設定使用。
在 GitHub 儲存庫中,前往 Settings -> Copilot -> Coding agent,並在 MCP 設定區段新增以下設定:
- 若要連線至 SonarQube Cloud:
{
"mcpServers": {
"sonarqube": {
"type": "local",
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"--rm",
"-i",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_ORG",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "COPILOT_MCP_SONARQUBE_TOKEN",
"SONARQUBE_ORG": "COPILOT_MCP_SONARQUBE_ORG"
},
"tools": ["*"]
}
}
}
若使用 SonarQube Cloud US,請將 "-e", "SONARQUBE_URL" 新增至 args 陣列,並將 "SONARQUBE_URL": "COPILOT_MCP_SONARQUBE_URL" 新增至 env 區段,然後設定密碼 COPILOT_MCP_SONARQUBE_URL=https://sonarqube.us。
- 若要連線至 SonarQube Server:
{
"mcpServers": {
"sonarqube": {
"type": "local",
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"--rm",
"-i",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_URL",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "COPILOT_MCP_SONARQUBE_USER_TOKEN",
"SONARQUBE_URL": "COPILOT_MCP_SONARQUBE_URL"
},
"tools": ["*"]
}
}
}
Kiro
在工作區目錄中建立 .kiro/settings/mcp.json 檔案(若已存在則編輯),並新增以下設定:
- 若要連線至 SonarQube Cloud:
{
"mcpServers": {
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_ORG",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<YOUR_TOKEN>",
"SONARQUBE_ORG": "<YOUR_ORG>"
},
"disabled": false,
"autoApprove": []
}
}
}
若使用 SonarQube Cloud US,請將 "-e", "SONARQUBE_URL" 新增至 args 陣列,並將 "SONARQUBE_URL": "https://sonarqube.us" 新增至 env 區段。
- 若要連線至 SonarQube Server:
{
"mcpServers": {
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_URL",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<YOUR_USER_TOKEN>",
"SONARQUBE_URL": "<YOUR_SERVER_URL>"
},
"disabled": false,
"autoApprove": []
}
}
}
VS Code
您可以使用以下按鈕,簡化在 VS Code 中的安裝流程。
若使用 SonarQube Cloud US,請在安裝後,將 "SONARQUBE_URL": "https://sonarqube.us" 手動新增至 MCP 設定中的 env 區段。
Windsurf
SonarQube MCP Server 可作為 Windsurf 外掛程式使用。請依照以下指示操作:
- 開啟 Windsurf Settings > Cascade > MCP Servers,然後選取 Open MCP Marketplace
- 在 Cascade MCP Marketplace 中搜尋
sonarqube - 選擇 SonarQube MCP Server,然後選取 Install
- 新增必要的 SonarQube 使用者權杖。您也可以選擇新增 SonarQube Cloud 的預設組織金鑰,或新增 SonarQube Server 或 Community Build 的 SonarQube URL。
若使用 SonarQube Cloud US,請將 URL 設為 https://sonarqube.us。
Zed
前往 Zed 中的 Extensions 檢視,並搜尋 SonarQube MCP Server。 安裝擴充功能時,系統會提示您提供必要的環境變數:
- 使用 SonarQube Cloud 時:
{
"sonarqube_token": "YOUR_SONARQUBE_TOKEN",
"sonarqube_org": "SONARQUBE_ORGANIZATION_KEY",
"docker_path": "DOCKER_PATH"
}
若使用 SonarQube Cloud US,請將 "sonarqube_url": "https://sonarqube.us" 新增至設定中。
- 使用 SonarQube Server 時:
{
"sonarqube_token": "YOUR_SONARQUBE_USER_TOKEN",
"sonarqube_url": "YOUR_SONARQUBE_SERVER_URL",
"docker_path": "DOCKER_PATH"
}
docker_path 是 docker 可執行檔的路徑。範例:
Linux/macOS:/usr/bin/docker 或 /usr/local/bin/docker
Windows:C:\Program Files\Docker\Docker\resources\bin\docker.exe
💡 **提示:**我們建議定期或在回報問題前拉取最新映像,以確保您使用的是最新功能與修正。
手動安裝
您可以將以下片段複製到 MCP 伺服器設定檔中,手動安裝 SonarQube MCP 伺服器:
- 若要連線至 SonarQube Cloud:
{
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_ORG",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_ORG": "<org>"
}
}
}
- 若要連線至 SonarQube Server:
{
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_URL",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_URL": "<url>"
}
}
}
與 SonarQube for IDE 整合
SonarQube MCP Server 可與 SonarQube for IDE 整合,進一步改善您的開發工作流程,並直接在 IDE 中提供更完善的程式碼分析與深入資訊。
設定
使用 SonarQube for IDE 時,應將 SONARQUBE_IDE_PORT 環境變數設為正確的連接埠號碼。SonarQube for VS Code 提供「快速安裝」按鈕,可自動設定正確的連接埠。
例如,使用 SonarQube Cloud 時:
{
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_ORG",
"-e",
"SONARQUBE_IDE_PORT",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_ORG": "<org>",
"SONARQUBE_IDE_PORT": "<64120-64130>"
}
}
}
在 Linux 上於容器中執行 MCP 伺服器時,容器無法存取執行於 localhost 的 SonarQube for IDE 內嵌伺服器。若要允許容器連線至 SonarQube for IDE 伺服器,請將
--network=host選項新增至容器執行命令。
設定
請依據您的環境提供特定的環境變數。
基本
執行 MCP Server 時,您應新增以下變數:
| 環境變數 | 說明 |
|---|---|
STORAGE_PATH | 必要的可寫入目錄絕對路徑,SonarQube MCP Server 會在此儲存檔案(例如建立、更新及保存檔案時);使用容器映像時會自動提供此路徑 |
SONARQUBE_PROJECT_KEY | 選用的預設專案金鑰。設定後,所有需要專案金鑰的工具都會自動使用此值 — 並從其結構描述中完全移除 projectKey 參數。適用於只處理單一專案的情況。 |
SONARQUBE_IDE_PORT | 選用的連接埠號碼,介於 64120 和 64130 之間,用於連接 SonarQube MCP Server 與 SonarQube for IDE。 |
SONARQUBE_FORCE_LOCAL_ANALYSIS | 設為 true,即可在 Cloud stdio 上使用 analyze_code_snippet,而非 Vortex 分析,包括適用於具備使用資格的組織。需要 analysis 工具集。預設值:false。不影響 SonarQube Server 或 Cloud HTTP。 |
SONARQUBE_DEBUG_ENABLED | 設為 true 時,會啟用偵錯記錄。偵錯記錄會同時寫入記錄檔和 STDERR。適用於疑難排解連線或設定問題。預設值:false。 |
SONARQUBE_LOG_TO_FILE_DISABLED | 設為 true 時,會完全停用將記錄寫入磁碟。STORAGE_PATH/logs/ 下不會建立任何記錄檔。適用於不需要檔案記錄的容器化或暫時性環境。預設值:false。 |
工作區掛載(減少內容膨脹)
依預設,分析工具 analyze_code_snippet 需要代理程式將完整檔案內容作為 fileContent 引數傳入。對於大型檔案,或在工作階段中分析多個檔案時,這會大幅增加內容視窗的使用量和成本。
**解決方案:**將專案目錄掛載至容器中的 /app/mcp-workspace。偵測到此掛載時,伺服器會使用相對於專案的 filePath 引數直接從磁碟讀取檔案 — 檔案內容不會經過代理程式的內容。
{
"args": [
"run", "-i", "--rm", "--init", "--pull=always",
"-e", "SONARQUBE_TOKEN",
"-e", "SONARQUBE_ORG",
"-v", "/path/to/your/project:/app/mcp-workspace",
"sonarsource/sonarqube-mcp"
]
}
掛載後,分析工具會使用 filePath 直接讀取檔案;不需要 fileContent。
透過 stdio 時,分析方式會在啟動時選定(視啟用的工具集而定):
- **SonarQube Cloud:**具備工作區掛載且至少有一個具備使用資格的組織時,使用 Vortex 分析(
run_advanced_code_analysis);否則使用本機分析。Vortex 分析僅接受具備使用資格的組織。 - **SonarQube Server:**對具備使用資格的執行個體使用 Vortex 分析(需要工作區掛載);對不具備 Vortex 的執行個體使用本機分析。
本機分析會在連線時使用 SonarQube for IDE,否則使用 analyze_code_snippet。若未掛載工作區,程式碼片段分析需要 fileContent。
在 Cloud stdio 上,SONARQUBE_ORG 會將使用資格檢查限制在該組織。若要對所有組織使用程式碼片段,請設定 SONARQUBE_FORCE_LOCAL_ANALYSIS=true、啟用 analysis 工具集,然後重新啟動。此設定僅影響 Cloud stdio。
透過 HTTP/HTTPS 時,若 MCP 伺服器具有啟動時的 SONARQUBE_TOKEN 且已啟用 analysis 工具集,Cloud 和 Server 都支援 analyze_code_snippet。初始化分析器需要啟動權杖(Cloud 外掛程式下載為匿名);呼叫會使用呼叫者的權杖,並在 Cloud 上使用請求所選的組織,以擷取品質設定檔和啟用中的規則。若沒有啟動權杖,即使設定了 SONARQUBE_FORCE_LOCAL_ANALYSIS=true,程式碼片段分析和分析器下載仍會停用。不需要 Vortex 使用資格。
選擇性啟用工具
MCP 用戶端會在每次對話中,將每個已啟用工具的名稱、說明和輸入結構描述載入代理程式的內容。使用 SONARQUBE_TOOLSETS 限制清單,可降低內容用量和每個工作階段的權杖成本。
依預設只會啟用重要工具集。只有在需要時才啟用其他工具集。
| 環境變數 | 說明 |
|---|---|
SONARQUBE_TOOLSETS | 以逗號分隔的工具集清單。設定後,只有這些工具集可供使用。若未設定,則會啟用預設的重要工具集(analysis、ide、issues、projects、quality-gates、rules、duplications、measures、security-hotspots、dependency-risks、coverage、cag)。注意:projects 工具集一律會啟用,因為其他操作需要透過它尋找專案金鑰。Vortex 內容工具(已棄用的名稱:Context Augmentation/CAG)和 Vortex 分析工具(已棄用的名稱:Advanced Analysis/A3S)僅適用於 stdio 模式,且共用單一組織使用資格 — 組織必須同時具備兩者的使用資格,才能使用其中任一工具。在 Cloud 內建分析中,會檢查執行階段所選的組織;代理內容工具則使用啟動時的組織。在 SonarQube Server 上,若 CAG 和 A3S 中樞都具備使用資格,stdio 會列出 Vortex 內容和 run_advanced_code_analysis。建議使用統一的 vortex 工具集金鑰。已棄用的 cag 和 analysis 金鑰仍可使用;若使用時未設定 vortex,系統會發出啟動警告,並在伺服器指示中加入棄用說明。在 Streamable HTTP 模式中,用戶端可傳送 SONARQUBE_TOOLSETS HTTP 標頭,進一步依每個請求縮小範圍,但無法啟用超出伺服器啟動時所設定的工具集(請參閱下方的 Streamable HTTP 傳輸)。 |
SONARQUBE_READ_ONLY | 設為 true 時,會啟用唯讀模式,停用所有寫入操作(例如變更問題狀態)。若同時設定 SONARQUBE_TOOLSETS,此篩選條件會與其累加。預設值:false。在 Streamable HTTP 模式中,用戶端可傳送 SONARQUBE_READ_ONLY HTTP 標頭,進一步將個別請求限制為唯讀,但無法解除伺服器層級的唯讀限制(請參閱下方的 Streamable HTTP 傳輸)。 |
可用的工具集
| 工具集 | Key | 說明 | |-----------------------|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------| | **Analysis** | `analysis` | 程式碼分析工具(透過 `analyze_code_snippet` 進行本機分析,**已棄用**,建議改用 `analyze_file_list`/Vortex 分析) | | **IDE** | `ide` | SonarQube for IDE 橋接工具(檔案分析、自動分析切換)— 目前也包含在 `analysis` 中 | | **Issues** | `issues` | 搜尋及管理 SonarQube 問題 | | **Security Hotspots** | `security-hotspots` | 搜尋及檢視 Security Hotspots | | **Projects** | `projects` | 瀏覽及搜尋 SonarQube 專案 | | **Quality Gates** | `quality-gates` | 存取品質閘道及其狀態 | | **Rules** | `rules` | 瀏覽及搜尋 SonarQube 規則 | | **Sources** | `sources` | 存取原始碼及 SCM 資訊 | | **Duplications** | `duplications` | 尋找專案間的程式碼重複 | | **Measures** | `measures` | 擷取度量值與指標(包含度量值和指標工具) | | **Languages** | `languages` | 列出支援的程式語言 | | **Portfolios** | `portfolios` | 管理投資組合與企業(Cloud 和 Server) | | **System** | `system` | 系統管理工具(僅限 Server) | | **Webhooks** | `webhooks` | 管理 Webhook | | **Dependency Risks** | `dependency-risks` | 分析相依性風險與安全性問題(SCA) | | **Coverage** | `coverage` | 測試涵蓋率分析與改善工具 | | **Vortex Context** | `cag` | Vortex context 工具 — 僅限 stdio。**已棄用**,建議改用 `vortex`(舊名稱:Context Augmentation/CAG) | | **Vortex** | `vortex` | 統一的**建議**工具集,以單一名稱提供 Vortex context 和 Vortex 分析工具(僅限 stdio;Cloud 需要組織具備合併 entitlement;Server 需要兩個 hub 都具備 entitlement) | | **Agentic Readiness** | `agentic-readiness` | Agentic Readiness 評估工具(SonarQube Cloud,需要組織 entitlement) | | **Architecture** | `architecture` | 讀取架構、取代預期模型並管理專案指示(預設未啟用) |範例
啟用 analysis、issues 和 quality gates 工具集(使用 Docker 搭配 SonarQube Cloud):
docker run --init --pull=always -i --rm \
-e SONARQUBE_TOKEN="<token>" \
-e SONARQUBE_ORG="<org>" \
-e SONARQUBE_TOOLSETS="analysis,issues,quality-gates" \
sonarsource/sonarqube-mcp
注意:projects 工具集一律會自動啟用,因此不需要將它加入 SONARQUBE_TOOLSETS。
啟用唯讀模式(使用 Docker 搭配 SonarQube Cloud):
docker run --init --pull=always -i --rm \
-e SONARQUBE_TOKEN="<token>" \
-e SONARQUBE_ORG="<org>" \
-e SONARQUBE_READ_ONLY="true" \
sonarsource/sonarqube-mcp
SonarQube Cloud
對於 stdio,請設定下列環境變數:
| 環境變數 | 說明 | 必要 |
|---|---|---|
SONARQUBE_TOKEN | 您的 SonarQube Cloud token | 是 |
SONARQUBE_ORG | 選用的固定 SonarQube Cloud 組織 key | 否 |
SONARQUBE_URL | 自訂 SonarQube Cloud URL(預設為 https://sonarcloud.io)。若要使用 SonarQube Cloud US,請設定此項:https://sonarqube.us | 否 |
範例:
- SonarQube Cloud:只需要
SONARQUBE_TOKEN。設定SONARQUBE_ORG後會固定組織。 - SonarQube Cloud US:設定
SONARQUBE_TOKEN和SONARQUBE_URL=https://sonarqube.us;SONARQUBE_ORG為選用。
SonarQube Server
| 環境變數 | 說明 | 必要 |
|---|---|---|
SONARQUBE_TOKEN | 您的 SonarQube Server USER token | 是 |
SONARQUBE_URL | 您的 SonarQube Server URL | 是 |
**版本需求:**必須使用 SonarQube Server 2025.1(SQS)或 25.1(SonarQube Community Build)或更新版本。啟動時,MCP 伺服器會讀取連線執行個體的版本;若版本過舊,便會以錯誤結束(例如,不支援舊版 9.x/10.x Server 發行版本及 Community Build 24.x)。SonarQube Cloud 不受此檢查限制。
⚠️ 連線至 SonarQube Server 必須使用 USER 類型的 token;若使用專案 token 或全域 token,將無法正常運作。
💡 設定提示(stdio 模式):未指定 URL 的 token 預設會連線至 SonarQube Cloud。設定
SONARQUBE_URL可連線至 SonarQube Server 或其他 Cloud 區域。設定SONARQUBE_ORG後會固定 Cloud 組織。
在 stdio 中,設定 SONARQUBE_ORG 會固定 Cloud 組織。工具 organization 的引數會從結構描述中省略,且會遭到拒絕;SONARQUBE_PROJECT_KEY 設定時的 projectKey 也一樣。
若未設定 SONARQUBE_ORG,代理程式可以呼叫 list_organizations,以探索使用者帳戶可存取的組織 key、名稱、ID 和 UUID,接著在支援組織的工具中,將 key 作為選用的 organization 引數傳入。若偵測到唯一組織,stdio 會將其保留為預設值;若沒有或有多個組織,則不會解析預設組織。工具引數只適用於該次呼叫,不會變更偵測到的預設值。程式碼片段設定檔和規則查詢會使用該次呼叫所選的組織。
對於 HTTP/HTTPS token 請求,伺服器層級的 SONARQUBE_ORG 仍受支援以維持相容性,但已棄用,並會在啟動時記錄警告。它會為每個請求固定組織,並拒絕用戶端組織標頭和工具 organization 引數。若未設定伺服器組織,可選用的用戶端 SONARQUBE_ORG 標頭會為該請求固定組織,並禁止使用工具 organization 引數。若未提供標頭,請使用工具引數。需要組織的工具在所有組織資訊皆缺少時,會提供探索指引。組織探索以及 API 僅使用資源識別碼的工具不需要組織引數。具備組織感知能力的問題搜尋和相依性風險 entitlement 檢查會使用所選組織。設定 OAuth 時,伺服器層級的 SONARQUBE_ORG 會遭到拒絕。OAuth 請求也會拒絕組織標頭;請透過工具引數選取組織。
Vortex context 工具會使用已設定或在啟動時唯一偵測到的組織。執行階段的組織引數不會重新設定代理的 context 引擎;若啟動時無法解析組織,則會略過該代理。內建的 run_advanced_code_analysis 工具會在每次呼叫時,檢查所選組織是否具備 Vortex 合併 entitlement。
傳輸模式
MCP 規格 定義兩種傳輸機制:Stdio 和 Streamable HTTP。SonarQube MCP Server 同時支援兩者:
| MCP 傳輸 | 伺服器模式 | 常見用途 |
|---|---|---|
| Stdio | 預設(未設定 SONARQUBE_TRANSPORT) | 在本機啟動伺服器作為子程序的 MCP 用戶端(Cursor、Claude Code、VS Code 等) |
| Streamable HTTP | SONARQUBE_TRANSPORT=http 或 https | 遠端或多使用者部署;用戶端透過 HTTP(S) 連線至 /mcp(例如,Windsurf 搭配自架伺服器 URL) |
**注意:**Streamable HTTP 是目前的 MCP 網路傳輸方式。舊版 MCP 中僅支援 SSE 的 HTTP 傳輸已棄用且不受支援。
1. Stdio(預設 — 建議用於本機開發)
建議用於本機開發和單一使用者設定,也是大多數 MCP 用戶端採用的模式。
範例 — Docker 搭配 SonarQube Cloud:
{
"mcpServers": {
"sonarqube": {
"command": "docker",
"args": ["run", "--init", "--pull=always", "-i", "--rm", "-e", "SONARQUBE_TOKEN", "-e", "SONARQUBE_ORG", "sonarsource/sonarqube-mcp"],
"env": {
"SONARQUBE_TOKEN": "<your-token>",
"SONARQUBE_ORG": "<your-org>"
}
}
}
}
2. HTTP(Streamable HTTP)
未加密的 Streamable HTTP 傳輸。多使用者部署請改用 HTTPS。
⚠️ **不建議:**本機開發請使用 Stdio;多使用者正式環境部署請使用 HTTPS(Streamable HTTP)。
| 環境變數 | 說明 | 預設值 |
|---|---|---|
SONARQUBE_TRANSPORT | 設定為 http 以啟用 Streamable HTTP 傳輸 | 未設定(stdio) |
SONARQUBE_HTTP_PORT | 連接埠號碼(1024-65535),或使用 0 由作業系統指派連接埠 | 8080 |
SONARQUBE_HTTP_HOST | 要繫結的主機(基於安全考量,預設為 localhost) | 127.0.0.1 |
SONARQUBE_HTTP_ALLOWED_ORIGINS | 以逗號分隔的允許 CORS 的瀏覽器來源(例如 https://my-app.example.com) | 未設定 |
SONARQUBE_MCP_IN_CONTAINER | 在容器內執行時設定為 true。官方 Docker 映像會自動設定此項;使用其他 OCI 執行環境(Podman、Kubernetes、Nomad 等)時,請自行設定。 | false |
注意: HTTP/HTTPS 會透過 Authorization: Bearer <token> 接受使用者的 SonarQube 權杖。Sonar 託管的 Cloud 服務也支援 OAuth。對於 SonarQube Cloud 權杖請求,組織的解析方式如下: |
SONARQUBE_ORG是選用的請求標頭,可為該請求固定組織。已棄用的伺服器層級設定會為所有請求固定組織,並拒絕用戶端組織標頭和工具組織引數。- 當伺服器設定或請求標頭都未固定組織時,支援組織的工具會接受選用的
organization引數。若組織已固定,則會拒絕提供此引數。 - 若未設定預設值,代理程式可以只使用權杖呼叫
list_organizations,然後將選定的金鑰傳遞給後續工具。
用戶端也可以透過提供 SONARQUBE_TOOLSETS 和/或 SONARQUBE_READ_ONLY 標頭,逐一請求縮小可見工具的範圍;這些標頭會在伺服器層級設定的基礎上進一步篩選,只能縮小範圍,不能擴大範圍。
請求之間不會維護工作階段狀態。
已棄用: 對於 SonarQube 權杖,仍接受
SONARQUBE_TOKEN請求標頭以維持回溯相容性,但將在未來版本中移除。請改用Authorization: Bearer <token>。
3. HTTPS(透過 TLS 的 Streamable HTTP)(建議用於多使用者正式環境部署)
使用 TLS 加密的安全 Streamable HTTP 傳輸。需要 SSL 憑證。
✅ 正式環境建議: 透過 Streamable HTTP 為多位使用者部署 MCP 伺服器時,請使用 HTTPS。基於安全考量,伺服器預設會繫結至
127.0.0.1(localhost)。
| 環境變數 | 說明 | 預設值 |
|---|---|---|
SONARQUBE_TRANSPORT | 設為 https 以啟用透過 TLS 的 Streamable HTTP 傳輸 | 未設定(stdio) |
SONARQUBE_HTTP_PORT | 連接埠號碼(HTTPS 通常使用 8443),或 0 以使用作業系統指派的連接埠 | 8080 |
SONARQUBE_HTTP_HOST | 要繫結的主機(基於安全考量,預設為 localhost) | 127.0.0.1 |
SONARQUBE_HTTP_ALLOWED_ORIGINS | 以逗號分隔的允許 CORS 的瀏覽器來源(例如 https://my-app.example.com) | 未設定 |
SONARQUBE_MCP_IN_CONTAINER | 在容器內執行時設為 true。官方 Docker 映像會自動設定此值;使用其他 OCI 執行環境(Podman、Kubernetes、Nomad 等)時,請自行設定。 | false |
SSL 憑證設定(選用):
| 環境變數 | 說明 | 預設值 |
|---|---|---|
SONARQUBE_HTTPS_KEYSTORE_PATH | 金鑰儲存庫檔案的路徑(.p12 或 .jks) | /etc/ssl/mcp/keystore.p12 |
SONARQUBE_HTTPS_KEYSTORE_PASSWORD | 金鑰儲存庫密碼 | sonarlint |
SONARQUBE_HTTPS_KEYSTORE_TYPE | 金鑰儲存庫類型(PKCS12 或 JKS) | PKCS12 |
範例 - 使用 SonarQube Cloud 的 Docker:
注意: 在容器中執行時,請設定
SONARQUBE_HTTP_HOST=0.0.0.0,讓容器監聽所有介面並使執行環境的連接埠對應正常運作;並設定SONARQUBE_MCP_IN_CONTAINER=true,告知伺服器其位於容器內。官方 Docker 映像會自動設定後者;使用其他 OCI 執行環境(Podman、Kubernetes、Nomad 等)時,請自行設定。主機端的連接埠旗標會控制容器外部哪些對象可以連線至伺服器。SONARQUBE_HTTP_HOST=0.0.0.0只會控制伺服器在容器內監聽的位置;瀏覽器 CORS 預設仍允許 localhost 來源。
對於在您的電腦本機執行的伺服器(僅可從 localhost 存取):
docker run --init --pull=always -p 127.0.0.1:8443:8443 \
-v $(pwd)/keystore.p12:/etc/ssl/mcp/keystore.p12:ro \
-e SONARQUBE_TRANSPORT=https \
-e SONARQUBE_HTTP_HOST=0.0.0.0 \
-e SONARQUBE_HTTP_PORT=8443 \
sonarsource/sonarqube-mcp
對於可從網路存取的伺服器(遠端部署):
docker run --init --pull=always -p 8443:8443 \
-v $(pwd)/keystore.p12:/etc/ssl/mcp/keystore.p12:ro \
-e SONARQUBE_TRANSPORT=https \
-e SONARQUBE_HTTP_HOST=0.0.0.0 \
-e SONARQUBE_HTTP_PORT=8443 \
sonarsource/sonarqube-mcp
用戶端設定(SonarQube Cloud):
對於 SonarQube 權杖請求,可選擇在用戶端標頭中設定 SONARQUBE_ORG 以固定請求的組織,或省略此標頭,透過工具引數選擇組織。
{
"mcpServers": {
"sonarqube-https": {
"url": "https://your-server:8443/mcp",
"headers": {
"Authorization": "Bearer <your-token>",
"SONARQUBE_TOOLSETS": "issues,quality-gates",
"SONARQUBE_READ_ONLY": "true"
}
}
}
}
用戶端設定(SonarQube Server):
{
"mcpServers": {
"sonarqube-https": {
"url": "https://your-server:8443/mcp",
"headers": {
"Authorization": "Bearer <your-token>",
"SONARQUBE_TOOLSETS": "issues,quality-gates",
"SONARQUBE_READ_ONLY": "true"
}
}
}
}
注意:
SONARQUBE_TOOLSETS和SONARQUBE_READ_ONLY是選用的逐請求標頭,可針對特定請求縮小伺服器層級的工具集合。它們只能縮小範圍,不能啟用工具集,也不能解除伺服器啟動時設定的限制。
注意: 本機開發請改用 Stdio 傳輸(預設值)。HTTPS Streamable HTTP 適用於配備適當 SSL 憑證的多使用者正式環境部署。
託管的 SonarQube Cloud OAuth
OAuth 支援的對象是 Sonar 託管的 SonarQube Cloud MCP 服務。請使用支援 OAuth 的 MCP 用戶端,並在系統提示時登入;用戶端設定不需要 OAuth 伺服器設定。現有的 SonarQube 權杖用戶端仍受支援。
mcp:tools:read 和 mcp:tools:write 範圍會分別控制讀取和寫入工具的存取權,並受您的 Cloud 權限限制。使用 list_organizations 和工具 organization 引數選擇組織。
自架部署仍使用 SonarQube 權杖;目前尚不支援 SonarQube Server 的 OAuth。
服務端點
以 Streamable HTTP 模式(http 或 https)執行時,除了位於 /mcp 的 MCP 端點外,伺服器還會公開幾個未經驗證的服務端點。這些端點供服務對服務用途(監控、協調、用戶端相容性檢查)使用,不需要 Authorization 標頭。
| 端點 | 方法 | 說明 | 範例回應 |
|---|---|---|---|
/health | GET | 存活探測。伺服器開始接受請求後,會以空白本文傳回 200 OK。 | (空白本文) |
/info | GET | 以 JSON 格式傳回 MCP 伺服器版本。可用於確認已部署的伺服器版本。 | {"version":"1.16.0"} |
使用 Stdio 傳輸時,無法使用這些端點。
自訂憑證
如果您的 SonarQube Server 使用自我簽署憑證或私人憑證授權單位(CA)簽發的憑證,您可以將自訂憑證新增至容器,系統會自動安裝這些憑證。
Configuration
使用磁碟區掛載
執行容器時,掛載包含憑證的目錄:
docker run --init --pull=always -i --rm \
-v /path/to/your/certificates/:/usr/local/share/ca-certificates/:ro \
-e SONARQUBE_TOKEN="<token>" \
-e SONARQUBE_URL="<url>" \
sonarsource/sonarqube-mcp
支援的憑證格式
容器支援下列憑證格式:
.crt檔案(PEM 或 DER 編碼).pem檔案(PEM 編碼)
使用憑證設定 MCP
使用自訂憑證時,您可以修改 MCP 設定以掛載憑證:
{
"sonarqube": {
"command": "docker",
"args": [
"run",
"--init",
"--pull=always",
"-i",
"--rm",
"-v",
"/path/to/your/certificates/:/usr/local/share/ca-certificates/:ro",
"-e",
"SONARQUBE_TOKEN",
"-e",
"SONARQUBE_URL",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_URL": "<url>"
}
}
}
注意: 要從 JAR 執行伺服器,而非使用容器嗎?上述磁碟區掛載會將憑證安裝至容器的作業系統信任存放區,伺服器也會讀取該存放區。如果無法使用作業系統信任存放區(尤其是 Windows 不會查詢該存放區),請將 JVM 指向包含 CA 憑證的 Java 信任存放區:
-Djavax.net.ssl.trustStore=/path/to/truststore.p12 -Djavax.net.ssl.trustStoreType=PKCS12 -Djavax.net.ssl.trustStorePassword=<passphrase>。此設定會在預設的受信任憑證之外新增憑證。
Proxy
SonarQube MCP Server 透過標準 Java Proxy 系統屬性支援 HTTP 和 SOCKS5 Proxy。
Configuration
HTTP/HTTPS Proxy
您可以使用 Java 系統屬性設定 Proxy。這些屬性可以設為環境變數,或以 JVM 引數傳入。
常用 Proxy 屬性:
| 屬性 | 說明 | 範例 |
|---|---|---|
http.proxyHost | HTTP Proxy 主機名稱 | proxy.example.com |
http.proxyPort | HTTP Proxy 連接埠 | 8080 |
https.proxyHost | HTTPS Proxy 主機名稱 | proxy.example.com |
https.proxyPort | HTTPS Proxy 連接埠 | 8443 |
http.nonProxyHosts | 略過 Proxy 的主機(以直線符號分隔) | localhost|127.0.0.1|*.internal.com |
HTTP/HTTPS Proxy 驗證:
| 屬性 | 說明 | 範例 |
|---|---|---|
http.proxyUser | HTTP Proxy 使用者名稱 | myuser |
http.proxyPassword | HTTP Proxy 密碼 | mypassword |
https.proxyUser | HTTPS Proxy 使用者名稱 | myuser |
https.proxyPassword | HTTPS Proxy 密碼 | mypassword |
SOCKS5 Proxy
支援 SOCKS5 Proxy。
| 屬性 | 說明 | 預設值 | 範例 |
|---|---|---|---|
socksProxyHost | SOCKS5 Proxy 主機名稱 | — | localhost |
socksProxyPort | SOCKS5 Proxy 連接埠 | 1080 | 1080 |
java.net.socks.username | SOCKS5 使用者名稱(若需要驗證) | — | myuser |
java.net.socks.password | SOCKS5 密碼(若需要驗證) | — | mypassword |
用戶端憑證(雙向 TLS)
如果您的 SonarQube Server 要求用戶端在 TLS 交握期間提供憑證(雙向 TLS),您可以將 PKCS12 金鑰儲存庫掛載至容器,並透過 JAVA_OPTS 傳入其位置。
Configuration
使用 PKCS12 金鑰儲存庫
將您的 .p12 或 .pfx 檔案掛載至容器,並設定 JAVA_OPTS 環境變數及金鑰儲存庫屬性:
docker run --init --pull=always -i --rm \
-v /path/to/client.p12:/etc/ssl/mcp/client.p12:ro \
-e JAVA_OPTS="-Djavax.net.ssl.keyStore=/etc/ssl/mcp/client.p12 -Djavax.net.ssl.keyStoreType=PKCS12 -Djavax.net.ssl.keyStorePassword=<passphrase>" \
-e SONARQUBE_TOKEN="<token>" \
-e SONARQUBE_URL="<url>" \
sonarsource/sonarqube-mcp
注意: 容器程序必須能讀取憑證檔案。如有需要,請檢查並修正權限:
ls -la /path/to/client.p12 # 檢查是否為 -rw-r--r-- (644) 或更寬鬆的權限 chmod 644 /path/to/client.p12 # 授予容器使用者讀取權限
如果金鑰儲存庫沒有密碼,請省略 -Djavax.net.ssl.keyStorePassword。請注意,此處使用的密碼可透過 docker inspect 或程序清單查看。
使用用戶端憑證設定 MCP
{
"sonarqube": {
"command": "docker",
"args": [
"run", "--init", "--pull=always", "-i", "--rm",
"-v", "/path/to/client.p12:/etc/ssl/mcp/client.p12:ro",
"-e", "JAVA_OPTS",
"-e", "SONARQUBE_TOKEN",
"-e", "SONARQUBE_URL",
"sonarsource/sonarqube-mcp"
],
"env": {
"JAVA_OPTS": "-Djavax.net.ssl.keyStore=/etc/ssl/mcp/client.p12 -Djavax.net.ssl.keyStoreType=PKCS12 -Djavax.net.ssl.keyStorePassword=<passphrase>",
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_URL": "<url>"
}
}
}
使用 PKCS12 金鑰儲存庫搭配獨立 JAR
從 JAR 執行伺服器時,請在 -jar 之前將金鑰儲存庫屬性作為 JVM 引數傳入:
java \
-Djavax.net.ssl.keyStore=/path/to/client.p12 \
-Djavax.net.ssl.keyStoreType=PKCS12 \
-Djavax.net.ssl.keyStorePassword=<passphrase> \
-jar <path_to_sonarqube_mcp_server_jar>
如果金鑰儲存庫沒有密碼,請省略 -Djavax.net.ssl.keyStorePassword。
使用用戶端憑證設定 MCP(JAR)
{
"sonarqube": {
"command": "java",
"args": [
"-Djavax.net.ssl.keyStore=/path/to/client.p12",
"-Djavax.net.ssl.keyStoreType=PKCS12",
"-Djavax.net.ssl.keyStorePassword=<passphrase>",
"-jar",
"<path_to_sonarqube_mcp_server_jar>"
],
"env": {
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_URL": "<url>"
}
}
}
注意: PEM 憑證和金鑰檔案(分開的
.crt/.key檔案)必須先轉換為 PKCS12 格式。請使用openssl pkcs12 -export -in client.crt -inkey client.key -out client.p12進行轉換。
工具
分析
-
analyze_code_snippet - 使用 SonarQube 分析器分析檔案內容,以找出程式碼品質和安全性問題。為確保準確性,一律分析完整檔案內容。也可以選擇將結果篩選至特定程式碼片段。
已棄用:
analyze_code_snippet將於未來版本中移除。請連線至 SonarQube for IDE 以使用analyze_file_list,或為您的組織啟用 Vortex 分析以使用run_advanced_code_analysis(請參閱下文)。用法:
- 已掛載工作區(建議):傳入
filePath(相對於專案的路徑)—伺服器會直接讀取檔案,讓檔案內容不會進入代理程式的上下文視窗 - 未掛載工作區:傳入完整的
fileContent以分析整個檔案(回報所有問題) - 加入選用的
codeSnippet以篩選結果—只會回報程式碼片段中的問題(自動偵測片段位置)
參數:
organization- SonarQube Cloud 組織金鑰 - 選用字串,僅限 CloudprojectKey- SonarQube 專案金鑰 - 必要字串 (若已定義SONARQUBE_PROJECT_KEY,則忽略)filePath- 要分析檔案的相對於專案的路徑(例如src/main/java/MyClass.java)。當工作區掛載於/app/mcp-workspace時使用 - 字串fileContent- 完整檔案內容字串。未掛載工作區時為必要參數 - 字串codeSnippet- 用於篩選問題的程式碼片段(必須符合 fileContent 中的內容) - 字串language- 程式碼語言(例如 'java'、'python'、'js'、'ts'、'tsx'、'jsx') - 字串scope- 檔案範圍:MAIN 或 TEST(預設:MAIN) - 字串
支援的語言: Java、Kotlin、Python、Ruby、Go、JavaScript(
js、jsx)、TypeScript(ts、tsx)、JSP、PHP、XML、HTML、CSS、CloudFormation、Kubernetes、Terraform、Azure Resource Manager、Ansible、Docker、機密資訊偵測 - 已掛載工作區(建議):傳入
啟用與 SonarQube for IDE 的整合時: (這兩項工具都標記於 analysis 和 ide 工具集)
-
analyze_file_list - 使用 SonarQube for IDE 分析目前工作目錄中的檔案。此工具會連線至執行中的 SonarQube for IDE 執行個體,對檔案清單執行程式碼品質分析。
file_absolute_paths- 要分析的絕對檔案路徑清單 - 必要字串[]
-
toggle_automatic_analysis - 啟用或停用 SonarQube for IDE 自動分析。啟用時,SonarQube for IDE 會在工作目錄中的檔案遭到修改時自動分析。停用時,則會關閉自動分析。
enabled- 啟用或停用自動分析 - 必要布林值
在 SonarQube Server 上,當 CAG 和 A3S 中樞都具有使用權時,stdio 會列出 Vortex 上下文工具和 run_advanced_code_analysis。
Vortex 分析可用時:
需要將工作區掛載於
/app/mcp-workspace
- run_advanced_code_analysis - 對單一檔案執行 Vortex 分析。SonarQube Server 使用 nil UUID 佔位符。
organization- SonarQube Cloud 組織金鑰 - 選用字串,僅限 CloudprojectKey- 專案金鑰 - 必要字串 (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 用於擷取最新分析上下文的分支名稱 - 必要字串filePath- 要分析檔案的相對於專案的路徑(例如src/main/java/MyClass.java)。- 必要字串fileScope- 定義檔案所屬範圍:'MAIN' 或 'TEST'(預設:MAIN) - 字串
涵蓋率
-
search_files_by_coverage - 依涵蓋率排序搜尋專案中的檔案(升冪排序,涵蓋率最低者優先)。此工具可協助找出需要改善測試涵蓋率的檔案。
projectKey- 要搜尋的專案金鑰 - 必要字串 (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串maxCoverage- 涵蓋率上限(0-100)。只回傳涵蓋率 <= 此值的檔案 - 數字pageIndex- 頁面索引(從 1 開始,預設:1) - 數字pageSize- 頁面大小(預設:100,最大:500) - 數字
-
get_file_coverage_details - 取得特定檔案逐行的涵蓋率資訊,包括哪些確切行未涵蓋,以及哪些行包含部分涵蓋的分支。此工具可協助精確找出需要新增測試涵蓋率的位置。使用 search_files_by_coverage 找出涵蓋率偏低的檔案後,再使用此工具。
key- 檔案金鑰(例如 my_project:src/foo/Bar.java) - 必要字串branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串from- 要分析的第一行(從 1 開始,預設:1) - 數字to- 要分析的最後一行(包含此行)。若未指定,則回傳所有行 - 數字
相依性風險
注意:相依性風險需要 SonarQube Advanced Security。在 SonarQube Server 上,需要 2025.4 Enterprise 或更高版本。
- search_dependency_risks - 搜尋 SonarQube 專案的軟體組成分析問題(相依性風險),並列出分析專案、應用程式或產品組合中出現的版本。
organization- SonarQube Cloud 組織金鑰 - 選用字串,僅限 CloudprojectKey- 專案金鑰 - 必要字串 (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串pageIndex- 選用的頁面索引(從 1 開始,預設:1) - 整數pageSize- 選用的頁面大小。必須大於 0 且小於或等於 500(預設:100) - 整數
企業
注意:企業僅在連線至 SonarQube Cloud 時提供。
- list_enterprises - 列出您有權存取的 SonarQube Cloud 企業。使用此工具探索可與其他工具搭配使用的企業 ID。
enterpriseKey- 用於篩選結果的選用企業金鑰 - 字串
問題
-
change_sonar_issue_status - 將 SonarQube 問題的狀態變更為 "accept"、"falsepositive" 或 "reopen"。
key- 問題金鑰 - 必要字串status- 問題的新狀態 - 必要列舉 {"accept", "falsepositive", "reopen"}comment- 說明狀態變更原因的選用註解 - 字串
-
search_sonar_issues_in_projects - 搜尋我組織專案中的 SonarQube 問題。
organization- SonarQube Cloud 組織金鑰 - 選用字串,僅限 CloudprojectKeys- SonarQube 專案金鑰的選用清單 - 字串[]branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串severities- 用於篩選的選用嚴重性清單。可能的值:INFO、LOW、MEDIUM、HIGH、BLOCKER - 字串[]impactSoftwareQualities- 用於篩選的選用軟體品質清單。可能的值:MAINTAINABILITY、RELIABILITY、SECURITY - 字串[]issueStatuses- 用於篩選的選用問題狀態清單。可能的值:OPEN、CONFIRMED、FALSE_POSITIVE、ACCEPTED、FIXED、IN_SANDBOX - 字串[]tags- 用於篩選的選用問題標籤清單。標籤皆為小寫 - 字串[]inNewCodePeriod- 僅回傳新程式碼期間內的問題。projectKeys和files中必須恰好有一個項目 - 布林值issueKey- 用於擷取特定問題的選用問題金鑰 - 字串pageIndex- 選用的頁面索引(從 1 開始,預設:1) - 整數pageSize- 選用的頁面大小。必須大於 0 且小於或等於 500(預設:100) - 整數
安全熱點
-
search_security_hotspots - 搜尋 SonarQube 專案中的安全熱點。
projectKey- 專案或應用程式金鑰 - 必要字串 (若已定義SONARQUBE_PROJECT_KEY,則忽略)hotspotKeys- 要擷取的特定安全熱點金鑰,以逗號分隔的清單 - 字串[]branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串files- 用於篩選的選用檔案路徑清單 - 字串[]status- 選用的狀態篩選條件:TO_REVIEW、REVIEWED - 字串resolution- 選用的解決方式篩選條件:FIXED、SAFE、ACKNOWLEDGED - 字串sinceLeakPeriod- 篩選自洩漏期間(新程式碼)以來建立的熱點 - 布林值onlyMine- 僅顯示指派給我的熱點 - 布林值pageIndex- 選用的頁面索引(從 1 開始,預設:1) - 整數pageSize- 選用的頁面大小。必須大於 0 且小於或等於 500(預設:100) - 整數
-
show_security_hotspot - 取得特定安全熱點的詳細資訊,包括規則詳細資料、程式碼上下文、流程和註解。
hotspotKey- 安全熱點金鑰 - 必要字串
-
change_security_hotspot_status - 變更安全熱點的狀態以進行審查。標記為 REVIEWED 時,必須指定解決方式(FIXED、SAFE 或 ACKNOWLEDGED)。
hotspotKey- 安全熱點金鑰 - 必要字串status- 新狀態 - 必要列舉 {"TO_REVIEW", "REVIEWED"}resolution- 狀態為 REVIEWED 時的解決方式 - 列舉 {"FIXED", "SAFE", "ACKNOWLEDGED"}comment- 選用的審查註解 - 字串
語言
- list_languages - 列出此 SonarQube 執行個體支援的所有程式設計語言。
q- 用於比對語言金鑰/名稱的選用模式 - 字串
度量值
- get_component_measures - 取得元件(專案、目錄、檔案)的 SonarQube 度量值。
projectKey- 專案金鑰 - 若未設定SONARQUBE_PROJECT_KEY,則為必要參數branch- 分支分析的選用分支名稱。使用list_branches探索有效名稱 - 字串metricKeys- 要擷取的選用度量值金鑰(例如 ncloc、complexity、violations、coverage) - 字串[]pullRequest- 選用的提取要求金鑰/ID。使用list_pull_requests探索有效金鑰 - 字串
指標
- search_metrics - 搜尋 SonarQube 指標。
pageIndex- 選用的頁面索引(從 1 開始,預設:1) - 整數pageSize- 選用的頁面大小。必須大於 0 且小於或等於 500(預設:100) - 整數
產品組合
-
list_portfolios - 列出 SonarQube 中可用的企業產品組合,並提供篩選和分頁選項。
適用於 SonarQube Server:
q- 用於依名稱或金鑰篩選產品組合的選用搜尋查詢 - 字串favorite- 若為 true,則只回傳最愛的產品組合 - 布林值pageIndex- 選用的頁碼(從 1 開始,預設:1) - 整數pageSize- 選用的頁面大小,最大 500(預設:100) - 整數
適用於 SonarQube Cloud:
enterpriseId- Enterprise uuid。只有在提供值為 true 的 'favorite' 參數時,才可省略 - Stringq- 可選的搜尋查詢,用於依名稱篩選 portfolios - Stringfavorite- 若省略 'enterpriseId' 參數,則必須設為 true。若為 true,只會傳回已登入使用者加入最愛的 portfolios。當 'draft' 為 true 時,不可設為 true - Booleandraft- 若為 true,只會傳回已登入使用者建立的草稿。當 'favorite' 為 true 時,不可設為 true - BooleanpageIndex- 要擷取頁面的可選索引(預設值:1)- IntegerpageSize- 要擷取頁面的可選大小(預設值:50)- Integer
專案
-
list_organizations - 列出使用者所屬的 SonarQube Cloud 組織,包括索引鍵、名稱、ID 和 UUID。後續工具可使用傳回的索引鍵作為
organization。 -
search_my_sonarqube_projects - 搜尋 SonarQube 專案。回應採分頁形式。
organization- SonarQube Cloud 組織索引鍵 - Optional String, Cloud onlypageIndex- 可選的從 1 開始的頁面索引(預設值:1)- IntegerpageSize- 可選的頁面大小。必須大於 0 且小於或等於 500(預設值:500)- Integerq- 可選的搜尋查詢,用於依名稱(部分比對)或索引鍵(完全比對)篩選專案 - String
-
list_branches - 列出專案中已分析的分支。
- SonarQube Cloud:傳回長期(
LONG)和短期(SHORT)分支,以及type和mergeBranch欄位。可選的branchTypes篩選條件:ALL(預設值)、LONG或SHORT。 - SonarQube Server:傳回所有已分析的分支(名稱、品質閘道、分析日期)。不提供
type、mergeBranch或branchTypes篩選條件。 - 其他工具可使用傳回的分支名稱作為
branch參數。若要分析提取要求,請改用list_pull_requests。 projectKey- 專案索引鍵(例如 my_project)- Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)branchTypes- (僅限 SonarQube Cloud) 可選的篩選條件:ALL(預設值)、LONG或SHORT- Enum {"ALL", "LONG", "SHORT"}
- SonarQube Cloud:傳回長期(
-
list_pull_requests - 列出專案的所有提取要求。使用此工具探索已啟用 PR 裝飾分析(涵蓋率、問題、品質閘道)的提取要求。傳回可搭配其他工具使用的提取要求索引鍵/ID。若要進行不含提取要求的分支分析,請改用
list_branches。projectKey- 專案索引鍵(例如 my_project)- Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)
品質閘道
-
get_project_quality_gate_status - 取得 SonarQube 專案的品質閘道狀態。
analysisId- 可選的分析 ID - Stringbranch- 分支分析的可選分支名稱。使用list_branches探索有效名稱 - StringprojectId- 可選的專案 ID - StringprojectKey- 可選的專案索引鍵 - StringpullRequest- 可選的提取要求索引鍵/ID。使用list_pull_requests探索有效索引鍵 - String
-
list_quality_gates - 列出我的 SonarQube 中的所有品質閘道。
organization- SonarQube Cloud 組織索引鍵 - Optional String, Cloud only
規則
- show_rule - 顯示 SonarQube 規則的詳細資訊。
organization- SonarQube Cloud 組織索引鍵 - Optional String, Cloud onlykey- 規則索引鍵 - Required String
重複程式碼
-
search_duplicated_files - 搜尋 SonarQube 專案中含有重複程式碼的檔案。預設會自動擷取所有頁面中的重複檔案(最多 10,000 個檔案)。只傳回含有重複程式碼的檔案。
projectKey- 專案索引鍵 - Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 分支分析的可選分支名稱。使用list_branches探索有效名稱 - StringpullRequest- 可選的提取要求索引鍵/ID。使用list_pull_requests探索有效索引鍵 - StringpageSize- 手動分頁時每頁的可選結果數量(上限:500)。若未指定,則自動擷取所有重複檔案 - IntegerpageIndex- 手動分頁時的可選頁碼(從 1 開始)。若未指定,則自動擷取所有重複檔案 - Integer
-
get_duplications - 取得檔案的重複程式碼資訊。需要具備該檔案所屬專案的 Browse 權限。
key- 檔案索引鍵 - Required Stringbranch- 分支分析的可選分支名稱。使用list_branches探索有效名稱 - StringpullRequest- 可選的提取要求索引鍵/ID。使用list_pull_requests探索有效索引鍵 - String
原始碼
-
get_raw_source - 以原始文字形式取得 SonarQube 的原始碼。需要具備檔案的 'See Source Code' 權限。
key- 檔案索引鍵 - Required Stringbranch- 分支分析的可選分支名稱。使用list_branches探索有效名稱 - StringpullRequest- 可選的提取要求索引鍵/ID。使用list_pull_requests探索有效索引鍵 - String
-
get_scm_info - 取得 SonarQube 原始碼檔案的 SCM 資訊。需要具備檔案所屬專案的 See Source Code 權限。
key- 檔案索引鍵 - Required Stringcommits_by_line- 若值為 false,則依 SCM 提交分組行;否則顯示每一行的提交資訊 - Stringfrom- 要傳回的第一行。從 1 開始 - Numberto- 要傳回的最後一行(包含)- Number
系統
注意:只有連線至 SonarQube Server 時,才能使用系統工具。
-
get_system_health - 取得 SonarQube Server 執行個體的健康狀態。傳回 GREEN(完全正常運作)、YELLOW(可使用但需要注意)或 RED(無法運作)。
-
get_system_info - 取得 SonarQube Server 系統組態的詳細資訊,包括 JVM 狀態、資料庫、搜尋索引和設定。需要 'Administer' 權限。
-
get_system_logs - 以純文字格式取得 SonarQube Server 系統記錄。需要系統管理權限。
name- 要取得的記錄名稱(可選)。可能的值:access、app、ce、deprecation、es、web。預設值:app - String
-
ping_system - Ping SonarQube Server 系統以檢查其是否正常運作。以純文字形式傳回 'pong'。
-
get_system_status - 取得 SonarQube Server 的狀態資訊。傳回狀態(STARTING、UP、DOWN、RESTARTING、DB_MIGRATION_NEEDED、DB_MIGRATION_RUNNING)、版本和 ID。
Webhook
-
create_webhook - 為 SonarQube 組織或專案建立新的 webhook。需要具備指定專案的 'Administer' 權限,或全域 'Administer' 權限。
organization- SonarQube Cloud 組織索引鍵 - Optional String, Cloud onlyname- Webhook 名稱 - Required Stringurl- Webhook URL - Required StringprojectKey- 專案專屬 webhook 的可選專案索引鍵 - Stringsecret- 用於保護 webhook payload 的可選 webhook 密鑰 - String
-
list_webhooks - 列出 SonarQube 組織或專案的所有 webhook。需要具備指定專案的 'Administer' 權限,或全域 'Administer' 權限。
organization- SonarQube Cloud 組織索引鍵 - Optional String, Cloud onlyprojectKey- 用於列出專案專屬 webhook 的可選專案索引鍵 - String
情境擴充
架構工具
-
search_by_signature_patterns - 使用 regex 模式比對宣告簽章,尋找程式碼元素(類別、方法、介面等)。
include_code_regex_list- 用於比對簽章的 regex 模式清單 - Required String[]exclude_code_regex_list- 用於從結果中排除項目的 regex 模式清單 - String[]include_glob- 檔案篩選 glob 模式(例如*.java)- Stringexclude_glob- 檔案排除 glob 模式 - Stringfields- 要納入回應的欄位清單,以逗號分隔 - Stringlimit- 要傳回的結果數量上限(預設值:10)- Integerregex_lists_operator- 多個模式的組合方式:OR(預設值)或AND- String
-
search_by_body_patterns - 使用 regex 模式比對實作主體,尋找程式碼元素。適合用來定位 API 或模式的實際使用位置。
include_code_regex_list- 用於比對程式碼主體的 regex 模式清單 - Required String[]exclude_code_regex_list- 用於從結果中排除項目的 regex 模式清單 - String[]include_glob- 檔案篩選 glob 模式 - Stringexclude_glob- 檔案排除 glob 模式 - Stringfields- 要納入回應的欄位清單,以逗號分隔 - Stringlimit- 要傳回的結果數量上限(預設值:10)- Integerregex_lists_operator- 多個模式的組合方式:OR(預設值)或AND- String
-
get_upstream_call_flow - 追蹤哪些函式呼叫指定函式。適合用來找出所有呼叫端和進入點,並了解簽章變更可能造成的影響。
fqn- 函式的完整限定名稱 - Required Stringdepth- 呼叫鏈深度(0=僅函式本身,1=直接呼叫端,以此類推)- Integerfields- 要納入回應的欄位清單,以逗號分隔 - String
-
get_downstream_call_flow - 追蹤指定函式呼叫哪些函式。適合用於影響分析和了解執行流程。
fqn- 函式的完整限定名稱 - Required Stringdepth- 呼叫鏈深度(0=僅函式本身,1=直接被呼叫的函式,以此類推)- Integerfields- 要納入回應的欄位清單,以逗號分隔 - String
-
get_source_code - 依完整限定名稱取得程式碼元素的完整原始碼(簽章和主體)。
fqn- 元素的完整限定名稱 - Required Stringfields- 要納入回應的欄位清單,以逗號分隔 - String
-
get_type_hierarchy - 取得類別型結構(類別、介面、列舉、記錄、例外、結構)的完整繼承階層。這對了解繼承樹和重構至關重要。
fqn- 類別型結構的完整限定名稱 - Required Stringfields- 要納入回應的欄位清單,以逗號分隔 - String
-
get_references - 取得類別或模組的直接輸入和輸出程式碼參照。只傳回直接(非遞移)參照。
fqn- 類別或模組的完整限定名稱 - Required Stringfields- 要納入回應的欄位清單,以逗號分隔 - String
-
get_current_architecture - 取得依路徑前綴和深度篩選的階層式架構圖。適合用來探索模組結構和高階相依性。
depth- 階層深度(0=僅根節點,1=根節點及其子節點,以此類推)- Required Integerpath_prefix- 用於篩選節點的可選路徑前綴(例如com.example.service)- Stringecosystem- 用於篩選的可選生態系統(java、cs、py、js、ts)- String
-
get_intended_architecture - 取得使用者定義的架構限制,指定哪些模組可以依賴其他模組。
Guidelines Tools
- get_guidelines - 根據 SonarQube 專案問題、目錄類別,或兩者的組合,取得程式碼撰寫指南。
mode- 指南擷取模式:project_based、category_based或combined- Required Stringcategories- 類別名稱清單(category_based和combined模式必填)- String[]languages- SonarQube 儲存庫金鑰格式的目標語言清單(提供categories時必填)- String[]file_paths- 用於篩選指南的選填檔案路徑清單 - String[]
Third-party Dependency Tools
- check_dependency - 在新增或更新第三方相依性之前,檢查其安全性漏洞、供應鏈惡意軟體及授權合規性。
purl- 含版本的套件 URL(purl),格式依據 purl-spec。格式:pkg:<type>/<namespace>/<name>@<version>(例如pkg:npm/lodash@4.17.21、pkg:maven/org.apache.logging.log4j/log4j-core@2.14.1、pkg:pypi/django@3.2.0)- Required String
Context Augmentation Environment Variables
| 變數 | 說明 | 必填 | 預設值 |
|---|---|---|---|
SONARQUBE_URL | SonarQube Cloud URL | 是 | https://sonarcloud.io |
SONARQUBE_TOKEN | 驗證權杖 | 是 | 無 |
SONARQUBE_ORG | Cloud 組織;從啟動設定或唯一偵測結果傳遞給 Proxy | 否 | 無 |
SONARQUBE_PROJECT_KEY | SonarQube Cloud 上的專案金鑰 | 是 | 無 |
SONAR_SQ_BRANCH | 明確指定 SonarQube 分支覆寫值 * | 否 | 無 |
SONARQUBE_DEBUG_ENABLED | 啟用偵錯記錄(用於疑難排解) | 否 | False |
SONAR_LOG_LEVEL | 記錄詳細程度(TRACE、DEBUG、INFO、WARNING、ERROR) | 否 | INFO |
- 不使用 git,或 git 分支名稱與 SonarQube 中的分支名稱不符時,必須提供此值。
Project-Specific Configuration (Recommended)
首先,將有效的專案 Personal Access Token (PAT) 匯出為 SONARQUBE_TOKEN 環境變數。
# macOS/Linux (Bash/Zsh)
export SONARQUBE_TOKEN="{<YourUserToken>}"
接著,掛載專案工作區,讓 Context Augmentation 伺服器能直接存取原始程式碼檔案:
{
"mcpServers": {
"sonarqube-mcp-server": {
"command": "docker",
"args": [
"run", "-i", "--rm", "--pull=always",
"-e", "SONARQUBE_URL",
"-e", "SONARQUBE_TOKEN",
"-e", "SONARQUBE_ORG",
"-e", "SONARQUBE_PROJECT_KEY",
"-e", "SONARQUBE_TOOLSETS",
"-v", "/ABSOLUTE/PATH/TO/YOUR/PROJECT:/app/mcp-workspace:rw",
"sonarsource/sonarqube-mcp"
],
"env": {
"SONARQUBE_URL": "https://sonarcloud.io",
"SONARQUBE_ORG": "<YourOrganizationKey>",
"SONARQUBE_PROJECT_KEY": "<YourProjectKey>",
"SONARQUBE_TOOLSETS": "cag"
}
}
}
}
重要:在專案範圍的設定中,請勿將 SONARQUBE_TOKEN 放入 env 區塊。請將它匯出為環境變數(export SONARQUBE_TOKEN=...)。Docker 會透過 -e SONARQUBE_TOKEN 將它傳遞至容器。
Agentic Readiness
注意:Agentic Readiness 工具僅適用於 SonarQube Cloud,且必須為您的組織啟用此功能。
在 stdio 模式下,系統會於啟動時檢查功能旗標。透過 HTTP/HTTPS 使用時,請將 agentic-readiness 加入伺服器的 SONARQUBE_TOOLSETS,以啟用這些工具。
-
start_agentic_readiness_assessment - 為專案啟動 agentic readiness 評估。此工具會立即傳回狀態
PENDING和assessmentId。使用get_agentic_readiness_assessment輪詢結果。projectKey- 專案金鑰 - Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 要評估的分支。省略時使用專案的預設分支 - String
-
get_agentic_readiness_assessment - 擷取評估結果。使用相同的
assessmentId重新呼叫,直到狀態為COMPLETED、FAILED或INTERRUPTED。完成後,會傳回整體等級,以及各支柱的細項結果、建議行動和佐證。assessmentId-start_agentic_readiness_assessment傳回的評估 ID - Required String
-
list_agentic_readiness_assessments - 依時間由新到舊列出專案的所有評估。使用
get_agentic_readiness_assessment取得完整的支柱層級結果。projectKey- 要列出評估的專案金鑰 - Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)branch- 依分支名稱篩選評估。省略時列出所有分支的評估 - StringpageIndex- 從 1 開始的頁面索引(預設值:1)- NumberpageSize- 每頁項目數,上限為 100(預設值:50)- Number
Architecture
注意:在 stdio 或 HTTP/HTTPS 模式下,使用 SONARQUBE_TOOLSETS=architecture 啟用這些工具。SonarQube Cloud 需要 Team 或 Enterprise 方案。SonarQube Server 需要 2026.6 或更新版本,且須為 Developer 版或更高版本;較舊版本或 Community Build 不提供這些工具。 請參閱架構工作流程與復原。
-
architecture_get - 讀取目前相依性、預期架構、專案指令或 Findings(偏差、缺陷和程式碼異味)。
target-current、intended、directives或findings;是否可用取決於 MCP 和 Server 組建版本 - Required EnumprojectKey- 專案金鑰 - Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)organization- 未設定時使用的 SonarQube Cloud 組織金鑰 - Optional String, Cloud onlycomponent- 完全相符的目前節點金鑰或預期群組路徑;省略時使用頂層 - Stringdepth- 僅適用於目前/預期架構:起始位置以下的層級;目前架構預設為 1,預期架構預設為全部 - Numberlanguage- 僅適用於目前/預期架構/Findings:java、cs、js、ts或py- EnumdirectiveId- 依 UUID 取得完整指令,包含說明;用來取代分頁 - Stringcategory/type- 僅適用於 Findings:依其公布的類型篩選偏差、缺陷或程式碼異味 - EnumpageIndex- 僅適用於指令/Findings:從 1 開始的頁面索引(預設值:1)- NumberpageSize- 指令:摘要,預設/上限為 50。Findings:預設為 50,上限為 1000;0 表示只計數 - Number
-
architecture_set - 取代整個預期架構,或建立、更新及刪除專案指令。在支援的 Cloud 和 Server 組建版本中,啟用寫入功能後即可使用。
target-intended或directives- Required EnumprojectKey- 專案金鑰 - Required String (若已定義SONARQUBE_PROJECT_KEY,則忽略)organization- 未設定時使用的 SonarQube Cloud 組織金鑰 - Optional String, Cloud onlymodel- JSON 字串:完整的預期架構匯出內容或models陣列(未提供的內容會被移除),或包含create、update和delete陣列的指令變更 - Required StringdryRun-true用於預覽,false用於套用 - Required Booleanrevision- 僅適用於預期架構:來自architecture_get的修訂版本;預設為完整匯出內容的修訂版本。套用清除操作時必填 - String
Example Prompts
設定好 SonarQube MCP Server 後,以下是一些常見實際情境的提示範例:
修復未通過的品質閘門
My quality gate is failing for my project. Can you help me understand why and fix the most critical issues?
The quality gate on my feature branch is red. What do I need to fix to get it passing before I can merge to main?
發佈前與合併前檢查
I'm about to merge my pull request <#247> for the <web-app> project. Can you check if there are any quality issues I should address first?
We're deploying to production tomorrow. Can you check the quality gate status and alert me to any critical issues in this branch?
改善程式碼品質
I want to reduce technical debt in my project. What are the top issues I should prioritize?
Our code coverage dropped below 70%. Can you identify which files have the lowest coverage and help me improve it?
了解並修復問題
I have 15 new code smells in my latest commit. Can you explain what they are and help me fix them?
SonarQube flagged a critical security vulnerability in <AuthController.java>. What's the issue and how do I fix it?
安全性與相依性管理
We need to pass a security audit. Can you check all our projects for security vulnerabilities and create a prioritized list of what needs to be fixed?
Are there any known vulnerabilities in our dependencies? Check this project for dependency risks.
程式碼審查協助
I just wrote this authentication function. Can you analyze it for security issues and code quality problems before I commit?
Review the changes in <src/database/migrations> for any potential bugs or security issues.
專案健康狀況監控
Give me a health report for my project: quality gate status, number of bugs, Security Hotspots, and code coverage.
Compare code quality between our main branch and the develop branch. Are we introducing new issues?
團隊協作
What are the most common rule violations across all our projects? We might need to update our coding standards.
Show me all the issues that were marked as false positives in the last month. Are we seeing patterns that suggest our rules need adjustment?
Build
建議使用 sonarsource/sonarqube-mcp 容器映像。
若要在不使用 Docker 的情況下以獨立 JAR 執行伺服器,請從 SonarSource binaries repository 下載預先建置的版本。每個已發佈版本都會以 sonarqube-mcp-server-<version>.jar 的形式發布於該處(例如 sonarqube-mcp-server-1.19.0.2785.jar)。
從 JAR 執行
從 binaries repository 下載所需版本的 JAR,然後設定 MCP 用戶端,使用 Java 21 或更新版本執行:
- 連線至 SonarQube Cloud:
{
"sonarqube": {
"command": "java",
"args": [
"-jar",
"<path_to_sonarqube_mcp_server_jar>"
],
"env": {
"STORAGE_PATH": "<path_to_your_mcp_storage>",
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_ORG": "<org>"
}
}
}
- 連線至 SonarQube Server:
{
"sonarqube": {
"command": "java",
"args": [
"-jar",
"<path_to_sonarqube_mcp_server_jar>"
],
"env": {
"STORAGE_PATH": "<path_to_your_mcp_storage>",
"SONARQUBE_TOKEN": "<token>",
"SONARQUBE_URL": "<url>"
}
}
}
從原始碼建置
建置 SonarQube MCP Server 需要 Java Development Kit (JDK) 21 或更新版本。
執行以下 Gradle 命令以清理專案並建置應用程式:
./gradlew clean build -x test
JAR 檔案會建立在 build/libs/。
新增或更新相依性後,請重新產生鎖定檔案:
./gradlew :dependencies --write-locks
./gradlew :its:dependencies --write-locks
使用上方的 從 JAR 執行 設定,並將 <path_to_sonarqube_mcp_server_jar> 指向 build/libs/ 中的 JAR。
Troubleshooting
應用程式記錄預設會寫入 STORAGE_PATH/logs/mcp.log 檔案。若要完全停用檔案記錄,請設定 SONARQUBE_LOG_TO_FILE_DISABLED=true。
Common Issues
"SonarQube server version is not supported"
MCP 伺服器會在啟動期間檢查連線的 SonarQube Server 版本。如果執行個體版本早於 2025.1(SQS)或 25.1(SQCB),啟動會失敗並顯示:
SonarQube server version is not supported, minimal version is SQS 2025.1 or SQCB 25.1
**解決方案:**將 SonarQube Server 升級至支援的版本。此檢查僅適用於連線至 SonarQube Server(SONARQUBE_URL 且未設定 SONARQUBE_ORG)時,不適用於 SonarQube Cloud。
"Feature is not working" or "Missing tools/functionality"
您可能正在使用過時的 Docker 映像。Docker 會在本機快取映像,因此不會自動取得更新。
**解決方案:**更新至最新版本:
docker pull sonarsource/sonarqube-mcp
取得最新映像後,請重新啟動 MCP 用戶端以使用更新版本。
您也可以在 docker run 命令中加入 --pull=always 旗標,讓 Docker 一律檢查並取得最新版本:
docker run --init --pull=always -i --rm -e SONARQUBE_TOKEN -e SONARQUBE_ORG sonarsource/sonarqube-mcp
"I want to pin to a specific version"
請在 sonarsource/sonarqube-mcp 瀏覽可用標籤,並指定所需版本:
docker pull sonarsource/sonarqube-mcp:1.19.0.2785
docker run --init -i --rm \
-e SONARQUBE_TOKEN -e SONARQUBE_ORG \
sonarsource/sonarqube-mcp:1.19.0.2785
在 MCP 用戶端設定中,使用 sonarsource/sonarqube-mcp:<version> 取代 sonarsource/sonarqube-mcp,並移除 --pull=always,避免 Docker 在未提示的情況下升級映像。
Data and telemetry
此伺服器會收集匿名使用資料並傳送給 SonarSource,以協助改進產品。系統不會收集原始碼或 IP 位址,SonarSource 也不會與任何其他人分享這些資料。您可以使用以下系統屬性或環境變數停用遙測資料收集:TELEMETRY_DISABLED=true。按一下此處,查看所收集資料的範例。
授權條款
Copyright 2025 SonarSource.
本軟體依據 SONAR Source-Available License v1.0 授權。依照本文件使用 SonarQube MCP Server 屬於非競爭用途,因此符合 SSAL 的允許範圍。
您透過 MCP 使用 SonarQube,須遵守 SonarQube Cloud 服務條款 或 SonarQube Server 條款與條件,包括僅將結果資料用於您的內部軟體開發用途。