> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# How to build an AI Agent with CopilotKit and the ClickHouse MCP server

> Learn how to build an agentic application using data stored in ClickHouse with ClickHouse MCP and CopilotKit

This is an example of how to build an agentic application using data stored in
ClickHouse. It uses the [ClickHouse MCP server](https://github.com/ClickHouse/mcp-clickhouse)
to query data from ClickHouse and generate charts based on the data.

[CopilotKit](https://github.com/CopilotKit/CopilotKit) is used to build the UI
and provide a chat interface to the user.

<Info>
  **Example code**

  The code for this example can be found in the [examples repository](https://github.com/ClickHouse/examples/edit/main/ai/mcp/copilotkit).
</Info>

<h2 id="prerequisites">
  Prerequisites
</h2>

* `Node.js >= 20.14.0`
* `uv >= 0.1.0`

<h2 id="install-dependencies">
  Install dependencies
</h2>

Clone the project locally: `git clone https://github.com/ClickHouse/examples` and
navigate to the `ai/mcp/copilotkit` directory.

Skip this section and run the script `./install.sh` to install dependencies. If
you want to install dependencies manually, follow the instructions below.

<h2 id="install-dependencies-manually">
  Install dependencies manually
</h2>

1. Install dependencies:

Run `npm install` to install node dependencies.

2. Install mcp-clickhouse:

Create a new folder `external` and clone the mcp-clickhouse repository into it.

```sh theme={null}
mkdir -p external
git clone https://github.com/ClickHouse/mcp-clickhouse external/mcp-clickhouse
```

Install Python dependencies and add fastmcp cli tool.

```sh theme={null}
cd external/mcp-clickhouse
uv sync
uv add fastmcp
```

<h2 id="configure-the-application">
  Configure the application
</h2>

Copy the `env.example` file to `.env` and edit it to provide your `ANTHROPIC_API_KEY`.

<h2 id="use-your-own-llm">
  Use your own LLM
</h2>

If you'd rather use another LLM provider than Anthropic, you can modify the
Copilotkit runtime to use a different LLM adapter.
[Here](https://docs.copilotkit.ai/guides/bring-your-own-llm) is a list of supported
providers.

<h2 id="use-your-own-clickhouse-cluster">
  Use your own ClickHouse cluster
</h2>

By default, the example is configured to connect to the
[ClickHouse demo cluster](https://sql.clickhouse.com/). You can also use your
own ClickHouse cluster by setting the following environment variables:

* `CLICKHOUSE_HOST`
* `CLICKHOUSE_PORT`
* `CLICKHOUSE_USER`
* `CLICKHOUSE_PASSWORD`
* `CLICKHOUSE_SECURE`

<h2 id="run-the-application">
  Run the application
</h2>

Run `npm run dev` to start the development server.

You can test the Agent using prompt like:

> "Show me the price evolution in
> Manchester for the last 10 years."

Open [http://localhost:3000](http://localhost:3000) with your browser to see
the result.
