Built-in Middleware
The server automatically registers these middleware functions during initialization:- Context Middleware - Sets up request context and dependencies
- Authentication Middleware - Verifies user tokens
- Authorization Middleware - Checks user permissions
Authentication
Configure authentication in your Mastra server config:Token Sources
The server checks for tokens in this order:Authorizationheader:Bearer <token>apiKeyquery parameter:?apiKey=<token>
Authorization
Path-Based Authorization
Control access based on request path and method:Rule-Based Authorization
Define authorization rules using patterns:Public Routes
Make specific routes publicly accessible:Development Playground Access
The development playground can bypass authentication:Custom Middleware (Hono)
Add custom middleware to your Hono app:Custom Middleware (Express)
Add custom middleware to your Express app:Request Context
The context middleware automatically parses and sets up request context:Error Handling
The server automatically handles errors and returns appropriate status codes:Next Steps
Server Adapters
Learn about different server adapters
Observability
Monitor your server with observability