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.
This guide walks you through setting up Sonar Vortex's agentic analysis features to ensure your agent verifies the code it writes or modifies.
Using these features requires a SonarQube Cloud Team (annual) or Enterprise plan, plus a separate subscription to the Sonar Agent Essentials product.
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:
Connected to SonarQube Cloud.
Analyzed in your CI pipeline on a long-lived branch.
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
Set up agentic analysis with Claude Code.
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:
Run
/pluginin Claude Code, open the Discover tab, and installsonarqubefrom the official Claude plugin marketplace.Reload SonarQube’s skills and hooks in Claude:
Either restart Claude Code, or
Run
/reload-pluginsif 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 claudeSecrets 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-integratelater 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 hook that 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. This is useful when you're 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:
Codex CLI
Set up agentic analysis with Codex CLI and the SonarQube plugin.
Prerequisites specific to this setup
Make sure you have OpenAI Codex installed on your machine.
Step 1: Install the SonarQube plugin
From your shell, register the SonarSource plugin marketplace:
Then start a Codex session, run /plugins, search for sonarqube, and install it.
For more information, see Codex CLI.
Step 2: Run the integration skill
From a Codex session opened in your project folder, run the SonarQube integration skill:
The skill installs the SonarQube CLI if needed, authenticates it, and runs sonar integrate codex. This installs a hook that runs agentic analysis after Codex edits files.
Step 3: Restart Codex
Restart Codex so the integration is loaded for the current project.
Step 4: Test the setup
Ask Codex to edit a file in your project. After Codex applies the patch, it should run agentic analysis and surface any new findings. This requires SonarQube Cloud and the agentic analysis entitlement on your organization.
GitHub Copilot
Set up agentic analysis with GitHub Copilot CLI and the SonarQube plugin.
Prerequisites specific to this setup
Make sure you have GitHub Copilot CLI installed on your machine.
Step 1: Install the SonarQube plugin
From your shell, run:
For more information, see GitHub Copilot.
Step 2: Run the integration skill
From a Copilot CLI session opened in your project folder, run the SonarQube integration skill:
The skill installs the SonarQube CLI if needed, authenticates it, and runs sonar integrate copilot. This installs instructions that tell Copilot to run sonar analyze agentic on modified files before ending the turn. Findings are returned in the turn response rather than injected mid-conversation.
Step 3: Restart GitHub Copilot CLI
Restart GitHub Copilot CLI so the integration is loaded for the current project.
Step 4: Test the setup
Ask Copilot's agent to edit a file in your project. At the end of the turn, before sending its final response, Copilot should run agentic analysis on modified files and surface any new findings. This requires SonarQube Cloud and the agentic analysis entitlement on your organization.
Cursor
Set up agentic analysis with Cursor's agent and the SonarQube plugin.
Prerequisites specific to this setup
Make sure you have:
Cursor installed on your machine.
SonarQube CLI installed and authenticated.
Step 1: Install the SonarQube plugin
In Cursor's chat, run:
For more information, see Cursor.
Step 2: Authenticate
In your terminal, run:
Step 3: Run the integration command
From your project folder, run:
This command installs an always-applied rule in .cursor/rules/ that tells Cursor to run sonar analyze agentic on the git change set at the end of a turn. Cursor uses the same instructions-based pattern as GitHub Copilot: findings are returned in the turn response rather than injected mid-conversation.
Step 4: Restart Cursor
Restart Cursor so the integration is loaded for the current project.
Step 5: Test the setup
Ask Cursor's agent to edit a file in your project. At the end of the turn, before sending its final response, Cursor should run agentic analysis on the git change set and surface any new findings. This requires SonarQube Cloud and the agentic analysis entitlement on your organization.
Antigravity
Set up agentic analysis with Antigravity and the SonarQube plugin.
Prerequisites specific to this setup
Make sure you have Antigravity installed on your machine.
Step 1: Install the SonarQube plugin
From your shell, run:
For more information, see Antigravity.
Step 2: Run the integration skill
From an Antigravity session opened in your project folder, run:
The skill installs the SonarQube CLI if needed, authenticates it, and runs sonar integrate antigravity. This installs instructions that tell Antigravity to run sonar analyze agentic on modified files before ending the turn. Antigravity uses the same instructions-based pattern as GitHub Copilot: findings are returned in the turn response rather than injected mid-conversation.
Step 3: Restart Antigravity
Restart Antigravity so the integration is loaded for the current project.
Step 4: Test the setup
Ask Antigravity to edit a file in your project. Before ending the turn, Antigravity should run agentic analysis and surface any new findings. This requires SonarQube Cloud and the agentic analysis entitlement on your organization.
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-hosted MCP servers, SonarQube Cloud-hosted and SonarQube Server-hosted, don't support context augmentation tools.
Integration steps
Step 1: Export the SonarQube token environment variable
Export the SONARQUBE_TOKEN Common variables with a valid Personal Access Token (PAT) for your project.
Step 2: Add or edit your MCP configuration file
MCP settings files are located at the following paths:
.cursor/mcp.jsonfor Cursor IDE.mcp.jsonfor Claude Code~/.gemini/config/mcp_config.jsonfor Antigravity.codex/config.tomlfor Codexuse a TOML config instead of JSON
.vscode/mcp.jsonfor 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/myappDon'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_TOOLSETSto"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_TOOLSETSvalue 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
roinstead ofrw. 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.
Add the directive to your agent's system prompt or instructions file.
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
Ask your agent "What is Guide and Verify?" You should see a description of the SonarQube Agentic workflow as configured in step 3.
Example response
Ask your agent "Verify the file
path/to/file.java." You should see it invoke therun_advanced_analysistool (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 agentic SonarQube CLI command from your project's folder:
Parameters
--file: Project-relative or absolute path to a file you want to analyze. Repeatable: pass--filemore than once to analyze several files in a single run. Omit it to analyze your local change set.--depth(optional): Analysis depth, eitherSTANDARD(each file analyzed on its own) orDEEP(adds cross-file taint analysis). Defaults toSTANDARDfor a single--file, andDEEPfor multiple files or a change set.--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.
For the full option list, see sonar analyze agentic in the SonarQube CLI documentation.
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:
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?

