@mastra/nestjs 0.0.0-observe-20260504181920

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.
Files changed (76) hide show
  1. package/CHANGELOG.md +95 -0
  2. package/LICENSE.md +30 -0
  3. package/README.md +324 -0
  4. package/dist/__tests__/test-helpers.d.ts +17 -0
  5. package/dist/__tests__/test-helpers.d.ts.map +1 -0
  6. package/dist/constants.d.ts +34 -0
  7. package/dist/constants.d.ts.map +1 -0
  8. package/dist/controllers/index.d.ts +3 -0
  9. package/dist/controllers/index.d.ts.map +1 -0
  10. package/dist/controllers/mastra.controller.d.ts +54 -0
  11. package/dist/controllers/mastra.controller.d.ts.map +1 -0
  12. package/dist/controllers/system.controller.d.ts +45 -0
  13. package/dist/controllers/system.controller.d.ts.map +1 -0
  14. package/dist/decorators/index.d.ts +3 -0
  15. package/dist/decorators/index.d.ts.map +1 -0
  16. package/dist/decorators/public.decorator.d.ts +17 -0
  17. package/dist/decorators/public.decorator.d.ts.map +1 -0
  18. package/dist/decorators/throttle.decorator.d.ts +34 -0
  19. package/dist/decorators/throttle.decorator.d.ts.map +1 -0
  20. package/dist/filters/index.d.ts +2 -0
  21. package/dist/filters/index.d.ts.map +1 -0
  22. package/dist/filters/mastra-exception.filter.d.ts +28 -0
  23. package/dist/filters/mastra-exception.filter.d.ts.map +1 -0
  24. package/dist/guards/index.d.ts +3 -0
  25. package/dist/guards/index.d.ts.map +1 -0
  26. package/dist/guards/mastra-auth.guard.d.ts +42 -0
  27. package/dist/guards/mastra-auth.guard.d.ts.map +1 -0
  28. package/dist/guards/mastra-route.guard.d.ts +23 -0
  29. package/dist/guards/mastra-route.guard.d.ts.map +1 -0
  30. package/dist/guards/mastra-throttle.guard.d.ts +53 -0
  31. package/dist/guards/mastra-throttle.guard.d.ts.map +1 -0
  32. package/dist/index.cjs +14298 -0
  33. package/dist/index.cjs.map +1 -0
  34. package/dist/index.d.ts +23 -0
  35. package/dist/index.d.ts.map +1 -0
  36. package/dist/index.js +14284 -0
  37. package/dist/index.js.map +1 -0
  38. package/dist/interceptors/index.d.ts +3 -0
  39. package/dist/interceptors/index.d.ts.map +1 -0
  40. package/dist/interceptors/request-tracking.interceptor.d.ts +13 -0
  41. package/dist/interceptors/request-tracking.interceptor.d.ts.map +1 -0
  42. package/dist/interceptors/streaming.interceptor.d.ts +40 -0
  43. package/dist/interceptors/streaming.interceptor.d.ts.map +1 -0
  44. package/dist/interceptors/tracing.interceptor.d.ts +12 -0
  45. package/dist/interceptors/tracing.interceptor.d.ts.map +1 -0
  46. package/dist/mastra-server.adapter.d.ts +10 -0
  47. package/dist/mastra-server.adapter.d.ts.map +1 -0
  48. package/dist/mastra.module.d.ts +173 -0
  49. package/dist/mastra.module.d.ts.map +1 -0
  50. package/dist/mastra.service.d.ts +51 -0
  51. package/dist/mastra.service.d.ts.map +1 -0
  52. package/dist/middleware/body-limit.middleware.d.ts +24 -0
  53. package/dist/middleware/body-limit.middleware.d.ts.map +1 -0
  54. package/dist/middleware/index.d.ts +3 -0
  55. package/dist/middleware/index.d.ts.map +1 -0
  56. package/dist/middleware/json-body.middleware.d.ts +17 -0
  57. package/dist/middleware/json-body.middleware.d.ts.map +1 -0
  58. package/dist/services/auth.service.d.ts +34 -0
  59. package/dist/services/auth.service.d.ts.map +1 -0
  60. package/dist/services/index.d.ts +5 -0
  61. package/dist/services/index.d.ts.map +1 -0
  62. package/dist/services/request-context.service.d.ts +52 -0
  63. package/dist/services/request-context.service.d.ts.map +1 -0
  64. package/dist/services/route-handler.service.d.ts +78 -0
  65. package/dist/services/route-handler.service.d.ts.map +1 -0
  66. package/dist/services/shutdown.service.d.ts +54 -0
  67. package/dist/services/shutdown.service.d.ts.map +1 -0
  68. package/dist/utils/constants.d.ts +3 -0
  69. package/dist/utils/constants.d.ts.map +1 -0
  70. package/dist/utils/format.d.ts +5 -0
  71. package/dist/utils/format.d.ts.map +1 -0
  72. package/dist/utils/parse-multipart.d.ts +18 -0
  73. package/dist/utils/parse-multipart.d.ts.map +1 -0
  74. package/dist/utils/route-path.d.ts +6 -0
  75. package/dist/utils/route-path.d.ts.map +1 -0
  76. package/package.json +84 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,95 @@
