server-adapters/koa/README.md
Koa server adapter for Mastra, enabling you to run Mastra with the Koa framework.
npm install @mastra/koa koa koa-bodyparser
import Koa from 'koa';
import bodyParser from 'koa-bodyparser';
import { MastraServer } from '@mastra/koa';
import { mastra } from './mastra';
const app = new Koa();
app.use(bodyParser());
const server = new MastraServer({ app, mastra });
await server.init();
app.listen(3000, () => {
console.log('Server running on http://localhost:3000');
});
Add routes directly to the Koa app with access to Mastra context:
// Routes added after init() have access to Mastra context via ctx.state
app.use(async ctx => {
if (ctx.path === '/health' && ctx.method === 'GET') {
const mastraInstance = ctx.state.mastra;
const agents = Object.keys(mastraInstance.listAgents());
ctx.body = { status: 'ok', agents };
}
});
const server = new MastraServer({
app,
mastra,
prefix: '/api/v2', // Route prefix
openapiPath: '/openapi.json', // OpenAPI spec endpoint
bodyLimitOptions: {
maxSize: 10 * 1024 * 1024, // 10MB
onError: err => ({ error: 'Payload too large' }),
},
streamOptions: { redact: true }, // Redact sensitive data from streams
});
Access these in route handlers via ctx.state:
| Key | Description |
|---|---|
mastra | Mastra instance |
requestContext | Request context map |
abortSignal | Request cancellation signal |
tools | Available tools |