Skip to main content

Already connecting an agent? Start with Connect AI SDK to Slack. This page is the adapter API.

@novu/framework/ai-sdk lets you write agent handlers with the Vercel AI SDK. Return an AI SDK result straight from onMessage and Novu delivers the text - including tool loops and approval gating - to the conversation.

Before you start#

Completed Connect AI SDK? Your agent, bridge, and project are already set up - jump to Minimal agent.

Otherwise connect first, then come back here for adapter details.

Prerequisites#

Install the framework, the AI SDK, and a model provider:

@novu/framework and ai are required; pick one provider package. Examples on this page use OpenAI - see the AI SDK providers list for others.

Minimal agent#

Import agent and toModelMessages from @novu/framework/ai-sdk, then return generateText() from onMessage:

Make sure the agent id ('support-bot') matches the Identifier in your dashboard, and that this handler is registered on your bridge route - see Connecting your app.

For handlers, replies, signals, typing, and tool approval, see Building blocks. The sections below cover what’s specific to the AI SDK adapter.

Returning from onMessage#

On the AI SDK path, onMessage (and onToolApproval) can return an AI SDK result in addition to the usual reply types:

ReturnBehavior
generateText(...)Novu delivers the generated text
streamText(...)Novu streams the text into the thread as the model writes it - see Stream a reply
string / JSX CardDelivered as a normal reply - see Reply
nothingAfter you call ctx.reply() yourself

Shorthand when you only need onMessage: pass the handler directly.

toModelMessages()#

Convert conversation context into AI SDK ModelMessage[]. Pass ctx so a workflow-origin notification (ctx.notification) is prepended as an assistant row with capped notification text and the full JSON payload. See Workflow-assigned agents for how that notification is assigned and hydrated.

  • ctx.history already includes the current inbound message - do not append the message argument on top of it.
  • When ctx.notification is set, it includes body (capped outbound notification text) plus payload, workflowId, and ids.
  • Tool approval cycles in history are mapped automatically, so auto-resume (below) works.
  • Wrap toModelMessages() in hydrateUnreachableAttachmentUrls before generateText / streamText. Public HTTPS signed URLs stay as URLs; local or HTTP URLs (for example LocalStack) are fetched and inlined as bytes so hosted providers can read the file.
  • Pass ctx.history instead of ctx to skip automatic origin injection when handling typed fields with isFromWorkflow:

Do not call toModelMessages(ctx) after a custom unshift — you would inject the origin twice.

Automatic tool approval#

Set needsApproval: true on an AI SDK tool(). When the model calls it, Novu posts the Approve / Deny card and pauses the turn. After the user decides, Novu re-runs onMessage - toModelMessages(ctx) now includes the decision, so the tool loop continues with no extra code from you.

Add onToolApproval when you need a hook after the click. See Tool approval for the full API.

Turn failures and onError#

When a returned generateText() / streamText() result rejects - for example the provider errors or stream consumption fails - the failure propagates to the same dispatch boundary as a thrown handler error. Register onError on the agent to log, suppress user notification, send custom copy, or fall back to Novu’s generic message.

Recoverable tool-error parts inside a multi-step AI SDK flow do not end the turn by themselves - only failures that reject the result or throw from your handler trigger onError. See Handlers and context - onError for the full pipeline.

MCP tools#

Self-hosted agents do not use the dashboard MCP server configuration - that OAuth flow and token vault are for managed agents only. On the custom code path, you wire external MCP servers yourself with the AI SDK MCP client:

Novu does not inject MCP credentials into ctx. Resolve tokens in your handler - for example, look up a per-subscriber token you stored after your own OAuth flow using ctx.subscriber?.subscriberId.