BetaDeveloper

Make your agent verify its code

How to set up a verification loop to ensure your agent reanalyzes the code it writes or modifies and fixes the detected issues.

Note: This feature is in Beta stage and is only available with SonarQube Cloud projects for organizations in the Team and Enterprise plans. See the Product release lifecycle page for more information on release stages.

This guide walks you through the setup of Agentic Analysis to ensure your agent verifies the code it writes or modifies.

Several setups are available. Choose the one that best fits your workflow.

General prerequisites

Before proceeding, ensure you meet the following prerequisites.

SonarQube Cloud admin settings

Agentic Analysis is enabled in your SonarQube Cloud organization's admin settings.

Project settings

Ensure that your project is:

Warning: Make sure your project is analyzed in SonarQube Cloud at least once after the Agentic Analysis service is enabled for the organization.

Claude Code with SonarQube CLI

Set up Agentic Analysis with Claude Code and SonarQube CLI.

Use the SonarQube CLI and the SonarQube plugin for Claude Code to enable Agentic Analysis in your Claude sessions. Once configured, Claude automatically runs SonarQube’s remote analysis after edits and surfaces findings directly in the chat. Also trigger Agentic Analysis on-demand with the SonarQube CLI.

Option 1: Install with the SonarQube plugin

Prerequisites specific to this setup

Make sure you have Claude Code installed on your machine.

Step 1: Install the SonarQube plugin in Claude Code

Inside a Claude Code session:

  1. Run /plugin in Claude Code, open the Discover tab, and install sonarqube from the official Claude plugin marketplace.

  2. Reload SonarQube’s skills and hooks in Claude:

    • Either restart Claude Code, or

    • Run /reload-plugins if your version supports it.

After reload, you should see skills such as:

  • /sonarqube:sonar-integrate

  • /sonarqube:sonar-analyze

These commands use the CLI and the SonarQube MCP server that you configure in the next step.

Step 2: Run the integration skill

From a Claude session opened in your project folder, run the SonarQube integration skill and specify your SonarQube Cloud project key:

The skill does the following:

  • Checks and updates the SonarQube CLI and authenticates to it.

  • Wires Claude Code via sonar integrate claude . This command:

    • Registers the SonarQube MCP server for Claude Code.

    • Installs secrets-scanning hooks.

    • Installs Agentic Analysis hooks for Claude (SQAA).

At the end of the flow, you should see a summary similar to:

SonarQube integration is ready. MCP + hooks: registered globally via sonar integrate claude Secrets scanning: hooks installed Authentication: token stored in system keychain Restart Claude Code if the SonarQube MCP tools don't appear immediately. You can safely rerun /sonarqube:sonar-integrate later to refresh CLI and wiring if needed.

Step 3: Check that the Agentic Analysis hook is active

After you're done integrating, restart Claude to initialize the Agentic Analysis hook. You should see a line that confirms the Agentic analysis is configured:

Step 4: Test the setup

Once setup is complete, trigger Agentic Analysis by asking Claude to edit a file in your project. For example, from a Claude Code session opened in your repo:

In a successful setup, you'll see in the Claude transcript or logs:

  • A tool call that edits the file (for example, applying a patch or write operation).

  • A follow-up sonar-sqaa PostToolUse hook invocation, which sends the changed file to SonarQube and reports any new issues.

Option 2: Install with the SonarQube CLI

Use this option to configure Claude Code directly from your terminal—useful when automating installations with scripts.

Prerequisites specific to this setup

Make sure you have:

  • Claude Code installed on your machine.

  • SonarQube CLI installed and authenticated.

Step 1: Run the integration command

From your project folder, run:

This command configures Claude Code for the current project. It does the following:

  • Registers the SonarQube integration for Claude Code.

  • Installs the required hooks for Agentic Analysis.

  • Binds the integration to the project key you provide.

Step 2: Restart and check that the Agentic Analysis hook is active

After restart, you should see a line that confirms Agentic Analysis is configured:

Step 3: Test the setup

Ask Claude to edit a file in your project. For example:

Step 4: Test the setup

SonarQube MCP Server integration

This integration configures the SonarQube MCP Server to expose the Agentic Analysis MCP tools to your AI agent.

Note: If you plan to use Agentic Analysis together with Context Augmentation, you must use a self-hosted SonarQube MCP Server. The SonarQube Cloud-hosted MCP server doesn't support Context Augmentation tools.

Integration steps

Step 1: Export the SonarQube token environment variable

Export the SONARQUBE_TOKEN https://docs.sonarsource.com/sonarqube-developer-tools/sonarqube-mcp-server/reference/environment-variables#common-variables with a valid Personal Access Token (PAT) for your project.

