# AI agents

SonarQube for IDE's code analysis workflow is enhanced by a set of tools designed to integrate with AI agents. These tools allow you to interact with AI agents in natural language to perform specific SonarQube-related tasks directly within your IDE. This integration streamlines your development process by bringing the power of SonarQube analysis into your AI-assisted conversations, helping you focus on and resolve code quality and security issues more efficiently.

### SonarQube MCP Server

The SonarQube MCP Server is a Model Context Protocol (MCP) server that runs locally and enables a seamless connection between your AI agents and your SonarQube platform. The tools are designed to bridge the divide between productivity and quality. Please see the full details in the [SonarQube MCP Server](https://docs.sonarsource.com/sonarqube/latest/sonarqube-mcp-server/) documentation.

Using SonarQube MCP Server together with the SonarQube for IDE plugin allows the MCP server to trigger analyses directly in the editor, just like regular on-the-fly analysis by SonarQube for IDE. This powerful combination helps AI agents deliver more reliable, maintainable, and secure code.

### GitHub Copilot plugin

SonarQube for IntelliJ includes tools for the GitHub Copilot plugin. The interactive tools come automatically with your installation and are best accessed with the Copilot **Agent** mode enabled.

1. To add the SonarQube MCP Server, open **GitHub Copilot Chat** and set Copilot to **Agent** mode.&#x20;
2. Select the <picture><source srcset="https://2586659349-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNvI4wotPmITyM0mnsmtp%2Fuploads%2FxSIVnwxtbuJejHZODrP3%2Fmcp-dark-mode.png?alt=media&#x26;token=28fd1dcc-eae2-40d8-a51b-fe4d714666c5" media="(prefers-color-scheme: dark)"><img src="https://2586659349-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNvI4wotPmITyM0mnsmtp%2Fuploads%2FYBGY9WaFQgLb6MpwuZaL%2Fmcp-icon-optimized.svg?alt=media&#x26;token=c190d3dd-7bfe-471f-a320-94660ebd0c56" alt=""></picture> icon at the top of your chat session; when the GitHub MCP Registry opens, search for `sonar`, choose `io.github.SonarSource/sonarqube-mcp-server`, and select **Install** to add the SonarQube MCP Server to your Copilot tools.&#x20;
3. Once installed, add your details to the mcp.json file created in your %LOCALAPPDATA%\github-copilot\intellij folder (.config/github-copilot/intellij in MacOS). Edit it directly with your [Environment variables #Common variables](https://app.gitbook.com/s/xNksbUaDXyfRoTpHP0vQ/build-and-configure/environment-variables#common-variables "mention"). See the code sample [below](#setup-in-intellij-using-the-github-mcp-registry) as an example.
   1. It's also possible to add your common variables via the **GitHub Copilot MCP Log** window in the IntelliJ Tool window bar; select **Edit Config** to open the mcp.json config file.&#x20;
4. Finally, when you've configured your variables, return to the **GitHub Copilot Chat** window and select **Configure Tools** to expose all of your Copilot tools. Find the **sonarqube-mcp-server** and select **Start** to run the SonarQube MCP Server.

<details>

<summary>Setup in IntelliJ using the GitHub MCP Registry</summary>

#### Setup in IntelliJ

The setup instructions below contain code samples for both SonarQube Cloud and SonarQube Server when using the GitHub Copilot plugin for IntelliJ and the [GitHub MCP Registry](https://github.com/mcp/SonarSource/sonarqube-mcp-server).

{% hint style="info" %}
Although the examples below use `docker`, any OCI-compatible container runtime works (for example, Podman, nerdctl, etc). Simply replace `docker` with commands specific to your preferred tool.
{% endhint %}

```json
{
  "servers": {
    "io.github.SonarSource/sonarqube-mcp-server": {
      "type": "stdio",
      "command": "docker",
      "args": [
        "run",
        "-i",
        "--rm",
        "docker.io/mcp/sonarqube:latest"
      ],
      "env": {
        "SONARQUBE_TOKEN": "", //Only User tokens are allowed.
        "SONARQUBE_ORG": "", //Use for connection to SonarQube Cloud.
        //"SONARQUBE_URL": "https://sonarqube.us", //For an organization in the SonarQube Cloud US region.
        "SONARQUBE_URL": "" //Use for connection to SonarQube Server.
      },
      "x-metadata": {
        "registry": {
          "api": {
            "baseUrl": "https://api.mcp.github.com",
            "version": "v0.1"
          },
          "mcpServer": {
            "name": "io.github.SonarSource/sonarqube-mcp-server",
            "version": "1.12.0"
          }
        }
      }
    }
  }
}
```

{% hint style="warning" %}
*User tokens* are required when setting up connected mode or an MCP Server between SonarQube (Server, Cloud) and SonarQube for IDE. Note that the binding will not function properly if *project tokens*, *global tokens*, or *scoped organization tokens* are used during the setup process.
{% endhint %}

</details>

#### **Instructions for the AI agent**

To get the best results, give the agent clear instructions so it uses the SonarQube MCP Server effectively. For GitHub Copilot, you can add a `copilot-instructions.md` file in the `.github` folder at the root of your project. That file is read by the agent each time you interact with it.

Once the MCP Server is configured, you can ask the agent in natural language to analyze code snippets, check dependency risks, change issue status (for example Accept or False Positive), list issues in your project or current file, summarize project status (coverage, duplication, open issues), or list issues for a pull request. Providing project context (such as build files with SonarQube properties) and defining guardrails in `copilot-instructions.md` helps the agent target the right project and use the MCP Server effectively.

### Setup with Claude Code

Details coming soon.

### The SonarQube MCP Server tools

Once you’ve set up the Sonar MCP Server with your IDE, you can start using the SonarQube MCP Server [Tools](https://app.gitbook.com/s/xNksbUaDXyfRoTpHP0vQ/using/tools "mention").
