SonarQube分析服务器

一个基于模型上下文协议(MCP)的服务器,允许AI助手与SonarQube交互,查询项目、审核拉取请求、定位重复代码并检查质量门限。

  • by viamus
  • C#
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 viamus/mcp-sonarqube

使用说明

MCP SonarQube服务器

A. 模型上下文协议(MCP) 该服务器允许人工智能助手与SonarQube通信——查询项目、深入问题、审计拉取请求、定位重复和检查质量门——所有这些都是通过一个HTTP传输完成的。

亮点

  • 一次通话中的公关分析 — analyze_pull_request 并行聚合质量门、新代码度量、问题和安全热点
  • 找到有问题的文件 — get_component_tree_measures 按文件/目录分解聚合指标,以便代理知道在哪里修复
  • 精确复制块 — get_duplications 返回行范围和配对文件
  • 清除错误 --声纳误差体(errors[].msg)通过每个包装器传播

快速开始

# 1. Clone the repository
git clone https://github.com/viamus/mcp-sonarqube.git
cd mcp-sonarqube

# 2. Configure credentials
cp .env.example .env
# Edit .env with your SonarQube URL and token

# 3. Run the server
docker compose up -d

先决条件

要求 版本 目的
.NET SDK 10.0+ 构建并运行
Docker 最新 容器部署(可选)
SonarQube实例 9.x+ API访问

必修的:

  • SonarQube基本URL(例如。, https://sonarqube.example.com)
  • SonarQube用户令牌(生成位置:您的SonarQube>我的账户>安全>令牌)

可用工具

13个MCP工具,按领域分组:

领域 工具 目的
拉取请求 analyze_pull_request 聚合PR视图——质量门、新代码度量、问题、安全热点(4个并行调用)
项目 search_projects 按名称或关键字搜索项目
get_project_status 质量门状态+项目关键措施
问题 search_issues 使用过滤器搜索问题;支持 pullRequest 范围界定
措施 get_measures 获取组件的指标
get_component_tree_measures 按文件/目录分解度量值——查找违规文件
重复 get_duplications 精确的复制块(行范围+成对文件)
质量门 list_quality_gates 列出所有闸门及其状态
热点 search_hotspots 搜索安全热点
get_hotspot 热点详细信息
规则 search_rules 按语言/严重性/标签搜索编码规则
系统 get_health SonarQube实例的运行状况

对于每个工具包装的底层SonarQube端点,请参阅 API 参考 在......下面


运行选项

选项1:Docker Compose(推荐)

docker compose up -d

服务器将在以下时间可用 http://localhost:8082.

选项2:。NET-CLI

dotnet run --project src/Viamus.Sonarqube.Mcp.Server

服务器将在以下时间可用 http://localhost:5100.

选项3:独立可执行文件

dotnet publish src/Viamus.Sonarqube.Mcp.Server -c Release -o ./publish
./publish/Viamus.Sonarqube.Mcp.Server

客户端配置

克劳德桌面版

添加到您的Claude Desktop配置(claude_desktop_config.json):

{
  "mcpServers": {
    "sonarqube": {
      "url": "http://localhost:8082"
    }
  }
}

克劳德代码

claude mcp add sonarqube --scope user --transport http http://localhost:8082

使用示例

端到端审核拉取请求

Analyze pull request 1234 on the "my-app" project. Did it break the gate?
If yes, find which files concentrate the new duplicated lines and show me
the exact duplication blocks.

在引擎盖下,代理通常会连锁 analyze_pull_request → get_component_tree_measures (按排序 new_duplicated_lines) → get_duplications 最糟糕的文件。

搜索项目

Search for all projects containing "backend" in my SonarQube instance.

检查工程质量

What is the quality gate status for the "my-app" project? Show me the coverage and bug count.

查找关键问题

Search for all CRITICAL and BLOCKER severity issues in the "my-app" project.

查看安全热点

Show me all security hotspots that need review in the "my-app" project.

搜索编码规则

Find all MAJOR severity rules for C# language.

检查系统运行状况

Is my SonarQube instance healthy?

配置

环境变量

变量 必填 描述
SONARQUBE_BASE_URL 是 SonarQube实例URL
SONARQUBE_TOKEN 是 SonarQube用户令牌
SONARQUBE_ORGANIZATION 否 SonarCloud组织密钥
SERVER_REQUIRE_API_KEY 否 启用API密钥身份验证(默认值: false)
SERVER_API_KEY 没有用于服务器访问的 neneneba API密钥

应用程序参数

{
  "SonarQube": {
    "BaseUrl": "https://your-sonarqube-instance.com",
    "Token": "your-token-here",
    "Organization": ""
  },
  "ServerSecurity": {
    "RequireApiKey": false,
    "ApiKey": ""
  }
}

用户秘密(开发)

cd src/Viamus.Sonarqube.Mcp.Server
dotnet user-secrets set "SonarQube:BaseUrl" "https://your-sonarqube-instance.com"
dotnet user-secrets set "SonarQube:Token" "your-token-here"
dotnet user-secrets set "SonarQube:Organization" "your-sonarcloud-organization"

故障排除

常见问题

连接被拒绝

  • 验证SonarQube基本URL是否正确且可访问
  • 检查服务器是否正在运行: curl http://localhost:8082/health

401未经SonarQube授权

  • 验证您的令牌是否有效且未过期
  • 在以下位置生成新代币:您的SonarQube>我的账户>安全>代币

未找到项目

  • 确保您的令牌具有足够的权限
  • 验证SonarQube实例中是否存在该项目

Docker容器未启动

  • 检查日志: docker compose logs mcp-sonarqube
  • 验证 .env 文件存在并且包含有效凭据

项目结构

mcp-sonarqube/
├── src/Viamus.Sonarqube.Mcp.Server/
│   ├── Configuration/          # Settings classes
│   ├── Middleware/              # API key authentication
│   ├── Models/                  # SonarQube API DTOs
│   ├── Services/                # HTTP client for SonarQube API
│   ├── Tools/                   # MCP tool implementations (13 tools)
│   └── Program.cs               # Entry point
├── tests/Viamus.Sonarqube.Mcp.Server.Tests/
│   ├── Configuration/           # Settings and middleware tests
│   ├── Models/                  # Serialization tests
│   └── Tools/                   # Tool behavior tests
├── docker-compose.yml
└── Solution.slnx

API 参考

使用的SonarQube API端点

端点 工具
/api/projects/search search_projects
/api/qualitygates/project_status get_project_status, analyze_pull_request
/api/qualitygates/list list_quality_gates
/api/measures/component get_project_status, get_measures, analyze_pull_request
/api/measures/component_tree get_component_tree_measures
/api/duplications/show get_duplications
/api/issues/search search_issues, analyze_pull_request
/api/hotspots/search search_hotspots, analyze_pull_request
/api/hotspots/show get_hotspot
/api/rules/search search_rules
/api/system/health get_health

发展

建筑

dotnet build Solution.slnx

测试

dotnet test Solution.slnx

添加新工具

看 贡献.md 详细说明。


许可证

此项目根据MIT许可证获得许可-请参阅 许可证 文件以获取详细信息。