@mastra/mcp-docs-server 1.0.0-beta.6 → 1.0.0-beta.8
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%2Fagent-builder.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +10 -10
- 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 +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +47 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +196 -196
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +19 -19
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +27 -27
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Flance.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +40 -40
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +16 -16
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +48 -48
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +52 -52
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +41 -41
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Freact.md +14 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +65 -65
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +48 -48
- 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/build-your-ui/ai-sdk-ui.mdx +381 -431
- package/.docs/raw/guides/getting-started/quickstart.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +40 -1
- 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/ai-sdk/chat-route.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +117 -0
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +64 -0
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +116 -0
- package/.docs/raw/reference/ai-sdk/network-route.mdx +99 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-stream.mdx +289 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +110 -0
- 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/custom-api-routes.mdx +5 -5
- package/.docs/raw/server-db/mastra-server.mdx +11 -32
- package/.docs/raw/server-db/server-adapters.mdx +286 -0
- package/.docs/raw/workflows/workflow-state.mdx +4 -5
- package/CHANGELOG.md +15 -0
- package/package.json +4 -4
|
@@ -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
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: MastraServer | Server"
|
|
3
|
+
description: "API reference for the MastraServer abstract class used to create server adapters."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# MastraServer
|
|
9
|
+
|
|
10
|
+
The `MastraServer` abstract class is the base for all server adapters. Extend this class to create adapters for frameworks other than Hono or Express.
|
|
11
|
+
|
|
12
|
+
## Import
|
|
13
|
+
|
|
14
|
+
```typescript copy
|
|
15
|
+
import { MastraServer } from '@mastra/server';
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Type parameters
|
|
19
|
+
|
|
20
|
+
```typescript copy
|
|
21
|
+
MastraServer<TApp, TRequest, TResponse>
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
| Parameter | Description |
|
|
25
|
+
|-----------|-------------|
|
|
26
|
+
| `TApp` | Framework app type (e.g., `Hono`, `Application`) |
|
|
27
|
+
| `TRequest` | Framework request type |
|
|
28
|
+
| `TResponse` | Framework response/context type |
|
|
29
|
+
|
|
30
|
+
## Constructor
|
|
31
|
+
|
|
32
|
+
```typescript copy
|
|
33
|
+
constructor(options: MastraServerOptions<TApp>)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Options
|
|
37
|
+
|
|
38
|
+
<PropertiesTable
|
|
39
|
+
content={[
|
|
40
|
+
{
|
|
41
|
+
name: "app",
|
|
42
|
+
type: "TApp",
|
|
43
|
+
description: "Framework app instance",
|
|
44
|
+
isOptional: false,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "mastra",
|
|
48
|
+
type: "Mastra",
|
|
49
|
+
description: "Mastra instance",
|
|
50
|
+
isOptional: false,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "prefix",
|
|
54
|
+
type: "string",
|
|
55
|
+
description: "Route path prefix (e.g., `/api/v2`)",
|
|
56
|
+
isOptional: true,
|
|
57
|
+
defaultValue: "''",
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: "openapiPath",
|
|
61
|
+
type: "string",
|
|
62
|
+
description: "Path to serve OpenAPI spec",
|
|
63
|
+
isOptional: true,
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: "bodyLimitOptions",
|
|
67
|
+
type: "BodyLimitOptions",
|
|
68
|
+
description: "Request body size limits",
|
|
69
|
+
isOptional: true,
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: "streamOptions",
|
|
73
|
+
type: "StreamOptions",
|
|
74
|
+
description: "Stream redaction config",
|
|
75
|
+
isOptional: true,
|
|
76
|
+
defaultValue: "{ redact: true }",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
name: "customRouteAuthConfig",
|
|
80
|
+
type: "Map<string, boolean>",
|
|
81
|
+
description: "Per-route auth overrides",
|
|
82
|
+
isOptional: true,
|
|
83
|
+
},
|
|
84
|
+
]}
|
|
85
|
+
/>
|
|
86
|
+
|
|
87
|
+
## Abstract methods
|
|
88
|
+
|
|
89
|
+
These methods must be implemented by adapters:
|
|
90
|
+
|
|
91
|
+
### registerContextMiddleware()
|
|
92
|
+
|
|
93
|
+
Attach Mastra context to every request.
|
|
94
|
+
|
|
95
|
+
```typescript copy
|
|
96
|
+
abstract registerContextMiddleware(): void
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**Context to attach:**
|
|
100
|
+
- `mastra` - Mastra instance
|
|
101
|
+
- `requestContext` - Request-scoped context
|
|
102
|
+
- `tools` - Available tools
|
|
103
|
+
- `abortSignal` - Request cancellation signal
|
|
104
|
+
|
|
105
|
+
### registerAuthMiddleware()
|
|
106
|
+
|
|
107
|
+
Register authentication and authorization middleware.
|
|
108
|
+
|
|
109
|
+
```typescript copy
|
|
110
|
+
abstract registerAuthMiddleware(): void
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### registerRoute()
|
|
114
|
+
|
|
115
|
+
Register a single route with the framework.
|
|
116
|
+
|
|
117
|
+
```typescript copy
|
|
118
|
+
abstract registerRoute(
|
|
119
|
+
app: TApp,
|
|
120
|
+
route: ServerRoute,
|
|
121
|
+
options: { prefix?: string }
|
|
122
|
+
): Promise<void>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### getParams()
|
|
126
|
+
|
|
127
|
+
Extract parameters from the request.
|
|
128
|
+
|
|
129
|
+
```typescript copy
|
|
130
|
+
abstract getParams(
|
|
131
|
+
route: ServerRoute,
|
|
132
|
+
request: TRequest
|
|
133
|
+
): Promise<{
|
|
134
|
+
urlParams: Record<string, string>;
|
|
135
|
+
queryParams: Record<string, string>;
|
|
136
|
+
body: unknown;
|
|
137
|
+
}>
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### sendResponse()
|
|
141
|
+
|
|
142
|
+
Send response based on route type.
|
|
143
|
+
|
|
144
|
+
```typescript copy
|
|
145
|
+
abstract sendResponse(
|
|
146
|
+
route: ServerRoute,
|
|
147
|
+
response: TResponse,
|
|
148
|
+
result: unknown
|
|
149
|
+
): Promise<unknown>
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### stream()
|
|
153
|
+
|
|
154
|
+
Handle streaming responses.
|
|
155
|
+
|
|
156
|
+
```typescript copy
|
|
157
|
+
abstract stream(
|
|
158
|
+
route: ServerRoute,
|
|
159
|
+
response: TResponse,
|
|
160
|
+
result: unknown
|
|
161
|
+
): Promise<unknown>
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Instance methods
|
|
165
|
+
|
|
166
|
+
### init()
|
|
167
|
+
|
|
168
|
+
Initialize the server by registering all middleware and routes.
|
|
169
|
+
|
|
170
|
+
```typescript copy
|
|
171
|
+
async init(): Promise<void>
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Calls in order:
|
|
175
|
+
1. `registerContextMiddleware()`
|
|
176
|
+
2. `registerAuthMiddleware()`
|
|
177
|
+
3. `registerRoutes()`
|
|
178
|
+
|
|
179
|
+
### registerRoutes()
|
|
180
|
+
|
|
181
|
+
Register all Mastra routes.
|
|
182
|
+
|
|
183
|
+
```typescript copy
|
|
184
|
+
async registerRoutes(): Promise<void>
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### getApp()
|
|
188
|
+
|
|
189
|
+
Get the framework app instance.
|
|
190
|
+
|
|
191
|
+
```typescript copy
|
|
192
|
+
getApp<T = TApp>(): T
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### parsePathParams()
|
|
196
|
+
|
|
197
|
+
Validate path parameters with the route's Zod schema.
|
|
198
|
+
|
|
199
|
+
```typescript copy
|
|
200
|
+
async parsePathParams(
|
|
201
|
+
route: ServerRoute,
|
|
202
|
+
params: Record<string, string>
|
|
203
|
+
): Promise<Record<string, unknown>>
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### parseQueryParams()
|
|
207
|
+
|
|
208
|
+
Validate query parameters with the route's Zod schema.
|
|
209
|
+
|
|
210
|
+
```typescript copy
|
|
211
|
+
async parseQueryParams(
|
|
212
|
+
route: ServerRoute,
|
|
213
|
+
params: Record<string, string>
|
|
214
|
+
): Promise<Record<string, unknown>>
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
### parseBody()
|
|
218
|
+
|
|
219
|
+
Validate request body with the route's Zod schema.
|
|
220
|
+
|
|
221
|
+
```typescript copy
|
|
222
|
+
async parseBody(
|
|
223
|
+
route: ServerRoute,
|
|
224
|
+
body: unknown
|
|
225
|
+
): Promise<unknown>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### mergeRequestContext()
|
|
229
|
+
|
|
230
|
+
Merge request context from multiple sources (query params and body).
|
|
231
|
+
|
|
232
|
+
```typescript copy
|
|
233
|
+
mergeRequestContext(options: {
|
|
234
|
+
paramsRequestContext?: Record<string, any>;
|
|
235
|
+
bodyRequestContext?: Record<string, any>;
|
|
236
|
+
}): RequestContext
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### registerOpenAPIRoute()
|
|
240
|
+
|
|
241
|
+
Register an endpoint that serves the OpenAPI specification.
|
|
242
|
+
|
|
243
|
+
```typescript copy
|
|
244
|
+
async registerOpenAPIRoute(
|
|
245
|
+
app: TApp,
|
|
246
|
+
config: OpenAPIConfig,
|
|
247
|
+
options: { prefix?: string }
|
|
248
|
+
): Promise<void>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
## Types
|
|
252
|
+
|
|
253
|
+
### BodyLimitOptions
|
|
254
|
+
|
|
255
|
+
```typescript copy
|
|
256
|
+
interface BodyLimitOptions {
|
|
257
|
+
maxSize: number;
|
|
258
|
+
onError: (error: unknown) => unknown;
|
|
259
|
+
}
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### StreamOptions
|
|
263
|
+
|
|
264
|
+
```typescript copy
|
|
265
|
+
interface StreamOptions {
|
|
266
|
+
redact?: boolean;
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## Example
|
|
271
|
+
|
|
272
|
+
```typescript copy showLineNumbers
|
|
273
|
+
import { MastraServer, ServerRoute } from '@mastra/server';
|
|
274
|
+
import type { Mastra } from '@mastra/core';
|
|
275
|
+
|
|
276
|
+
export class MyServer extends MastraServer<MyApp, MyRequest, MyResponse> {
|
|
277
|
+
registerContextMiddleware(): void {
|
|
278
|
+
this.app.use('*', (req, res, next) => {
|
|
279
|
+
res.locals.mastra = this.mastra;
|
|
280
|
+
next();
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
registerAuthMiddleware(): void {
|
|
285
|
+
const auth = this.mastra.getServer()?.auth;
|
|
286
|
+
if (!auth) return;
|
|
287
|
+
// Implement auth
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
async registerRoute(app, route, { prefix }) {
|
|
291
|
+
// Implement route registration
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
async getParams(route, request) {
|
|
295
|
+
return {
|
|
296
|
+
urlParams: request.params,
|
|
297
|
+
queryParams: request.query,
|
|
298
|
+
body: request.body,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
async sendResponse(route, response, result) {
|
|
303
|
+
return response.json(result);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
async stream(route, response, result) {
|
|
307
|
+
// Implement streaming
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
```
|
|
311
|
+
|
|
312
|
+
## Related
|
|
313
|
+
|
|
314
|
+
- [Server Adapters](/docs/v1/server-db/server-adapters) - Using adapters
|
|
315
|
+
- [Custom Adapters](/docs/v1/server-db/custom-adapters) - Creating custom adapters
|
|
316
|
+
- [createRoute()](/reference/v1/server/create-route) - Creating custom routes
|