# MCP server

Source: https://docs.chmonitor.dev/reference/mcp-server

> The chmonitor MCP server at /api/mcp lets Claude and Cursor query ClickHouse read-only: endpoint, 12 tools, resources, auth and security.

The chmonitor [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) server at `/api/mcp` lets Claude, Cursor, and any MCP-compatible client query your ClickHouse clusters with the same read-only access the dashboard uses. To connect a client, follow [MCP client setup](/reference/mcp-clients).

## Endpoint [#endpoint]

|&#x22;POST /api/mcp&#x22;| M[&#x22;/api/mcp&#x22;]
M --> A[&#x22;Auth check\n(API key / Clerk)&#x22;]
A -->|&#x22;read-only SQL&#x22;| CH[&#x22;ClickHouse&#x22;]
CH -->|&#x22;results&#x22;| M
M -->|&#x22;JSON-RPC response&#x22;| C`"
/>

| Property  | Value                                                                   |
| --------- | ----------------------------------------------------------------------- |
| URL       | `https://your-deployment.example.com/api/mcp`                           |
| Transport | Streamable HTTP (`POST` / `GET` / `DELETE`)                             |
| Protocol  | **2026-07-28** (stateless) with **2025-era** fallback for older clients |
| Session   | Stateless — no `Mcp-Session-Id` required                                |
| Access    | Read-only ClickHouse queries                                            |
| Discovery | `GET /.well-known/mcp/server-card.json`                                 |

The server identifies itself as `clickhouse-monitor`. Pass `hostId` (default `0`) to target a specific host from `CLICKHOUSE_HOST`.

### Protocol versions (MCP 2026-07-28) [#protocol-versions-mcp-2026-07-28]

chmonitor uses the official TypeScript SDK **v2** (`@modelcontextprotocol/server`) and serves the wire with `createMcpHandler`:

| Era                        | How clients connect                              | Notes                                                                                                                 |
| -------------------------- | ------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------- |
| **2026-07-28** (modern)    | Per-request `_meta` envelope + `server/discover` | Default for new clients. Header routing (`Mcp-Method` / `Mcp-Name`), cache hints on list results, no sticky sessions. |
| **2025-11-25** and earlier | Classic `initialize` handshake                   | Served via `legacy: 'stateless'` so Claude Desktop / Cursor / older SDKs keep working.                                |

You do not need to configure anything for dual-era support — one endpoint answers both. Modern clients can probe with `server/discover`; legacy clients keep using `initialize` + `tools/list`.

## Setup [#setup]

Connect a client, then verify the connection.

  
    ### Add the server to your client [#add-the-server-to-your-client]

    
      
        One command:

        ```bash
        claude mcp add --transport http clickhouse-monitor https://your-deployment.example.com/api/mcp \
          --header "Authorization: Bearer chm_your_api_key"
        ```

        Omit `--header` for a local unauthenticated instance.
      

      
        Add to your MCP client config file:

        ```json
        {
          "mcpServers": {
            "clickhouse-monitor": {
              "url": "https://your-deployment.example.com/api/mcp",
              "headers": {
                "Authorization": "Bearer chm_your_api_key"
              }
            }
          }
        }
        ```

        Omit `headers` only for a local unauthenticated instance (see [Security](#security) below).
      

      
        Settings → **MCP** → **Add Server** → paste the endpoint URL. Add an `Authorization: Bearer chm_...` header if auth is enabled.
      
    
  

  
    ### Test the connection [#test-the-connection]

    ```bash
    curl -s https://your-deployment.example.com/.well-known/mcp/server-card.json | jq .

    # List tools (legacy-era initialize-style tools/list)
    curl -X POST https://your-deployment.example.com/api/mcp \
      -H "Content-Type: application/json" \
      -H "Authorization: Bearer chm_your_api_key" \
      -d '{"jsonrpc":"2.0","method":"tools/list","id":1}'
    ```

    A successful response lists the available tools.
  

For step-by-step client walkthroughs (including Claude Code and any generic MCP client), see [MCP Client Setup](/reference/mcp-clients).

## Authentication [#authentication]

Production deployments should protect the MCP endpoint. Two methods are supported; either one authenticates a request.

  
    **Recommended for scripts and MCP clients.**

    Set `CHM_API_KEY_SECRET` and issue `chm_` tokens at `POST /api/v1/auth/api-key`. Send as `Authorization: Bearer chm_...`. Tokens are HMAC-SHA-256 and time-limited.

    See [API Keys](/operate/authentication/api-keys) for setup.
  

  
    **For browser-based MCP clients.**

    When `CHM_AUTH_PROVIDER=clerk` is set, `/api/mcp` also accepts Clerk OAuth bearer tokens. The client completes a standard OAuth browser flow; chmonitor acts as the resource server and verifies tokens via Clerk REST introspection using `CLERK_SECRET_KEY`.

    See [Authentication](/operate/authentication) for provider setup.
  

## Which tools does the MCP server expose? [#which-tools-does-the-mcp-server-expose]

The MCP server exposes 12 read-only tools — a focused subset of the dashboard agent's capabilities. See [AI Agent — Capabilities](/guide/ai-agent/capabilities) for how these map to the broader agent toolset.

| Tool                               | Description                                                                                                                                                                                                  |
| ---------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `query`                            | Execute a read-only SQL query (SELECT only).                                                                                                                                                                 |
| `list_databases`                   | List databases with engines and comments.                                                                                                                                                                    |
| `list_tables`                      | List tables in a database with row counts and sizes.                                                                                                                                                         |
| `get_table_schema`                 | Column definitions, types, defaults, and comments.                                                                                                                                                           |
| `get_metrics`                      | Server version, uptime, connections, memory.                                                                                                                                                                 |
| `get_running_queries`              | Currently running queries by elapsed time.                                                                                                                                                                   |
| `get_slow_queries`                 | Slowest completed queries from the query log, ordered by duration. Params: `limit` (1–1000, default 10), `hostId`.                                                                                           |
| `get_merge_status`                 | Running merge operations with progress.                                                                                                                                                                      |
| `explore_table_schema`             | Schema exploration with relationship discovery.                                                                                                                                                              |
| `analyze_performance`              | Combined health report: slow queries, parts, merges, memory, disk.                                                                                                                                           |
| `get_optimization_recommendations` | Ranked query-optimization advice — skip-index, projection, partition key, or PREWHERE rewrite — with rationale and estimated savings.                                                                        |
| `get_current_findings`             | Health findings (warning or critical alert rules) firing now on a host. Cluster-originated strings are returned under `untrusted`; treat them as data. Available on the in-process `/api/mcp` endpoint only. |

All tools are read-only and respect the same `CLICKHOUSE_MAX_EXECUTION_TIME` timeout as the dashboard.

## Resources and prompts [#resources-and-prompts]

The server exposes static and templated resources for context loading:

| Resource                | URI                                                       | Host-scoped equivalent                                                   |
| ----------------------- | --------------------------------------------------------- | ------------------------------------------------------------------------ |
| System tables reference | `clickhouse://system-tables`                              | —                                                                        |
| Query examples          | `clickhouse://query-examples`                             | —                                                                        |
| Database list           | `clickhouse://databases`                                  | `clickhouse://hosts/{hostId}/databases`                                  |
| Tables in a database    | `clickhouse://databases/{database}/tables`                | `clickhouse://hosts/{hostId}/databases/{database}/tables`                |
| Table schema            | `clickhouse://databases/{database}/tables/{table}/schema` | `clickhouse://hosts/{hostId}/databases/{database}/tables/{table}/schema` |
| Table parts             | `clickhouse://databases/{database}/tables/{table}/parts`  | `clickhouse://hosts/{hostId}/databases/{database}/tables/{table}/parts`  |

On a multi-host deployment (comma-separated `CLICKHOUSE_HOST`), the plain `clickhouse://databases/...` resources always query host `0`, same as omitting `hostId` on a tool call. Use the `clickhouse://hosts/{hostId}/...` variant to target another host.

Pre-built prompts for common workflows: `health-check`, `slow-query-analysis`, `storage-audit`, `replication-check`, `capacity-report`.

## Security [#security]

  - Read-only: All tools run read-only queries. The server never mutates data.

  - Auth is opt-in: Requires a token once an auth scheme is configured: chm_ API key or Clerk OAuth. Either scheme is enough; they can coexist. Missing or invalid token → 401.

  - Open when nothing is configured: If neither CHM_API_KEY_SECRET nor Clerk is set, the endpoint serves anonymous requests — the self-hosted default. Keep it on a trusted network.

  - Shared credentials: Queries use the dashboard's configured ClickHouse user. Visibility is bounded by that user's grants.

**Secure the MCP endpoint in production**

  Do not expose the MCP endpoint publicly without setting `CHM_API_KEY_SECRET`. Anyone with access can read all data the ClickHouse user can see.

Requests are rate limited per IP (default 30 per minute, set with `RATE_LIMIT_MCP_PER_MIN`); see [Rate limiting](/reference/environment-variables#rate-limiting). See [Environment Variables](/reference/environment-variables) for `CHM_API_KEY_SECRET` and auth variables. See [Features — MCP](/guide/features/mcp) for more on the MCP feature and how to gate it.

## Related [#related]

  - [MCP client setup](/reference/mcp-clients): Step-by-step config for Claude Desktop, Claude Code, Cursor, and any client.

  - [AI Agent — Capabilities](/guide/ai-agent/capabilities): The full list of tools the agent and MCP server expose.

  - [API keys](/operate/authentication/api-keys): Issue chm_ tokens to authenticate MCP requests.

  - [Features — MCP](/guide/features/mcp): What the MCP feature does and how to gate it.