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

[Edit this page](https://github.com/ClickHouse/clickhouse-docs/blob/main/docs/use-cases/AI_ML/MCP/ai_agent_libraries/crewai.md)

In this guide you'll learn how to build a [CrewAI](https://docs.crewai.com/) AI agent that can interact with
[ClickHouse's SQL playground](https://sql.clickhouse.com/) using [ClickHouse's MCP server](https://github.com/ClickHouse/mcp-clickhouse).

Example notebook

This example can be found as a notebook in the [examples repository](https://github.com/ClickHouse/examples/blob/main/ai/mcp/crewai/crewai.ipynb).

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

- You'll need to have Python installed on your system.
- You'll need to have `pip` installed on your system.
- You'll need an OpenAI API key

You can run the following steps either from your Python REPL or via script.

Install libraries

## Install libraries[​](#install-libraries "Direct link to Install libraries")

Install the CrewAI library by running the following commands:

```
pip install -q --upgrade pip
pip install -q "crewai-tools[mcp]"
pip install -q ipywidgets
```

Setup credentials

## Setup credentials[​](#setup-credentials "Direct link to Setup credentials")

Next, you'll need to provide your OpenAI API key:

```
import os, getpass
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter OpenAI API Key:")
```

```
Enter OpenAI API Key: ········
```

Next, define the credentials needed to connect to the ClickHouse SQL playground:

```
env = {
    "CLICKHOUSE_HOST": "sql-clickhouse.clickhouse.com",
    "CLICKHOUSE_PORT": "8443",
    "CLICKHOUSE_USER": "demo",
    "CLICKHOUSE_PASSWORD": "",
    "CLICKHOUSE_SECURE": "true"
}
```

Initialize MCP server and CrewAI agent

## Initialize MCP server and CrewAI agent[​](#initialize-mcp-and-agent "Direct link to Initialize MCP server and CrewAI agent")

Now configure the ClickHouse MCP server to point at the ClickHouse SQL playground
and also initialize our agent and ask it a question:

```
from crewai import Agent
from crewai_tools import MCPServerAdapter
from mcp import StdioServerParameters
```

```
server_params=StdioServerParameters(
    command='uv',
    args=[
        "run",
        "--with", "mcp-clickhouse",
        "--python", "3.10",
        "mcp-clickhouse"
    ],
    env=env
)

with MCPServerAdapter(server_params, connect_timeout=60) as mcp_tools:
    print(f"Available tools: {[tool.name for tool in mcp_tools]}")

    my_agent = Agent(
        llm="gpt-5-mini-2025-08-07",
        role="MCP Tool User",
        goal="Utilize tools from an MCP server.",
        backstory="I can connect to MCP servers and use their tools.",
        tools=mcp_tools,
        reasoning=True,
        verbose=True
    )
    my_agent.kickoff(messages=[
        {"role": "user", "content": "Tell me about property prices in London between 2024 and 2025"}
    ])
```

```
🤖 LiteAgent: MCP Tool User
Status: In Progress
╭─────────────────────────────────────────────────────────── LiteAgent Started ────────────────────────────────────────────────────────────╮
│                                                                                                                                          │
│  LiteAgent Session Started                                                                                                               │
│  Name: MCP Tool User                                                                                                                     │
│  id: af96f7e6-1e2c-4d76-9ed2-6589cee4fdf9                                                                                                │
│  role: MCP Tool User                                                                                                                     │
│  goal: Utilize tools from an MCP server.                                                                                                 │
│  backstory: I can connect to MCP servers and use their tools.                                                                            │
│  tools: [CrewStructuredTool(name='list_databases', description='Tool Name: list_databases                                                │
│  Tool Arguments: {'properties': {}, 'title': 'DynamicModel', 'type': 'object'}                                                           │
│  Tool Description: List available ClickHouse databases'), CrewStructuredTool(name='list_tables', description='Tool Name: list_tables     │
│  Tool Arguments: {'properties': {'database': {'anyOf': [], 'description': '', 'enum': None, 'items': None, 'properties': {}, 'title':    │
│  '', 'type': 'string'}, 'like': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None, 'description': '', 'enum': None,      │
│  'items': None, 'properties': {}, 'title': ''}, 'not_like': {'anyOf': [{'type': 'string'}, {'type': 'null'}], 'default': None,           │
│  'description': '', 'enum': None, 'items': None, 'properties': {}, 'title': ''}}, 'required': ['database'], 'title': 'DynamicModel',     │
│  'type': 'object'}                                                                                                                       │
│  Tool Description: List available ClickHouse tables in a database, including schema, comment,                                            │
│  row count, and column count.'), CrewStructuredTool(name='run_select_query', description='Tool Name: run_select_query                    │
│  Tool Arguments: {'properties': {'query': {'anyOf': [], 'description': '', 'enum': None, 'items': None, 'properties': {}, 'title': '',   │
│  'type': 'string'}}, 'required': ['query'], 'title': 'DynamicModel', 'type': 'object'}                                                   │
│  Tool Description: Run a SELECT query in a ClickHouse database')]                                                                        │
│  verbose: True                                                                                                                           │
│  Tool Args:                                                                                                                              │
│                                                                                                                                          │
│                                                                                                                                          │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