How to build a SlackBot agent using ClickHouse MCP Server
In this guide, you'll learn how to build a SlackBot agent. This bot allows you to ask questions about your ClickHouse data directly from Slack, using natural language. It uses the ClickHouse MCP Server and PydanticAI.
The code for this example can be found in the examples repository.
Prerequisites
- You'll need to have
uv
installed - You'll need access to a Slack workspace
- You'll need an Anthropic API key, or API key from another LLM provider
Create a Slack App
- Go to slack.com/apps and click
Create New App
. - Choose option
From scratch
and give your app a name. - Select your Slack workspace.
Install the app to your workspace
Next, you'll need to add the app created in the previous step to your workspace. You can follow the instructions for "Add apps to your Slack workspace" in the Slack documentation.
Configure Slack app settings
- Go to
App Home
- Under
Show Tabs
→Messages Tab
: EnableAllow users to send Slash commands and messages from the messages tab
- Go to
Socket Mode
- Enable
Socket Mode
**` - Note down the
Socket Mode Handler
**for the environment variable
SLACK_APP_TOKEN`
- Enable
- Go to
OAuth & Permissions
- Add the following
Bot Token Scopes
:app_mentions:read
assistant:write
chat:write
im:history
im:read
im:write
channels:history
- Install the app to your workspace and note down the
Bot User OAuth Token
for the environment variableSLACK_BOT_TOKEN
.
- Add the following
- Go to
Event Subscriptions
- Enable
Events
- Under
Subscribe to bot events
, add:app_mention
assistant_thread_started
message:im
- Save Changes
- Enable
- Go to
- Under
Add environment variables (.env
)
Create a .env
file in the project root with the following environment variables
which will allow your app to connect to ClickHouse's SQL playground.
You can adapt the ClickHouse variables to use your own ClickHouse server or Cloud instance, if you would prefer.
Using the bot
-
Start the bot:
-
In Slack:
- Mention the bot in a channel:
@yourbot Who are the top contributors to the ClickHouse git repo?
- Reply to the thread with a mention:
@yourbot how many contributions did these users make last week?
- DM the bot:
Show me all tables in the demo database.
- Mention the bot in a channel:
The bot will reply in the thread, using all previous thread messages as context if applicable.
Thread Context: When replying in a thread, the bot loads all previous messages (except the current one) and includes them as context for the AI.
Tool Usage: The bot uses only the tools available via MCP (e.g., schema discovery, SQL execution) and will always show the SQL used and a summary of how the answer was found.