docs/agents/get-started/mastra.mdx
Connect a Mastra agent to Novu Connect via @novu/framework. Call generate() in onMessage and return the text. Novu delivers the reply on Slack. The same handler works on other providers after you link them.
There is no @novu/framework/mastra adapter yet. Use the base Framework package and return text from onMessage (or call ctx.reply()).
OPENAI_API_KEY (or another model key Mastra supports)In the Novu dashboard:
Alternatively run npx novu connect, choose Custom code and Slack, and skip creating a starter project when prompted.
</Step>
npm install @novu/framework @mastra/core
Add to .env.local:
NOVU_SECRET_KEY=your-novu-secret-key
OPENAI_API_KEY=sk-...
import { Agent } from '@mastra/core/agent';
export const mastraSupport = new Agent({
id: 'mastra-support',
name: 'Support Agent',
instructions: 'You are a helpful support agent. Keep answers short.',
model: 'openai/gpt-4o',
});
import { agent } from '@novu/framework';
import { mastraSupport } from './mastra-support';
function toMastraMessages(history: { role: string; type: string; content: string }[]) {
return history
.filter((entry) => entry.type === 'message' && entry.content.trim())
.map((entry) => ({
role: entry.role === 'agent' || entry.role === 'assistant' ? 'assistant' : 'user',
content: entry.content,
}));
}
export const supportBot = agent('support-bot', {
onMessage: async (_message, ctx) => {
const result = await mastraSupport.generate(toMastraMessages(ctx.history));
return result.text;
},
});
Match agent('...') to the dashboard Identifier. Export the Novu agent from your agents index.
</Step>
import { serve } from '@novu/framework/next';
import { supportBot } from '../../novu/agents';
export const { GET, POST, OPTIONS } = serve({
agents: [supportBot],
});
For Express, Hono, and other servers, see Connecting your app. </Step>
<Step> ## Run locally and message Slacknpx novu dev --port 4000
DM or @mention the bot in Slack. The reply should come from Mastra in the same thread. </Step>
</Steps>For sensitive tools, use ctx.toolApproval.request() from Tool approval so the turn pauses for Approve / Deny before execution.
npx novu connect
Choose Custom code, connect Slack, accept the starter project, install @mastra/core, and replace the demo handler with the code above.
For an existing codebase, use Connect an existing app instead.
<Prompt description="Connect my Mastra agent to Slack with Novu" icon="plug" actions={["copy", "cursor"]}>
Follow https://docs.novu.co/agents/get-started/mastra
Add Novu bridge + Mastra handler to this repo and connect Slack. Prefer the existing project over scaffolding a new one.
ALWAYS:
agent('id') to the dashboard agent Identifiertype === 'message' history entries into Mastra messagesopenai/gpt-4oNEVER:
@novu/framework/ai-sdk or @novu/framework/langchain for this path