BetaDeveloper

Add context to generate better code

How to provide context to your agents to ensure the resulting code meets your software architecture and code quality standards.

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 Context Augmentation for your AI agent. The recommended setup path for supported agents is the SonarQube CLI. It installs an agent skill that can access Context Augmentation without exposing the tools through MCP. If you need direct MCP configuration or use a workflow not covered by sonar integrate, you can manually configure a locally running SonarQube MCP Server that connects to SonarQube Cloud.

Two setup paths are available: use the recommended SonarQube CLI setup for supported agents, or manually configure the SonarQube MCP Server for direct MCP access.

General prerequisites

Before you start, make sure you meet the following prerequisites.

Environment

  • Docker is installed and configured so your agent can run docker commands.

  • The integration also works with any other OCI-compatible container runtime, such as Podman or nerdctl.

SonarQube Cloud admin settings

Context Augmentation is enabled in your SonarQube Cloud organization's admin settings.

Project settings

Make sure your project is:

Setup

Choose the setup path that matches your agent.

Supported agents with SonarQube CLI

Use this recommended option to configure an agent supported by sonar integrate, such as Claude Code, GitHub Copilot, or Codex. For Context Augmentation, the CLI installs the local Context Augmentation tool and an agent skill that can use it directly. The same command can also configure a locally running SonarQube MCP Server for SonarQube MCP tools other than the Context Augmentation tools.

Prerequisites specific to this setup

Make sure you have:

  • A supported agent installed on your machine.

  • SonarQube CLI installed and authenticated.

Step 1: Run the integration command

From your project folder, run:

Replace <agent> with the supported integration you want to configure, for example claude, copilot, or codex.

This command configures the selected agent for the current project. It installs a project-specific Context Augmentation skill, binds it to the project key you provide, and configures the skill to access the Context Augmentation capabilities through the CLI.

Context Augmentation is project-specific rather than global. If you run sonar integrate <agent> --global, Context Augmentation is skipped; rerun the command without --global from a project directory to install it there.

Step 2: Restart your agent

Restart your agent so the generated skill and configuration are loaded for the current project.

Step 3: Verify your setup

Ask your agent "What is the current architecture of the projects? Limit the answer to the top-level blocks.". The agent uses Context Augmentation to retrieve the current architecture (it might ask for your confirmation), then presents a high-level overview of the current project.

Example response

SonarQube MCP Server integration

Use this option when you want to expose Context Augmentation through a manually configured SonarQube MCP Server. This remains useful for agents and workflows where you want direct control over MCP configuration, such as Cursor, Codex, Gemini CLI, Copilot, and custom setups.

Warning: Context Augmentation requires a locally running SonarQube MCP Server with a local filesystem mount. You can't use a SonarQube-hosted MCP server, whether SonarQube Cloud-hosted or SonarQube Server-hosted, for CAG tools.

Step 1: Export the SonarQube token environment variable

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

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

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

The SONARQUBE_TOOLSETS value determines which MCP toolsets are enabled. The following configuration enables the cag toolset required for Context Augmentation.

Note: 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.

  • Enable Agentic Analysis: To also enable Agentic Analysis, 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: To mount the project workspace in read-only mode, specify ro instead of rw. Some tools might work in degraded mode.

Tool usage can be inconsistent depending on available tools, context, and agent behavior. To increase the chance of Context Augmentation tools being called at the right time, we recommend two additional configurations:

1. Ensure tools are loaded on startup:

Context Augmentation tools are designed to be called automatically based on their descriptions, so they must be available to the model from the start. Some agents lazy-load MCP tool descriptions, meaning they are only made available when explicitly requested—reducing the chance tools are invoked at the right time. Other agents load them eagerly by default, such as Codex, where defer_loading defaults to false. If your agent lazy-loads tools, configure it to load them eagerly on startup. For example, with Claude Code, set ENABLE_TOOL_SEARCH to false.

2. Add an operational directive:

Adding a directive is optional. It is only needed if you want to enforce a specific Guide-phase workflow—without one, Context Augmentation still works, and the agent decides the right workflow and when to call the tools on its own, based on the tool descriptions.

To enforce a workflow, instruct your agent by adding a directive to its configuration file. Where to add it depends on your agent:

  • Cursor: Create a .cursor/rules/sonar-context-augmentation.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 following directive is one example of a Guide-and-Verify flow. You can customize your guidance by picking from the available Context Augmentation tools to match your team's workflow. If you're only using Context Augmentation without Agentic Analysis, you can remove the VERIFY Phase section.

Step 4: Restart your agent

Restart your AI agent to apply the new MCP configuration.

Step 5: Verify your setup

  1. Ask your agent "What is Guide and Verify?". Your agent describes the SonarQube Agentic workflow as configured in step 3.

Example response

  1. Ask your agent "What is the current architecture of the projects? Limit the answer to the top-level blocks.". The agent calls the get_current_architecture tool (it might ask for your confirmation). The agent then presents a high-level overview of the current project.

Example response

Troubleshooting

Enabling detailed logs

If you encounter issues with the Context Augmentation MCP tools, first enable detailed logging to identify the root cause.

Add the following environment variables to the env object in your MCP configuration:

Then expose them to the Docker container by adding these entries to the args array:

Exporting logs

After you enable detailed logging, use one of the following methods to retrieve the logs.

Option 1: Export logs from the running Docker container

  1. Identify the container ID used by the MCP server:

    If multiple containers are running the same image, filter by your project key:

    Replace <YourProjectKey> with the project key configured in your MCP server.

  2. Extract the logs as an archive file:

Option 2: Mount logs to a local directory

Add the following volume mount to the args array in your MCP configuration, replacing <LOCAL_LOG_DIRECTORY> with an absolute path to an existing local directory:

After you restart the MCP server, logs are written directly to that local directory.

Context Augmentation tools not loading

Symptom: After setting up the MCP server, only two tools appear: search_my_sonarqube_projects and run_advanced_code_analysis and none of the Context Augmentation tools are available.

Fix: Pull the latest version of the Docker image, then restart your AI agent:


SonarQube connection failed

Symptom: After you complete the setup, architecture tools don't appear. Only get_guidelines and two general SonarQube MCP tools (search_my_sonarqube_projects and run_advanced_code_analysis) are available. The following error messages appear in the logs:

Fix: Verify the value of SONARQUBE_URL in your MCP configuration. It must point to the SonarQube Cloud instance hosting your project's analysis. For example, https://sonarcloud.io.

Additional fix for Linux systems on corporate or restricted networks:

The default Docker DNS pass-through might be blocked, preventing the container from resolving hostnames. This issue can also appear intermittently.

Pass an explicit DNS server address to Docker by adding the following entry to the args array:

Replace 8.8.8.8 with your organization's DNS server address if needed.

As a last resort, configure the container to share the host network:

Or instruct the container to use your host's DNS resolver directly:

Warning: Using --network=host removes Docker's network isolation for this container, which might reduce security. Use this option only if other approaches haven't resolved the issue.

Architecture tools fail with "Universal Dependency Graph (UDG) not loaded"

Symptom: Architecture tools such as get_type_hierarchy and search_by_signature_patterns return an error message: "Universal Dependency Graph (UDG) not loaded".

Cause: The MCP server is configured to use a branch that has not been analyzed or does not exist in SonarQube Cloud.

Fix: Verify that the SONAR_SQ_BRANCH environment variable in your MCP configuration points to a valid branch that SonarQube Cloud has analyzed. If you recently created the branch or changed its name, make sure a fresh analysis has completed successfully.

Need help?

Post a question on the community forum or contact support if your license includes access to commercial support.

Last updated

Was this helpful?