SuperAPIAI Directory

sonar-mcp-server

github.com · AI coding

Read-only access to self-hosted SonarQube Community Build: issues, security hotspots, rules and code snippets so an agent can fix findings locally.

Open website Alternatives
PricingOpen source开源项目(可免费自部署)
PlatformsLinux

About this tool

Purpose

sonar-mcp-server is a local MCP (Model Context Protocol) server that provides read-only access to a self-hosted SonarQube Community Build 26.4+ instance. It enables AI coding agents (Claude Code, Cursor, VS Code Copilot, etc.) to retrieve a project's issue list, the files and locations where issues occur, rule descriptions, source code snippets near issues, and security hotspots, so that code issues can be fixed locally.

The typical scenario is "find and fix Sonar issues in a project": the LLM calls listIssues, optionally calls getRule and getIssueSnippets, and then edits files locally.

Main capabilities

The server exports 13 read-only MCP tools covering the following operations: listing components, issues, and hotspots; drilling into a single finding; retrieving rule descriptions; and pulling source code snippets near issue locations. The toolset is deliberately kept lean, focused only on the "diagnose -> fix code locally" workflow, and does not mirror the full SonarQube API.

All tools are read-only: the server does not create, modify, or delete anything in SonarQube, including not marking issues as false positives, not editing comments, and not calling administrative endpoints.

Usage

  1. Install JDK 25+.
  2. Download sonar-mcp-server.jar from the latest release, or build it yourself:./gradlew bootJar. A Docker image is also provided.
  3. Obtain the SonarQube URL and user token.
  4. Add the JAR to the AI client's MCP configuration.

The server supports only stdio transport. The AI client starts the server as a subprocess and communicates over stdin/stdout using the MCP protocol. The server does not open any HTTP port and does not accept incoming connections.

The configuration requires the SonarQube URL and token; the rest is optional. The MCP tool list is divided into four groups, each controlled by an environment flag, all enabled by default. A group can be set to false (e.g. SONAR_MCP_TOOLS_HOTSPOT=false) to narrow the list, which is suitable for local models with a smaller context.

Obtaining the SonarQube URL: open SonarQube in a browser, copy the address from the address bar, remove the path, and keep only the scheme and host. If it can only be accessed via IP, use the IP directly; if it is deployed under a subpath (e.g. /sonar), the URL must also include that subpath.

Obtaining a token: log in to SonarQube, open My Account -> Security, enter a token name under Generate Tokens and select the User Token type, then click Generate. The token is shown only once and must be copied immediately to use as SONAR_TOKEN. If the token is lost, it can only be regenerated.

The server uses HTTP Bearer authentication, passing the token in the Authorization header. Logs are written to ${SONAR_MCP_DATA_DIR:-~/.sonar-mcp-server}/logs/sonar-mcp-server.log, rotating by date and size: 10MB, retained for 30 days, with a total limit of 512MB.

The Docker image is published to GHCR with each release. The MCP client should start it with the same command (-i keeps stdin open to support stdio transport). Mounting a host directory to /data preserves logs. If SonarQube runs on the same machine, use its hostname instead of localhost, or add --network host on Linux. Building the image locally:docker build -t sonar-mcp-server ..

After adding the configuration, the client must be restarted. CLI clients also have commands to view and remove registrations:claude mcp list / claude mcp remove --scope user sonar(similar to qwen).

Branches and pull requests

