mastra-framework-integration
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Add Mastra agents to an existing web, server, or desktop app: Next.js, React with Vite, Astro, SvelteKit, Nuxt, Angular, Electron, Express, Hono, NestJS, and TanStack Start. Covers the three integration shapes (call the agent in your own route, mount the Mastra API with a server adapter, or run a separate Mastra server and call it from the frontend), mastra init, chat routes, history loading, CORS, CSP, and bundler fixes. Use this skill whenever the user wants to put a Mastra agent into a specific framework, asks where the src/mastra folder goes, how to stream chat into their app, or how to expose /api/agents from their app. Triggers on Next.js App Router route.ts, @mastra/next, createNextRouteHandler, @mastra/tanstack-start, createStartRouteHandler, @mastra/express, @mastra/hono, @mastra/nestjs, MastraModule, SvelteKit +server.ts, Nuxt defineEventHandler, Astro APIRoute, Angular Chat, Electron renderer, Vite React, mastra init, handleChatStream in a framework route.
- Reads credentials
- Sends data out
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
10 rules. About 0 are common, 0 are distinctive, and 10 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
10
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 55
Every guide starts the same way: run npx mastra@latest init in the project root.
- Very specific: line 117
Verify the API: If node_modules/@mastra/ exists, read the embedded docs in node_modules/@mastra/ai-sdk/dist/docs/ and the adapter package.
- Very specific: line 119
Initialize Mastra: Run npx mastra@latest init and enter the provider key.
- Very specific: line 124
Allow cross-origin calls for shape C: Add server.cors and, for Electron, add http://localhost:4111 to the CSP connect-src.
- Very specific: line 132
Add a GET handler next to the chat POST that reads memory and converts it for useChat():
- Very specific: line 161
NestJS ESM build fails on imports: Add .js to every relative import in the generated files.
- Very specific: line 162
NestJS CommonJS build fails on
await: Remove the top-level await (the DuckDBStore observability domain) from src/mastra/index.ts. - Very specific: line 165
TanStack Start dev crashes on a
.nodefile: Add optimizeDeps.exclude: ['@mastra/duckdb', 'execa'] and Nitro external /^@duckdb\//. - Very specific: line 166
Electron or Vite chat fails with a CORS or CSP error: Add server.cors to the Mastra config and connect-src http://localhost:4111 to the …
- Very specific: line 170
Hard-coded thread IDs in production: Every user shares one conversation.
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Framework Integration
Wire Mastra into an existing application framework so its routes and UI can call agents, with the least moving parts for that framework.
When to use
- Adding Mastra to an existing Next.js, Vite + React, Astro, SvelteKit, Nuxt, Angular, or Electron app
- Exposing Mastra's HTTP API from an Express, Hono, NestJS, Next.js, or TanStack Start app
- Deciding whether Mastra should run inside the app or as a separate server
- Fixing framework-specific build errors (ESM imports, top-level await, browser build pulling in server code, native modules)
- Hydrating chat history from memory on page load
When NOT to use
- Starting a brand new standalone Mastra project (use
mastra-project-setup) - Server options, custom routes, middleware, adapter internals (use
mastra-server) - Chat UI components,
useChat()details, custom tool rendering (usemastra-agentic-ui) - Calling a remote Mastra server with
MastraClient(usemastra-client-sdk) - Securing the routes (use
mastra-auth) - Hosting the result on Vercel, Netlify, Cloudflare, or containers (use
mastra-deployment)
Required inputs
- The framework, its version, and its router style (for example Next.js App Router, SvelteKit, Nuxt server routes)
- Whether the framework has a server runtime (SSR or API routes) or is browser-only
- Package manager and module system (
"type": "module"or CommonJS) - What the UI needs: a chat page, a single API call, or the full Mastra API
- The model provider key to configure
The framework: three integration shapes
| Shape | How it works | Frameworks in the docs |
| A. In-app route | Import mastra into a server route and call handleChatStream() or the agent directly | Next.js, Astro, SvelteKit, Nuxt |
| B. Server adapter | Mount all Mastra routes (/api/agents/...) inside the app | Express, Hono, NestJS, Next.js (@mastra/next), TanStack Start |
| C. Separate Mastra server | Run mastra dev on port 4111, add chatRoute() to apiRoutes, call it from the browser | Vite + React, Angular, Electron |
Every guide starts the same way: run npx mastra@latest init in the project root. It creates src/mastra/ with index.ts, agents/weather-agent.ts, and tools/weather-tool.ts, and asks for a provider key.
Shape A, Next.js App Router:
// src/app/api/chat/route.ts
import { handleChatStream } from '@mastra/ai-sdk'
import { createUIMessageStreamResponse } from 'ai'
import { mastra } from '@/mastra'
export async function POST(req: Request) {
const params = await req.json()
const stream = await handleChatStream({
mastra,
agentId: 'weather-agent',
version: 'v7',
params,
})
return createUIMessageStreamResponse({ stream })
}Shape B, Express:
import express from 'express'
import { MastraServer } from '@mastra/express'
import { mastra } from './mastra'
const app = express()
app.use(express.json())
const server = new MastraServer({ app, mastra })
await server.init()
app.listen(3000)Shape C, Mastra config:
import { Mastra } from '@mastra/core/mastra'
import { chatRoute } from '@mastra/ai-sdk'
export const mastra = new Mastra({
server: {
apiRoutes: [chatRoute({ path: '/chat/:agentId' })],
},
})The browser then points DefaultChatTransport at http://localhost:4111/chat/weather-agent.
Picking a shape
- The app has a Node server and you only need chat: shape A.
- The app is a backend (Express, Hono, NestJS) or other services need the whole API: shape B.
- The app is browser-only (Vite SPA, Angular CSR, Electron renderer): shape C.
- You need Studio, file-based agents, or independent scaling: shape C.
Workflow
- Verify the API. If
node_modules/@mastra/exists, read the embedded docs innode_modules/@mastra/ai-sdk/dist/docs/and the adapter package. Otherwise fetch the framework page as Markdown, for examplehttps://mastra.ai/integrations/frameworks/next-js.md. Trust these over memory. - Pick the shape with the table above.
- Initialize Mastra. Run
npx mastra@latest initand enter the provider key. Check the generatedsrc/mastra/index.ts. - Fix the module system. ESM projects with
nodenextneed.json relative imports. CommonJS projects can't use top-levelawaitinsrc/mastra/index.ts. - Install packages. Shape A or C chat:
@mastra/ai-sdk,ai, and the UI binding (@ai-sdk/react,@ai-sdk/svelte,@ai-sdk/vue,@ai-sdk/angular). Shape B: the adapter package. - Add the server piece. In-app route, adapter mount, or
chatRoute()inapiRoutes. - Keep server code out of the browser. Angular: exclude
src/mastra/**intsconfig.app.json. TanStack Start: exclude DuckDB andexecafrom Vite and Nitro. - Allow cross-origin calls for shape C. Add
server.corsand, for Electron, addhttp://localhost:4111to the CSPconnect-src. - Add the UI (see
mastra-agentic-ui). - Test.
curl -X POST http://localhost:3000/api/agents/weather-agent/generate -H "Content-Type: application/json" -d '{"messages":[{"role":"user","content":"Weather in Seoul?"}]}'for adapters, or open the chat page.
Deep dive: loading history in shape A
Add a GET handler next to the chat POST that reads memory and converts it for useChat():
import { toAISdkMessages } from '@mastra/ai-sdk/ui'
import { NextResponse } from 'next/server'
import { mastra } from '@/mastra'
const THREAD_ID = 'example-user-id'
const RESOURCE_ID = 'weather-chat'
export async function GET() {
const memory = await mastra.getAgentById('weather-agent').getMemory()
let response = null
try {
response = await memory?.recall({ threadId: THREAD_ID, resourceId: RESOURCE_ID })
} catch {
console.log('No previous messages found.')
}
return NextResponse.json(toAISdkMessages(response?.messages || [], { version: 'v7' }))
}In the POST handler, force the same memory.thread and memory.resource into params. Replace the constants with the signed-in user's IDs in real apps.
Failure patterns
- Studio and the app see different data.
next devandmastra devresolve a relative SQLite path from different directories. Use an absolutefile:URL in storage. - NestJS ESM build fails on imports. Add
.jsto every relative import in the generated files. - NestJS CommonJS build fails on
await. Remove the top-levelawait(theDuckDBStoreobservability domain) fromsrc/mastra/index.ts. - NestJS unrelated routes 404.
MastraModuleis a catch-all. Import it last or give it its own prefix. - Angular build errors on
process.env.src/mastra/**is compiled into the browser build. Exclude it intsconfig.app.json. - TanStack Start dev crashes on a
.nodefile. AddoptimizeDeps.exclude: ['@mastra/duckdb', 'execa']and Nitro external/^@duckdb\//. - Electron or Vite chat fails with a CORS or CSP error. Add
server.corsto the Mastra config andconnect-src http://localhost:4111to the renderer CSP. - Adapter prefix mismatch.
@mastra/nextand@mastra/tanstack-startneedprefixequal to the mount path (for example/api/mastra). - AI SDK message type errors.
handleChatStream(),chatRoute(), and converters default to the v5 contract. Passversionto match the installedaipackage. ai-elementsTypeScript errors. The registry ships Radix UI components only. Use a Radix style incomponents.json.- Hard-coded thread IDs in production. Every user shares one conversation. Derive
threadandresourcefrom auth.
Output format
A finished integration contains:
src/mastra/(agents, tools,index.ts) compiling under the app's module system- The server piece: a chat route file, an adapter mount, or
chatRoute()inapiRoutes - Build config changes (tsconfig excludes, Vite or Nitro settings, CSP, CORS) with a comment on why
- Provider key and
MASTRA_API_URL(shape C) in.env.example - A run note: which commands start the app (and the Mastra server for shape C), and a working
curlor chat test
Reference files
references/frameworks.md- One row per framework: shape, packages, files to create, and gotchas.references/api.md- Signatures for the helpers the guides use:handleChatStream,chatRoute,toAISdkMessages, adapter mount functions.references/examples.md- Complete SvelteKit, Nuxt, Angular, and NestJS integrations.references/checklist.md- Integration checklist.
Live docs
- https://mastra.ai/integrations/frameworks/next-js
- https://mastra.ai/integrations/frameworks/vite-react
- https://mastra.ai/integrations/frameworks/astro
- https://mastra.ai/integrations/frameworks/sveltekit
- https://mastra.ai/integrations/frameworks/nuxt
- https://mastra.ai/integrations/frameworks/angular
- https://mastra.ai/integrations/frameworks/electron
- https://mastra.ai/integrations/frameworks/express
- https://mastra.ai/integrations/frameworks/hono
- https://mastra.ai/integrations/frameworks/nestjs
- https://mastra.ai/integrations/frameworks/tanstack-start
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

