Skip to main content
Glama
SonarQube REST API MCP
by darshan1005
README.md
# SonarQube REST API MCP for VS Code

Connect VS Code's Model Context Protocol (MCP) directly to SonarQube and SonarCloud over standard input/output (`stdio`).

This extension embeds a lightweight, dependency-free Node.js MCP server that communicates directly with your SonarQube instance's REST APIs. It provides zero-configuration discovery for AI assistants (such as GitHub Copilot, Claude, Cursor, Cline, and Roo Code) by automatically setting up and synchronizing `.vscode/mcp.json`.

---

## Features

- **Direct SonarQube REST API**: Direct communication with your SonarQube server or SonarCloud instance (`/api/projects`, `/api/measures`, `/api/issues`, `/api/hotspots`, `/api/qualitygates`, `/api/ce`, etc.).
- **Automatic `mcp.json` Provisioning**: Automatically configures `.vscode/mcp.json` when opening a workspace, preserving any existing third-party MCP servers.
- **Fail-Safe Server Lifecycle**: `server.js` maintains persistent stdio communication during initialization and unconfigured states, preventing child process crashes.
- **13 Built-in Tools**: Full coverage of code quality, test coverage, security hotspots, technical debt, project activity, and quality gates.
- **Optional Access Token**: Works seamlessly with anonymous/public instances or with personal access tokens for private instances.
- **Encrypted Credential Storage**: Secrets are stored securely in VS Code `SecretStorage`, never committed to source control.
- **Zero External Runtime Dependencies**: Built entirely with native Node.js 20+ standard APIs.

---

## Requirements

- **VS Code**: 1.102 or newer.
- **Node.js**: 20 or newer (runs the bundled `stdio` server).
- **SonarQube Instance**: SonarQube Server (`http://localhost:9000` or self-hosted domain) or [SonarCloud](https://sonarcloud.io).
- *(Optional)* A SonarQube User Access Token (**User > My Account > Security > Generate Tokens**).

---

## Installation and Quick Start

### 1. Install from VSIX

1. In VS Code, open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`).
2. Select **Extensions: Install from VSIX...**.
3. Choose the generated `sonarqube-rest-api-mcp-0.1.3.vsix` package.

### 2. Automatic Configuration

When you open any project folder in VS Code, the extension automatically inspects `.vscode/mcp.json`:
- If missing, it creates `.vscode/mcp.json` configured for SonarQube.
- If it already exists, it safely adds or updates the `sonarqube` entry without touching your other MCP servers.

### 3. Configure Your Instance

Open the Command Palette (`Ctrl+Shift+P` / `Cmd+Shift+P`) and run:

```text
SonarQube MCP: Configure
```

Enter:
1. **SonarQube Server URL**: (e.g. `https://sonarcloud.io`, `https://sonarqube.example.com`, or `http://localhost:9000`).
2. **Access Token** *(Optional)*: Your personal token, or leave blank for public/anonymous instances.
3. **Default Project Key** *(Optional)*: Default project key to use when none is specified in an AI prompt.

Your settings are securely saved and automatically synchronized to `.vscode/mcp.json`.

---

## Available Commands

| Command | Title | Description |
| :--- | :--- | :--- |
| `sonarqubeMcp.configure` | **SonarQube MCP: Configure** | Set or update SonarQube URL, access token, and default project key. Automatically syncs to `mcp.json`. |
| `sonarqubeMcp.testConnection` | **SonarQube MCP: Test Connection** | Tests reachability via `/api/system/status` or verifies your token with `/api/authentication/validate`. |
| `sonarqubeMcp.setupWorkspaceMcpJson` | **SonarQube MCP: Setup Workspace mcp.json** | Generates or updates `.vscode/mcp.json` in the currently open workspace folder. |
| `sonarqubeMcp.setupGlobalMcpJson` | **SonarQube MCP: Setup Global User mcp.json** | Adds SonarQube MCP configuration to your global VS Code user profile. |
| `sonarqubeMcp.showStatus` | **SonarQube MCP: Show Status** | Displays current target URL, token status, and quick-action buttons. |
| `sonarqubeMcp.clearCredentials` | **SonarQube MCP: Clear Credentials** | Clears stored URL, tokens, and project keys from VS Code state. |

---

## Configuration Reference

### Environment Variables

| Variable | Required | Description | Default |
| :--- | :---: | :--- | :--- |
| `SONARQUBE_URL` | **Yes** | Base URL of your SonarQube server or SonarCloud | `http://localhost:9000` |
| `SONARQUBE_TOKEN` | *Optional* | Personal access token for private projects | *(none / anonymous)* |
| `SONARQUBE_PROJECT_KEY` | *Optional* | Default project key fallback for tool calls | *(none)* |

---

### `mcp.json` Configuration Template

If you manage your configuration manually in `.vscode/mcp.json` or your global MCP config, use this format:

```jsonc
{
  "mcpServers": {
    "sonarqube": {
      "command": "node",
      "args": [
        // Path to server.js in the installed extension or repository
        "path/to/server.js"
      ],
      "env": {
        // Base domain (NOT a gateway or subpath)
        "SONARQUBE_URL": "https://sonarqube.example.com",
        // Optional access token (omit or leave blank for public instances)
        "SONARQUBE_TOKEN": "YOUR_SONARQUBE_ACCESS_TOKEN",
        // Optional default project key
        "SONARQUBE_PROJECT_KEY": "YOUR_PROJECT_KEY"
      }
    }
  }
}
```

---

## Available MCP Tools

Once connected, any AI agent in VS Code can invoke the following tools:

| Tool | Description | Key Parameters |
| :--- | :--- | :--- |
| `list_projects` | Search and list accessible SonarQube projects | `query`, `qualifiers`, `p`, `ps` |
| `list_branches` | List project branches and branch-specific quality gate statuses | `projectKey` |
| `get_quality_gate_status` | Retrieve Quality Gate pass/fail status and conditions | `projectKey`, `branch` |
| `list_quality_gates` | List all quality gates and condition definitions | `organization`, `gateId` |
| `get_measures` | Fetch 30+ preconfigured metrics (coverage, bugs, vulnerabilities, code smells, debt, ratings) | `projectKey`, `branch`, `metricKeys` |
| `get_measures_history` | Retrieve historical measure data points for trend analysis | `projectKey`, `branch`, `metricKeys`, `from`, `to` |
| `search_issues` | Comprehensive issue search across files, severities, and assignees | `projectKey`, `branch`, `types`, `severities`, `statuses`, `assignees`, `tags`, `rules` |
| `get_issue` | Get complete details and locations for specific issue keys | `issueKeys` (required) |
| `search_hotspots` | Search security hotspots with review statuses | `projectKey`, `branch`, `status` (`TO_REVIEW`, `REVIEWED`) |
| `get_hotspot` | Get detailed vulnerability and fix instructions for a hotspot | `hotspotKey` (required) |
| `get_project_activity` | Browse project analysis events, scanner activity, and versions | `projectKey`, `branch`, `category`, `from`, `to` |
| `list_background_tasks` | Check Compute Engine (CE) analysis queue and status | `projectKey`, `status` |
| `get_system_status` | Verify SonarQube server operational status and token validity | *(none)* |

---

## Development and Verification

Run unit tests:

```bash
npm test
```

Run JavaScript syntax checks:

```bash
npm run check
```

Build `.vsix` extension package:

```bash
npm run package
```

Launch Extension Development Host:
- Open the project in VS Code and press **`F5`** to debug the extension.

---

## Repository

```text
https://github.com/darshan1005/sonarqube-mcp
```

## License

MIT. See [LICENSE](LICENSE).