Mastra
Give a Mastra agent speak, listen, transcribe and end_call tools with voiceTools from voiceai-sdk/mastra.
A Mastra agent takes a map of tools. voiceTools(session) from voiceai-sdk/mastra returns that map for one session, so the agent talks to a caller with the same tool calls it uses for everything else.
Install
npm install voiceai-sdk @mastra/corepnpm add voiceai-sdk @mastra/coreyarn add voiceai-sdk @mastra/corebun add voiceai-sdk @mastra/coreThe voiceai-sdk/mastra subpath ships inside voiceai-sdk. @mastra/core provides the Agent class. The examples use the model string "openai/gpt-5", which Mastra resolves with its own credentials.
Quickstart
This server opens a session for each WebSocket connection, builds an agent with that session's tools, and lets the agent run the call.
import { Agent } from "@mastra/core/agent";
import { createSession } from "voiceai-sdk";
import { deepgram } from "voiceai-sdk/deepgram";
import { voiceTools } from "voiceai-sdk/mastra";
import { webSocketAudio } from "voiceai-sdk/ws";
Bun.serve({
port: 3000,
fetch: (req, server) => (server.upgrade(req) ? undefined : new Response("upgrade required", { status: 426 })),
websocket: {
async open(ws) {
await using session = await createSession(deepgram());
session.connect(webSocketAudio(ws));
const agent = new Agent({
name: "front-desk",
instructions:
"You answer calls for a dental clinic. Use speak to talk and listen to hear the caller. Call end_call when they are done.",
model: "openai/gpt-5",
tools: voiceTools(session),
});
await agent.generate("A caller just connected. Greet them.", { maxSteps: 50 });
},
},
});The agent calls speak to talk and listen to wait for the caller, one step after another, until it calls end_call or runs out of steps. When generate returns, the block ends and await using closes the session.
One agent per session
The tools are bound to the session you pass in. A speak call always goes to that session, so an agent built with one caller's tools must never answer another caller. Build the agent inside the connection handler, as above, not at module scope.
- Share the parts that don't change. Keep the instructions string and the model at module scope and reuse them for every agent.
- Add your own tools next to the voice tools. The map is a plain object, so spread it:
tools: { ...voiceTools(session), ...bookingTools }. - Keep the tool names. The instructions refer to
speak,listenandend_callby name. Renaming them means rewriting the instructions too.
Where keys stay
| Credential | Read by | Where it lives |
|---|---|---|
DEEPGRAM_API_KEY | deepgram() | Your server's environment |
| The model's credentials | Mastra, for "openai/gpt-5" | Your server's environment |
| None | The browser | It streams audio over the WebSocket and plays what comes back |
The agent, the session and both sets of credentials run in the same server process. If you serve the agent from Mastra's own server instead, the same rule applies: create the session there, never in code that ships to the browser.
Next steps
- Give your agent a voice: the full walkthrough, from the first turn to hanging up.
- Build a phone agent: put this agent on a phone number.
- Agent tools: each tool's input and output.