MastraServer
The MastraServer abstract class is the base for all server adapters. Extend this class to create adapters for frameworks other than Hono or Express.
ImportDirect link to Import
import { MastraServer } from '@mastra/server/server-adapter';
Type parametersDirect link to Type parameters
MastraServer<TApp, TRequest, TResponse>
| Parameter | Description |
|---|---|
TApp | Framework app type (e.g., Hono, Application) |
TRequest | Framework request type |
TResponse | Framework response/context type |
ConstructorDirect link to Constructor
constructor(options: MastraServerOptions<TApp>)
OptionsDirect link to Options
app:
mastra:
prefix?:
openapiPath?:
bodyLimitOptions?:
streamOptions?:
customRouteAuthConfig?:
tools?:
taskStore?:
playground?:
isDev?:
Abstract methodsDirect link to Abstract methods
These methods must be implemented by adapters:
registerContextMiddleware()Direct link to registerContextMiddleware()
Attach Mastra context to every request.
abstract registerContextMiddleware(): void
Context to attach:
mastra- Mastra instancerequestContext- Request-scoped contexttools- Available toolsabortSignal- Request cancellation signal
registerAuthMiddleware()Direct link to registerAuthMiddleware()
Register authentication and authorization middleware.
abstract registerAuthMiddleware(): void
registerRoute()Direct link to registerRoute()
Register a single route with the framework.
abstract registerRoute(
app: TApp,
route: ServerRoute,
options: { prefix?: string }
): Promise<void>
getParams()Direct link to getParams()
Extract parameters from the request.
abstract getParams(
route: ServerRoute,
request: TRequest
): Promise<{
urlParams: Record<string, string>;
queryParams: Record<string, string>;
body: unknown;
}>
sendResponse()Direct link to sendResponse()
Send response based on route type.
abstract sendResponse(
route: ServerRoute,
response: TResponse,
result: unknown
): Promise<unknown>
stream()Direct link to stream()
Handle streaming responses.
abstract stream(
route: ServerRoute,
response: TResponse,
result: unknown
): Promise<unknown>
Instance methodsDirect link to Instance methods
init()Direct link to init()
Initialize the server by registering all middleware and routes.
async init(): Promise<void>
Calls in order:
registerContextMiddleware()registerAuthMiddleware()registerRoutes()
registerRoutes()Direct link to registerRoutes()
Register all Mastra routes.
async registerRoutes(): Promise<void>
getApp()Direct link to getApp()
Get the framework app instance.
getApp<T = TApp>(): T
parsePathParams()Direct link to parsePathParams()
Validate path parameters with the route's Zod schema.
async parsePathParams(
route: ServerRoute,
params: Record<string, string>
): Promise<Record<string, unknown>>
parseQueryParams()Direct link to parseQueryParams()
Validate query parameters with the route's Zod schema.
async parseQueryParams(
route: ServerRoute,
params: Record<string, string>
): Promise<Record<string, unknown>>
parseBody()Direct link to parseBody()
Validate request body with the route's Zod schema.
async parseBody(
route: ServerRoute,
body: unknown
): Promise<unknown>
registerOpenAPIRoute()Direct link to registerOpenAPIRoute()
Register an endpoint that serves the OpenAPI specification.
async registerOpenAPIRoute(
app: TApp,
config: OpenAPIConfig,
options: { prefix?: string }
): Promise<void>
Protected methodsDirect link to Protected methods
mergeRequestContext()Direct link to mergeRequestContext()
Merge request context from multiple sources (query params and body).
protected mergeRequestContext(options: {
paramsRequestContext?: Record<string, any>;
bodyRequestContext?: Record<string, any>;
}): RequestContext
TypesDirect link to Types
BodyLimitOptionsDirect link to BodyLimitOptions
interface BodyLimitOptions {
maxSize: number;
onError: (error: unknown) => unknown;
}
StreamOptionsDirect link to StreamOptions
interface StreamOptions {
redact?: boolean;
}
ExampleDirect link to Example
import { MastraServer, ServerRoute } from '@mastra/server/server-adapter';
import type { Mastra } from '@mastra/core';
export class MyServer extends MastraServer<MyApp, MyRequest, MyResponse> {
registerContextMiddleware(): void {
this.app.use('*', (req, res, next) => {
res.locals.mastra = this.mastra;
next();
});
}
registerAuthMiddleware(): void {
const auth = this.mastra.getServer()?.auth;
if (!auth) return;
// Implement auth
}
async registerRoute(app, route, { prefix }) {
// Implement route registration
}
async getParams(route, request) {
return {
urlParams: request.params,
queryParams: request.query,
body: request.body,
};
}
async sendResponse(route, response, result) {
return response.json(result);
}
async stream(route, response, result) {
// Implement streaming
}
}
RelatedDirect link to Related
- Server Adapters - Using adapters
- Custom Adapters - Creating custom adapters
- createRoute() - Creating custom routes