Skip to content

Mastra Slack Agent Template.

A GitHub template for a Slack agent built on Mastra, with a sandbox for every thread.

Status
Template
Platform
Slack

A customizable Slack agent built with Mastra, Bun and TypeScript, published as a GitHub template under the MIT license. It's community-maintained and not an official Mastra product.

Mention it, message it or reply in a thread and it answers in Slack, streaming the reply as it works. It can search the web with Exa, generate images, run subagents and scheduled tasks, use skills and servers, and even send emails and open PRs and issues.

Where it came from

This one grew out of Gorkie, the Slack bot Devarsh built and where I led the sandbox and runtime work. I wrote up that whole saga, from Vercel Sandboxes to E2B to Daytona, in How we taught a Slack bot to run code. By the time I started this template, Gorkie ran on Mastra, so I took its code, stripped out the Gorkie-specific parts and shaped what was left into something anyone can clone and make their own.

The README's Features list and Required services table, with Slack, PostgreSQL, OpenRouter, E2B and Exa

What it does

The main agent is the orchestrator. It talks to you in Slack and hands work off to subagents for exploring, research and running code. It can read, write and update Slack canvases, add reactions and set up recurring tasks on a cron. A cheap model writes thread titles and memory notes, and posting a new message somewhere else waits for a human to approve it.

It also tries to behave in a busy channel. Start a line with ## and it stays quiet. Send !stop and it stops mid-reply. Ask it to leave a thread and it stops following along until someone mentions it again.

The Messaging guide's Steer a thread section with three example replies

How the build went

I scaffolded it on July 8 and spent the next day trimming it down to just the Slack template and rewriting the docs. Then Slack itself got interesting.

First I tried Slack's newer agent view for DMs. It never sent the events that say a DM thread has started, so getting streaming and live tool cards into DMs took a hand-rolled workaround that pinned replies to the incoming message. I reverted to Slack's assistant view, which the Chat SDK Slack adapter already handles properly, and the workaround went away.

Scheduled tasks broke in a sneakier way. Slack's native streaming in a channel needs to know who it's streaming to, and the Chat SDK only learns that from a live incoming message. A scheduled run wakes up a quiet thread where nobody just said anything, so it fell back to plain post-and-edit and dropped every tool card. The fix was to remember the recipient for each thread from earlier live messages, saved in Postgres with an in-memory map in front so it isn't writing on every message.

DM titles were another small one. The first version just chopped the first message to 60 characters, which looked as bad as it sounds. Now the summarizer names a DM once, after the first exchange, so it can see both the question and the answer.

After that came the specialized agents, a wait tool, reactions, tool approvals, more canvas tools, follow-up messages and custom model providers. The last few days were mostly the opposite: simplifying providers, waits and scheduled tasks, swapping in Mastra's observational memory, and folding the Slack runtime into fewer pieces.

How it works

Slack talks to the agent over Socket Mode through Mastra's channels, which wrap the Chat SDK. One set of handlers covers mentions, DMs and replies in threads it's following, and each tool call shows up as a live task card while it runs. The models come through OpenRouter.

Each thread gets its own E2B sandbox. Its ID is a hash of the thread ID, so the next message in the thread reconnects to the same machine with the same files waiting. The image is Debian with Node.js and Python, plus a browser it can drive, the GitHub CLI, ffmpeg, ImageMagick and some Python data libraries, and you rebuild it from a recipe in the repo when you want more.

The part I like most is how the sandbox gets credentials without ever seeing them. The GitHub and AgentMail tokens stay on the host. E2B network rules add the right Authorization header to requests on their way out to GitHub or AgentMail, so the agent inside can push code, open PRs and send emails using placeholder values, and there's no real token in there for it to leak.

Memory is Mastra's, stored in PostgreSQL. It keeps the last 20 messages plus observational memory, and both are scoped to the thread on purpose. Slack channels are shared, and memory that follows a person around would happily carry details from one conversation into another.

What's next

There's a plan in the repo for adding Discord and Telegram through the same Chat SDK adapters, so one agent with one memory and one set of tools could answer on all three. That would be a fun full circle after my old Discord AI Bot.