@mastra/nestjs 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +324 -0
- package/dist/__tests__/test-helpers.d.ts +17 -0
- package/dist/__tests__/test-helpers.d.ts.map +1 -0
- package/dist/constants.d.ts +34 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/controllers/index.d.ts +3 -0
- package/dist/controllers/index.d.ts.map +1 -0
- package/dist/controllers/mastra.controller.d.ts +54 -0
- package/dist/controllers/mastra.controller.d.ts.map +1 -0
- package/dist/controllers/system.controller.d.ts +45 -0
- package/dist/controllers/system.controller.d.ts.map +1 -0
- package/dist/decorators/index.d.ts +3 -0
- package/dist/decorators/index.d.ts.map +1 -0
- package/dist/decorators/public.decorator.d.ts +17 -0
- package/dist/decorators/public.decorator.d.ts.map +1 -0
- package/dist/decorators/throttle.decorator.d.ts +34 -0
- package/dist/decorators/throttle.decorator.d.ts.map +1 -0
- package/dist/filters/index.d.ts +2 -0
- package/dist/filters/index.d.ts.map +1 -0
- package/dist/filters/mastra-exception.filter.d.ts +28 -0
- package/dist/filters/mastra-exception.filter.d.ts.map +1 -0
- package/dist/guards/index.d.ts +3 -0
- package/dist/guards/index.d.ts.map +1 -0
- package/dist/guards/mastra-auth.guard.d.ts +42 -0
- package/dist/guards/mastra-auth.guard.d.ts.map +1 -0
- package/dist/guards/mastra-route.guard.d.ts +23 -0
- package/dist/guards/mastra-route.guard.d.ts.map +1 -0
- package/dist/guards/mastra-throttle.guard.d.ts +53 -0
- package/dist/guards/mastra-throttle.guard.d.ts.map +1 -0
- package/dist/index.cjs +14298 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +14284 -0
- package/dist/index.js.map +1 -0
- package/dist/interceptors/index.d.ts +3 -0
- package/dist/interceptors/index.d.ts.map +1 -0
- package/dist/interceptors/request-tracking.interceptor.d.ts +13 -0
- package/dist/interceptors/request-tracking.interceptor.d.ts.map +1 -0
- package/dist/interceptors/streaming.interceptor.d.ts +40 -0
- package/dist/interceptors/streaming.interceptor.d.ts.map +1 -0
- package/dist/interceptors/tracing.interceptor.d.ts +12 -0
- package/dist/interceptors/tracing.interceptor.d.ts.map +1 -0
- package/dist/mastra-server.adapter.d.ts +10 -0
- package/dist/mastra-server.adapter.d.ts.map +1 -0
- package/dist/mastra.module.d.ts +173 -0
- package/dist/mastra.module.d.ts.map +1 -0
- package/dist/mastra.service.d.ts +51 -0
- package/dist/mastra.service.d.ts.map +1 -0
- package/dist/middleware/body-limit.middleware.d.ts +24 -0
- package/dist/middleware/body-limit.middleware.d.ts.map +1 -0
- package/dist/middleware/index.d.ts +3 -0
- package/dist/middleware/index.d.ts.map +1 -0
- package/dist/middleware/json-body.middleware.d.ts +17 -0
- package/dist/middleware/json-body.middleware.d.ts.map +1 -0
- package/dist/services/auth.service.d.ts +34 -0
- package/dist/services/auth.service.d.ts.map +1 -0
- package/dist/services/index.d.ts +5 -0
- package/dist/services/index.d.ts.map +1 -0
- package/dist/services/request-context.service.d.ts +52 -0
- package/dist/services/request-context.service.d.ts.map +1 -0
- package/dist/services/route-handler.service.d.ts +78 -0
- package/dist/services/route-handler.service.d.ts.map +1 -0
- package/dist/services/shutdown.service.d.ts +54 -0
- package/dist/services/shutdown.service.d.ts.map +1 -0
- package/dist/utils/constants.d.ts +3 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/format.d.ts +5 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/parse-multipart.d.ts +18 -0
- package/dist/utils/parse-multipart.d.ts.map +1 -0
- package/dist/utils/route-path.d.ts +6 -0
- package/dist/utils/route-path.d.ts.map +1 -0
- package/package.json +84 -0
package/README.md
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
# @mastra/nestjs
|
|
2
|
+
|
|
3
|
+
NestJS server adapter for [Mastra](https://mastra.ai). Use it to expose agents, workflows, tools, MCP, and streaming endpoints through NestJS with native guards, interceptors, and DI.
|
|
4
|
+
|
|
5
|
+
This package supports NestJS running on the Express adapter only. If your app uses Fastify, `MastraModule` now fails fast during bootstrap with a clear error instead of partially initializing.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- **NestJS-native integration** via modules, DI, guards, interceptors, and filters
|
|
10
|
+
- **Rate limiting** enabled by default (opt-out)
|
|
11
|
+
- **Graceful shutdown** with in-flight request tracking and optional SSE notifications
|
|
12
|
+
- **Streaming** for AI responses with optional redaction and SSE heartbeats
|
|
13
|
+
- **MCP transport** (HTTP + SSE) exposed under the API prefix
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @mastra/nestjs @mastra/core
|
|
19
|
+
# or
|
|
20
|
+
pnpm add @mastra/nestjs @mastra/core
|
|
21
|
+
# or
|
|
22
|
+
yarn add @mastra/nestjs @mastra/core
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
// app.module.ts
|
|
29
|
+
import { Module } from '@nestjs/common';
|
|
30
|
+
import { MastraModule } from '@mastra/nestjs';
|
|
31
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
32
|
+
import { z } from 'zod';
|
|
33
|
+
import { createTool } from '@mastra/core/tools';
|
|
34
|
+
|
|
35
|
+
/** Simple tool used by the demo agent. */
|
|
36
|
+
const pingTool = createTool({
|
|
37
|
+
id: 'ping',
|
|
38
|
+
description: 'Returns a pong response',
|
|
39
|
+
inputSchema: z.object({ message: z.string() }),
|
|
40
|
+
execute: async ({ message }) => ({ ok: true, message }),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Minimal Mastra instance for NestJS integration. */
|
|
44
|
+
const mastra = new Mastra({
|
|
45
|
+
tools: { ping: pingTool },
|
|
46
|
+
agents: {
|
|
47
|
+
greeter: {
|
|
48
|
+
name: 'greeter',
|
|
49
|
+
description: 'Greets the user and can call tools.',
|
|
50
|
+
model: 'openai/gpt-4o-mini',
|
|
51
|
+
tools: ['ping'],
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
@Module({
|
|
57
|
+
imports: [MastraModule.register({ mastra })],
|
|
58
|
+
})
|
|
59
|
+
export class AppModule {}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
// main.ts
|
|
64
|
+
import { NestFactory } from '@nestjs/core';
|
|
65
|
+
import { AppModule } from './app.module';
|
|
66
|
+
|
|
67
|
+
async function bootstrap() {
|
|
68
|
+
const app = await NestFactory.create(AppModule);
|
|
69
|
+
await app.listen(3000);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
bootstrap();
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
With the default prefix (`/api`), Mastra routes mount under `http://localhost:3000/api`.
|
|
76
|
+
|
|
77
|
+
## Async Module Registration
|
|
78
|
+
|
|
79
|
+
Use async registration when the Mastra config depends on runtime services (e.g., `ConfigService`).
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { Module } from '@nestjs/common';
|
|
83
|
+
import { ConfigModule, ConfigService } from '@nestjs/config';
|
|
84
|
+
import { MastraModule } from '@mastra/nestjs';
|
|
85
|
+
import { Mastra } from '@mastra/core/mastra';
|
|
86
|
+
|
|
87
|
+
@Module({
|
|
88
|
+
imports: [
|
|
89
|
+
ConfigModule.forRoot(),
|
|
90
|
+
MastraModule.registerAsync({
|
|
91
|
+
imports: [ConfigModule],
|
|
92
|
+
useFactory: (config: ConfigService) => ({
|
|
93
|
+
mastra: new Mastra({
|
|
94
|
+
agents: {
|
|
95
|
+
greeter: {
|
|
96
|
+
name: 'greeter',
|
|
97
|
+
description: 'Greets users with a short response.',
|
|
98
|
+
model: config.get('MASTRA_MODEL', 'openai/gpt-4o-mini'),
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
}),
|
|
102
|
+
prefix: config.get('MASTRA_PREFIX', '/api'),
|
|
103
|
+
}),
|
|
104
|
+
inject: [ConfigService],
|
|
105
|
+
}),
|
|
106
|
+
],
|
|
107
|
+
})
|
|
108
|
+
export class AppModule {}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
## Injecting Mastra in Services
|
|
112
|
+
|
|
113
|
+
Use `MASTRA` for direct access or `MastraService` for helper methods.
|
|
114
|
+
|
|
115
|
+
```typescript
|
|
116
|
+
import { Injectable, Inject } from '@nestjs/common';
|
|
117
|
+
import { MASTRA, MastraService } from '@mastra/nestjs';
|
|
118
|
+
import type { Mastra } from '@mastra/core/mastra';
|
|
119
|
+
|
|
120
|
+
@Injectable()
|
|
121
|
+
export class AgentService {
|
|
122
|
+
constructor(@Inject(MASTRA) private readonly mastra: Mastra) {}
|
|
123
|
+
|
|
124
|
+
async greet() {
|
|
125
|
+
const agent = this.mastra.getAgent('greeter');
|
|
126
|
+
return agent.generate({
|
|
127
|
+
messages: [{ role: 'user', content: 'Hello from NestJS' }],
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
@Injectable()
|
|
133
|
+
export class WorkflowService {
|
|
134
|
+
constructor(private readonly mastraService: MastraService) {}
|
|
135
|
+
|
|
136
|
+
async runWorkflow(workflowId: string, inputData: Record<string, unknown>) {
|
|
137
|
+
const workflow = this.mastraService.getWorkflow(workflowId);
|
|
138
|
+
return workflow.start({ inputData });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Request Context (GET + POST)
|
|
144
|
+
|
|
145
|
+
Pass request context via query string or JSON body. The adapter accepts JSON or base64-encoded JSON.
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
curl "http://localhost:3000/api/agents/greeter/generate?requestContext=%7B%22userId%22%3A%22123%22%7D"
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
```bash
|
|
152
|
+
curl -X POST "http://localhost:3000/api/agents/greeter/generate" \
|
|
153
|
+
-H "Content-Type: application/json" \
|
|
154
|
+
-d '{"messages":[{"role":"user","content":"hi"}],"requestContext":{"userId":"123"}}'
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Rate Limiting
|
|
158
|
+
|
|
159
|
+
Rate limiting is on by default. Disable it or customize limits:
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
MastraModule.register({
|
|
163
|
+
mastra,
|
|
164
|
+
rateLimitOptions: {
|
|
165
|
+
enabled: true,
|
|
166
|
+
defaultLimit: 200,
|
|
167
|
+
generateLimit: 20,
|
|
168
|
+
windowMs: 60_000,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
## Mastra Auth Compatibility
|
|
174
|
+
|
|
175
|
+
Mastra's built-in token auth is disabled by default because most NestJS apps already have their own auth layer. When enabled, bearer tokens from the `Authorization` header are the default credential source.
|
|
176
|
+
|
|
177
|
+
Query-string `?apiKey=` auth is available only as an explicit backward-compatibility option:
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
MastraModule.register({
|
|
181
|
+
mastra,
|
|
182
|
+
auth: {
|
|
183
|
+
enabled: true,
|
|
184
|
+
allowQueryApiKey: true,
|
|
185
|
+
},
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Streaming Options
|
|
190
|
+
|
|
191
|
+
```typescript
|
|
192
|
+
MastraModule.register({
|
|
193
|
+
mastra,
|
|
194
|
+
streamOptions: {
|
|
195
|
+
redact: true,
|
|
196
|
+
heartbeatMs: 20_000,
|
|
197
|
+
},
|
|
198
|
+
});
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
## MCP Transport (HTTP + SSE)
|
|
202
|
+
|
|
203
|
+
MCP endpoints are exposed under the API prefix:
|
|
204
|
+
|
|
205
|
+
- `POST /api/mcp/:serverId/mcp`
|
|
206
|
+
- `GET /api/mcp/:serverId/sse`
|
|
207
|
+
- `POST /api/mcp/:serverId/messages`
|
|
208
|
+
|
|
209
|
+
## Health Endpoints
|
|
210
|
+
|
|
211
|
+
These are always at the root (not under the prefix):
|
|
212
|
+
|
|
213
|
+
- `GET /health`
|
|
214
|
+
- `GET /ready`
|
|
215
|
+
- `GET /info`
|
|
216
|
+
|
|
217
|
+
## Decorators
|
|
218
|
+
|
|
219
|
+
Skip auth or rate limiting on specific controller routes:
|
|
220
|
+
|
|
221
|
+
```typescript
|
|
222
|
+
import { Controller, Get, Post } from '@nestjs/common';
|
|
223
|
+
import { Public, SkipThrottle, MastraThrottle } from '@mastra/nestjs';
|
|
224
|
+
|
|
225
|
+
@Controller('custom')
|
|
226
|
+
export class CustomController {
|
|
227
|
+
@Get('public')
|
|
228
|
+
@Public()
|
|
229
|
+
publicRoute() {}
|
|
230
|
+
|
|
231
|
+
@Get('unlimited')
|
|
232
|
+
@SkipThrottle()
|
|
233
|
+
unlimitedRoute() {}
|
|
234
|
+
|
|
235
|
+
@Post('custom-limit')
|
|
236
|
+
@MastraThrottle({ limit: 5, windowMs: 60_000 })
|
|
237
|
+
customLimitRoute() {}
|
|
238
|
+
}
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
## Configuration Options
|
|
242
|
+
|
|
243
|
+
| Option | Type | Default | Description |
|
|
244
|
+
| ----------------------------------- | --------------------------------------------------- | -------------------- | ------------------------------------------- |
|
|
245
|
+
| `mastra` | `Mastra` | required | The Mastra instance |
|
|
246
|
+
| `prefix` | `string` | `/api` | Route prefix |
|
|
247
|
+
| `rateLimitOptions` | `object` | enabled | Rate limiting configuration |
|
|
248
|
+
| `rateLimitOptions.enabled` | `boolean` | `true` | Enable/disable rate limiting |
|
|
249
|
+
| `rateLimitOptions.defaultLimit` | `number` | `100` | Requests per window |
|
|
250
|
+
| `rateLimitOptions.generateLimit` | `number` | `10` | Stricter limit for `/generate` |
|
|
251
|
+
| `rateLimitOptions.windowMs` | `number` | `60000` | Window size in ms |
|
|
252
|
+
| `shutdownOptions` | `object` | - | Graceful shutdown configuration |
|
|
253
|
+
| `shutdownOptions.timeoutMs` | `number` | `30000` | Max wait time for in-flight requests |
|
|
254
|
+
| `shutdownOptions.notifyClients` | `boolean` | `true` | Send shutdown event to SSE clients |
|
|
255
|
+
| `bodyLimitOptions` | `object` | - | Request body size limits |
|
|
256
|
+
| `bodyLimitOptions.maxSize` | `number` | `10MB` | Max JSON body size |
|
|
257
|
+
| `bodyLimitOptions.maxFileSize` | `number` | - | Max multipart file size (no limit if unset) |
|
|
258
|
+
| `bodyLimitOptions.allowedMimeTypes` | `string[]` | - | Allowed upload MIME types |
|
|
259
|
+
| `streamOptions` | `{ redact?: boolean; heartbeatMs?: number }` | - | Streaming config |
|
|
260
|
+
| `tracingOptions` | `{ enabled?: boolean; serviceName?: string }` | - | OpenTelemetry tracing |
|
|
261
|
+
| `customRouteAuthConfig` | `Map<string, boolean>` | - | Per-route auth overrides |
|
|
262
|
+
| `mcpOptions` | `object` | - | MCP transport options |
|
|
263
|
+
| `mcpOptions.serverless` | `boolean` | `false` | Stateless MCP HTTP mode |
|
|
264
|
+
| `mcpOptions.sessionIdGenerator` | `() => string` | - | Custom MCP session IDs |
|
|
265
|
+
| `auth` | `{ enabled?: boolean; allowQueryApiKey?: boolean }` | `{ enabled: false }` | Enable Mastra's built-in token auth |
|
|
266
|
+
|
|
267
|
+
## Requirements
|
|
268
|
+
|
|
269
|
+
- Node.js >= 22.13.0
|
|
270
|
+
- NestJS with Express adapter (`@nestjs/platform-express`)
|
|
271
|
+
- Express 4.x or 5.x
|
|
272
|
+
|
|
273
|
+
**Note:** This adapter supports NestJS with Express only. Fastify is not supported in v1, and `MastraModule` throws during bootstrap if another Nest HTTP adapter is in use.
|
|
274
|
+
|
|
275
|
+
## API Reference
|
|
276
|
+
|
|
277
|
+
### `MastraModule.register(options)`
|
|
278
|
+
|
|
279
|
+
Register Mastra with NestJS DI.
|
|
280
|
+
|
|
281
|
+
### `MastraModule.registerAsync(options)`
|
|
282
|
+
|
|
283
|
+
Async registration supporting `useFactory`, `useClass`, and `useExisting`.
|
|
284
|
+
|
|
285
|
+
### `MastraService`
|
|
286
|
+
|
|
287
|
+
```typescript
|
|
288
|
+
class MastraService {
|
|
289
|
+
getMastra(): Mastra;
|
|
290
|
+
getOptions(): MastraModuleOptions;
|
|
291
|
+
getAgent(agentId: string): Agent;
|
|
292
|
+
getWorkflow(workflowId: string): Workflow;
|
|
293
|
+
isShuttingDown: boolean;
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### `MASTRA`
|
|
298
|
+
|
|
299
|
+
Injection token for the Mastra instance.
|
|
300
|
+
|
|
301
|
+
## Exported Components
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
import {
|
|
305
|
+
MastraAuthGuard,
|
|
306
|
+
MastraThrottleGuard,
|
|
307
|
+
StreamingInterceptor,
|
|
308
|
+
RequestTrackingInterceptor,
|
|
309
|
+
MastraExceptionFilter,
|
|
310
|
+
RouteHandlerService,
|
|
311
|
+
RequestContextService,
|
|
312
|
+
ShutdownService,
|
|
313
|
+
} from '@mastra/nestjs';
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
## Related Packages
|
|
317
|
+
|
|
318
|
+
- [@mastra/core](https://www.npmjs.com/package/@mastra/core)
|
|
319
|
+
- [@mastra/express](https://www.npmjs.com/package/@mastra/express)
|
|
320
|
+
- [@mastra/hono](https://www.npmjs.com/package/@mastra/hono)
|
|
321
|
+
|
|
322
|
+
## License
|
|
323
|
+
|
|
324
|
+
Apache-2.0
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Readable } from 'node:stream';
|
|
2
|
+
import type { Application } from 'express';
|
|
3
|
+
export type MockHttpRequest = {
|
|
4
|
+
method: string;
|
|
5
|
+
path: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
body?: unknown;
|
|
8
|
+
rawBody?: string | Buffer;
|
|
9
|
+
};
|
|
10
|
+
export type MockHttpResponse = {
|
|
11
|
+
status: number;
|
|
12
|
+
headers: Record<string, string>;
|
|
13
|
+
body: unknown;
|
|
14
|
+
stream?: Readable;
|
|
15
|
+
};
|
|
16
|
+
export declare function executeExpressRequest(app: Application, req: MockHttpRequest): Promise<MockHttpResponse>;
|
|
17
|
+
//# sourceMappingURL=test-helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"test-helpers.d.ts","sourceRoot":"","sources":["../../src/__tests__/test-helpers.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAE5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAC;AAE3C,MAAM,MAAM,eAAe,GAAG;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC3B,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,QAAQ,CAAC;CACnB,CAAC;AAWF,wBAAsB,qBAAqB,CAAC,GAAG,EAAE,WAAW,EAAE,GAAG,EAAE,eAAe,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAqI7G"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Injection token for the Mastra instance.
|
|
3
|
+
* Use with @Inject(MASTRA) to inject the Mastra instance.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* @Injectable()
|
|
8
|
+
* export class MyService {
|
|
9
|
+
* constructor(@Inject(MASTRA) private readonly mastra: Mastra) {}
|
|
10
|
+
* }
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare const MASTRA: unique symbol;
|
|
14
|
+
/**
|
|
15
|
+
* Injection token for the Mastra module options.
|
|
16
|
+
* Used internally by MastraService.
|
|
17
|
+
*/
|
|
18
|
+
export declare const MASTRA_OPTIONS: unique symbol;
|
|
19
|
+
/**
|
|
20
|
+
* Metadata key for @Public() decorator.
|
|
21
|
+
* Routes marked as public skip authentication.
|
|
22
|
+
*/
|
|
23
|
+
export declare const IS_PUBLIC_KEY = "isPublic";
|
|
24
|
+
/**
|
|
25
|
+
* Metadata key for @MastraThrottle() decorator.
|
|
26
|
+
* Custom rate limits per route.
|
|
27
|
+
*/
|
|
28
|
+
export declare const THROTTLE_KEY = "mastraThrottle";
|
|
29
|
+
/**
|
|
30
|
+
* Metadata key for route information.
|
|
31
|
+
* Stores the Mastra route definition for the handler.
|
|
32
|
+
*/
|
|
33
|
+
export declare const MASTRA_ROUTE_KEY = "mastraRoute";
|
|
34
|
+
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,MAAM,eAAmB,CAAC;AAEvC;;;GAGG;AACH,eAAO,MAAM,cAAc,eAA2B,CAAC;AAEvD;;;GAGG;AACH,eAAO,MAAM,aAAa,aAAa,CAAC;AAExC;;;GAGG;AACH,eAAO,MAAM,YAAY,mBAAmB,CAAC;AAE7C;;;GAGG;AACH,eAAO,MAAM,gBAAgB,gBAAgB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/controllers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { Request, Response } from 'express';
|
|
2
|
+
import type { MastraModuleOptions } from '../mastra.module.js';
|
|
3
|
+
import { RequestContextService } from '../services/request-context.service.js';
|
|
4
|
+
import { RouteHandlerService } from '../services/route-handler.service.js';
|
|
5
|
+
/**
|
|
6
|
+
* Main Mastra controller that handles all routes dynamically.
|
|
7
|
+
* Routes are matched against SERVER_ROUTES from @mastra/server.
|
|
8
|
+
*
|
|
9
|
+
* Auth and rate limiting are handled via MastraRouteGuard so they only apply
|
|
10
|
+
* to matched Mastra routes and do not affect the rest of the user's app.
|
|
11
|
+
*/
|
|
12
|
+
export declare class MastraController {
|
|
13
|
+
private readonly options;
|
|
14
|
+
private readonly routeHandler;
|
|
15
|
+
private readonly requestContext;
|
|
16
|
+
constructor(options: MastraModuleOptions, routeHandler: RouteHandlerService, requestContext: RequestContextService);
|
|
17
|
+
/**
|
|
18
|
+
* Catch-all handler that matches incoming requests to Mastra routes.
|
|
19
|
+
*/
|
|
20
|
+
handleRequest(req: Request, _res: Response): Promise<unknown>;
|
|
21
|
+
/**
|
|
22
|
+
* Parse and normalize query parameters.
|
|
23
|
+
* Handles type coercion for numbers, booleans, arrays, and JSON strings.
|
|
24
|
+
*/
|
|
25
|
+
private parseQueryParams;
|
|
26
|
+
/**
|
|
27
|
+
* Coerce a query parameter value to its appropriate type.
|
|
28
|
+
*
|
|
29
|
+
* Type coercion rules:
|
|
30
|
+
* - "true"/"false" → boolean
|
|
31
|
+
* - "null" → null
|
|
32
|
+
* - Numeric strings → number (except IDs with leading zeros like "007")
|
|
33
|
+
* - JSON objects/arrays → parsed object/array
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* // Numbers
|
|
37
|
+
* coerceQueryValue("42") // → 42
|
|
38
|
+
* coerceQueryValue("3.14") // → 3.14
|
|
39
|
+
* coerceQueryValue("007") // → "007" (preserved as string)
|
|
40
|
+
*
|
|
41
|
+
* // Booleans
|
|
42
|
+
* coerceQueryValue("true") // → true
|
|
43
|
+
* coerceQueryValue("false") // → false
|
|
44
|
+
*
|
|
45
|
+
* // JSON
|
|
46
|
+
* coerceQueryValue('{"a":1}') // → { a: 1 }
|
|
47
|
+
*/
|
|
48
|
+
private coerceQueryValue;
|
|
49
|
+
/**
|
|
50
|
+
* Parse request body, handling multipart/form-data and JSON.
|
|
51
|
+
*/
|
|
52
|
+
private parseBody;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=mastra.controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mastra.controller.d.ts","sourceRoot":"","sources":["../../src/controllers/mastra.controller.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAQjD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAC;AAC5E,OAAO,EAAE,mBAAmB,EAAE,MAAM,mCAAmC,CAAC;AAIxE;;;;;;GAMG;AACH,qBAIa,gBAAgB;IAED,OAAO,CAAC,QAAQ,CAAC,OAAO;IACnB,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC3B,OAAO,CAAC,QAAQ,CAAC,cAAc;gBAFrB,OAAO,EAAE,mBAAmB,EACvB,YAAY,EAAE,mBAAmB,EAC/B,cAAc,EAAE,qBAAqB;IAGvF;;OAEG;IAEG,aAAa,CAAQ,GAAG,EAAE,OAAO,EAA8B,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC;IA+BtG;;;OAGG;IACH,OAAO,CAAC,gBAAgB;IAoBxB;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,OAAO,CAAC,gBAAgB;IAoCxB;;OAEG;YACW,SAAS;CAsBxB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import type { Mastra } from '@mastra/core/mastra';
|
|
2
|
+
import type { Response } from 'express';
|
|
3
|
+
import type { MastraModuleOptions } from '../mastra.module.js';
|
|
4
|
+
import { ShutdownService } from '../services/shutdown.service.js';
|
|
5
|
+
/**
|
|
6
|
+
* System controller for health checks and diagnostics.
|
|
7
|
+
*
|
|
8
|
+
* Routes are at the root level (not under the Mastra prefix) for compatibility
|
|
9
|
+
* with Kubernetes, Docker, and container orchestration systems that typically
|
|
10
|
+
* expect /health at root level.
|
|
11
|
+
*
|
|
12
|
+
* These routes are public (@Public) and skip rate limiting (@SkipThrottle).
|
|
13
|
+
*/
|
|
14
|
+
export declare class SystemController {
|
|
15
|
+
private readonly mastra;
|
|
16
|
+
private readonly options;
|
|
17
|
+
private readonly shutdownService;
|
|
18
|
+
constructor(mastra: Mastra, options: MastraModuleOptions, shutdownService: ShutdownService);
|
|
19
|
+
/**
|
|
20
|
+
* Health check endpoint.
|
|
21
|
+
* Returns 200 if healthy, 503 if shutting down.
|
|
22
|
+
*/
|
|
23
|
+
health(res: Response): {
|
|
24
|
+
status: 'ok' | 'shutting_down';
|
|
25
|
+
timestamp: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Readiness check endpoint.
|
|
29
|
+
* Returns 200 if ready to accept traffic, 503 if not.
|
|
30
|
+
*/
|
|
31
|
+
ready(res: Response): {
|
|
32
|
+
ready: boolean;
|
|
33
|
+
activeRequests: number;
|
|
34
|
+
timestamp: string;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Version and info endpoint.
|
|
38
|
+
*/
|
|
39
|
+
info(): {
|
|
40
|
+
version: string;
|
|
41
|
+
prefix: string;
|
|
42
|
+
timestamp: string;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=system.controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"system.controller.d.ts","sourceRoot":"","sources":["../../src/controllers/system.controller.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAKxC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,kBAAkB,CAAC;AAC5D,OAAO,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAgB/D;;;;;;;;GAQG;AACH,qBACa,gBAAgB;IAET,OAAO,CAAC,QAAQ,CAAC,MAAM;IACf,OAAO,CAAC,QAAQ,CAAC,OAAO;IACvB,OAAO,CAAC,QAAQ,CAAC,eAAe;gBAFxB,MAAM,EAAE,MAAM,EACN,OAAO,EAAE,mBAAmB,EAC3B,eAAe,EAAE,eAAe;IAG5E;;;OAGG;IAIH,MAAM,CAA6B,GAAG,EAAE,QAAQ,GAAG;QAAE,MAAM,EAAE,IAAI,GAAG,eAAe,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE;IAYxG;;;OAGG;IAIH,KAAK,CAA6B,GAAG,EAAE,QAAQ,GAAG;QAAE,KAAK,EAAE,OAAO,CAAC;QAAC,cAAc,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE;IAa/G;;OAEG;IAIH,IAAI,IAAI;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE;CAO/D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/decorators/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,KAAK,eAAe,EAAE,MAAM,sBAAsB,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorator to mark a route as public (no authentication required).
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```typescript
|
|
6
|
+
* @Controller('health')
|
|
7
|
+
* export class HealthController {
|
|
8
|
+
* @Get()
|
|
9
|
+
* @Public()
|
|
10
|
+
* check() {
|
|
11
|
+
* return { status: 'ok' };
|
|
12
|
+
* }
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare const Public: () => import("@nestjs/common").CustomDecorator<string>;
|
|
17
|
+
//# sourceMappingURL=public.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"public.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/public.decorator.ts"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,MAAM,wDAAyC,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface ThrottleOptions {
|
|
2
|
+
/** Maximum number of requests in the window */
|
|
3
|
+
limit: number;
|
|
4
|
+
/** Time window in milliseconds */
|
|
5
|
+
windowMs: number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Decorator to set custom rate limits for a route.
|
|
9
|
+
* Overrides the default rate limits from MastraModuleOptions.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```typescript
|
|
13
|
+
* @Post('generate')
|
|
14
|
+
* @MastraThrottle({ limit: 5, windowMs: 60000 }) // 5 requests per minute
|
|
15
|
+
* async generate(@Body() body: GenerateDto) {
|
|
16
|
+
* // ...
|
|
17
|
+
* }
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare const MastraThrottle: (options: ThrottleOptions) => import("@nestjs/common").CustomDecorator<string>;
|
|
21
|
+
/**
|
|
22
|
+
* Decorator to skip rate limiting for a route.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* @Get('health')
|
|
27
|
+
* @SkipThrottle()
|
|
28
|
+
* check() {
|
|
29
|
+
* return { status: 'ok' };
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const SkipThrottle: () => import("@nestjs/common").CustomDecorator<string>;
|
|
34
|
+
//# sourceMappingURL=throttle.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.decorator.d.ts","sourceRoot":"","sources":["../../src/decorators/throttle.decorator.ts"],"names":[],"mappings":"AAIA,MAAM,WAAW,eAAe;IAC9B,+CAA+C;IAC/C,KAAK,EAAE,MAAM,CAAC;IACd,kCAAkC;IAClC,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,GAAI,SAAS,eAAe,qDAAuC,CAAC;AAE/F;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,YAAY,wDAAkD,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/filters/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ArgumentsHost, ExceptionFilter } from '@nestjs/common';
|
|
2
|
+
/**
|
|
3
|
+
* Global exception filter that normalizes all errors to a consistent format.
|
|
4
|
+
*
|
|
5
|
+
* Response format:
|
|
6
|
+
* ```json
|
|
7
|
+
* {
|
|
8
|
+
* "error": "Error message",
|
|
9
|
+
* "code": "ERROR_CODE",
|
|
10
|
+
* "issues": [{ "field": "fieldName", "message": "..." }],
|
|
11
|
+
* "requestId": "...",
|
|
12
|
+
* "timestamp": "..."
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare class MastraExceptionFilter implements ExceptionFilter {
|
|
17
|
+
private readonly logger;
|
|
18
|
+
catch(exception: unknown, host: ArgumentsHost): void;
|
|
19
|
+
/**
|
|
20
|
+
* Normalize any error type to a consistent format.
|
|
21
|
+
*/
|
|
22
|
+
private normalizeError;
|
|
23
|
+
/**
|
|
24
|
+
* Get error code from HTTP status.
|
|
25
|
+
*/
|
|
26
|
+
private getErrorCode;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=mastra-exception.filter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mastra-exception.filter.d.ts","sourceRoot":"","sources":["../../src/filters/mastra-exception.filter.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAyBrE;;;;;;;;;;;;;GAaG;AACH,qBACa,qBAAsB,YAAW,eAAe;IAC3D,OAAO,CAAC,QAAQ,CAAC,MAAM,CAA0C;IAEjE,KAAK,CAAC,SAAS,EAAE,OAAO,EAAE,IAAI,EAAE,aAAa,GAAG,IAAI;IAkCpD;;OAEG;IACH,OAAO,CAAC,cAAc;IAgHtB;;OAEG;IACH,OAAO,CAAC,YAAY;CAkCrB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/guards/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,mBAAmB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { CanActivate, ExecutionContext } from '@nestjs/common';
|
|
2
|
+
import { Reflector } from '@nestjs/core';
|
|
3
|
+
import { AuthService } from '../services/auth.service.js';
|
|
4
|
+
/**
|
|
5
|
+
* Guard that bridges to Mastra's authentication system.
|
|
6
|
+
* Checks for @Public() decorator to skip auth.
|
|
7
|
+
*
|
|
8
|
+
* **IMPORTANT: This guard is for custom user routes, NOT for Mastra API routes.**
|
|
9
|
+
*
|
|
10
|
+
* Mastra routes (agents, workflows, etc.) use AuthService internally which has
|
|
11
|
+
* access to route-specific auth configuration (customRouteAuthConfig, isProtectedPath,
|
|
12
|
+
* canAccessPublicly). This guard is a simplified version for when you want to
|
|
13
|
+
* protect your own NestJS routes with Mastra's auth config.
|
|
14
|
+
*
|
|
15
|
+
* Differences from AuthService (used internally for Mastra routes):
|
|
16
|
+
* - Does NOT check `customRouteAuthConfig`
|
|
17
|
+
* - Does NOT check `isProtectedPath` / `canAccessPublicly` from @mastra/server/auth
|
|
18
|
+
* - Has simpler dev playground detection (just checks header + config)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* // Protect a custom route with Mastra auth
|
|
23
|
+
* @Controller('my-custom')
|
|
24
|
+
* @UseGuards(MastraAuthGuard)
|
|
25
|
+
* export class MyController {
|
|
26
|
+
* @Get('protected')
|
|
27
|
+
* protectedRoute() { ... }
|
|
28
|
+
*
|
|
29
|
+
* @Get('open')
|
|
30
|
+
* @Public() // Skip auth for this route
|
|
31
|
+
* openRoute() { ... }
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare class MastraAuthGuard implements CanActivate {
|
|
36
|
+
private readonly reflector;
|
|
37
|
+
private readonly authService;
|
|
38
|
+
private readonly logger;
|
|
39
|
+
constructor(reflector: Reflector, authService: AuthService);
|
|
40
|
+
canActivate(context: ExecutionContext): Promise<boolean>;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=mastra-auth.guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mastra-auth.guard.d.ts","sourceRoot":"","sources":["../../src/guards/mastra-auth.guard.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAIzC,OAAO,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAEvD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,qBACa,eAAgB,YAAW,WAAW;IAI5B,OAAO,CAAC,QAAQ,CAAC,SAAS;IACxB,OAAO,CAAC,QAAQ,CAAC,WAAW;IAJnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;gBAGrB,SAAS,EAAE,SAAS,EAClB,WAAW,EAAE,WAAW;IAG1D,WAAW,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;CAoB/D"}
|