@mastra/mcp-docs-server 1.0.0-beta.6 → 1.0.0-beta.7
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/.docs/organized/changelogs/%40mastra%2Fastra.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +58 -58
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +31 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +176 -176
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +15 -15
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Flance.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +32 -32
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +31 -31
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Freact.md +7 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +58 -58
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +33 -33
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
- package/.docs/organized/changelogs/create-mastra.md +7 -7
- package/.docs/organized/changelogs/mastra.md +14 -14
- package/.docs/organized/code-examples/ai-elements.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-useChat.md +1 -1
- package/.docs/organized/code-examples/ai-sdk-v5.md +1 -1
- package/.docs/organized/code-examples/assistant-ui.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs-and-eval.md +1 -1
- package/.docs/organized/code-examples/bird-checker-with-nextjs.md +1 -1
- package/.docs/organized/code-examples/crypto-chatbot.md +1 -1
- package/.docs/organized/code-examples/mcp-server-adapters.md +2 -2
- package/.docs/organized/code-examples/server-app-access.md +2 -2
- package/.docs/organized/code-examples/server-express-adapter.md +87 -0
- package/.docs/organized/code-examples/server-hono-adapter.md +85 -0
- package/.docs/raw/agents/overview.mdx +2 -111
- package/.docs/raw/agents/processors.mdx +1 -1
- package/.docs/raw/agents/structured-output.mdx +224 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +10 -2
- package/.docs/raw/memory/working-memory.mdx +1 -0
- package/.docs/raw/observability/tracing/bridges/otel.mdx +25 -1
- package/.docs/raw/observability/tracing/exporters/arize.mdx +19 -0
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +63 -0
- package/.docs/raw/observability/tracing/exporters/otel.mdx +30 -19
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +20 -0
- package/.docs/raw/observability/tracing/overview.mdx +6 -1
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +27 -0
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +25 -0
- package/.docs/raw/reference/observability/tracing/exporters/langfuse.mdx +43 -0
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +27 -43
- package/.docs/raw/reference/server/create-route.mdx +314 -0
- package/.docs/raw/reference/server/express-adapter.mdx +193 -0
- package/.docs/raw/reference/server/hono-adapter.mdx +174 -0
- package/.docs/raw/reference/server/mastra-server.mdx +316 -0
- package/.docs/raw/reference/server/routes.mdx +250 -0
- package/.docs/raw/reference/streaming/workflows/timeTravelStream.mdx +170 -0
- package/.docs/raw/reference/tools/mcp-client.mdx +54 -1
- package/.docs/raw/reference/workflows/run-methods/timeTravel.mdx +310 -0
- package/.docs/raw/reference/workflows/run.mdx +14 -0
- package/.docs/raw/server-db/custom-adapters.mdx +380 -0
- package/.docs/raw/server-db/mastra-server.mdx +11 -32
- package/.docs/raw/server-db/server-adapters.mdx +286 -0
- package/CHANGELOG.md +8 -0
- package/package.json +4 -4
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: createRoute() | Server"
|
|
3
|
+
description: "API reference for createRoute() function used to define type-safe routes with validation and OpenAPI generation."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# createRoute()
|
|
9
|
+
|
|
10
|
+
The `createRoute()` function creates type-safe routes with Zod validation. When an `openapiPath` is configured on the server adapter, it generates OpenAPI schema entries from the supplied Zod schemas.
|
|
11
|
+
|
|
12
|
+
## Import
|
|
13
|
+
|
|
14
|
+
```typescript copy
|
|
15
|
+
import { createRoute } from '@mastra/server';
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Signature
|
|
19
|
+
|
|
20
|
+
```typescript copy
|
|
21
|
+
function createRoute<TPath, TQuery, TBody, TResponse, TResponseType>(
|
|
22
|
+
config: RouteConfig<TPath, TQuery, TBody, TResponse, TResponseType>
|
|
23
|
+
): ServerRoute
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Parameters
|
|
27
|
+
|
|
28
|
+
<PropertiesTable
|
|
29
|
+
content={[
|
|
30
|
+
{
|
|
31
|
+
name: "method",
|
|
32
|
+
type: "'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'ALL'",
|
|
33
|
+
description: "HTTP method",
|
|
34
|
+
isOptional: false,
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: "path",
|
|
38
|
+
type: "string",
|
|
39
|
+
description: "Route path with optional params (e.g., `/api/items/:id`)",
|
|
40
|
+
isOptional: false,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: "responseType",
|
|
44
|
+
type: "'json' | 'stream'",
|
|
45
|
+
description: "Response format. Internal routes may use additional types (`datastream-response`, `mcp-http`, `mcp-sse`).",
|
|
46
|
+
isOptional: false,
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: "handler",
|
|
50
|
+
type: "ServerRouteHandler",
|
|
51
|
+
description: "Route handler function",
|
|
52
|
+
isOptional: false,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: "pathParamSchema",
|
|
56
|
+
type: "ZodSchema",
|
|
57
|
+
description: "Validates URL path parameters",
|
|
58
|
+
isOptional: true,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "queryParamSchema",
|
|
62
|
+
type: "ZodSchema",
|
|
63
|
+
description: "Validates query string parameters",
|
|
64
|
+
isOptional: true,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: "bodySchema",
|
|
68
|
+
type: "ZodSchema",
|
|
69
|
+
description: "Validates request body",
|
|
70
|
+
isOptional: true,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
name: "responseSchema",
|
|
74
|
+
type: "ZodSchema",
|
|
75
|
+
description: "Documents response shape for OpenAPI",
|
|
76
|
+
isOptional: true,
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "streamFormat",
|
|
80
|
+
type: "'sse' | 'stream'",
|
|
81
|
+
description: "Stream format (when responseType is 'stream')",
|
|
82
|
+
isOptional: true,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: "maxBodySize",
|
|
86
|
+
type: "number",
|
|
87
|
+
description: "Override default body size limit in bytes",
|
|
88
|
+
isOptional: true,
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
name: "summary",
|
|
92
|
+
type: "string",
|
|
93
|
+
description: "OpenAPI summary",
|
|
94
|
+
isOptional: true,
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "description",
|
|
98
|
+
type: "string",
|
|
99
|
+
description: "OpenAPI description",
|
|
100
|
+
isOptional: true,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "tags",
|
|
104
|
+
type: "string[]",
|
|
105
|
+
description: "OpenAPI tags",
|
|
106
|
+
isOptional: true,
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: "deprecated",
|
|
110
|
+
type: "boolean",
|
|
111
|
+
description: "Mark route as deprecated",
|
|
112
|
+
isOptional: true,
|
|
113
|
+
},
|
|
114
|
+
]}
|
|
115
|
+
/>
|
|
116
|
+
|
|
117
|
+
## Handler parameters
|
|
118
|
+
|
|
119
|
+
The handler receives validated parameters plus runtime context:
|
|
120
|
+
|
|
121
|
+
```typescript copy showLineNumbers
|
|
122
|
+
handler: async (params) => {
|
|
123
|
+
// From schemas (typed from Zod)
|
|
124
|
+
params.id; // From pathParamSchema
|
|
125
|
+
params.filter; // From queryParamSchema
|
|
126
|
+
params.name; // From bodySchema
|
|
127
|
+
|
|
128
|
+
// Runtime context (always available)
|
|
129
|
+
params.mastra; // Mastra instance
|
|
130
|
+
params.requestContext; // Request-scoped context
|
|
131
|
+
params.tools; // Available tools
|
|
132
|
+
params.abortSignal; // Request cancellation signal
|
|
133
|
+
params.taskStore; // A2A task storage
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Return value
|
|
138
|
+
|
|
139
|
+
Returns a `ServerRoute` object that can be registered with an adapter.
|
|
140
|
+
|
|
141
|
+
## Examples
|
|
142
|
+
|
|
143
|
+
### GET route with path params
|
|
144
|
+
|
|
145
|
+
```typescript copy showLineNumbers
|
|
146
|
+
import { createRoute } from '@mastra/server';
|
|
147
|
+
import { z } from 'zod';
|
|
148
|
+
|
|
149
|
+
const getAgent = createRoute({
|
|
150
|
+
method: 'GET',
|
|
151
|
+
path: '/api/agents/:agentId',
|
|
152
|
+
responseType: 'json',
|
|
153
|
+
pathParamSchema: z.object({
|
|
154
|
+
agentId: z.string(),
|
|
155
|
+
}),
|
|
156
|
+
responseSchema: z.object({
|
|
157
|
+
name: z.string(),
|
|
158
|
+
description: z.string().optional(),
|
|
159
|
+
}),
|
|
160
|
+
summary: 'Get agent by ID',
|
|
161
|
+
tags: ['Agents'],
|
|
162
|
+
handler: async ({ agentId, mastra }) => {
|
|
163
|
+
return mastra.getAgent(agentId);
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### POST route with body
|
|
169
|
+
|
|
170
|
+
```typescript copy showLineNumbers
|
|
171
|
+
const createItem = createRoute({
|
|
172
|
+
method: 'POST',
|
|
173
|
+
path: '/api/items',
|
|
174
|
+
responseType: 'json',
|
|
175
|
+
bodySchema: z.object({
|
|
176
|
+
name: z.string(),
|
|
177
|
+
value: z.number(),
|
|
178
|
+
}),
|
|
179
|
+
responseSchema: z.object({
|
|
180
|
+
id: z.string(),
|
|
181
|
+
name: z.string(),
|
|
182
|
+
value: z.number(),
|
|
183
|
+
}),
|
|
184
|
+
handler: async ({ name, value, mastra }) => {
|
|
185
|
+
// name and value are typed from bodySchema
|
|
186
|
+
return { id: 'new-id', name, value };
|
|
187
|
+
},
|
|
188
|
+
});
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Query params with coercion
|
|
192
|
+
|
|
193
|
+
```typescript copy showLineNumbers
|
|
194
|
+
const listItems = createRoute({
|
|
195
|
+
method: 'GET',
|
|
196
|
+
path: '/api/items',
|
|
197
|
+
responseType: 'json',
|
|
198
|
+
queryParamSchema: z.object({
|
|
199
|
+
page: z.coerce.number().default(0),
|
|
200
|
+
limit: z.coerce.number().default(50),
|
|
201
|
+
enabled: z.coerce.boolean().optional(),
|
|
202
|
+
}),
|
|
203
|
+
handler: async ({ page, limit, enabled, mastra }) => {
|
|
204
|
+
// page, limit, enabled are typed and coerced
|
|
205
|
+
return { items: [], page, limit };
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
### Streaming route
|
|
211
|
+
|
|
212
|
+
```typescript copy showLineNumbers
|
|
213
|
+
const streamAgent = createRoute({
|
|
214
|
+
method: 'POST',
|
|
215
|
+
path: '/api/agents/:agentId/stream',
|
|
216
|
+
responseType: 'stream',
|
|
217
|
+
streamFormat: 'sse',
|
|
218
|
+
pathParamSchema: z.object({
|
|
219
|
+
agentId: z.string(),
|
|
220
|
+
}),
|
|
221
|
+
bodySchema: z.object({
|
|
222
|
+
messages: z.array(z.any()),
|
|
223
|
+
}),
|
|
224
|
+
handler: async ({ agentId, messages, mastra, abortSignal }) => {
|
|
225
|
+
const agent = mastra.getAgent(agentId);
|
|
226
|
+
return agent.stream({ messages, abortSignal });
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
### Custom body size limit
|
|
232
|
+
|
|
233
|
+
```typescript copy showLineNumbers
|
|
234
|
+
const uploadRoute = createRoute({
|
|
235
|
+
method: 'POST',
|
|
236
|
+
path: '/api/upload',
|
|
237
|
+
responseType: 'json',
|
|
238
|
+
maxBodySize: 50 * 1024 * 1024, // 50MB
|
|
239
|
+
bodySchema: z.object({
|
|
240
|
+
file: z.string(),
|
|
241
|
+
}),
|
|
242
|
+
handler: async ({ file }) => {
|
|
243
|
+
return { uploaded: true };
|
|
244
|
+
},
|
|
245
|
+
});
|
|
246
|
+
```
|
|
247
|
+
|
|
248
|
+
## Schema patterns
|
|
249
|
+
|
|
250
|
+
### Passthrough for extensibility
|
|
251
|
+
|
|
252
|
+
```typescript copy
|
|
253
|
+
const bodySchema = z.object({
|
|
254
|
+
required: z.string(),
|
|
255
|
+
}).passthrough(); // Allow unknown fields
|
|
256
|
+
```
|
|
257
|
+
|
|
258
|
+
### Date coercion
|
|
259
|
+
|
|
260
|
+
```typescript copy
|
|
261
|
+
const querySchema = z.object({
|
|
262
|
+
fromDate: z.coerce.date().optional(),
|
|
263
|
+
toDate: z.coerce.date().optional(),
|
|
264
|
+
});
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
### Union types
|
|
268
|
+
|
|
269
|
+
```typescript copy
|
|
270
|
+
const bodySchema = z.object({
|
|
271
|
+
messages: z.union([
|
|
272
|
+
z.array(z.any()),
|
|
273
|
+
z.string(),
|
|
274
|
+
]),
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Error handling
|
|
279
|
+
|
|
280
|
+
Throw `HTTPException` to return specific HTTP status codes from handlers:
|
|
281
|
+
|
|
282
|
+
```typescript copy showLineNumbers
|
|
283
|
+
import { createRoute, HTTPException } from '@mastra/server';
|
|
284
|
+
|
|
285
|
+
const getAgent = createRoute({
|
|
286
|
+
method: 'GET',
|
|
287
|
+
path: '/api/agents/:agentId',
|
|
288
|
+
responseType: 'json',
|
|
289
|
+
pathParamSchema: z.object({ agentId: z.string() }),
|
|
290
|
+
handler: async ({ agentId, mastra }) => {
|
|
291
|
+
const agent = mastra.getAgent(agentId);
|
|
292
|
+
if (!agent) {
|
|
293
|
+
throw new HTTPException(404, { message: `Agent '${agentId}' not found` });
|
|
294
|
+
}
|
|
295
|
+
return agent;
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
Common status codes:
|
|
301
|
+
|
|
302
|
+
| Code | Meaning |
|
|
303
|
+
|------|---------|
|
|
304
|
+
| 400 | Bad Request |
|
|
305
|
+
| 401 | Unauthorized |
|
|
306
|
+
| 403 | Forbidden |
|
|
307
|
+
| 404 | Not Found |
|
|
308
|
+
| 500 | Internal Server Error |
|
|
309
|
+
|
|
310
|
+
## Related
|
|
311
|
+
|
|
312
|
+
- [Server Routes](/reference/v1/server/routes) - Default Mastra routes
|
|
313
|
+
- [MastraServer](/reference/v1/server/mastra-server) - Server adapter class
|
|
314
|
+
- [Server Adapters](/docs/v1/server-db/server-adapters) - Using adapters
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Express Adapter | Server"
|
|
3
|
+
description: "API reference for the @mastra/express server adapter."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Steps from "@site/src/components/Steps";
|
|
7
|
+
import StepItem from "@site/src/components/StepItem";
|
|
8
|
+
|
|
9
|
+
# Express Adapter
|
|
10
|
+
|
|
11
|
+
The `@mastra/express` package provides a server adapter for running Mastra with [Express](https://expressjs.com).
|
|
12
|
+
|
|
13
|
+
:::info
|
|
14
|
+
|
|
15
|
+
For general adapter concepts (constructor options, initialization flow, etc.), see [Server Adapters](/docs/v1/server-db/server-adapters).
|
|
16
|
+
|
|
17
|
+
:::
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
<Steps>
|
|
22
|
+
|
|
23
|
+
<StepItem>
|
|
24
|
+
|
|
25
|
+
Install the Express adapter and Express framework:
|
|
26
|
+
|
|
27
|
+
```bash copy
|
|
28
|
+
npm install @mastra/express express
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
</StepItem>
|
|
32
|
+
|
|
33
|
+
<StepItem>
|
|
34
|
+
|
|
35
|
+
Create your server file:
|
|
36
|
+
|
|
37
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
38
|
+
import express from 'express';
|
|
39
|
+
import { MastraServer } from '@mastra/express';
|
|
40
|
+
import { mastra } from './mastra';
|
|
41
|
+
|
|
42
|
+
const app = express();
|
|
43
|
+
app.use(express.json()); // Required for body parsing
|
|
44
|
+
|
|
45
|
+
const server = new MastraServer({ app, mastra });
|
|
46
|
+
await server.init();
|
|
47
|
+
|
|
48
|
+
app.listen(4111, () => {
|
|
49
|
+
console.log('Server running on port 4111');
|
|
50
|
+
});
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
</StepItem>
|
|
54
|
+
|
|
55
|
+
</Steps>
|
|
56
|
+
|
|
57
|
+
:::note
|
|
58
|
+
|
|
59
|
+
Express requires `express.json()` middleware for JSON body parsing. Add it before creating the `MastraServer`.
|
|
60
|
+
|
|
61
|
+
:::
|
|
62
|
+
|
|
63
|
+
## Full example
|
|
64
|
+
|
|
65
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
66
|
+
import express from 'express';
|
|
67
|
+
import { MastraServer } from '@mastra/express';
|
|
68
|
+
import { mastra } from './mastra';
|
|
69
|
+
|
|
70
|
+
const app = express();
|
|
71
|
+
app.use(express.json());
|
|
72
|
+
|
|
73
|
+
const server = new MastraServer({
|
|
74
|
+
app,
|
|
75
|
+
mastra,
|
|
76
|
+
prefix: '/api/v2',
|
|
77
|
+
openapiPath: '/openapi.json',
|
|
78
|
+
bodyLimitOptions: {
|
|
79
|
+
maxSize: 10 * 1024 * 1024, // 10MB
|
|
80
|
+
onError: (err) => ({ error: 'Payload too large', maxSize: '10MB' }),
|
|
81
|
+
},
|
|
82
|
+
streamOptions: { redact: true },
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
await server.init();
|
|
86
|
+
|
|
87
|
+
app.listen(4111);
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Differences from Hono
|
|
91
|
+
|
|
92
|
+
| Aspect | Express | Hono |
|
|
93
|
+
|--------|---------|------|
|
|
94
|
+
| Body parsing | Requires `express.json()` | Handled by framework |
|
|
95
|
+
| Context storage | `res.locals` | `c.get()` / `c.set()` |
|
|
96
|
+
| Middleware signature | `(req, res, next)` | `(c, next)` |
|
|
97
|
+
| Streaming | `res.write()` / `res.end()` | `stream()` helper |
|
|
98
|
+
| AbortSignal | Created from `req.on('close')` | `c.req.raw.signal` |
|
|
99
|
+
|
|
100
|
+
## Adding custom routes
|
|
101
|
+
|
|
102
|
+
Add routes directly to the Express app:
|
|
103
|
+
|
|
104
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
105
|
+
const app = express();
|
|
106
|
+
app.use(express.json());
|
|
107
|
+
|
|
108
|
+
const server = new MastraServer({ app, mastra });
|
|
109
|
+
|
|
110
|
+
// Before init - runs before Mastra middleware
|
|
111
|
+
app.get('/early-health', (req, res) => res.json({ status: 'ok' }));
|
|
112
|
+
|
|
113
|
+
await server.init();
|
|
114
|
+
|
|
115
|
+
// After init - has access to Mastra context
|
|
116
|
+
app.get('/custom', (req, res) => {
|
|
117
|
+
const mastraInstance = res.locals.mastra;
|
|
118
|
+
res.json({ agents: Object.keys(mastraInstance.listAgents()) });
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
app.listen(4111);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
:::tip
|
|
125
|
+
|
|
126
|
+
Routes added before `init()` run without Mastra context. Add routes after `init()` to access the Mastra instance and request context.
|
|
127
|
+
|
|
128
|
+
:::
|
|
129
|
+
|
|
130
|
+
## Accessing context
|
|
131
|
+
|
|
132
|
+
In Express middleware and routes, access Mastra context via `res.locals`:
|
|
133
|
+
|
|
134
|
+
```typescript copy showLineNumbers
|
|
135
|
+
app.get('/custom', (req, res) => {
|
|
136
|
+
const mastra = res.locals.mastra;
|
|
137
|
+
const requestContext = res.locals.requestContext;
|
|
138
|
+
const abortSignal = res.locals.abortSignal;
|
|
139
|
+
|
|
140
|
+
const agent = mastra.getAgent('myAgent');
|
|
141
|
+
res.json({ agent: agent.name });
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Available properties on `res.locals`:
|
|
146
|
+
|
|
147
|
+
| Key | Description |
|
|
148
|
+
|-----|-------------|
|
|
149
|
+
| `mastra` | Mastra instance |
|
|
150
|
+
| `requestContext` | Request context map |
|
|
151
|
+
| `abortSignal` | Request cancellation signal |
|
|
152
|
+
| `tools` | Available tools |
|
|
153
|
+
| `user` | Authenticated user (if auth configured) |
|
|
154
|
+
|
|
155
|
+
## Adding middleware
|
|
156
|
+
|
|
157
|
+
Add Express middleware before or after `init()`:
|
|
158
|
+
|
|
159
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
160
|
+
const app = express();
|
|
161
|
+
app.use(express.json());
|
|
162
|
+
|
|
163
|
+
// Middleware before init
|
|
164
|
+
app.use((req, res, next) => {
|
|
165
|
+
console.log(`${req.method} ${req.url}`);
|
|
166
|
+
next();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const server = new MastraServer({ app, mastra });
|
|
170
|
+
await server.init();
|
|
171
|
+
|
|
172
|
+
// Middleware after init has access to Mastra context
|
|
173
|
+
app.use((req, res, next) => {
|
|
174
|
+
const mastra = res.locals.mastra;
|
|
175
|
+
// ...
|
|
176
|
+
next();
|
|
177
|
+
});
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Manual initialization
|
|
181
|
+
|
|
182
|
+
For custom middleware ordering, call each method separately instead of `init()`. See [Server Adapters: Manual initialization](/docs/v1/server-db/server-adapters#manual-initialization) for details.
|
|
183
|
+
|
|
184
|
+
## Examples
|
|
185
|
+
|
|
186
|
+
- [Express Adapter](https://github.com/mastra-ai/mastra/tree/main/examples/server-express-adapter) - Basic Express server setup
|
|
187
|
+
|
|
188
|
+
## Related
|
|
189
|
+
|
|
190
|
+
- [Server Adapters](/docs/v1/server-db/server-adapters) - Shared adapter concepts
|
|
191
|
+
- [Hono Adapter](/reference/v1/server/hono-adapter) - Alternative adapter
|
|
192
|
+
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
193
|
+
- [createRoute() Reference](/reference/v1/server/create-route) - Creating type-safe custom routes
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: Hono Adapter | Server"
|
|
3
|
+
description: "API reference for the @mastra/hono server adapter."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import Steps from "@site/src/components/Steps";
|
|
7
|
+
import StepItem from "@site/src/components/StepItem";
|
|
8
|
+
|
|
9
|
+
# Hono Adapter
|
|
10
|
+
|
|
11
|
+
The `@mastra/hono` package provides a server adapter for running Mastra with [Hono](https://hono.dev).
|
|
12
|
+
|
|
13
|
+
:::info
|
|
14
|
+
|
|
15
|
+
For general adapter concepts (constructor options, initialization flow, etc.), see [Server Adapters](/docs/v1/server-db/server-adapters).
|
|
16
|
+
|
|
17
|
+
:::
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
<Steps>
|
|
22
|
+
|
|
23
|
+
<StepItem>
|
|
24
|
+
|
|
25
|
+
Install the Hono adapter and Hono framework:
|
|
26
|
+
|
|
27
|
+
```bash copy
|
|
28
|
+
npm install @mastra/hono hono
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
</StepItem>
|
|
32
|
+
|
|
33
|
+
<StepItem>
|
|
34
|
+
|
|
35
|
+
Create your server file:
|
|
36
|
+
|
|
37
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
38
|
+
import { Hono } from 'hono';
|
|
39
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
40
|
+
import { mastra } from './mastra';
|
|
41
|
+
|
|
42
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
43
|
+
const server = new MastraServer({ app, mastra });
|
|
44
|
+
|
|
45
|
+
await server.init();
|
|
46
|
+
|
|
47
|
+
export default app;
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
</StepItem>
|
|
51
|
+
|
|
52
|
+
</Steps>
|
|
53
|
+
|
|
54
|
+
## Full example
|
|
55
|
+
|
|
56
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
57
|
+
import { Hono } from 'hono';
|
|
58
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
59
|
+
import { mastra } from './mastra';
|
|
60
|
+
|
|
61
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
62
|
+
|
|
63
|
+
const server = new MastraServer({
|
|
64
|
+
app,
|
|
65
|
+
mastra,
|
|
66
|
+
prefix: '/api/v2',
|
|
67
|
+
openapiPath: '/openapi.json',
|
|
68
|
+
bodyLimitOptions: {
|
|
69
|
+
maxSize: 10 * 1024 * 1024, // 10MB
|
|
70
|
+
onError: (err) => ({ error: 'Payload too large', maxSize: '10MB' }),
|
|
71
|
+
},
|
|
72
|
+
streamOptions: { redact: true },
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
await server.init();
|
|
76
|
+
|
|
77
|
+
export default app;
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Adding custom routes
|
|
81
|
+
|
|
82
|
+
Add routes directly to the Hono app:
|
|
83
|
+
|
|
84
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
85
|
+
import { Hono } from 'hono';
|
|
86
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
87
|
+
|
|
88
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
89
|
+
const server = new MastraServer({ app, mastra });
|
|
90
|
+
|
|
91
|
+
// Before init - runs before Mastra middleware
|
|
92
|
+
app.get('/early-health', (c) => c.json({ status: 'ok' }));
|
|
93
|
+
|
|
94
|
+
await server.init();
|
|
95
|
+
|
|
96
|
+
// After init - has access to Mastra context
|
|
97
|
+
app.get('/custom', (c) => {
|
|
98
|
+
const mastraInstance = c.get('mastra');
|
|
99
|
+
return c.json({ agents: Object.keys(mastraInstance.listAgents()) });
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
:::tip
|
|
104
|
+
|
|
105
|
+
Routes added before `init()` run without Mastra context. Add routes after `init()` to access the Mastra instance and request context.
|
|
106
|
+
|
|
107
|
+
:::
|
|
108
|
+
|
|
109
|
+
## Accessing context
|
|
110
|
+
|
|
111
|
+
In Hono middleware and route handlers, access Mastra context via `c.get()`:
|
|
112
|
+
|
|
113
|
+
```typescript copy showLineNumbers
|
|
114
|
+
app.get('/custom', async (c) => {
|
|
115
|
+
const mastra = c.get('mastra');
|
|
116
|
+
const requestContext = c.get('requestContext');
|
|
117
|
+
const abortSignal = c.get('abortSignal');
|
|
118
|
+
|
|
119
|
+
const agent = mastra.getAgent('myAgent');
|
|
120
|
+
return c.json({ agent: agent.name });
|
|
121
|
+
});
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Available context keys:
|
|
125
|
+
|
|
126
|
+
| Key | Description |
|
|
127
|
+
|-----|-------------|
|
|
128
|
+
| `mastra` | Mastra instance |
|
|
129
|
+
| `requestContext` | Request context map |
|
|
130
|
+
| `abortSignal` | Request cancellation signal |
|
|
131
|
+
| `tools` | Available tools |
|
|
132
|
+
| `user` | Authenticated user (if auth configured) |
|
|
133
|
+
|
|
134
|
+
## Adding middleware
|
|
135
|
+
|
|
136
|
+
Add Hono middleware before or after `init()`:
|
|
137
|
+
|
|
138
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
139
|
+
import { Hono } from 'hono';
|
|
140
|
+
import { HonoBindings, HonoVariables, MastraServer } from '@mastra/hono';
|
|
141
|
+
|
|
142
|
+
const app = new Hono<{ Bindings: HonoBindings; Variables: HonoVariables }>();
|
|
143
|
+
|
|
144
|
+
// Middleware before init
|
|
145
|
+
app.use('*', async (c, next) => {
|
|
146
|
+
console.log(`${c.req.method} ${c.req.url}`);
|
|
147
|
+
await next();
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
const server = new MastraServer({ app, mastra });
|
|
151
|
+
await server.init();
|
|
152
|
+
|
|
153
|
+
// Middleware after init has access to Mastra context
|
|
154
|
+
app.use('*', async (c, next) => {
|
|
155
|
+
const mastra = c.get('mastra');
|
|
156
|
+
// ...
|
|
157
|
+
await next();
|
|
158
|
+
});
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Manual initialization
|
|
162
|
+
|
|
163
|
+
For custom middleware ordering, call each method separately instead of `init()`. See [Server Adapters: Manual initialization](/docs/v1/server-db/server-adapters#manual-initialization) for details.
|
|
164
|
+
|
|
165
|
+
## Examples
|
|
166
|
+
|
|
167
|
+
- [Hono Adapter](https://github.com/mastra-ai/mastra/tree/main/examples/server-hono-adapter) - Basic Hono server setup
|
|
168
|
+
|
|
169
|
+
## Related
|
|
170
|
+
|
|
171
|
+
- [Server Adapters](/docs/v1/server-db/server-adapters) - Shared adapter concepts
|
|
172
|
+
- [Express Adapter](/reference/v1/server/express-adapter) - Alternative adapter
|
|
173
|
+
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
174
|
+
- [createRoute() Reference](/reference/v1/server/create-route) - Creating type-safe custom routes
|