Step 2: Add or edit your project-specific MCP configuration file

Important: Agentic Analysis requires a project-specific configuration. Don't use your global MCP configuration.

Project-specific MCP settings files are located in the following files (relative to project root):

  • .cursor/mcp.json for Cursor IDE

  • .mcp.json for Claude Code

  • .gemini/settings.json for Gemini CLI

  • .codex/config.toml for Codex

    • use a TOML config instead of JSON

  • .vscode/mcp.json for Copilot in Visual Studio Code

Note: Replace the placeholders above with appropriate values:

  • Paths must be absolute: Relative paths like ./ or ../ are not accepted.

    • ⚠️ The path format is critical.

  • Windows users:

    • Use forward slashes. Example: C:/Users/john/projects/myapp

    • Don't use back slashes. Example: C:\\Users\\john\\projects\\myapp

The SONARQUBE_TOOLSETS value determines which MCP toolsets are enabled. The configuration below enables the analysis and projects toolsets required for Agentic Analysis.

Note: Optional configurations:

  • Enable Context Augmentation: To also enable Context Augmentation, change SONARQUBE_TOOLSETS to "cag,projects,analysis".

  • Include default SonarQube MCP tools: To include the default SonarQube MCP tools in addition to the Agentic Workflow tools, change the SONARQUBE_TOOLSETS value to: "analysis,issues,projects,quality-gates,rules,duplications,measures,security-hotspots,dependency-risks,coverage,cag".

  • Read-only volume mounting of the project workspace: The project workspace can be mounted in read-only mode by specifying ro instead of rw. Some tools may work in degraded mode.

Step 3: Configure agent operational constraints (recommended)

Tool usage can be inconsistent depending on available tools, context, and agent behavior. To ensure the agent follows the Guide-and-Verify workflow consistently, explicitly guide your AI agent to use the Agentic Analysis tools appropriately.

How to configure:

  • Cursor: Create a .cursor/rules/sonar-agentic-analysis.md Cursor rule file

  • Claude Code: Add the directive to your CLAUDE.md in the project root, or use Claude Skills

  • Codex: Add the directive to your AGENTS.md file in the project root

  • Gemini CLI: Add the directive to your GEMINI.md file in the project root

  • Copilot: Add the directive to your .github/copilot-instructions.md file in the project root

Example directive:

Note: The example below covers the full Guide-and-Verify workflow. If you are only using Agentic Analysis without Context Augmentation, you can remove the GUIDE Phase section.

Step 4: Restart your agent

Restart your agent and make sure that your MCP configuration is taken into account and applied correctly.

Step 5: Verify your setup

  1. Try asking your agent "What is Guide and Verify?". You should see a description of the SonarQube Agentic workflow as configured in step 3.

Example response

  1. Try asking your agent "Verify the file path/to/file.java". You should see it invoke the run_advanced_analysis tool (it might ask for confirmation). You should see a report of which issues are currently present in the file, broken down by severity.

Example response

Direct SonarQube CLI calls

Use the SonarQube CLI when you want to trigger Agentic Analysis directly from the command line.

Prerequisites

You've installed SonarQube CLI and authenticated to SonarQube Cloud. See Quickstart guide for more information.

Run the analysis

To trigger Agentic Analysis with SonarQube CLI, use the sonar analyze sqaa SonarQube CLI command from your project's folder:

Parameters

  • --file : Project-relative or absolute path to the file you want to analyze.

  • --project : Explicit project key if the CLI cannot infer it from the current directory or your integration.

  • --branch (optional): Branch name whose CI context should be used. Defaults to the current branch if not specified.

Example: Analyze a TypeScript file on the default branch

This triggers agentic analysis on src/app.ts using the latest CI analysis context for the linked project and branch.

Note: Agentic Analysis uses the same rules and quality profiles as your CI analysis.

API calls

To run Agentic Analysis without integrating with the SonarQube CLI or SonarQube MCP Server, call the Agentic Analysis API directly.

Calling the Agentic Analysis API

To trigger an Agentic Analysis through an API call, send a POST request to https://api.sonarcloud.io/a3s-analysis/analyses.

Provide the following parameters in your request:

Parameters
  • projectKey (key, required) The key of your project (go to your project > Information to find it).

  • branchName (string, required) Branch name used to retrieve the latest analysis context.

  • filePath (string, required) Project-relative path of the file to analyze (e.g., "src/main/java/MyClass.java").

  • fileContent (string, required) The original content of the file to analyze.

  • fileScope (string, optional) Defines in which scope the file originates from (main or test code): "MAIN" or "TEST". Default is "MAIN".

Example with curl

Here is an example of an API call using curl:

Last updated

Was this helpful?