Skip to main content

Creating Slack Bot Agent with BeeAI Framework and MCP

This tutorial guides you through creating an AI agent that can post messages to a Slack channel using the Model Context Protocol (MCP).

Table of Contents


Slack agent prerequisites

  • Python: Version 3.11 or higher
  • Ollama: Installed with the granite3.3:8b model pulled
  • BeeAI framework installed with pip install beeai-framework
  • Project setup:
    • Create project directory: mkdir beeai-slack-agent && cd beeai-slack-agent
    • Set up Python virtual environment: python -m venv venv && source venv/bin/activate
    • Create environment file: echo -e "SLACK_BOT_TOKEN=\nSLACK_TEAM_ID=" >> .env
    • Create agent module: mkdir my_agents && touch my_agents/slack_agent.py
Once you’ve completed these prerequisites, you’ll be ready to implement your Slack agent.

Slack configuration

To configure the Slack API integration:
  1. Create a Slack app
    • Visit https://api.slack.com/apps and click “Create New App” > “From scratch”
    • Name your app (e.g., Bee) and select a workspace to develop your app in
  2. Configure bot permissions
    • Navigate to OAuth & Permissions in the sidebar
    • Under “Bot Token Scopes”, add the chat:write scope
    • Click “Install to [Workspace]” and authorize the app
  3. Gather credentials
    • Copy the “Bot User OAuth Token” and add it to your .env file as SLACK_BOT_TOKEN=xoxb-your-token
    • Get your Slack Team ID from your workspace URL (https://app.slack.com/client/TXXXXXXX/...)
      • Tip: Visit https://<your-workspace>.slack.com, after redirect, your URL will change to https://app.slack.com/client/TXXXXXXX/CXXXXXXX, pick the segment starting with TXXXXXXX
    • Add the Team ID to your .env file as SLACK_TEAM_ID=TXXXXXXX
  4. Create a channel
    • Create a public channel named bee-playground in your Slack workspace
    • Invite your bot to the channel by typing /invite @Bee in the channel

Implementing the Slack agent

The framework doesn’t have any specialized tools for using Slack API. However, it supports tools exposed via Model Context Protocol (MCP) and performs automatic tool discovery. We will use that to give our agent the capability to post Slack messages. Now, copy and paste the following code into slack_agent.py module. Then, follow along with the comments for an explanation.
Python
Source: python/examples/tools/mcp_slack_agent.py

Running the Slack agent

Execute your agent with:
You will observe the agent:
  • Analyze the task
  • Determine it needs to check the weather in Boston
  • Use the OpenMeteo tool to get the current temperature
  • Use the slack_post_message tool to post to the #bee-playground Slack channel
As you might have noticed, we made some restrictions to make the agent work with smaller models so that it can be executed locally. With larger LLMs, we could further simplify the code, use more tools, and create simpler prompts.
This tutorial can be easily generalized to any MCP server with tools capability. Just plug it into Bee and execute.