@mastra/mcp-docs-server 1.0.0-beta.5 → 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%2Fagent-builder.md +9 -9
- package/.docs/organized/changelogs/%40mastra%2Fai-sdk.md +67 -67
- 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 +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +110 -110
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fcodemod.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +60 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +358 -358
- package/.docs/organized/changelogs/%40mastra%2Fcouchbase.md +11 -11
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +24 -24
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-netlify.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +79 -79
- package/.docs/organized/changelogs/%40mastra%2Fduckdb.md +42 -0
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Felasticsearch.md +61 -0
- package/.docs/organized/changelogs/%40mastra%2Fevals.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Flance.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +55 -55
- package/.docs/organized/changelogs/%40mastra%2Floggers.md +12 -12
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +17 -17
- package/.docs/organized/changelogs/%40mastra%2Fmcp.md +125 -125
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +36 -36
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fopensearch.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +59 -59
- package/.docs/organized/changelogs/%40mastra%2Fpinecone.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +77 -77
- package/.docs/organized/changelogs/%40mastra%2Fqdrant.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Frag.md +43 -43
- package/.docs/organized/changelogs/%40mastra%2Freact.md +16 -0
- package/.docs/organized/changelogs/%40mastra%2Fs3vectors.md +9 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +113 -113
- package/.docs/organized/changelogs/%40mastra%2Fturbopuffer.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +57 -57
- package/.docs/organized/changelogs/%40mastra%2Fvectorize.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fvoice-google.md +19 -19
- package/.docs/organized/changelogs/create-mastra.md +15 -15
- package/.docs/organized/changelogs/mastra.md +30 -30
- package/.docs/organized/code-examples/agui.md +1 -0
- 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 +2 -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 +721 -0
- package/.docs/organized/code-examples/server-app-access.md +342 -0
- 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/agent-approval.mdx +189 -0
- package/.docs/raw/agents/guardrails.mdx +13 -9
- package/.docs/raw/agents/networks.mdx +1 -0
- package/.docs/raw/agents/overview.mdx +8 -152
- package/.docs/raw/agents/processors.mdx +279 -0
- package/.docs/raw/agents/structured-output.mdx +224 -0
- package/.docs/raw/deployment/cloud-providers/index.mdx +19 -26
- package/.docs/raw/deployment/cloud-providers/netlify-deployer.mdx +44 -13
- package/.docs/raw/evals/running-in-ci.mdx +0 -2
- package/.docs/raw/{guides/getting-started → getting-started}/manual-install.mdx +2 -2
- package/.docs/raw/getting-started/start.mdx +1 -1
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +8 -0
- package/.docs/raw/guides/getting-started/quickstart.mdx +1 -1
- package/.docs/raw/guides/guide/whatsapp-chat-bot.mdx +421 -0
- package/.docs/raw/guides/index.mdx +3 -35
- package/.docs/raw/guides/migrations/upgrade-to-v1/agent.mdx +11 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/workflows.mdx +37 -0
- package/.docs/raw/index.mdx +1 -1
- package/.docs/raw/memory/memory-processors.mdx +265 -79
- package/.docs/raw/memory/working-memory.mdx +11 -2
- package/.docs/raw/observability/overview.mdx +0 -1
- package/.docs/raw/observability/tracing/bridges/otel.mdx +200 -0
- package/.docs/raw/observability/tracing/exporters/arize.mdx +36 -0
- package/.docs/raw/observability/tracing/exporters/braintrust.mdx +19 -0
- package/.docs/raw/observability/tracing/exporters/langfuse.mdx +83 -0
- package/.docs/raw/observability/tracing/exporters/langsmith.mdx +12 -0
- package/.docs/raw/observability/tracing/exporters/otel.mdx +34 -22
- package/.docs/raw/observability/tracing/exporters/posthog.mdx +20 -0
- package/.docs/raw/observability/tracing/overview.mdx +76 -6
- package/.docs/raw/observability/tracing/processors/sensitive-data-filter.mdx +0 -1
- package/.docs/raw/rag/retrieval.mdx +23 -6
- package/.docs/raw/rag/vector-databases.mdx +93 -2
- package/.docs/raw/reference/agents/generate.mdx +55 -6
- package/.docs/raw/reference/agents/network.mdx +44 -0
- package/.docs/raw/reference/client-js/memory.mdx +43 -0
- package/.docs/raw/reference/client-js/workflows.mdx +92 -63
- package/.docs/raw/reference/deployer/netlify.mdx +1 -2
- package/.docs/raw/reference/evals/scorer-utils.mdx +362 -0
- package/.docs/raw/reference/index.mdx +1 -0
- package/.docs/raw/reference/observability/tracing/bridges/otel.mdx +177 -0
- package/.docs/raw/reference/observability/tracing/configuration.mdx +0 -4
- package/.docs/raw/reference/observability/tracing/exporters/arize.mdx +29 -0
- package/.docs/raw/reference/observability/tracing/exporters/langfuse.mdx +43 -0
- package/.docs/raw/reference/observability/tracing/exporters/langsmith.mdx +17 -1
- package/.docs/raw/reference/observability/tracing/exporters/otel.mdx +33 -43
- package/.docs/raw/reference/observability/tracing/instances.mdx +0 -4
- package/.docs/raw/reference/observability/tracing/interfaces.mdx +29 -4
- package/.docs/raw/reference/observability/tracing/spans.mdx +0 -4
- package/.docs/raw/reference/processors/language-detector.mdx +9 -2
- package/.docs/raw/reference/processors/message-history-processor.mdx +131 -0
- package/.docs/raw/reference/processors/moderation-processor.mdx +10 -3
- package/.docs/raw/reference/processors/pii-detector.mdx +10 -3
- package/.docs/raw/reference/processors/processor-interface.mdx +502 -0
- package/.docs/raw/reference/processors/prompt-injection-detector.mdx +9 -2
- package/.docs/raw/reference/processors/semantic-recall-processor.mdx +197 -0
- package/.docs/raw/reference/processors/system-prompt-scrubber.mdx +2 -2
- package/.docs/raw/reference/processors/tool-call-filter.mdx +125 -0
- package/.docs/raw/reference/processors/working-memory-processor.mdx +221 -0
- 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/storage/cloudflare-d1.mdx +37 -0
- package/.docs/raw/reference/storage/convex.mdx +164 -0
- package/.docs/raw/reference/storage/lance.mdx +33 -0
- package/.docs/raw/reference/storage/libsql.mdx +37 -0
- package/.docs/raw/reference/storage/mongodb.mdx +39 -0
- package/.docs/raw/reference/storage/mssql.mdx +37 -0
- package/.docs/raw/reference/storage/postgresql.mdx +37 -0
- package/.docs/raw/reference/streaming/ChunkType.mdx +1 -1
- package/.docs/raw/reference/streaming/agents/stream.mdx +56 -1
- package/.docs/raw/reference/streaming/workflows/observeStream.mdx +7 -9
- package/.docs/raw/reference/streaming/workflows/{resumeStreamVNext.mdx → resumeStream.mdx} +51 -11
- package/.docs/raw/reference/streaming/workflows/stream.mdx +83 -24
- package/.docs/raw/reference/streaming/workflows/timeTravelStream.mdx +170 -0
- package/.docs/raw/reference/tools/mcp-client.mdx +128 -18
- package/.docs/raw/reference/vectors/convex.mdx +429 -0
- package/.docs/raw/reference/vectors/duckdb.mdx +462 -0
- package/.docs/raw/reference/vectors/elasticsearch.mdx +310 -0
- package/.docs/raw/reference/voice/google.mdx +159 -20
- package/.docs/raw/reference/workflows/run-methods/restart.mdx +142 -0
- package/.docs/raw/reference/workflows/run-methods/resume.mdx +44 -0
- package/.docs/raw/reference/workflows/run-methods/start.mdx +44 -0
- package/.docs/raw/reference/workflows/run-methods/timeTravel.mdx +310 -0
- package/.docs/raw/reference/workflows/run.mdx +27 -5
- package/.docs/raw/reference/workflows/step.mdx +13 -0
- package/.docs/raw/reference/workflows/workflow.mdx +19 -0
- package/.docs/raw/server-db/custom-adapters.mdx +380 -0
- package/.docs/raw/server-db/mastra-server.mdx +16 -8
- package/.docs/raw/server-db/request-context.mdx +0 -1
- package/.docs/raw/server-db/server-adapters.mdx +286 -0
- package/.docs/raw/server-db/storage.mdx +11 -0
- package/.docs/raw/streaming/overview.mdx +6 -6
- package/.docs/raw/streaming/tool-streaming.mdx +2 -2
- package/.docs/raw/streaming/workflow-streaming.mdx +5 -11
- package/.docs/raw/workflows/error-handling.mdx +1 -0
- package/.docs/raw/workflows/human-in-the-loop.mdx +4 -4
- package/.docs/raw/workflows/overview.mdx +56 -44
- package/.docs/raw/workflows/snapshots.mdx +1 -0
- package/.docs/raw/workflows/suspend-and-resume.mdx +85 -16
- package/.docs/raw/workflows/time-travel.mdx +313 -0
- package/.docs/raw/workflows/workflow-state.mdx +191 -0
- package/CHANGELOG.md +16 -0
- package/package.json +4 -4
- package/.docs/raw/agents/human-in-the-loop-with-tools.mdx +0 -91
- package/.docs/raw/reference/streaming/workflows/observeStreamVNext.mdx +0 -47
- package/.docs/raw/reference/streaming/workflows/streamVNext.mdx +0 -153
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Server Adapters | Server & DB"
|
|
3
|
+
description: "Manually configure a Mastra server using Hono or Express adapters."
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
import Tabs from "@theme/Tabs";
|
|
8
|
+
import TabItem from "@theme/TabItem";
|
|
9
|
+
|
|
10
|
+
# Server Adapters
|
|
11
|
+
|
|
12
|
+
Server adapters let you run Mastra with your own HTTP server instead of using `mastra build`. While `mastra build` generates a standalone server for you, adapters give you full control over the server setup, letting you integrate Mastra into any Node.js application.
|
|
13
|
+
|
|
14
|
+
Use adapters when you need to:
|
|
15
|
+
|
|
16
|
+
- **Integrate Mastra into an existing application** - Add Mastra endpoints to an Express or Hono app you already have running
|
|
17
|
+
- **Control middleware ordering** - Insert your own middleware before or after Mastra's authentication and routing
|
|
18
|
+
- **Deploy to platforms requiring specific server setup** - Some platforms like Cloudflare Workers or custom Docker setups need direct access to the server instance
|
|
19
|
+
- **Use Express instead of Hono** - `mastra build` uses Hono internally, but you may prefer Express for its ecosystem or team familiarity
|
|
20
|
+
|
|
21
|
+
:::info
|
|
22
|
+
|
|
23
|
+
For simple deployments without custom server requirements, use `mastra build` instead. It configures server setup, registers middleware, and applies deployment settings based on your project configuration. See [Server Configuration](/docs/v1/server-db/mastra-server).
|
|
24
|
+
|
|
25
|
+
:::
|
|
26
|
+
|
|
27
|
+
## Available adapters
|
|
28
|
+
|
|
29
|
+
- **[@mastra/hono](/reference/v1/server/hono-adapter)** - Hono framework adapter
|
|
30
|
+
- **[@mastra/express](/reference/v1/server/express-adapter)** - Express framework adapter
|
|
31
|
+
|
|
32
|
+
## Basic setup
|
|
33
|
+
|
|
34
|
+
Both adapters follow the same pattern. The adapter wraps your framework's app instance and registers Mastra's routes and middleware onto it.
|
|
35
|
+
|
|
36
|
+
1. **Create your framework's app instance** - Initialize Hono or Express as you normally would
|
|
37
|
+
2. **Create a `MastraServer`** - Pass your app and Mastra instance to the adapter
|
|
38
|
+
3. **Call `init()`** - This registers context middleware, authentication, and all API routes
|
|
39
|
+
|
|
40
|
+
After `init()`, your app has all the Mastra endpoints (`/api/agents`, `/api/workflows`, etc.) and you can add your own routes before or after.
|
|
41
|
+
|
|
42
|
+
See the [Hono Adapter](/reference/v1/server/hono-adapter) or [Express Adapter](/reference/v1/server/express-adapter) docs for framework-specific setup and code examples.
|
|
43
|
+
|
|
44
|
+
## Constructor options
|
|
45
|
+
|
|
46
|
+
Both adapters accept the same options:
|
|
47
|
+
|
|
48
|
+
<PropertiesTable
|
|
49
|
+
content={[
|
|
50
|
+
{
|
|
51
|
+
name: "app",
|
|
52
|
+
type: "Hono | Application",
|
|
53
|
+
description: "Framework app instance",
|
|
54
|
+
isOptional: false,
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: "mastra",
|
|
58
|
+
type: "Mastra",
|
|
59
|
+
description: "Mastra instance",
|
|
60
|
+
isOptional: false,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "prefix",
|
|
64
|
+
type: "string",
|
|
65
|
+
description: "Route path prefix (e.g., `/api/v2`)",
|
|
66
|
+
isOptional: true,
|
|
67
|
+
defaultValue: "''",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
name: "openapiPath",
|
|
71
|
+
type: "string",
|
|
72
|
+
description: "Path to serve OpenAPI spec (e.g., `/openapi.json`)",
|
|
73
|
+
isOptional: true,
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: "bodyLimitOptions",
|
|
77
|
+
type: "{ maxSize: number, onError: (err) => unknown }",
|
|
78
|
+
description: "Request body size limits",
|
|
79
|
+
isOptional: true,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "streamOptions",
|
|
83
|
+
type: "{ redact?: boolean }",
|
|
84
|
+
description: "Stream redaction config. When true, redacts sensitive data from streams.",
|
|
85
|
+
isOptional: true,
|
|
86
|
+
defaultValue: "{ redact: true }",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "customRouteAuthConfig",
|
|
90
|
+
type: "Map<string, boolean>",
|
|
91
|
+
description: "Per-route auth overrides. Keys are `METHOD:PATH` (e.g., `GET:/api/health`). Value `false` makes route public, `true` requires auth.",
|
|
92
|
+
isOptional: true,
|
|
93
|
+
},
|
|
94
|
+
]}
|
|
95
|
+
/>
|
|
96
|
+
|
|
97
|
+
## Initialization flow
|
|
98
|
+
|
|
99
|
+
Calling `init()` runs three steps in order. Understanding this flow helps when you need to insert your own middleware at specific points.
|
|
100
|
+
|
|
101
|
+
1. **`registerContextMiddleware()`** - Attaches the Mastra instance, request context, tools, and abort signal to every request. This makes Mastra available to all subsequent middleware and route handlers.
|
|
102
|
+
2. **`registerAuthMiddleware()`** - Adds authentication and authorization middleware, but only if `server.auth` is configured in your Mastra instance. Skipped entirely if no auth is configured.
|
|
103
|
+
3. **`registerRoutes()`** - Registers all Mastra API routes for agents, workflows, and other features. Also registers MCP routes if MCP servers are configured.
|
|
104
|
+
|
|
105
|
+
### Manual initialization
|
|
106
|
+
|
|
107
|
+
For custom middleware ordering, call each method separately instead of `init()`. This is useful when you need middleware that runs before Mastra's context is set up, or when you need to insert logic between the initialization steps.
|
|
108
|
+
|
|
109
|
+
```typescript title="server.ts" copy showLineNumbers
|
|
110
|
+
const server = new MastraServer({ app, mastra });
|
|
111
|
+
|
|
112
|
+
// Your middleware first
|
|
113
|
+
app.use(loggingMiddleware);
|
|
114
|
+
|
|
115
|
+
server.registerContextMiddleware();
|
|
116
|
+
|
|
117
|
+
// Middleware that needs Mastra context
|
|
118
|
+
app.use(customMiddleware);
|
|
119
|
+
|
|
120
|
+
server.registerAuthMiddleware();
|
|
121
|
+
await server.registerRoutes();
|
|
122
|
+
|
|
123
|
+
// Routes after Mastra
|
|
124
|
+
app.get('/health', ...);
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
:::tip
|
|
128
|
+
|
|
129
|
+
Use manual initialization when you need middleware that runs before Mastra's context is available, or when you need to insert middleware between the context and auth steps.
|
|
130
|
+
|
|
131
|
+
:::
|
|
132
|
+
|
|
133
|
+
## Adding custom routes
|
|
134
|
+
|
|
135
|
+
You can add your own routes to the app alongside Mastra's routes. Routes added after `init()` have access to the Mastra context (the Mastra instance, request context, authenticated user, etc.) through framework-specific mechanisms.
|
|
136
|
+
|
|
137
|
+
The timing matters: routes added before `init()` won't have Mastra context available, while routes added after `init()` will. See the framework-specific docs for syntax and examples:
|
|
138
|
+
|
|
139
|
+
- [Hono: Adding custom routes](/reference/v1/server/hono-adapter#adding-custom-routes)
|
|
140
|
+
- [Express: Adding custom routes](/reference/v1/server/express-adapter#adding-custom-routes)
|
|
141
|
+
|
|
142
|
+
## Route prefixes
|
|
143
|
+
|
|
144
|
+
By default, Mastra routes are registered at `/api/agents`, `/api/workflows`, etc. Use the `prefix` option to change this, which is useful for API versioning or when integrating with an existing app that has its own `/api` routes.
|
|
145
|
+
|
|
146
|
+
```typescript copy showLineNumbers
|
|
147
|
+
const server = new MastraServer({
|
|
148
|
+
app,
|
|
149
|
+
mastra,
|
|
150
|
+
prefix: '/api/v2',
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
With this prefix, Mastra routes become `/api/v2/agents`, `/api/v2/workflows`, etc. Custom routes you add directly to the app are not affected by this prefix.
|
|
155
|
+
|
|
156
|
+
## OpenAPI spec
|
|
157
|
+
|
|
158
|
+
Mastra can generate an OpenAPI specification for all registered routes. This is useful for documentation, client generation, or integration with API tools. Enable it by setting the `openapiPath` option:
|
|
159
|
+
|
|
160
|
+
```typescript copy showLineNumbers
|
|
161
|
+
const server = new MastraServer({
|
|
162
|
+
app,
|
|
163
|
+
mastra,
|
|
164
|
+
openapiPath: '/openapi.json',
|
|
165
|
+
});
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
The spec is generated from the Zod schemas defined on each route and served at the specified path. It includes all Mastra routes as well as any custom routes created with `createRoute()`.
|
|
169
|
+
|
|
170
|
+
## Stream data redaction
|
|
171
|
+
|
|
172
|
+
When streaming agent responses over HTTP, the HTTP streaming layer redacts sensitive information from stream chunks before sending them to clients. This prevents accidental exposure of:
|
|
173
|
+
|
|
174
|
+
- System prompts and agent instructions
|
|
175
|
+
- Tool definitions and their parameters
|
|
176
|
+
- API keys and other credentials in request bodies
|
|
177
|
+
- Internal configuration data
|
|
178
|
+
|
|
179
|
+
This redaction happens at the HTTP boundary, so internal callbacks like `onStepFinish` still have access to the full request data for debugging and observability purposes.
|
|
180
|
+
|
|
181
|
+
By default, redaction is enabled. Configure this behavior via `streamOptions`:
|
|
182
|
+
|
|
183
|
+
```typescript copy showLineNumbers
|
|
184
|
+
const server = new MastraServer({
|
|
185
|
+
app,
|
|
186
|
+
mastra,
|
|
187
|
+
streamOptions: {
|
|
188
|
+
redact: true, // Default
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
Set `redact: false` only for internal services or debugging scenarios where you need access to the full request data in stream responses.
|
|
194
|
+
|
|
195
|
+
## Per-route auth overrides
|
|
196
|
+
|
|
197
|
+
When authentication is configured on your Mastra instance, all routes require authentication by default. Sometimes you need exceptions: public health check endpoints, webhook receivers, or admin routes that need stricter controls.
|
|
198
|
+
|
|
199
|
+
Use `customRouteAuthConfig` to override authentication behavior for specific routes:
|
|
200
|
+
|
|
201
|
+
```typescript copy showLineNumbers
|
|
202
|
+
const server = new MastraServer({
|
|
203
|
+
app,
|
|
204
|
+
mastra,
|
|
205
|
+
customRouteAuthConfig: new Map([
|
|
206
|
+
['GET:/api/health', false], // Public health check
|
|
207
|
+
['GET:/api/openapi.json', false], // Public API spec
|
|
208
|
+
['POST:/api/webhooks/*', false], // Public webhook endpoints
|
|
209
|
+
['POST:/api/admin/reset', true], // Require auth even if globally disabled
|
|
210
|
+
['ALL:/api/internal/*', true], // Protect all methods on internal routes
|
|
211
|
+
]),
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Keys follow the format `METHOD:PATH` where method is `GET`, `POST`, `PUT`, `DELETE`, or `ALL`. Paths support wildcards (`*`) for matching multiple routes. Setting a value to `false` makes the route public, while `true` requires authentication.
|
|
216
|
+
|
|
217
|
+
## Accessing the app
|
|
218
|
+
|
|
219
|
+
After creating the adapter, you may need to access the underlying framework app instance—for example, to pass it to a platform's serve function or to add routes programmatically from another module.
|
|
220
|
+
|
|
221
|
+
```typescript copy showLineNumbers
|
|
222
|
+
// Via the MastraServer instance
|
|
223
|
+
const app = server.getApp();
|
|
224
|
+
|
|
225
|
+
// Via the Mastra instance (available after adapter construction)
|
|
226
|
+
const app = mastra.getServerApp();
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Both methods return the same app instance. Use whichever is more convenient based on what's in scope.
|
|
230
|
+
|
|
231
|
+
## Server config vs adapter options
|
|
232
|
+
|
|
233
|
+
When using server adapters, configuration comes from two places: the Mastra `server` config (passed to the `Mastra` constructor) and the adapter constructor options. Understanding which options come from where helps avoid confusion when settings don't seem to take effect.
|
|
234
|
+
|
|
235
|
+
### Used by adapters
|
|
236
|
+
|
|
237
|
+
The adapter reads these settings from `mastra.getServer()`:
|
|
238
|
+
|
|
239
|
+
| Option | Description |
|
|
240
|
+
|--------|-------------|
|
|
241
|
+
| `auth` | Authentication config, used by `registerAuthMiddleware()`. |
|
|
242
|
+
| `bodySizeLimit` | Default body size limit in bytes. Can be overridden per-adapter via `bodyLimitOptions`. |
|
|
243
|
+
|
|
244
|
+
### Adapter constructor only
|
|
245
|
+
|
|
246
|
+
These options are passed directly to the adapter constructor and are not read from the Mastra config:
|
|
247
|
+
|
|
248
|
+
| Option | Description |
|
|
249
|
+
|--------|-------------|
|
|
250
|
+
| `prefix` | Route path prefix |
|
|
251
|
+
| `openapiPath` | OpenAPI spec endpoint |
|
|
252
|
+
| `bodyLimitOptions` | Body size limit with custom error handler |
|
|
253
|
+
| `streamOptions` | Stream redaction settings |
|
|
254
|
+
| `customRouteAuthConfig` | Per-route auth overrides |
|
|
255
|
+
|
|
256
|
+
### Not used by adapters
|
|
257
|
+
|
|
258
|
+
These `server` config options are only used by `mastra build` and have no effect when using adapters directly:
|
|
259
|
+
|
|
260
|
+
| Option | Used by |
|
|
261
|
+
|--------|---------|
|
|
262
|
+
| `port`, `host` | `mastra dev`, `mastra build` |
|
|
263
|
+
| `cors` | `mastra build` adds CORS middleware |
|
|
264
|
+
| `timeout` | `mastra build` |
|
|
265
|
+
| `apiRoutes` | `registerApiRoute()` for `mastra build` |
|
|
266
|
+
| `middleware` | Middleware config for `mastra build` |
|
|
267
|
+
|
|
268
|
+
When using adapters, configure these features directly with your framework. For example, add CORS middleware using Hono's or Express's built-in CORS packages, and set the port when calling your framework's listen function.
|
|
269
|
+
|
|
270
|
+
## MCP support
|
|
271
|
+
|
|
272
|
+
Server adapters register MCP (Model Context Protocol) routes during `registerRoutes()` when MCP servers are configured in your Mastra instance. MCP allows external tools and services to connect to your Mastra server and interact with your agents.
|
|
273
|
+
|
|
274
|
+
The adapter registers routes for both HTTP and SSE (Server-Sent Events) transports, enabling different client connection patterns.
|
|
275
|
+
|
|
276
|
+
See [MCP](/docs/v1/mcp/overview) for configuration details and how to set up MCP servers.
|
|
277
|
+
|
|
278
|
+
## Related
|
|
279
|
+
|
|
280
|
+
- [Hono Adapter](/reference/v1/server/hono-adapter) - Hono-specific setup
|
|
281
|
+
- [Express Adapter](/reference/v1/server/express-adapter) - Express-specific setup
|
|
282
|
+
- [Custom Adapters](/docs/v1/server-db/custom-adapters) - Building adapters for other frameworks
|
|
283
|
+
- [Server Configuration](/docs/v1/server-db/mastra-server) - Using `mastra build` instead
|
|
284
|
+
- [Authentication](/docs/v1/auth) - Configuring auth for your server
|
|
285
|
+
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
286
|
+
- [createRoute() Reference](/reference/v1/server/create-route) - Creating type-safe custom routes
|
|
@@ -489,12 +489,23 @@ console.log(result.messages); // MastraDBMessage[]
|
|
|
489
489
|
console.log(result.total); // Total count
|
|
490
490
|
console.log(result.hasMore); // Whether more pages exist
|
|
491
491
|
|
|
492
|
+
// Get messages from multiple threads at once
|
|
493
|
+
const multiThreadResult = await mastra
|
|
494
|
+
.getStorage()
|
|
495
|
+
.listMessages({
|
|
496
|
+
threadId: ["thread-1", "thread-2", "thread-3"],
|
|
497
|
+
page: 0,
|
|
498
|
+
perPage: 100
|
|
499
|
+
});
|
|
500
|
+
|
|
492
501
|
// Get messages by their IDs
|
|
493
502
|
const messages = await mastra
|
|
494
503
|
.getStorage()
|
|
495
504
|
.listMessagesById({ messageIds: messageIdArr });
|
|
496
505
|
```
|
|
497
506
|
|
|
507
|
+
The `threadId` parameter accepts either a single thread ID string or an array of thread IDs to query messages from multiple threads in a single request.
|
|
508
|
+
|
|
498
509
|
All message queries return `MastraDBMessage[]` format. If you need to convert messages to AI SDK formats for UI rendering, use the conversion utilities from `@mastra/ai-sdk/ui`:
|
|
499
510
|
|
|
500
511
|
```typescript copy
|
|
@@ -132,14 +132,14 @@ console.log("Token usage:", await networkStream.usage);
|
|
|
132
132
|
|
|
133
133
|
Streaming from a workflow returns a sequence of structured events describing the run lifecycle, rather than incremental text chunks. This event-based format makes it possible to track and respond to workflow progress in real time once a run is created using `.createRun()`.
|
|
134
134
|
|
|
135
|
-
### Using `Run.
|
|
135
|
+
### Using `Run.stream()`
|
|
136
136
|
|
|
137
|
-
|
|
137
|
+
The `stream()` method returns a `ReadableStream` of events directly.
|
|
138
138
|
|
|
139
139
|
```typescript {3,9} showLineNumbers copy
|
|
140
140
|
const run = await testWorkflow.createRun();
|
|
141
141
|
|
|
142
|
-
const stream = await run.
|
|
142
|
+
const stream = await run.stream({
|
|
143
143
|
inputData: {
|
|
144
144
|
value: "initial data",
|
|
145
145
|
},
|
|
@@ -150,16 +150,16 @@ for await (const chunk of stream) {
|
|
|
150
150
|
}
|
|
151
151
|
```
|
|
152
152
|
|
|
153
|
-
> See Run.
|
|
153
|
+
> See [Run.stream()](/reference/v1/streaming/workflows/stream) method documentation for more information.
|
|
154
154
|
|
|
155
155
|
### Output from `Run.stream()`
|
|
156
156
|
|
|
157
|
-
The
|
|
157
|
+
The event structure includes `runId` and `from` at the top level, making it easier to identify and track workflow runs without digging into the payload.
|
|
158
158
|
|
|
159
159
|
```typescript
|
|
160
160
|
// ...
|
|
161
161
|
{
|
|
162
|
-
type: '
|
|
162
|
+
type: 'workflow-start',
|
|
163
163
|
runId: '1eeaf01a-d2bf-4e3f-8d1b-027795ccd3df',
|
|
164
164
|
from: 'WORKFLOW',
|
|
165
165
|
payload: {
|
|
@@ -162,7 +162,7 @@ For detailed documentation on all lifecycle hooks, see the [createTool() referen
|
|
|
162
162
|
|
|
163
163
|
## Tool using an agent
|
|
164
164
|
|
|
165
|
-
Pipe an agent's `
|
|
165
|
+
Pipe an agent's `fullStream` to the tool's `writer`. This streams partial output, and Mastra automatically aggregates the agent's usage into the tool run.
|
|
166
166
|
|
|
167
167
|
```typescript showLineNumbers copy
|
|
168
168
|
import { createTool } from "@mastra/core/tools";
|
|
@@ -176,7 +176,7 @@ export const testTool = createTool({
|
|
|
176
176
|
const testAgent = context?.mastra?.getAgent("testAgent");
|
|
177
177
|
const stream = await testAgent?.stream(`What is the weather in ${city}?`);
|
|
178
178
|
|
|
179
|
-
await stream!.
|
|
179
|
+
await stream!.fullStream.pipeTo(context?.writer!);
|
|
180
180
|
|
|
181
181
|
return {
|
|
182
182
|
value: await stream!.text,
|
|
@@ -16,12 +16,6 @@ By combining writable workflow streams with agent streaming, you gain fine-grain
|
|
|
16
16
|
|
|
17
17
|
### Using the `writer` argument
|
|
18
18
|
|
|
19
|
-
:::warning
|
|
20
|
-
|
|
21
|
-
The writer is only available when using `streamVNext`.
|
|
22
|
-
|
|
23
|
-
:::
|
|
24
|
-
|
|
25
19
|
The `writer` argument is passed to a workflow step's `execute` function and can be used to emit custom events, data, or values into the active stream. This enables workflow steps to provide intermediate results or status updates while execution is still in progress.
|
|
26
20
|
|
|
27
21
|
:::warning
|
|
@@ -66,7 +60,7 @@ const testWorkflow = mastra.getWorkflow("testWorkflow");
|
|
|
66
60
|
|
|
67
61
|
const run = await testWorkflow.createRun();
|
|
68
62
|
|
|
69
|
-
const stream = await run.
|
|
63
|
+
const stream = await run.stream({
|
|
70
64
|
inputData: {
|
|
71
65
|
value: "initial data",
|
|
72
66
|
},
|
|
@@ -77,8 +71,8 @@ for await (const chunk of stream) {
|
|
|
77
71
|
}
|
|
78
72
|
|
|
79
73
|
if (result!.status === "suspended") {
|
|
80
|
-
// if the workflow is suspended, we can resume it with the
|
|
81
|
-
const resumedStream = await run.
|
|
74
|
+
// if the workflow is suspended, we can resume it with the resumeStream method
|
|
75
|
+
const resumedStream = await run.resumeStream({
|
|
82
76
|
resumeData: { value: "resume data" },
|
|
83
77
|
});
|
|
84
78
|
|
|
@@ -90,10 +84,10 @@ if (result!.status === "suspended") {
|
|
|
90
84
|
|
|
91
85
|
### Resuming an interrupted workflow stream
|
|
92
86
|
|
|
93
|
-
If a workflow stream is closed or interrupted for any reason, you can resume it with the `
|
|
87
|
+
If a workflow stream is closed or interrupted for any reason, you can resume it with the `resumeStream` method. This will return a new `ReadableStream` that you can use to observe the workflow events.
|
|
94
88
|
|
|
95
89
|
```typescript showLineNumbers copy
|
|
96
|
-
const newStream = await run.
|
|
90
|
+
const newStream = await run.resumeStream();
|
|
97
91
|
|
|
98
92
|
for await (const chunk of newStream) {
|
|
99
93
|
console.log(chunk);
|
|
@@ -189,4 +189,5 @@ for await (const chunk of stream.stream) {
|
|
|
189
189
|
|
|
190
190
|
- [Control Flow](/docs/v1/workflows/control-flow)
|
|
191
191
|
- [Suspend & Resume](/docs/v1/workflows/suspend-and-resume)
|
|
192
|
+
- [Time Travel](/docs/v1/workflows/time-travel)
|
|
192
193
|
- [Human-in-the-loop](/docs/v1/workflows/human-in-the-loop)
|
|
@@ -11,7 +11,7 @@ Some workflows need to pause for human input before continuing. When a workflow
|
|
|
11
11
|
|
|
12
12
|
Human-in-the-loop input works much like [pausing a workflow](/docs/v1/workflows/suspend-and-resume) using `suspend()`. The key difference is that when human input is required, you can return `suspend()` with a payload that provides context or guidance to the user on how to continue.
|
|
13
13
|
|
|
14
|
-
](/img/workflows/workflows-suspend.jpg)
|
|
15
15
|
|
|
16
16
|
```typescript {12-17,22-26} title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
17
17
|
import { createWorkflow, createStep } from "@mastra/core/workflows";
|
|
@@ -66,7 +66,7 @@ When a workflow is suspended, you can access the payload returned by `suspend()`
|
|
|
66
66
|
|
|
67
67
|
```typescript {12} title="src/test-workflow.ts" showLineNumbers copy
|
|
68
68
|
const workflow = mastra.getWorkflow("testWorkflow");
|
|
69
|
-
const run = await workflow.
|
|
69
|
+
const run = await workflow.createRun();
|
|
70
70
|
|
|
71
71
|
const result = await run.start({
|
|
72
72
|
inputData: {
|
|
@@ -93,11 +93,11 @@ The data returned by the step can include a reason and help the user understand
|
|
|
93
93
|
|
|
94
94
|
As with [restarting a workflow](/docs/v1/workflows/suspend-and-resume#restarting-a-workflow-with-resume), use `resume()` with `resumeData` to continue a workflow after receiving input from a human. The workflow resumes from the step where it was paused.
|
|
95
95
|
|
|
96
|
-
](/img/workflows/workflows-resume.jpg)
|
|
97
97
|
|
|
98
98
|
```typescript {13} showLineNumbers copy
|
|
99
99
|
const workflow = mastra.getWorkflow("testWorkflow");
|
|
100
|
-
const run = await workflow.
|
|
100
|
+
const run = await workflow.createRun();
|
|
101
101
|
|
|
102
102
|
await run.start({
|
|
103
103
|
inputData: {
|
|
@@ -90,60 +90,28 @@ Workflows can be composed using a number of different methods. The method you ch
|
|
|
90
90
|
|
|
91
91
|
## Workflow state
|
|
92
92
|
|
|
93
|
-
Workflow state lets you share values across steps without passing them through every step
|
|
93
|
+
Workflow state lets you share values across steps without passing them through every step's inputSchema and outputSchema. Use state for tracking progress, accumulating results, or sharing configuration across the entire workflow.
|
|
94
94
|
|
|
95
95
|
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
96
96
|
const step1 = createStep({
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}),
|
|
97
|
+
id: "step-1",
|
|
98
|
+
inputSchema: z.object({ message: z.string() }),
|
|
99
|
+
outputSchema: z.object({ formatted: z.string() }),
|
|
100
|
+
stateSchema: z.object({ counter: z.number() }),
|
|
101
101
|
execute: async ({ inputData, state, setState }) => {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
setState({
|
|
106
|
-
...state,
|
|
107
|
-
processedItems: [...processedItems, "item-1", "item-2"],
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
return {
|
|
111
|
-
formatted: message.toUpperCase(),
|
|
112
|
-
};
|
|
113
|
-
},
|
|
114
|
-
});
|
|
102
|
+
// Read from state
|
|
103
|
+
console.log(state.counter);
|
|
115
104
|
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
stateSchema: z.object({
|
|
119
|
-
metadata: z.object({
|
|
120
|
-
processedBy: z.string(),
|
|
121
|
-
}),
|
|
122
|
-
}),
|
|
123
|
-
execute: async ({ inputData, state }) => {
|
|
124
|
-
const { formatted } = inputData;
|
|
125
|
-
const { metadata } = state;
|
|
105
|
+
// Update state for subsequent steps
|
|
106
|
+
setState({ ...state, counter: state.counter + 1 });
|
|
126
107
|
|
|
127
|
-
return {
|
|
128
|
-
emphasized: `${formatted}!! ${metadata.processedBy}`,
|
|
129
|
-
};
|
|
108
|
+
return { formatted: inputData.message.toUpperCase() };
|
|
130
109
|
},
|
|
131
110
|
});
|
|
132
|
-
|
|
133
|
-
export const testWorkflow = createWorkflow({
|
|
134
|
-
// ...
|
|
135
|
-
stateSchema: z.object({
|
|
136
|
-
processedItems: z.array(z.string()),
|
|
137
|
-
metadata: z.object({
|
|
138
|
-
processedBy: z.string(),
|
|
139
|
-
}),
|
|
140
|
-
}),
|
|
141
|
-
})
|
|
142
|
-
.then(step1)
|
|
143
|
-
.then(step2)
|
|
144
|
-
.commit();
|
|
145
111
|
```
|
|
146
112
|
|
|
113
|
+
> See [Workflow State](/docs/v1/workflows/workflow-state) for complete documentation on state schemas, initial state, persistence across suspend/resume, and nested workflows.
|
|
114
|
+
|
|
147
115
|
## Workflows as steps
|
|
148
116
|
|
|
149
117
|
Use a workflow as a step to reuse its logic within a larger composition. Input and output follow the same schema rules described in [Core principles](/docs/v1/workflows/control-flow).
|
|
@@ -302,6 +270,49 @@ The workflow output includes the full execution lifecycle, showing the input and
|
|
|
302
270
|
}
|
|
303
271
|
```
|
|
304
272
|
|
|
273
|
+
## Restarting active workflow runs
|
|
274
|
+
|
|
275
|
+
When a workflow run loses connection to the server, it can be restarted from the last active step. This is useful for long-running workflows that might still be running when the server loses connection. Restarting a workflow run will resume execution from the last active step, and the workflow will continue from there.
|
|
276
|
+
|
|
277
|
+
### Restarting all active workflow runs of a workflow with `restartAllActiveWorkflowRuns()`
|
|
278
|
+
|
|
279
|
+
Use `restartAllActiveWorkflowRuns()` to restart all active workflow runs of a workflow. This helps restart all active workflow runs of a workflow, without having to manually loop through each run and restart.
|
|
280
|
+
|
|
281
|
+
```typescript showLineNumbers copy
|
|
282
|
+
workflow.restartAllActiveWorkflowRuns();
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### Restarting an active workflow run with `restart()`
|
|
286
|
+
|
|
287
|
+
Use `restart()` to restart an active workflow run from the last active step. This will resume execution from the last active step, and the workflow will continue from there.
|
|
288
|
+
|
|
289
|
+
```typescript showLineNumbers copy
|
|
290
|
+
const run = await workflow.createRun();
|
|
291
|
+
|
|
292
|
+
const result = await run.start({ inputData: { value: "initial data" } });
|
|
293
|
+
|
|
294
|
+
//.. server connection lost,
|
|
295
|
+
|
|
296
|
+
const restartedResult = await run.restart();
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
### Identifying active workflow runs
|
|
300
|
+
|
|
301
|
+
When a workflow run is active, it will have a `status` of `running` or `waiting`. You can check the workflow's `status` to confirm it's active, and use `active` to identify the active workflow run.
|
|
302
|
+
|
|
303
|
+
```typescript showLineNumbers copy
|
|
304
|
+
const activeRuns = await workflow.listActiveWorkflowRuns();
|
|
305
|
+
if (activeRuns.runs.length > 0) {
|
|
306
|
+
console.log(activeRuns.runs);
|
|
307
|
+
}
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
:::Note
|
|
311
|
+
|
|
312
|
+
When running the local mastra server, all active workflow runs will be restarted automatically when the server starts.
|
|
313
|
+
|
|
314
|
+
:::
|
|
315
|
+
|
|
305
316
|
## Using `RequestContext`
|
|
306
317
|
|
|
307
318
|
Use [RequestContext](/docs/v1/server-db/request-context) to access request-specific values. This lets you conditionally adjust behavior based on the context of the request.
|
|
@@ -335,6 +346,7 @@ Use [Studio](/docs/v1/getting-started/studio) to easily run workflows with diffe
|
|
|
335
346
|
|
|
336
347
|
For a closer look at workflows, see our [Workflow Guide](/guides/v1/guide/ai-recruiter), which walks through the core concepts with a practical example.
|
|
337
348
|
|
|
349
|
+
- [Workflow State](/docs/v1/workflows/workflow-state)
|
|
338
350
|
- [Control Flow](/docs/v1/workflows/control-flow)
|
|
339
351
|
- [Suspend & Resume](/docs/v1/workflows/suspend-and-resume)
|
|
340
352
|
- [Error Handling](/docs/v1/workflows/error-handling)
|
|
@@ -268,4 +268,5 @@ if (result.status === "suspended") {
|
|
|
268
268
|
|
|
269
269
|
- [Control Flow](/docs/v1/workflows/control-flow)
|
|
270
270
|
- [Suspend & Resume](/docs/v1/workflows/suspend-and-resume)
|
|
271
|
+
- [Time Travel](/docs/v1/workflows/time-travel)
|
|
271
272
|
- [Human-in-the-loop](/docs/v1/workflows/human-in-the-loop)
|