Sonar treats branch and pull request analysis as two independent and mutually exclusive scopes. The Sonar web-api accepts only one of branch= or pullRequest= in a single request, not both.

  • branch: long-lived branches (main, develop, feature/...). Resolution order is: explicit branch parameter → SONAR_DEFAULT_BRANCH → none (Sonar uses the project's main branch).
  • pullRequest: the Sonar PR key, usually the PR/MR number. Independent from branch analysis; PR analysis usually contains the most relevant findings for work in progress. The PR key does not fall back to a server-level default and must be passed explicitly.

Passing both branch and pullRequest in the same tool call is an error. Use listProjectBranches / listProjectPullRequests to discover available references.

Branch scope matters. Each Sonar branch is an independent analysis: the number of unresolved issues on main and a feature branch can differ greatly, because a feature branch may have new fixes (or new code) that have not yet been merged. If the agent silently omits branch=, it reads from main (or SONAR_DEFAULT_BRANCH), may miss work already completed on the feature branch, or even duplicate fixing the same issue. When the user's local git is on feature/..., the agent should first call listProjectBranches, find the matching Sonar branch, and pass it explicitly in every issue / summary / breakdown call.

As a server-side safety net, when (1) neither branch nor pullRequest is passed, and (2) the project has other analyzed branches in Sonar, listIssues, getProjectIssuesSummary, and getProjectIssuesBreakdown append a branchAdvisory field to the response. This field contains the valid (main) branch name and a list of non-main branches sorted by the most recent analysisDate.In other cases the field is absent.

Tech stack

Java 25, Spring Boot 4.0.0, Spring AI MCP 2.0.0 (stdio transport), Jackson 3 (tools.jackson) for JSON, Gradle 9.3.1 with version catalog (gradle/libs.versions.toml).

Windows build:.\gradlew.bat build. Output: build/libs/sonar-mcp-server.jar.

Applicable Scenarios

Suitable for teams using self-hosted SonarQube Community Build 26.4+ who want to let AI coding agents read Sonar findings and fix code locally. The official SonarSource MCP server targets SonarQube 10+ and SonarCloud and assumes cloud-style deployment; this server is built specifically for self-hosted installations that expose the classic /api/ web-api, supports Standard Experience and MQR mode, and handles each software quality impact in MQR mode.

Limitations

  • Only stdio transport is supported; no HTTP port is opened.
  • All tools are read-only and do not modify SonarQube data.
  • HTTP timeouts and retry policies are not yet individually configurable.
  • The Sonar API uses page-based pagination (p / ps); tools accept offset / limit, and when offset is not a multiple of limit it is rounded down to the nearest page boundary. Sonar also caps ps at 500.
  • componentPathPrefix is a client-side filter: the server paginates through the project's issues/hotspots and keeps items whose componentPath starts with that prefix (directory-boundary safe). The scan has an upper limit (SONAR_MCP_PATH_FILTER_MAX_SCANNED_ISSUES, default 10000); when the limit is reached the response sets pathPrefixTruncated=true and the caller should tighten the prefix.
  • The author field on an Issue is the SCM author of the line where the issue is located (Sonar populates this when an SCM provider is configured). Sonar does not return a separate scmAuthor / scmDate field in issues/search; for line-level SCM use getIssueSnippets.

Security and Operations

The server is designed to run locally alongside the AI client, does not open an HTTP port, and does not accept incoming network connections. The server runs with the permissions of the SonarQube user corresponding to the token in SONAR_TOKEN. The available projects and issues are determined by the user's permissions in SonarQube . Tokens should be treated as confidential and must not be committed to repositories, shell scripts, .vscode/mcp.json, .cursor/mcp.json, or any shared project files.

The data the AI client receives is exactly the data it requests through the MCP tools: issue lists (including rules, severities, types, file paths, line numbers, messages, tags, SCM authors), issue change history, Sonar rule descriptions (including HTML/markdown), source code snippets near issue locations (via getIssueSnippets), security hotspots and their details. Before connecting an external or cloud AI client, check your company's internal policy: source code may contain trade secrets.

Troubleshooting

  • "Gradle requires JVM 17 or later": set JAVA_HOME to JDK 25+.
  • Connection refused / 401: check the URL and token. Test:curl -H "Authorization: Bearer $SONAR_TOKEN" "$SONAR_URL/api/components/search?qualifiers=TRK&p=1&ps=1". The expected response is a JSON project list. 401 means the token is invalid or expired; 403 means the user lacks API permissions.
  • 403 Forbidden: the token user does not have permission for the project or web-api; check the roles in SonarQube.
  • Package/module scope returns 0 issues: pass a path-style prefix as componentPathPrefix (such as bc-doc/src/main/java/ru/foo), not a Sonar component key. Convert Java/Kotlin package dots to slashes. The filter is directory-boundary safe, so bc-doc/src will not match bc-doc/srcExtra.
  • pathPrefixTruncated=true in the response: the client-side scan hit SONAR_MCP_PATH_FILTER_MAX_SCANNED_ISSUES before reaching the end. Tighten the prefix to reduce the scan, or raise the limit if 10k is genuinely not enough.

Integration tests require an accessible SonarQube and real data. Unit tests exclude the integration tag by default.

Up/Down to navigate · Enter to open · ESC to closeBrowse all tools