Documentation

Integrating kagent with Slack

Kagent enables you to create AI agents that run inside your Kubernetes cluster. They have access to a variety of built-in tools like Kubernetes, Istio, Grafana, Prometheus, Argo and can be extended with any other tools using MCP. They can also draw information from your data and execute other agents.

Slack is a popular platform for team communication and collaboration. What if you could bridge these two worlds, allowing you to manage your Kubernetes resources directly from Slack or allow agents to send notifications to your Slack channels?

This guide demonstrates how to achieve exactly that. We will explore a two-way integration between kagent and Slack:

  1. Invoking kagent from Slack: Learn how to set up a Slack bot that uses kagent's A2A (Agent-to-Agent) protocol to interact with a kagent agent, allowing you to run commands or ask questions about your cluster directly from Slack.
  2. Sending messages from kagent to Slack: Discover how to configure a kagent agent to use an MCP (Model Context Protocol) server to send messages and notifications to specific Slack channels.

To follow along, you'll first need to create a Slack App. We'll cover the necessary steps below, but you can also refer to the official Slack documentation.

Creating a Slack App

You will need a Slack workspace and sufficient permissions to create and install Slack apps. Assuming you have that, you can head over to https://api.slack.com/apps and follow the instructions below.

Note that these instructions might change, so make sure you check out the latest instructions on the Slack website.

  1. From the Your Apps page click the "Create an app" button.
  1. Choose "From scratch" options and give your app a name (we'll use My kagent app) and pick a workspace.
  1. Click Create App to create the app.

You'll be redirected to the app's basic information page. Next, we need to give app the permissions it needs to send or view messages, for example:

  1. Click the OAuth & Permissions tab from the left sidebar.
  2. Scroll down to the "Scopes" section.
  3. Click "Add an OAuth Scope" in the "Bot Token Scopes" section.
  4. From the dropdown menu add the the following scopes (you can add more scopes if you want to experiment with other features):
    • chat:write
    • commands

These scopes will allow the app to send messages as the "My kagent app" and with the commands we'll be able to configure slash commands users can invoke.

Now that we have the scopes set up, we can install the app (that won't do anything yet) to the workspace. Scroll up to the top of the page to the "OAuth Tokens" section and click "Install to your_workspace_name" button.

You'll be redirected to the app's installation page. Make sure you select a channel where the app will be able to post messages and click "Allow" to install the app to your workspace.

If you open your Slack workspaces you'll notice the "My kagent app" is added to the list of apps.

Let's go back to the "OAuth & Permissions" page - notice the Bot User OAuth Token (SLACK_BOT_TOKEN). Copy it and save it somewhere as we'll use it later.

Click on the "Basic Information" and go to the "App-Level Tokens" section and click the "Generate token and Scopes" button. From the dialog, name your token (mytoken) and click the Add Scope and add the connections:write scope as shown below.

Click "Generate", copy the token and save it somewhere as we'll use it later (SLACK_APP_TOKEN).

Before we go and write some code, we'll also need to set up the events. For the sake of simplicity, we'll use the "Socket Mode" - this allows us to use the events API without deploying the bot a publicly accessible URL - it greatly simplifies the development process. However, once you're ready to deploy your bot to production, you'll need to set up the Slack bot app on a publicly accessible URL.

To enable "Socket Mode":

  1. Click the "Socket Mode" tab from the left sidebar.
  2. Click the "Enable Socket Mode" toggle to enable it.

Lastly we'll add a slash command that will allow us to invoke the bot directly using the /mykagent command:

  1. Click the "Slash Commands" tab from the left sidebar.
  2. Click "Create New Command" button.
  3. Fill out the form with the following values:
  4. Click Save.

Writing a Slack bot

Armed with two tokens, we can now go and write some code! We'll be using Bolt for Python, but you can also use Bolt for other languages. Start by cloning the A2A Slack template.

  1. Clone the repository:
git clone https://github.com/kagent-dev/a2a-slack-template.git
  1. Copy the .env.example file to .env:
cp .env.example .env
  1. Open the .env file and add your tokens (don't worry about the KAGENT_A2A_URL variable for now, we'll set it later):
SLACK_BOT_TOKEN=xoxb-...
SLACK_APP_TOKEN=xapp-...
  1. Create a virtual environment and install the dependencies:
uv venv
source .venv/bin/activate
uv sync

(Note: If you don't have uv installed, you can typically install it using pip install uv.)

  1. Run the bot:
uv run main.py

The bot should start and the output should look like this:

INFO:root:Starting Kagent Slack bot
DEBUG:asyncio:Using selector: KqueueSelector
⚡️ Bolt app is running!

If you go to the Slack channel where you installed the app, you should see the bot online and you can try the /mykagent command.

Deploying an agent

Before deploying an agent, make sure you have installed kagent in your cluster. If you haven't, you can install it by following the instructions here.

Next, we'll deploy a sample agent to the cluster that will be exposed through the A2A protocol and then we'll be able to call it from the Slack bot. The agent is a simple Kubernetes agent that has access to a couple of tools and is configured to answer questions about Kubernetes.

Let's deploy it:

kubectl apply -f - <<EOF
apiVersion: kagent.dev/v1alpha1
kind: Agent
metadata:
name: my-k8s-agent
namespace: kagent
spec:
description: My Kubernetes Agent
modelConfig: openai-model-config
systemMessage: |-
You're an expert Kubernetes agent that uses tools to answer users questions and help them with their Kubernetes clusters. Your answers should be concise and to the point.
a2aConfig:
skills:
- id: answer-questions-about-your-cluster
name: Answer Questions About Your Cluster
description: Answer questions about your Kubernetes cluster
inputModes:
- text
outputModes:
- text
tags: ["kubernetes"]
tools:
- builtin:
name: kagent.tools.k8s.GetResources
type: Builtin
- builtin:
name: kagent.tools.k8s.GetResourceYAML
type: Builtin
- builtin:
name: kagent.tools.k8s.GetClusterConfiguration
type: Builtin
- builtin:
name: kagent.tools.k8s.GetPodLogs
type: Builtin
EOF

We're using the builtin tools here, however, you can also deploy a ToolServer resource and use tools from any MCP server.

To access the agent through A2A, we'll need to port-forward the kagent service to our local machine:

kubectl port-forward -n kagent svc/kagent-service 8083:8083

(Note: This command needs to be kept running in a separate terminal window for the duration you need to access the agent locally.)

The agent is now be accessible through the A2A protocol! The agent card is available at http://host/api/a2a/[agent-namespace]/[agent-name]/.well-known/agent.json.

Let's try getting the agent card:

curl localhost:8083/api/a2a/kagent/my-k8s-agent/.well-known/agent.json
{
"name": "my-k8s-agent",
"description": "My Kubernetes Agent",
"url": "http://127.0.0.1:8083/api/a2a/kagent/my-k8s-agent",
"version": "1",
"capabilities": {
"streaming": false,
"pushNotifications": false,
"stateTransitionHistory": false
},
"defaultInputModes": [
"text"
],
"defaultOutputModes": [
"text"
],
"skills": [
{
"id": "answer-questions-about-your-cluster",
"name": "Answer Questions About Your Cluster",
"description": "Answer questions about your Kubernetes cluster",
"tags": [
"kubernetes"
],
"inputModes": [
"text"
],
"outputModes": [
"text"
]
}
]
}

Perfect! We can now go back to the Slack bot and connect it to the agent.

Calling an agent from Slack

In the invoke_a2a_agent function in the handler.py file, we already have a working example on how to invoke the agent. The function is triggered when the /mykagent command is used in Slack (through the mykagent_command function). That function checks whether the agent URL is provided through an environment variable and if so, it invokes the agent.

Open the .env file and set the KAGENT_A2A_URL environment variable to the agent's A2A URL:

KAGENT_A2A_URL=http://127.0.0.1:8083/api/a2a/kagent/my-k8s-agent

Now restart the bot with uv run main.py and try the /mykagent command in Slack. For example, you can ask the agent to list the pods in the cluster:

/mykagent show me the pods in the cluster

You can see the output of the agent in the Slack channel:

You can try asking other questions and the agent will use the tools to answer your questions.

Sending messages to Slack from kagent

Let's see how we can do the reverse - sending messages to Slack from kagent. We'll use an existing Slack MCP server that will allow us to send messages to Slack channels.

Before we configure the MCP server, you'll need to gather the following information:

  • Slack bot token (SLACK_BOT_TOKEN)
  • Slack team ID
  • Slack channel IDs

The easiest way to get the IDs is to open your Slack workspaces in the browser - you can do that by vistiing the https://[slack-workspace-name].slack.com/. Once the workspace loads you'll be able to get the team ID and the channnel ID by looking at the URL -- for example: https://app.slack.com/client/T08S1QPFY1F/C08S1QQ53BK - in this case the team ID is T08S1QPFY1F and the channel ID is C08S1QQ53BK.

Let's set all those as environment variables:

export SLACK_BOT_TOKEN="your_slack_bot_token"
export SLACK_TEAM_ID="your_slack_team_id"
# Not a typo, you can have multiple channel IDs separated by commas
export SLACK_CHANNEL_IDS="your_slack_channel_id"

Important: These export commands set environment variables only for the current terminal session. Ensure you run the kubectl apply command below in the same terminal session where you ran the exports. For production or more robust setups, consider using Kubernetes Secrets to manage these values.

Now we can deploy the MCP server:

kubectl apply -f - <<EOF
apiVersion: kagent.dev/v1alpha1
kind: ToolServer
metadata:
name: slack-mcp
namespace: kagent
spec:
config:
stdio:
args:
- -y
- '@modelcontextprotocol/server-slack'
command: npx
env:
SLACK_BOT_TOKEN: $SLACK_BOT_TOKEN
SLACK_TEAM_ID: $SLACK_TEAM_ID
SLACK_CHANNEL_IDS: $SLACK_CHANNEL_IDS
description: ""
EOF

The MCP server has tool called send_message_to_slack that can be used to send messages to Slack. Let's update our agent and add the Slack tool to it:

kubectl apply -f - <<EOF
apiVersion: kagent.dev/v1alpha1
kind: Agent
metadata:
name: my-k8s-agent
namespace: kagent
spec:
description: My Kubernetes Agent
modelConfig: openai-model-config
systemMessage: |-
You're an expert Kubernetes agent that uses tools to answer users questions and help them with their Kubernetes clusters. Your answers should be concise and to the point.
a2aConfig:
skills:
- id: answer-questions-about-your-cluster
name: Answer Questions About Your Cluster
description: Answer questions about your Kubernetes cluster
inputModes:
- text
outputModes:
- text
tags: ["kubernetes"]
tools:
- builtin:
name: kagent.tools.k8s.GetResources
type: Builtin
- builtin:
name: kagent.tools.k8s.GetResourceYAML
type: Builtin
- builtin:
name: kagent.tools.k8s.GetClusterConfiguration
type: Builtin
- builtin:
name: kagent.tools.k8s.GetPodLogs
type: Builtin
# Adding the MCP server and the tool
- mcpServer:
toolNames:
- send_message_to_slack
toolServer: slack-mcp
type: McpServer
EOF

Open your Slack workspace and make sure the "My kagent app" is invited to the channel you want to send messages to. (You can invite it by typing /invite @mykagent in the channel.)

Now let's open the kagent UI and try chatting with the agent. Let's ask the agent the following:

Can you show me all deployments in my cluster and send it to Slack?

The agent will run the tools it needs to run and finally caall the send_message_to_slack tool to send a message to Slack:

You should see the message in the Slack channel:

Conclusion

In this guide, we walked through the process of creating a two-way integration between kagent and Slack using the A2A protocol and MCP ToolServers. You learned how to:

  1. Create and configure a Slack App with the necessary permissions and tokens.
  2. Set up a local Python bot using Bolt to handle Slack interactions.
  3. Deploy a kagent Agent equipped with Kubernetes tools and exposed using A2A.
  4. Connect the Slack bot to the kagent Agent via A2A, allowing users to invoke agent skills directly from Slack using slash commands.
  5. Enable the kagent Agent to send messages back to Slack by deploying a Slack MCP ToolServer and adding its tools to the agent.

With kagent and by leveraging A2A and MCP, you can build sophisticated workflows connecting your agents with various external platforms like Slack, enabling seamless interaction and automation.

From here, you could explore:

  • Deploying the Slack bot and agent to a production environment (requiring URL setup instead of Socket Mode and port-forwarding). You can use kgateway to expose your local agent to the public internet.
  • Adding more skills and tools to your agent.
  • Exploring other available MCP ToolServers or even creating your own.
  • Implementing more sophisticated error handling and user feedback in the Slack bot.

If you have any questions or feedback, please reach out to us on Discord.