Agentic Analysis

Agentic Analysis enables AI agents to verify generated code with the same precision as a full CI scan, but in seconds instead of minutes, closing the Guide-and-Verify loop.

circle-check

The Agentic Workflow

Agentic Analysis is the Verify phase of the SonarQube Agentic Workflow — a continuous loop that ensures AI-generated code is both architecturally sound and meets your quality standards.

  1. Guide: Context Augmentation injects architectural awareness and coding guidelines into the LLM context before code generation.

  2. Generate: The LLM generates code based on the augmented context.

  3. Verify: Agentic Analysis verifies the generated code with full CI-level precision.

  4. Loop: The LLM refines the code based on analysis results and repeats until quality gates pass.

This workflow unlocks AI productivity without trade-offs, ensuring clean pull requests pass quality gates the first time, leading to faster code reviews with no back-and-forth.

spinner

For the Guide phase, see Context Augmentation.


Overview

With Agentic Analysis, AI agents can analyze code changes with the same precision as a full CI scan, but in seconds instead of minutes. Agentic Analysis leverages context collected during the last SonarQube CI analysis and restores it on-demand for single-file or multi-file analysis.

Supported languages

Agentic Analysis is available for the following languages:

  • Java

  • Python

  • Javascript/Typescript

  • CSS

  • HTML

  • XML

Secrets detection and the IaC domains Docker, Kubernetes, and Terraform, are also supported.

circle-info

If your Java project was analyzed using Automatic analysis, only basic analysis results are returned.

About the context mechanism

Agentic Analysis achieves high-precision analysis through a two-phase approach: it first collects context during CI analysis, then restores that context on demand.

Phase 1: Context collection

During a regular CI analysis, SonarQube may collect and store:

  • Dependencies: JAR files, npm packages, or Python libraries the project uses.

  • Compiled artifacts: .class files.

  • Type information: Symbol tables, type hierarchies, import graphs.

  • Build configuration: Project structure, source paths, compiler settings.

This context is stored in the SonarQube Cloud backend (AWS), tagged by project key and branch.

Diagram of how context is stored in the context storage during a CI scan

Phase 2: Context restoration

When analyzing files using Agentic Analysis, the context collected for this project during a CI analysis is restored, based on the specified project and branch name. This allows running an analysis with full access to dependencies and compiled artifacts. Analysis results have a level of precision that matches a full CI scan analysis.

Diagram showing the context restoration process.

SonarQube MCP Server Integration

Prerequisites

Your project must be:

  • Connected to SonarQube Cloud.

  • Analyzed in your CI pipeline on a long-lived branch.

  • Analyzed in SonarQube Cloud after the Agentic Analysis service was enabled for the organization to ensure context collection.

Agentic Analysis is compatible with Claude Code, Cursor, Codex, Gemini CLI, and VS Code with Copilot.

Integration steps

Step 1: Install the SonarQube MCP Server

First, install the SonarQube MCP Server in your MCP client using the SonarQube Cloud setup. Detailed setup instructions are available for the most popular MCP clients; find yours in the IDE setup article.

Step 2: Export the SonarQube Token environment variable

Export the SONARQUBE_TOKEN environment variable with a valid Personal Access Token (PAT)arrow-up-right for your project.

Step 3: Set the Agentic Analysis environment variable

Set your SONARQUBE_ADVANCED_ANALYSIS_ENABLED environment variable to true (default is false). This makes the run_advanced_code_analysis tool available in the SonarQube MCP Server.

For more information on the tool, see the SonarQube MCP Tools section.

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

triangle-exclamation

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

circle-check
circle-info

Optional configurations:

  • SONAR_SQ_BRANCH: Provide it when not using git, or when your git branch name doesn't match the branch name in SonarQube. Add "-e", "SONAR_SQ_BRANCH", to the args array and "SONAR_SQ_BRANCH": "<YourBranchName>" to the env object.

  • Agentic Analysis tools only: By default, SONARQUBE_TOOLSETS enables both Agentic Analysis and Context Augmentation tools (cag,projects,analysis). If you want to use Agentic Analysis without Context Augmentation, change the value to "analysis,projects".

  • 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"

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:

Example directive:

circle-info

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 6: Restart your agent

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


Direct API calls

Depending on the way you have set up your AI agent to work, you may want to run an Agentic Analysis without integrating with the SonarQube MCP Server. In this case, you can make an API call to the Agentic Analysis API directly.

Prerequisites

The project must have been analyzed in SonarQube Cloud after the Agentic Analysis service was enabled for the organization to ensure context collection.

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/analysesarrow-up-right.

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?