1
+ # @mastra/nestjs
2
+
3
+ ## 0.0.0-observe-20260504181920
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`6dcd65f`](https://github.com/mastra-ai/mastra/commit/6dcd65f2a34069e6dc43ba35f1d11119b9b40bef), [`86c0298`](https://github.com/mastra-ai/mastra/commit/86c0298e647306423c842f9d5ac827bd616bd13d), [`fb0719a`](https://github.com/mastra-ai/mastra/commit/fb0719aef8072132efbcdca740e265f5f2b98a99), [`c05c9a1`](https://github.com/mastra-ai/mastra/commit/c05c9a13230988cef6d438a62f37760f31927bc7), [`ca28c23`](https://github.com/mastra-ai/mastra/commit/ca28c232a2f18801a6cf20fe053479237b4d4fb0), [`e24aacb`](https://github.com/mastra-ai/mastra/commit/e24aacba07bd66f5d95b636dc24016fca26b52cf), [`7fce309`](https://github.com/mastra-ai/mastra/commit/7fce30912b14170bfc41f0ac736cca0f39fe0cd4), [`cd96779`](https://github.com/mastra-ai/mastra/commit/cd9677937f113b2856dc8b9f3d4bdabcee58bb2e), [`1c2dda8`](https://github.com/mastra-ai/mastra/commit/1c2dda805fbfccc0abf55d4cb20cc34402dc3f0c), [`c721164`](https://github.com/mastra-ai/mastra/commit/c7211643f7ac861f83b19a3757cc921487fc9d75), [`1b55954`](https://github.com/mastra-ai/mastra/commit/1b559541c1e08a10e49d01ffc51a634dfc37a286), [`7997c2e`](https://github.com/mastra-ai/mastra/commit/7997c2e55ddd121562a4098cd8d2b89c68433bf1), [`5adc55e`](https://github.com/mastra-ai/mastra/commit/5adc55e63407be8ee977914957d68bcc2a075ceb), [`5adc55e`](https://github.com/mastra-ai/mastra/commit/5adc55e63407be8ee977914957d68bcc2a075ceb), [`39162cb`](https://github.com/mastra-ai/mastra/commit/39162cb952c0053fdd4ed7217ec7802a2027b19d), [`e97ccb9`](https://github.com/mastra-ai/mastra/commit/e97ccb900f8b7a390ce82c9f8eb8d6eb2c5e3777), [`f5afe62`](https://github.com/mastra-ai/mastra/commit/f5afe62beff3ae69148a35e55fe5375168897829), [`c5daf48`](https://github.com/mastra-ai/mastra/commit/c5daf48556e98c46ae06caf00f92c249912007e9), [`70017d7`](https://github.com/mastra-ai/mastra/commit/70017d72ab741b5d7040e2a15c251a317782e39e), [`cd96779`](https://github.com/mastra-ai/mastra/commit/cd9677937f113b2856dc8b9f3d4bdabcee58bb2e), [`e4942bc`](https://github.com/mastra-ai/mastra/commit/e4942bc7fdc903572f7d84f26d5e15f9d39c763d), [`86c0298`](https://github.com/mastra-ai/mastra/commit/86c0298e647306423c842f9d5ac827bd616bd13d)]:
8
+ - @mastra/core@0.0.0-observe-20260504181920
9
+ - @mastra/server@0.0.0-observe-20260504181920
10
+
11
+ ## 0.1.1-alpha.3
12
+
13
+ ### Patch Changes
14
+
15
+ - Updated dependencies [[`fb0719a`](https://github.com/mastra-ai/mastra/commit/fb0719aef8072132efbcdca740e265f5f2b98a99), [`ca28c23`](https://github.com/mastra-ai/mastra/commit/ca28c232a2f18801a6cf20fe053479237b4d4fb0), [`39162cb`](https://github.com/mastra-ai/mastra/commit/39162cb952c0053fdd4ed7217ec7802a2027b19d)]:
16
+ - @mastra/server@1.32.0-alpha.3
17
+ - @mastra/core@1.32.0-alpha.3
18
+
19
+ ## 0.1.1-alpha.2
20
+
21
+ ### Patch Changes
22
+
23
+ - Updated dependencies [[`86c0298`](https://github.com/mastra-ai/mastra/commit/86c0298e647306423c842f9d5ac827bd616bd13d), [`7fce309`](https://github.com/mastra-ai/mastra/commit/7fce30912b14170bfc41f0ac736cca0f39fe0cd4), [`cd96779`](https://github.com/mastra-ai/mastra/commit/cd9677937f113b2856dc8b9f3d4bdabcee58bb2e), [`7997c2e`](https://github.com/mastra-ai/mastra/commit/7997c2e55ddd121562a4098cd8d2b89c68433bf1), [`e97ccb9`](https://github.com/mastra-ai/mastra/commit/e97ccb900f8b7a390ce82c9f8eb8d6eb2c5e3777), [`f5afe62`](https://github.com/mastra-ai/mastra/commit/f5afe62beff3ae69148a35e55fe5375168897829), [`c5daf48`](https://github.com/mastra-ai/mastra/commit/c5daf48556e98c46ae06caf00f92c249912007e9), [`cd96779`](https://github.com/mastra-ai/mastra/commit/cd9677937f113b2856dc8b9f3d4bdabcee58bb2e), [`86c0298`](https://github.com/mastra-ai/mastra/commit/86c0298e647306423c842f9d5ac827bd616bd13d)]:
24
+ - @mastra/core@1.32.0-alpha.2
25
+ - @mastra/server@1.32.0-alpha.2
26
+
27
+ ## 0.1.1-alpha.1
28
+
29
+ ### Patch Changes
30
+
31
+ - Updated dependencies [[`c05c9a1`](https://github.com/mastra-ai/mastra/commit/c05c9a13230988cef6d438a62f37760f31927bc7), [`e24aacb`](https://github.com/mastra-ai/mastra/commit/e24aacba07bd66f5d95b636dc24016fca26b52cf), [`c721164`](https://github.com/mastra-ai/mastra/commit/c7211643f7ac861f83b19a3757cc921487fc9d75), [`1b55954`](https://github.com/mastra-ai/mastra/commit/1b559541c1e08a10e49d01ffc51a634dfc37a286), [`5adc55e`](https://github.com/mastra-ai/mastra/commit/5adc55e63407be8ee977914957d68bcc2a075ceb), [`5adc55e`](https://github.com/mastra-ai/mastra/commit/5adc55e63407be8ee977914957d68bcc2a075ceb), [`70017d7`](https://github.com/mastra-ai/mastra/commit/70017d72ab741b5d7040e2a15c251a317782e39e), [`e4942bc`](https://github.com/mastra-ai/mastra/commit/e4942bc7fdc903572f7d84f26d5e15f9d39c763d)]:
32
+ - @mastra/core@1.32.0-alpha.1
33
+ - @mastra/server@1.32.0-alpha.1
34
+
35
+ ## 0.1.1-alpha.0
36
+
37
+ ### Patch Changes
38
+
39
+ - Updated dependencies [[`6dcd65f`](https://github.com/mastra-ai/mastra/commit/6dcd65f2a34069e6dc43ba35f1d11119b9b40bef), [`1c2dda8`](https://github.com/mastra-ai/mastra/commit/1c2dda805fbfccc0abf55d4cb20cc34402dc3f0c)]:
40
+ - @mastra/core@1.31.1-alpha.0
41
+ - @mastra/server@1.31.1-alpha.0
42
+
43
+ ## 0.1.0
44
+
45
+ ### Minor Changes
46
+
47
+ - Add NestJS server adapter (`@mastra/nestjs`) for running Mastra with NestJS Express applications. Provides native module registration, DI-based service injection, rate limiting, graceful shutdown, streaming, and MCP transport support. ([#12751](https://github.com/mastra-ai/mastra/pull/12751))
48
+
49
+ ```typescript
50
+ import { Module } from '@nestjs/common';
51
+ import { MastraModule } from '@mastra/nestjs';
52
+ import { mastra } from './mastra';
53
+
54
+ @Module({
55
+ imports: [MastraModule.register({ mastra })],
56
+ })
57
+ export class AppModule {}
58
+ ```
59
+
60
+ ### Patch Changes
61
+
62
+ - Updated dependencies [[`1723e09`](https://github.com/mastra-ai/mastra/commit/1723e099829892419ddbfe49287acfeac2522724), [`629f9e9`](https://github.com/mastra-ai/mastra/commit/629f9e9a7e56aa8f129515a3923c5813298790c7), [`25168fb`](https://github.com/mastra-ai/mastra/commit/25168fb9c1de9db7f8171df4f58ceb842c53aa29), [`ab34b5a`](https://github.com/mastra-ai/mastra/commit/ab34b5a2191b8e4353df1dbf7b9155e7d6628d79), [`5fb6c2a`](https://github.com/mastra-ai/mastra/commit/5fb6c2a95c1843cc231704b91354311fc1f34a71), [`2b0f355`](https://github.com/mastra-ai/mastra/commit/2b0f3553be3e9e5524da539a66e5cf82668440a4), [`f0d3c1a`](https://github.com/mastra-ai/mastra/commit/f0d3c1a9a2b283abc322d363f4f87e04e16837cf), [`394f0cf`](https://github.com/mastra-ai/mastra/commit/394f0cfc31e6b4d801219fdef2e9cc69e5bc8682), [`b2deb29`](https://github.com/mastra-ai/mastra/commit/b2deb29412b300c868655b5840463614fbb7962d), [`66644be`](https://github.com/mastra-ai/mastra/commit/66644beac1aa560f0e417956ff007c89341dc382), [`7dfea5e`](https://github.com/mastra-ai/mastra/commit/7dfea5eff7774eeeccd55ceb655392d70886206b), [`e109607`](https://github.com/mastra-ai/mastra/commit/e10960749251e34d46b480a20648c490fd30381b), [`310b953`](https://github.com/mastra-ai/mastra/commit/310b95345f302dcd5ba3ed862bdc96f059d44122), [`c600d54`](https://github.com/mastra-ai/mastra/commit/c600d5427277f44bc246b4daf70f77605ff1265c), [`3d7f709`](https://github.com/mastra-ai/mastra/commit/3d7f709b615e588050bb6283c4ee5cfe2978cbde), [`48a42f1`](https://github.com/mastra-ai/mastra/commit/48a42f114a4006a95e0b7a1b5ad1a24815a175c2), [`8091c7c`](https://github.com/mastra-ai/mastra/commit/8091c7c944d15e13fef6d61b6cfd903f158d4006), [`2c83efc`](https://github.com/mastra-ai/mastra/commit/2c83efc4482b3efe50830e3b8b4ba9a8d219edff), [`43f0e1d`](https://github.com/mastra-ai/mastra/commit/43f0e1d5d5a74ba6fc746f2ad89ebe0c64777a7d), [`da0b9e2`](https://github.com/mastra-ai/mastra/commit/da0b9e2ba7ecc560213b426d6c097fe63946086e), [`282a10c`](https://github.com/mastra-ai/mastra/commit/282a10c9446e9922afe80e10e3770481c8ac8a28), [`04151c7`](https://github.com/mastra-ai/mastra/commit/04151c7dcea934b4fe9076708a23fac161195414), [`8091c7c`](https://github.com/mastra-ai/mastra/commit/8091c7c944d15e13fef6d61b6cfd903f158d4006)]:
63
+ - @mastra/core@1.31.0
64
+ - @mastra/server@1.31.0
65
+
66
+ ## 0.1.0-alpha.1
67
+
68
+ ### Patch Changes
69
+
70
+ - Updated dependencies [[`f0d3c1a`](https://github.com/mastra-ai/mastra/commit/f0d3c1a9a2b283abc322d363f4f87e04e16837cf)]:
71
+ - @mastra/server@1.31.0-alpha.5
72
+ - @mastra/core@1.31.0-alpha.5
73
+
74
+ ## 0.1.0-alpha.0
75
+
76
+ ### Minor Changes
77
+
78
+ - Add NestJS server adapter (`@mastra/nestjs`) for running Mastra with NestJS Express applications. Provides native module registration, DI-based service injection, rate limiting, graceful shutdown, streaming, and MCP transport support. ([#12751](https://github.com/mastra-ai/mastra/pull/12751))
79
+
80
+ ```typescript
81
+ import { Module } from '@nestjs/common';
82
+ import { MastraModule } from '@mastra/nestjs';
83
+ import { mastra } from './mastra';
84
+
85
+ @Module({
86
+ imports: [MastraModule.register({ mastra })],
87
+ })
88
+ export class AppModule {}
89
+ ```
90
+
91
+ ### Patch Changes
92
+
93
+ - Updated dependencies [[`c600d54`](https://github.com/mastra-ai/mastra/commit/c600d5427277f44bc246b4daf70f77605ff1265c), [`8091c7c`](https://github.com/mastra-ai/mastra/commit/8091c7c944d15e13fef6d61b6cfd903f158d4006), [`04151c7`](https://github.com/mastra-ai/mastra/commit/04151c7dcea934b4fe9076708a23fac161195414), [`8091c7c`](https://github.com/mastra-ai/mastra/commit/8091c7c944d15e13fef6d61b6cfd903f158d4006)]:
94
+ - @mastra/server@1.31.0-alpha.4
95
+ - @mastra/core@1.31.0-alpha.4
package/LICENSE.md ADDED
@@ -0,0 +1,30 @@
1
+ Portions of this software are licensed as follows:
2
+
3
+ - All content that resides under any directory named "ee/" within this
4
+ repository, including but not limited to:
5
+ - `packages/core/src/auth/ee/`
6
+ - `packages/server/src/server/auth/ee/`
7
+ is licensed under the license defined in `ee/LICENSE`.
8
+
9
+ - All third-party components incorporated into the Mastra Software are
10
+ licensed under the original license provided by the owner of the
11
+ applicable component.
12
+
13
+ - Content outside of the above-mentioned directories or restrictions is
14
+ available under the "Apache License 2.0" as defined below.
15
+
16
+ # Apache License 2.0
17
+
18
+ Copyright (c) 2025 Kepler Software, Inc.
19
+
20
+ Licensed under the Apache License, Version 2.0 (the "License");
21
+ you may not use this file except in compliance with the License.
22
+ You may obtain a copy of the License at
23
+
24
+ http://www.apache.org/licenses/LICENSE-2.0
25
+
26
+ Unless required by applicable law or agreed to in writing, software
27
+ distributed under the License is distributed on an "AS IS" BASIS,
28
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29
+ See the License for the specific language governing permissions and
30
+ limitations under the License.
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,3 @@
1
+ export { MastraController } from './mastra.controller.js';
2
+ export { SystemController } from './system.controller.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -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,3 @@
1
+ export { Public } from './public.decorator.js';
2
+ export { MastraThrottle, SkipThrottle, type ThrottleOptions } from './throttle.decorator.js';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -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"}