@guillem_puche/mastra-effect 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/LICENSE +201 -0
- package/NOTICE +25 -0
- package/README.md +219 -0
- package/dist/index.cjs +1003 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +222 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.ts +222 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +996 -0
- package/dist/index.js.map +1 -0
- package/package.json +100 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Guillem Puche
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
@guillem_puche/mastra-effect
|
|
2
|
+
Copyright 2026 Guillem Puche
|
|
3
|
+
|
|
4
|
+
This product includes software developed as part of the Mastra project
|
|
5
|
+
(https://github.com/mastra-ai/mastra), Copyright Mastra AI, Inc., licensed
|
|
6
|
+
under the Apache License, Version 2.0.
|
|
7
|
+
|
|
8
|
+
Portions of src/index.ts are derived from the in-tree Mastra server adapters,
|
|
9
|
+
principally @mastra/elysia (server-adapters/elysia). The behaviour carried over
|
|
10
|
+
includes the stream framing for both SSE and record-separated formats, the
|
|
11
|
+
error-swallowing forwarding stream, the stripping of Transfer-Encoding from a
|
|
12
|
+
forwarded response, the structure of the responseType dispatch, and the decision
|
|
13
|
+
not to await the MCP transport start call. Further portions follow @mastra/hono
|
|
14
|
+
(server-adapters/hono): propagating a client disconnect to an MCP transport
|
|
15
|
+
(from its mcp-disconnect.ts), carrying the caller's auth into MCP transports,
|
|
16
|
+
answering a failed MCP transport start with a server error, closing a stream
|
|
17
|
+
that fails partway, reading every value of a repeated query key, reading the
|
|
18
|
+
requestContext query parameter on POST, and passing an error's failingItems
|
|
19
|
+
through. Those portions remain under the Apache License, Version 2.0.
|
|
20
|
+
|
|
21
|
+
This package depends on Effect (https://github.com/Effect-TS/effect), which is
|
|
22
|
+
licensed under the MIT License and is a peer dependency rather than redistributed
|
|
23
|
+
here. A copy of Effect's source is vendored under docs/repos/effect as a git
|
|
24
|
+
subtree for reference only; it is excluded from the published package and remains
|
|
25
|
+
under its own license.
|
package/README.md
ADDED
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
# Mastra on an Effect HTTP server
|
|
2
|
+
|
|
3
|
+
`@guillem_puche/mastra-effect` runs [Mastra](https://mastra.ai) — AI agents, workflows, memory, tools
|
|
4
|
+
and MCP — inside an [Effect](https://effect.website) v4 HTTP server, instead of Hono, Express,
|
|
5
|
+
Fastify, Koa, NestJS or Elysia.
|
|
6
|
+
|
|
7
|
+
It registers Mastra's whole server route table onto an Effect `HttpRouter`, so Mastra's routes sit
|
|
8
|
+
alongside your own on one port, one router and one middleware stack. A community adapter, maintained
|
|
9
|
+
out of tree, verified against Mastra's own published conformance suites.
|
|
10
|
+
|
|
11
|
+
## Why
|
|
12
|
+
|
|
13
|
+
Mastra ships adapters for the popular Node frameworks, but not for Effect. Without one, an Effect
|
|
14
|
+
application that wants agents has to run Mastra as a second process behind its own server, and then
|
|
15
|
+
reconcile two routers, two sets of middleware and two ideas of what a request is.
|
|
16
|
+
|
|
17
|
+
This removes that. Mastra's routes join your Effect server: one port, one router, one middleware
|
|
18
|
+
stack. Because the adapter binds to a live `HttpRouter` **service** rather than a Layer, you hand it
|
|
19
|
+
the router you already own — Mastra does not take the server over. A client that disconnects cancels
|
|
20
|
+
the Mastra work it started, so an abandoned agent stops calling its model, and with a tracer
|
|
21
|
+
installed every request gets a server span carrying its route pattern.
|
|
22
|
+
|
|
23
|
+
Mastra itself runs its agents, tools and workflow steps as ordinary promises, outside the request's
|
|
24
|
+
fiber. Three things cross back into Effect:
|
|
25
|
+
|
|
26
|
+
- **Server errors.** A route that answers with a 5xx also fails the Effect request, as a
|
|
27
|
+
`MastraRouteError` that answers with Mastra's own response. Clients see no difference; your
|
|
28
|
+
middleware can `catchTag('MastraRouteError')`, and your logs and traces record the failure. A 4xx
|
|
29
|
+
stays a normal response. `new MastraServer({ errorChannel: false })` turns this off.
|
|
30
|
+
|
|
31
|
+
Like any failed Effect request, these skip middleware that changes the response with `Effect.map`.
|
|
32
|
+
To touch every answer, use a pre-response handler, as Effect's CORS middleware does —
|
|
33
|
+
[`alongside-your-routes`](examples/alongside-your-routes) shows one.
|
|
34
|
+
- **Your services and the request's span, inside tools and steps.** `runInRequest` runs Effect code
|
|
35
|
+
from a tool or step through your own runtime, with the HTTP request's span as its parent, and
|
|
36
|
+
interrupts it when Mastra's abort signal fires:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const runtime = ManagedRuntime.make(AppServicesLive);
|
|
40
|
+
|
|
41
|
+
const findUser = createTool({
|
|
42
|
+
id: 'find-user',
|
|
43
|
+
inputSchema: z.object({ id: z.string() }),
|
|
44
|
+
execute: (input, context) => runInRequest(runtime, Users.find(input.id), context),
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
For a tool an agent calls, that signal fires when the client leaves. A workflow step's belongs
|
|
49
|
+
to the run and fires only when the run is cancelled. The spans that code creates are made by the
|
|
50
|
+
runtime's own tracer, so give the runtime your tracer as well, or they are never exported. With
|
|
51
|
+
OpenTelemetry, `runtimeTracing` in [`examples/full-app`](examples/full-app/src/observability.ts)
|
|
52
|
+
does this.
|
|
53
|
+
|
|
54
|
+
- **Mastra's own spans.** With Mastra's OpenTelemetry bridge, agent runs, model calls with token
|
|
55
|
+
counts and tool calls land in the same trace as the HTTP request, under its span.
|
|
56
|
+
[`examples/full-app`](examples/full-app) sets this up. It needs the tracer provider registered
|
|
57
|
+
globally, which Effect's `NodeSdk.layer` does not do by itself
|
|
58
|
+
([mastra-ai/mastra#24950](https://github.com/mastra-ai/mastra/issues/24950)).
|
|
59
|
+
|
|
60
|
+
## Install
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
pnpm add @guillem_puche/mastra-effect effect@rc @mastra/core zod
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`effect@rc` matters: `effect@latest` is still v3, which has no `effect/unstable/http`, and the
|
|
67
|
+
install will succeed and then fail at runtime. Requires Node >= 22.13, `effect@>=4.0.0-rc.117` and
|
|
68
|
+
`@mastra/core@>=1.68.0`.
|
|
69
|
+
|
|
70
|
+
To let Effect own the server, as below, add `@effect/platform-node` at the same `rc` version.
|
|
71
|
+
|
|
72
|
+
## Quick start
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
import { createServer } from 'node:http';
|
|
76
|
+
|
|
77
|
+
import { NodeHttpServer } from '@effect/platform-node';
|
|
78
|
+
import { createMastraServer } from '@guillem_puche/mastra-effect';
|
|
79
|
+
import { Effect, Layer } from 'effect';
|
|
80
|
+
import { HttpServer } from 'effect/unstable/http';
|
|
81
|
+
|
|
82
|
+
import { mastra } from './mastra';
|
|
83
|
+
|
|
84
|
+
const { router } = await createMastraServer({ mastra });
|
|
85
|
+
|
|
86
|
+
const server = HttpServer.serve()(router.asHttpEffect()).pipe(
|
|
87
|
+
Layer.provide(NodeHttpServer.layer(() => createServer(), { port: 3000 })),
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
await Effect.runPromise(Layer.launch(server));
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
Mastra now answers on `http://localhost:3000/api/*`.
|
|
94
|
+
|
|
95
|
+
To add your own routes, or to mount something else like an auth handler, create the router yourself
|
|
96
|
+
and hand the same instance to the adapter:
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
import { MastraServer, createRouter } from '@guillem_puche/mastra-effect';
|
|
100
|
+
import { Effect } from 'effect';
|
|
101
|
+
import { HttpServerResponse } from 'effect/unstable/http';
|
|
102
|
+
|
|
103
|
+
const router = createRouter();
|
|
104
|
+
|
|
105
|
+
await Effect.runPromise(router.add('GET', '/healthz', HttpServerResponse.text('ok')));
|
|
106
|
+
|
|
107
|
+
const adapter = new MastraServer({ app: router, mastra, prefix: '/api' });
|
|
108
|
+
await adapter.init();
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
If you would rather embed Mastra in an existing fetch-based setup than run an Effect server,
|
|
112
|
+
`toWebHandler(router)` returns `{ handler, dispose }`. `handler(request)` answers a Web `Request` with
|
|
113
|
+
a `Response`. Its optional second argument is an Effect `Context`, so give a framework that passes a
|
|
114
|
+
second argument of its own — Next.js does — `request => handler(request)` instead. Effect logs every
|
|
115
|
+
request it answers; `toWebHandler(router, { disableLogger: true })` turns that off. `dispose()`
|
|
116
|
+
releases the handler, not Mastra.
|
|
117
|
+
|
|
118
|
+
## Examples
|
|
119
|
+
|
|
120
|
+
Each example is a small runnable app with its own README, and runs with no API keys, no database and
|
|
121
|
+
no containers. CI runs every example's tests, so none of them can quietly stop working.
|
|
122
|
+
|
|
123
|
+
| I want to… | Example | Open first |
|
|
124
|
+
|---|---|---|
|
|
125
|
+
| serve Mastra on Effect, and nothing else | [`minimal`](examples/minimal) | [`server.ts`](examples/minimal/src/server.ts) |
|
|
126
|
+
| add Mastra next to my own Effect routes | [`alongside-your-routes`](examples/alongside-your-routes) | [`server.ts`](examples/alongside-your-routes/src/server.ts) |
|
|
127
|
+
| keep Mastra behind one route, with middleware for Mastra only | [`mounted-as-one-route`](examples/mounted-as-one-route) | [`server.ts`](examples/mounted-as-one-route/src/server.ts) |
|
|
128
|
+
| use Mastra from Bun, Deno, Workers or Next.js, with no Effect server | [`fetch-handler`](examples/fetch-handler) | [`handler.ts`](examples/fetch-handler/src/handler.ts) |
|
|
129
|
+
| share one sign-in (Better Auth) between my app and Mastra | [`full-app`](examples/full-app) | [`auth.ts`](examples/full-app/src/auth.ts) |
|
|
130
|
+
| serve Mastra's agents and tools over MCP | [`full-app`](examples/full-app) | [`mastra.ts`](examples/full-app/src/mastra.ts) |
|
|
131
|
+
| export traces and logs with OpenTelemetry, locally or to a vendor | [`full-app`](examples/full-app) | [`observability.ts`](examples/full-app/src/observability.ts) |
|
|
132
|
+
| show interactive API docs (Scalar) for Mastra's routes | [`full-app`](examples/full-app) | [`scalar.ts`](examples/full-app/src/scalar.ts) |
|
|
133
|
+
|
|
134
|
+
```bash
|
|
135
|
+
cd examples/minimal # or any other
|
|
136
|
+
pnpm test
|
|
137
|
+
pnpm dev # http://localhost:3000 — every example except fetch-handler, which has no server
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### One router, or Mastra as one route?
|
|
141
|
+
|
|
142
|
+
By default Mastra does not bring a router of its own: it adds each of its ~400 routes to *your*
|
|
143
|
+
router, next to your routes, so one middleware stack covers everything. That is what Mastra's
|
|
144
|
+
adapters for Hono, Express, Fastify and Koa do, and it is what
|
|
145
|
+
[`alongside-your-routes`](examples/alongside-your-routes) shows.
|
|
146
|
+
|
|
147
|
+
Effect can also keep Mastra behind a single route — its own router, forwarded from `/api/*` — so
|
|
148
|
+
middleware and Mastra's request log cover Mastra alone. The trade-off: Mastra's custom API routes
|
|
149
|
+
live outside `/api` and each needs a forward of its own. [`mounted-as-one-route`](examples/mounted-as-one-route)
|
|
150
|
+
shows it and compares the two.
|
|
151
|
+
|
|
152
|
+
## API
|
|
153
|
+
|
|
154
|
+
| Export | Purpose |
|
|
155
|
+
|---|---|
|
|
156
|
+
| `createMastraServer(options)` | Builds a router, constructs the adapter, runs `init()`. Returns `{ router, adapter }` |
|
|
157
|
+
| `createRouter(config?)` | An empty `HttpRouter` to hand to `new MastraServer({ app })`. Takes a partial `FindMyWay.RouterConfig` |
|
|
158
|
+
| `MastraServer` | The adapter itself, for when you own the router and the lifecycle |
|
|
159
|
+
| `toWebHandler(router, options?)` | `{ handler, dispose }` — a fetch handler over a populated router. `options.disableLogger` stops Effect logging each request |
|
|
160
|
+
| `runInRequest(runtime, effect, context)` | Runs Effect code from a Mastra tool or workflow step: your runtime's services, the request's span as parent, Mastra's abort signal |
|
|
161
|
+
| `requestSpan(requestContext)` | The HTTP request's Effect span, from a tool's or step's request context; `undefined` without a tracer |
|
|
162
|
+
| `MastraRouteError` | What a route's server error (5xx) becomes in Effect's error channel. Answers with Mastra's own response |
|
|
163
|
+
| `EffectRouter`, `EffectRequestContext`, `MastraServerOptions`, `CreateMastraServerOptions`, `ToolsInput` | Types |
|
|
164
|
+
|
|
165
|
+
## Status
|
|
166
|
+
|
|
167
|
+
Alpha. The route surface is verified against Mastra's published conformance suites
|
|
168
|
+
([`@mastra/server-adapters-test-suite`](https://www.npmjs.com/package/@mastra/server-adapters-test-suite)),
|
|
169
|
+
which iterate the live `SERVER_ROUTES` table rather than a hand-written list — so the check tracks
|
|
170
|
+
Mastra's routes as they move rather than a snapshot of them.
|
|
171
|
+
|
|
172
|
+
| Suite | Status |
|
|
173
|
+
|---|---|
|
|
174
|
+
| `createRouteAdapterTestSuite` | passing |
|
|
175
|
+
| `createMCPRouteTestSuite` | passing |
|
|
176
|
+
| `createBodyLimitTestSuite` | passing |
|
|
177
|
+
| `createHttpLoggingTestSuite` | passing |
|
|
178
|
+
| `createMultipartTestSuite` | passing |
|
|
179
|
+
| `createMCPTransportTestSuite` | passing |
|
|
180
|
+
|
|
181
|
+
CI runs them on Node 22.13 and 24 against the newest Mastra, and again against the oldest Mastra
|
|
182
|
+
the package declares. MCP is tested with servers from both `@mastra/mcp` 1.x and 2.x. The adapter's own tests add what the suites
|
|
183
|
+
do not reach: a disconnecting client cancelling Mastra's work, streamed answers, request bodies
|
|
184
|
+
already read by middleware, cookies and session refresh. The examples exercise auth, OpenAPI, the
|
|
185
|
+
docs page, OpenTelemetry export, both ways of attaching Mastra to a router and the fetch handler end
|
|
186
|
+
to end.
|
|
187
|
+
|
|
188
|
+
### Differences from the official adapters
|
|
189
|
+
|
|
190
|
+
- **`server.middleware` and `mastra.setServerMiddleware()` are not run.** They take Hono middleware
|
|
191
|
+
(`(c, next) => …`), which only `@mastra/hono` can execute; Mastra logs a warning and skips them, as
|
|
192
|
+
it does under Express, Fastify and Koa. Add Effect middleware to the router instead
|
|
193
|
+
(`router.addGlobalMiddleware`).
|
|
194
|
+
- **Cookies pass through Effect's cookie model.** Attributes it does not have — anything beyond
|
|
195
|
+
Domain, Path, Expires, Max-Age, HttpOnly, Secure, SameSite, Priority and Partitioned — are dropped,
|
|
196
|
+
and a cookie whose value cannot be sent is dropped with a warning instead of failing the response.
|
|
197
|
+
- **A trailing slash still matches** (`/api/agents/`), as under `@mastra/elysia`; `@mastra/hono`
|
|
198
|
+
answers 404.
|
|
199
|
+
|
|
200
|
+
## Effect version support
|
|
201
|
+
|
|
202
|
+
Tracks the `rc` line, not the frozen `beta` line. Every API this adapter depends on is asserted at
|
|
203
|
+
compile time in `src/effect-api-gate.ts`, so an incompatible Effect upgrade fails `tsc` instead of
|
|
204
|
+
failing subtly at runtime.
|
|
205
|
+
|
|
206
|
+
Two behaviours differ from what you might expect coming from the Effect beta or from stock
|
|
207
|
+
`find-my-way`:
|
|
208
|
+
|
|
209
|
+
- Effect vendors its own FindMyWay, which defaults `ignoreDuplicateSlashes` to **true**. This adapter
|
|
210
|
+
forces it off, so `/api//agents` 404s instead of being served as `/api/agents`.
|
|
211
|
+
- Routes differing only in their path-parameter name at the same segment — Mastra has four such
|
|
212
|
+
pairs — register and dispatch correctly, binding the right name to each. The positional rewrite
|
|
213
|
+
that `@mastra/elysia` needs is unnecessary here.
|
|
214
|
+
|
|
215
|
+
## License
|
|
216
|
+
|
|
217
|
+
Apache-2.0 — matching Mastra, from whose in-tree adapters parts of this one are derived. See
|
|
218
|
+
[NOTICE](NOTICE) for what was carried over and from where. Effect itself is MIT and is a peer
|
|
219
|
+
dependency, not redistributed here.
|