@mastra/mcp-docs-server 1.0.0-beta.10 → 1.0.0-beta.13
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 +50 -50
- package/.docs/organized/changelogs/%40mastra%2Fchroma.md +10 -10
- package/.docs/organized/changelogs/%40mastra%2Fclickhouse.md +45 -45
- package/.docs/organized/changelogs/%40mastra%2Fclient-js.md +109 -109
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare-d1.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Fcloudflare.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Fconvex.md +38 -0
- package/.docs/organized/changelogs/%40mastra%2Fcore.md +264 -264
- package/.docs/organized/changelogs/%40mastra%2Fdeployer-cloud.md +25 -25
- package/.docs/organized/changelogs/%40mastra%2Fdeployer.md +37 -37
- package/.docs/organized/changelogs/%40mastra%2Fdynamodb.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Ffastembed.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Flance.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Flibsql.md +45 -45
- package/.docs/organized/changelogs/%40mastra%2Fmcp-docs-server.md +22 -22
- package/.docs/organized/changelogs/%40mastra%2Fmemory.md +13 -13
- package/.docs/organized/changelogs/%40mastra%2Fmongodb.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Fmssql.md +39 -39
- package/.docs/organized/changelogs/%40mastra%2Fpg.md +45 -45
- package/.docs/organized/changelogs/%40mastra%2Fplayground-ui.md +104 -104
- package/.docs/organized/changelogs/%40mastra%2Freact.md +66 -0
- package/.docs/organized/changelogs/%40mastra%2Fschema-compat.md +6 -0
- package/.docs/organized/changelogs/%40mastra%2Fserver.md +59 -59
- package/.docs/organized/changelogs/%40mastra%2Fupstash.md +39 -39
- package/.docs/organized/changelogs/create-mastra.md +31 -31
- package/.docs/organized/changelogs/mastra.md +49 -49
- package/.docs/organized/code-examples/quick-start.md +0 -4
- package/.docs/organized/code-examples/stock-price-tool.md +21 -2
- package/.docs/raw/agents/agent-approval.mdx +136 -2
- package/.docs/raw/agents/agent-memory.mdx +4 -4
- package/.docs/raw/agents/guardrails.mdx +44 -7
- package/.docs/raw/agents/networks.mdx +1 -1
- package/.docs/raw/agents/overview.mdx +2 -2
- package/.docs/raw/agents/processors.mdx +151 -0
- package/.docs/raw/agents/using-tools.mdx +1 -1
- package/.docs/raw/course/01-first-agent/07-creating-your-agent.md +1 -2
- package/.docs/raw/course/01-first-agent/12-connecting-tool-to-agent.md +1 -1
- package/.docs/raw/course/01-first-agent/16-adding-memory-to-agent.md +1 -2
- package/.docs/raw/course/02-agent-tools-mcp/05-updating-your-agent.md +1 -1
- package/.docs/raw/course/02-agent-tools-mcp/10-updating-agent-instructions-zapier.md +1 -1
- package/.docs/raw/course/02-agent-tools-mcp/16-updating-agent-instructions-github.md +1 -1
- package/.docs/raw/course/02-agent-tools-mcp/21-updating-agent-instructions-hackernews.md +1 -1
- package/.docs/raw/course/02-agent-tools-mcp/27-updating-agent-instructions-filesystem.md +1 -1
- package/.docs/raw/course/02-agent-tools-mcp/31-enhancing-memory-configuration.md +2 -2
- package/.docs/raw/course/03-agent-memory/04-creating-basic-memory-agent.md +1 -2
- package/.docs/raw/course/03-agent-memory/08-configuring-conversation-history.md +1 -2
- package/.docs/raw/course/03-agent-memory/16-configuring-semantic-recall.md +3 -4
- package/.docs/raw/course/03-agent-memory/21-configuring-working-memory.md +2 -3
- package/.docs/raw/course/03-agent-memory/22-custom-working-memory-templates.md +2 -3
- package/.docs/raw/course/03-agent-memory/25-combining-memory-features.md +1 -2
- package/.docs/raw/course/03-agent-memory/27-creating-learning-assistant.md +2 -3
- package/.docs/raw/course/04-workflows/11-creating-an-ai-agent.md +2 -3
- package/.docs/raw/deployment/cloud-providers.mdx +20 -0
- package/.docs/raw/deployment/{building-mastra.mdx → mastra-server.mdx} +2 -2
- package/.docs/raw/deployment/monorepo.mdx +23 -44
- package/.docs/raw/deployment/overview.mdx +28 -53
- package/.docs/raw/deployment/web-framework.mdx +12 -14
- package/.docs/raw/getting-started/mcp-docs-server.mdx +57 -0
- package/.docs/raw/getting-started/start.mdx +10 -1
- package/.docs/raw/getting-started/studio.mdx +25 -2
- package/.docs/raw/guides/build-your-ui/ai-sdk-ui.mdx +1021 -67
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/aws-lambda.mdx +3 -6
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/azure-app-services.mdx +4 -6
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/cloudflare-deployer.mdx +4 -0
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/digital-ocean.mdx +3 -6
- package/.docs/raw/guides/deployment/index.mdx +32 -0
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/netlify-deployer.mdx +4 -0
- package/.docs/raw/{deployment/cloud-providers → guides/deployment}/vercel-deployer.mdx +4 -0
- package/.docs/raw/guides/getting-started/express.mdx +71 -152
- package/.docs/raw/guides/getting-started/hono.mdx +227 -0
- package/.docs/raw/guides/getting-started/next-js.mdx +173 -63
- package/.docs/raw/guides/getting-started/vite-react.mdx +307 -137
- package/.docs/raw/guides/guide/research-assistant.mdx +4 -4
- package/.docs/raw/guides/migrations/upgrade-to-v1/agent.mdx +70 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/client.mdx +17 -0
- package/.docs/raw/guides/migrations/upgrade-to-v1/overview.mdx +6 -0
- package/.docs/raw/index.mdx +1 -1
- package/.docs/raw/{deployment/mastra-cloud → mastra-cloud}/dashboard.mdx +2 -6
- package/.docs/raw/{deployment/mastra-cloud → mastra-cloud}/observability.mdx +1 -5
- package/.docs/raw/{deployment/mastra-cloud → mastra-cloud}/overview.mdx +2 -6
- package/.docs/raw/{deployment/mastra-cloud → mastra-cloud}/setting-up.mdx +3 -6
- package/.docs/raw/memory/overview.mdx +1 -1
- package/.docs/raw/memory/storage/memory-with-libsql.mdx +1 -1
- package/.docs/raw/memory/storage/memory-with-mongodb.mdx +1 -1
- package/.docs/raw/memory/storage/memory-with-pg.mdx +1 -1
- package/.docs/raw/memory/storage/memory-with-upstash.mdx +1 -1
- package/.docs/raw/{server-db/storage.mdx → memory/storage/overview.mdx} +2 -2
- package/.docs/raw/observability/logging.mdx +1 -1
- package/.docs/raw/observability/tracing/exporters/cloud.mdx +1 -1
- package/.docs/raw/observability/tracing/exporters/default.mdx +1 -1
- package/.docs/raw/rag/chunking-and-embedding.mdx +12 -25
- package/.docs/raw/rag/graph-rag.mdx +220 -0
- package/.docs/raw/rag/overview.mdx +1 -2
- package/.docs/raw/rag/retrieval.mdx +13 -29
- package/.docs/raw/rag/vector-databases.mdx +7 -3
- package/.docs/raw/reference/agents/agent.mdx +11 -4
- package/.docs/raw/reference/agents/getDefaultGenerateOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getDefaultStreamOptions.mdx +1 -1
- package/.docs/raw/reference/agents/getInstructions.mdx +1 -1
- package/.docs/raw/reference/agents/getLLM.mdx +1 -1
- package/.docs/raw/reference/agents/getMemory.mdx +1 -1
- package/.docs/raw/reference/agents/getModel.mdx +1 -1
- package/.docs/raw/reference/agents/listScorers.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/chat-route.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/handle-chat-stream.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/handle-network-stream.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/handle-workflow-stream.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/network-route.mdx +1 -1
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-v4-messages.mdx +127 -0
- package/.docs/raw/reference/ai-sdk/to-ai-sdk-v5-messages.mdx +107 -0
- package/.docs/raw/reference/ai-sdk/workflow-route.mdx +1 -1
- package/.docs/raw/reference/auth/auth0.mdx +1 -1
- package/.docs/raw/reference/auth/clerk.mdx +1 -1
- package/.docs/raw/reference/auth/firebase.mdx +1 -1
- package/.docs/raw/reference/auth/jwt.mdx +1 -1
- package/.docs/raw/reference/auth/supabase.mdx +1 -1
- package/.docs/raw/reference/auth/workos.mdx +1 -1
- package/.docs/raw/reference/cli/mastra.mdx +1 -1
- package/.docs/raw/reference/client-js/mastra-client.mdx +1 -1
- package/.docs/raw/reference/client-js/workflows.mdx +20 -0
- package/.docs/raw/reference/core/getServer.mdx +3 -3
- package/.docs/raw/reference/core/getStorage.mdx +1 -1
- package/.docs/raw/reference/core/getStoredAgentById.mdx +1 -1
- package/.docs/raw/reference/core/listStoredAgents.mdx +1 -1
- package/.docs/raw/reference/core/setStorage.mdx +1 -1
- package/.docs/raw/reference/logging/pino-logger.mdx +1 -1
- package/.docs/raw/reference/processors/processor-interface.mdx +314 -13
- package/.docs/raw/reference/rag/database-config.mdx +1 -1
- package/.docs/raw/reference/server/create-route.mdx +1 -1
- package/.docs/raw/reference/server/express-adapter.mdx +4 -4
- package/.docs/raw/reference/server/hono-adapter.mdx +4 -4
- package/.docs/raw/reference/server/mastra-server.mdx +2 -2
- package/.docs/raw/reference/server/routes.mdx +28 -1
- package/.docs/raw/reference/streaming/ChunkType.mdx +23 -2
- package/.docs/raw/reference/streaming/agents/stream.mdx +38 -29
- package/.docs/raw/reference/streaming/workflows/stream.mdx +33 -20
- package/.docs/raw/reference/tools/create-tool.mdx +23 -1
- package/.docs/raw/reference/tools/graph-rag-tool.mdx +3 -3
- package/.docs/raw/reference/tools/vector-query-tool.mdx +3 -3
- package/.docs/raw/reference/workflows/run-methods/startAsync.mdx +143 -0
- package/.docs/raw/reference/workflows/workflow-methods/create-run.mdx +35 -0
- package/.docs/raw/reference/workflows/workflow-methods/foreach.mdx +68 -3
- package/.docs/raw/reference/workflows/workflow.mdx +37 -0
- package/.docs/raw/{auth → server/auth}/auth0.mdx +1 -1
- package/.docs/raw/{auth → server/auth}/clerk.mdx +1 -1
- package/.docs/raw/{auth → server/auth}/firebase.mdx +1 -1
- package/.docs/raw/{auth → server/auth}/index.mdx +6 -6
- package/.docs/raw/{auth → server/auth}/jwt.mdx +1 -1
- package/.docs/raw/{auth → server/auth}/supabase.mdx +1 -1
- package/.docs/raw/{auth → server/auth}/workos.mdx +1 -1
- package/.docs/raw/{server-db → server}/custom-adapters.mdx +3 -3
- package/.docs/raw/{server-db → server}/custom-api-routes.mdx +1 -1
- package/.docs/raw/{server-db → server}/mastra-client.mdx +2 -2
- package/.docs/raw/{server-db → server}/mastra-server.mdx +12 -10
- package/.docs/raw/{server-db → server}/middleware.mdx +2 -2
- package/.docs/raw/{server-db → server}/request-context.mdx +3 -3
- package/.docs/raw/{server-db → server}/server-adapters.mdx +6 -6
- package/.docs/raw/tools-mcp/overview.mdx +2 -2
- package/.docs/raw/workflows/control-flow.mdx +348 -2
- package/.docs/raw/workflows/error-handling.mdx +162 -1
- package/.docs/raw/workflows/overview.mdx +2 -2
- package/CHANGELOG.md +21 -0
- package/package.json +5 -5
- package/.docs/organized/changelogs/%40internal%2Fai-sdk-v4.md +0 -1
- package/.docs/raw/deployment/cloud-providers/index.mdx +0 -55
- /package/.docs/raw/{deployment/cloud-providers → guides/deployment}/amazon-ec2.mdx +0 -0
|
@@ -11,9 +11,9 @@ You can start with simple shared secret JWT authentication and switch to provide
|
|
|
11
11
|
|
|
12
12
|
## Available providers
|
|
13
13
|
|
|
14
|
-
- [JSON Web Token (JWT)](/docs/v1/auth/jwt)
|
|
15
|
-
- [Clerk](/docs/v1/auth/clerk)
|
|
16
|
-
- [Supabase](/docs/v1/auth/supabase)
|
|
17
|
-
- [Firebase](/docs/v1/auth/firebase)
|
|
18
|
-
- [WorkOS](/docs/v1/auth/workos)
|
|
19
|
-
- [Auth0](/docs/v1/auth/auth0)
|
|
14
|
+
- [JSON Web Token (JWT)](/docs/v1/server/auth/jwt)
|
|
15
|
+
- [Clerk](/docs/v1/server/auth/clerk)
|
|
16
|
+
- [Supabase](/docs/v1/server/auth/supabase)
|
|
17
|
+
- [Firebase](/docs/v1/server/auth/firebase)
|
|
18
|
+
- [WorkOS](/docs/v1/server/auth/workos)
|
|
19
|
+
- [Auth0](/docs/v1/server/auth/auth0)
|
|
@@ -51,7 +51,7 @@ export const mastraClient = new MastraClient({
|
|
|
51
51
|
});
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
> See [Mastra Client SDK](/docs/v1/server
|
|
54
|
+
> See [Mastra Client SDK](/docs/v1/server/mastra-client) for more configuration options.
|
|
55
55
|
|
|
56
56
|
### Making authenticated requests
|
|
57
57
|
|
|
@@ -90,7 +90,7 @@ export const mastraClient = new MastraClient({
|
|
|
90
90
|
|
|
91
91
|
> **Note:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
92
92
|
|
|
93
|
-
> See [Mastra Client SDK](/docs/v1/server
|
|
93
|
+
> See [Mastra Client SDK](/docs/v1/server/mastra-client) for more configuration options.
|
|
94
94
|
|
|
95
95
|
### Making authenticated requests
|
|
96
96
|
|
|
@@ -152,7 +152,7 @@ export const createMastraClient = (accessToken: string) => {
|
|
|
152
152
|
|
|
153
153
|
> **Note:** The access token must be prefixed with `Bearer` in the Authorization header.
|
|
154
154
|
|
|
155
|
-
> See [Mastra Client SDK](/docs/v1/server
|
|
155
|
+
> See [Mastra Client SDK](/docs/v1/server/mastra-client) for more configuration options.
|
|
156
156
|
|
|
157
157
|
### Making authenticated requests
|
|
158
158
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Custom Adapters | Server
|
|
2
|
+
title: "Custom Adapters | Server"
|
|
3
3
|
description: "Create a custom server adapter for frameworks other than Hono or Express."
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -296,7 +296,7 @@ constructor(options: {
|
|
|
296
296
|
}
|
|
297
297
|
```
|
|
298
298
|
|
|
299
|
-
See [Server Adapters](/docs/v1/server
|
|
299
|
+
See [Server Adapters](/docs/v1/server/server-adapters#constructor-options) for full documentation on each option.
|
|
300
300
|
|
|
301
301
|
## Full example
|
|
302
302
|
|
|
@@ -379,7 +379,7 @@ The existing [@mastra/hono](https://github.com/mastra-ai/mastra/blob/main/server
|
|
|
379
379
|
|
|
380
380
|
## Related
|
|
381
381
|
|
|
382
|
-
- [Server Adapters](/docs/v1/server
|
|
382
|
+
- [Server Adapters](/docs/v1/server/server-adapters) - Overview and shared concepts
|
|
383
383
|
- [Hono Adapter](/reference/v1/server/hono-adapter) - Reference implementation
|
|
384
384
|
- [Express Adapter](/reference/v1/server/express-adapter) - Reference implementation
|
|
385
385
|
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Mastra Client SDK | Server
|
|
2
|
+
title: "Mastra Client SDK | Server"
|
|
3
3
|
description: "Learn how to set up and use the Mastra Client SDK"
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -8,7 +8,7 @@ import TabItem from "@theme/TabItem";
|
|
|
8
8
|
|
|
9
9
|
# Mastra Client SDK
|
|
10
10
|
|
|
11
|
-
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](/docs/v1/server
|
|
11
|
+
The Mastra Client SDK provides a simple and type-safe interface for interacting with your [Mastra Server](/docs/v1/server/mastra-server) from your client environment.
|
|
12
12
|
|
|
13
13
|
## Prerequisites
|
|
14
14
|
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Server Configuration | Server
|
|
2
|
+
title: "Server Configuration | Server"
|
|
3
3
|
description: "Configure the Mastra server with custom settings for port, timeout, CORS, and more."
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -9,7 +9,7 @@ When deploying your Mastra application to production, it runs as an HTTP server
|
|
|
9
9
|
|
|
10
10
|
:::info
|
|
11
11
|
|
|
12
|
-
This page covers the `server` configuration options passed to the `Mastra` constructor. For running Mastra with your own HTTP server (Hono, Express, etc.), see [Server Adapters](/docs/v1/server
|
|
12
|
+
This page covers the `server` configuration options passed to the `Mastra` constructor. For running Mastra with your own HTTP server (Hono, Express, etc.), see [Server Adapters](/docs/v1/server/server-adapters).
|
|
13
13
|
|
|
14
14
|
:::
|
|
15
15
|
|
|
@@ -27,26 +27,29 @@ The server provides:
|
|
|
27
27
|
- Configuration of port
|
|
28
28
|
- Configuration of body limit
|
|
29
29
|
|
|
30
|
-
See the [Middleware](/docs/v1/server
|
|
31
|
-
[Custom API Routes](/docs/v1/server
|
|
30
|
+
See the [Middleware](/docs/v1/server/middleware) and
|
|
31
|
+
[Custom API Routes](/docs/v1/server/custom-api-routes) pages for details on
|
|
32
32
|
adding additional server behaviour.
|
|
33
33
|
|
|
34
34
|
## Server configuration
|
|
35
35
|
|
|
36
|
-
You can configure server `port` and `timeout` in the Mastra instance.
|
|
36
|
+
You can configure server `port`, `host`, `studioBase`, and `timeout` in the Mastra instance.
|
|
37
37
|
|
|
38
|
-
```typescript title="src/mastra/index.ts" copy
|
|
38
|
+
```typescript title="src/mastra/index.ts" copy
|
|
39
39
|
import { Mastra } from "@mastra/core";
|
|
40
40
|
|
|
41
41
|
export const mastra = new Mastra({
|
|
42
|
-
// ...
|
|
43
42
|
server: {
|
|
44
43
|
port: 3000, // Defaults to 4111
|
|
44
|
+
host: "0.0.0.0", // Defaults to 'localhost'
|
|
45
|
+
studioBase: "/my-mastra-studio", // Sub-path for the studio (optional)
|
|
45
46
|
timeout: 10000, // Defaults to 3 * 60 * 1000 (3 minutes)
|
|
46
47
|
},
|
|
47
48
|
});
|
|
48
49
|
```
|
|
49
50
|
|
|
51
|
+
The `studioBase` option allows you to host Mastra Studio on a sub-path of your existing application. For example, with `studioBase: "/my-mastra-studio"`, the studio will be available at `http://your-host:port/my-mastra-studio` instead of the root.
|
|
52
|
+
|
|
50
53
|
## TypeScript configuration
|
|
51
54
|
|
|
52
55
|
Mastra requires `module` and `moduleResolution` values that support modern Node.js versions. Older settings like `CommonJS` or `node` are incompatible with Mastra’s packages and will cause resolution errors.
|
|
@@ -74,11 +77,10 @@ Mastra requires `module` and `moduleResolution` values that support modern Node.
|
|
|
74
77
|
|
|
75
78
|
Mastra allows you to configure CORS (Cross-Origin Resource Sharing) settings for your server.
|
|
76
79
|
|
|
77
|
-
```typescript title="src/mastra/index.ts" copy
|
|
80
|
+
```typescript title="src/mastra/index.ts" copy
|
|
78
81
|
import { Mastra } from "@mastra/core";
|
|
79
82
|
|
|
80
83
|
export const mastra = new Mastra({
|
|
81
|
-
// ...
|
|
82
84
|
server: {
|
|
83
85
|
cors: {
|
|
84
86
|
origin: ["https://example.com"], // Allow specific origins or '*' for all
|
|
@@ -94,4 +96,4 @@ export const mastra = new Mastra({
|
|
|
94
96
|
|
|
95
97
|
When streaming agent responses, the HTTP streaming layer redacts system prompts, tool definitions, API keys, and similar data from each chunk before sending it to clients. This is enabled by default.
|
|
96
98
|
|
|
97
|
-
If you're using server adapters directly, you can configure redaction behavior. See [Stream data redaction](/docs/v1/server
|
|
99
|
+
If you're using server adapters directly, you can configure redaction behavior. See [Stream data redaction](/docs/v1/server/server-adapters#stream-data-redaction) in the Server Adapters docs.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Middleware | Server
|
|
2
|
+
title: "Middleware | Server"
|
|
3
3
|
description: "Apply custom middleware functions to intercept requests."
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -174,4 +174,4 @@ be inspected by middleware to tailor behavior:
|
|
|
174
174
|
|
|
175
175
|
# Related
|
|
176
176
|
|
|
177
|
-
- [Request Context](/docs/v1/server
|
|
177
|
+
- [Request Context](/docs/v1/server/request-context)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Request Context | Server
|
|
2
|
+
title: "Request Context | Server"
|
|
3
3
|
description: Learn how to use Mastra's RequestContext to provide dynamic, request-specific configuration to agents.
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -99,7 +99,7 @@ export const mastra = new Mastra({
|
|
|
99
99
|
});
|
|
100
100
|
```
|
|
101
101
|
|
|
102
|
-
> See [Middleware](/docs/v1/server
|
|
102
|
+
> See [Middleware](/docs/v1/server/middleware) for how to use server middleware.
|
|
103
103
|
|
|
104
104
|
## Accessing values with agents
|
|
105
105
|
|
|
@@ -190,4 +190,4 @@ export const weatherTool = createTool({
|
|
|
190
190
|
- [Agent Request Context](/docs/v1/agents/overview#using-requestcontext)
|
|
191
191
|
- [Workflow Request Context](../workflows/overview#using-requestcontext)
|
|
192
192
|
- [Tool Request Context](../mcp/overview#using-requestcontext)
|
|
193
|
-
- [Server Middleware](/docs/v1/server
|
|
193
|
+
- [Server Middleware](/docs/v1/server/middleware)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
---
|
|
2
|
-
title: "Server Adapters | Server
|
|
2
|
+
title: "Server Adapters | Server"
|
|
3
3
|
description: "Manually configure a Mastra server using Hono or Express adapters."
|
|
4
4
|
---
|
|
5
5
|
|
|
@@ -18,7 +18,7 @@ Server adapters let you run Mastra with your own HTTP server instead of the Hono
|
|
|
18
18
|
|
|
19
19
|
:::tip
|
|
20
20
|
|
|
21
|
-
For
|
|
21
|
+
For 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/mastra-server).
|
|
22
22
|
|
|
23
23
|
:::
|
|
24
24
|
|
|
@@ -29,7 +29,7 @@ Mastra currently provides two official server adapters:
|
|
|
29
29
|
- [@mastra/express](/reference/v1/server/express-adapter): Express framework adapter
|
|
30
30
|
- [@mastra/hono](/reference/v1/server/hono-adapter): Hono framework adapter
|
|
31
31
|
|
|
32
|
-
You can build your own adapter, read [Custom Adapters](/docs/v1/server
|
|
32
|
+
You can build your own adapter, read [Custom Adapters](/docs/v1/server/custom-adapters) for details.
|
|
33
33
|
|
|
34
34
|
## Installation
|
|
35
35
|
|
|
@@ -305,8 +305,8 @@ See [MCP](/docs/v1/mcp/overview) for configuration details and how to set up MCP
|
|
|
305
305
|
|
|
306
306
|
- [Hono Adapter](/reference/v1/server/hono-adapter) - Hono-specific setup
|
|
307
307
|
- [Express Adapter](/reference/v1/server/express-adapter) - Express-specific setup
|
|
308
|
-
- [Custom Adapters](/docs/v1/server
|
|
309
|
-
- [Server Configuration](/docs/v1/server
|
|
310
|
-
- [Authentication](/docs/v1/auth) - Configuring auth for your server
|
|
308
|
+
- [Custom Adapters](/docs/v1/server/custom-adapters) - Building adapters for other frameworks
|
|
309
|
+
- [Server Configuration](/docs/v1/server/mastra-server) - Using `mastra build` instead
|
|
310
|
+
- [Authentication](/docs/v1/server/auth) - Configuring auth for your server
|
|
311
311
|
- [MastraServer Reference](/reference/v1/server/mastra-server) - Full API reference
|
|
312
312
|
- [createRoute() Reference](/reference/v1/server/create-route) - Creating type-safe custom routes
|
|
@@ -56,7 +56,7 @@ To make tools available to an agent, you configure them in the agent's definitio
|
|
|
56
56
|
|
|
57
57
|
## Using `RequestContext`
|
|
58
58
|
|
|
59
|
-
Use [RequestContext](/docs/v1/server
|
|
59
|
+
Use [RequestContext](/docs/v1/server/request-context) to access request-specific values. This lets you conditionally adjust behavior based on the context of the request.
|
|
60
60
|
|
|
61
61
|
```typescript title="src/mastra/tools/test-tool.ts" showLineNumbers
|
|
62
62
|
export type UserTier = {
|
|
@@ -81,7 +81,7 @@ export const testTool = createTool({
|
|
|
81
81
|
});
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
-
> See [Request Context](/docs/v1/server
|
|
84
|
+
> See [Request Context](/docs/v1/server/request-context) for more information.
|
|
85
85
|
|
|
86
86
|
## Testing with Studio
|
|
87
87
|
|
|
@@ -59,7 +59,7 @@ export const testWorkflow = createWorkflow({
|
|
|
59
59
|
|
|
60
60
|
## Simultaneous steps with `.parallel()`
|
|
61
61
|
|
|
62
|
-
Use `.parallel()` to run steps at the same time. Each step's `id` is used when defining a following step's `inputSchema` and becomes the key on the `inputData` object used to access the previous step's values. The outputs of parallel steps can then be referenced or combined by a following step.
|
|
62
|
+
Use `.parallel()` to run steps at the same time. All parallel steps must complete before the workflow continues to the next step. Each step's `id` is used when defining a following step's `inputSchema` and becomes the key on the `inputData` object used to access the previous step's values. The outputs of parallel steps can then be referenced or combined by a following step.
|
|
63
63
|
|
|
64
64
|

|
|
65
65
|
|
|
@@ -176,6 +176,8 @@ export const testWorkflow = createWorkflow({
|
|
|
176
176
|
- The next step receives an object containing all parallel step outputs
|
|
177
177
|
- You must define the `inputSchema` of the following step to match this structure
|
|
178
178
|
|
|
179
|
+
> See [Choosing the right pattern](#choosing-the-right-pattern) to understand when to use `.parallel()` vs `.foreach()`.
|
|
180
|
+
|
|
179
181
|
## Conditional logic with `.branch()`
|
|
180
182
|
|
|
181
183
|
Use `.branch()` to choose which step to run based on a condition. All steps in a branch need the same `inputSchema` and `outputSchema` because branching requires consistent schemas so workflows can follow different paths.
|
|
@@ -428,7 +430,7 @@ export const testWorkflow = createWorkflow({
|
|
|
428
430
|
|
|
429
431
|
### Looping with `.foreach()`
|
|
430
432
|
|
|
431
|
-
Use `.foreach()` to run the same step for each item in an array. The input must be of type `array` so the loop can iterate over its values, applying the step
|
|
433
|
+
Use `.foreach()` to run the same step for each item in an array. The input must be of type `array` so the loop can iterate over its values, applying the step's logic to each one. See [Choosing the right pattern](#choosing-the-right-pattern) for guidance on when to use `.foreach()` vs other methods.
|
|
432
434
|
|
|
433
435
|

|
|
434
436
|
|
|
@@ -454,6 +456,32 @@ export const testWorkflow = createWorkflow({
|
|
|
454
456
|
.commit();
|
|
455
457
|
```
|
|
456
458
|
|
|
459
|
+
#### Output structure
|
|
460
|
+
|
|
461
|
+
The `.foreach()` method always returns an array containing the output of each iteration. The order of outputs matches the order of inputs.
|
|
462
|
+
|
|
463
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
464
|
+
const addTenStep = createStep({
|
|
465
|
+
id: "add-ten",
|
|
466
|
+
inputSchema: z.object({ value: z.number() }),
|
|
467
|
+
outputSchema: z.object({ value: z.number() }),
|
|
468
|
+
execute: async ({ inputData }) => ({
|
|
469
|
+
value: inputData.value + 10
|
|
470
|
+
})
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
export const testWorkflow = createWorkflow({
|
|
474
|
+
id: "foreach-output-example",
|
|
475
|
+
inputSchema: z.array(z.object({ value: z.number() })),
|
|
476
|
+
outputSchema: z.array(z.object({ value: z.number() }))
|
|
477
|
+
})
|
|
478
|
+
.foreach(addTenStep)
|
|
479
|
+
.commit();
|
|
480
|
+
|
|
481
|
+
// When executed with [{ value: 1 }, { value: 22 }, { value: 333 }]
|
|
482
|
+
// Output: [{ value: 11 }, { value: 32 }, { value: 343 }]
|
|
483
|
+
```
|
|
484
|
+
|
|
457
485
|
#### Concurrency limits
|
|
458
486
|
|
|
459
487
|
Use `concurrency` to control the number of array items processed at the same time. The default is `1`, which runs steps sequentially. Increasing the value allows `.foreach()` to process multiple items simultaneously.
|
|
@@ -466,6 +494,324 @@ export const testWorkflow = createWorkflow({...})
|
|
|
466
494
|
.commit();
|
|
467
495
|
```
|
|
468
496
|
|
|
497
|
+
#### Aggregating results after `.foreach()`
|
|
498
|
+
|
|
499
|
+
Since `.foreach()` outputs an array, you can use `.then()` or `.map()` to aggregate or transform the results. The step following `.foreach()` receives the entire array as its input.
|
|
500
|
+
|
|
501
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
502
|
+
const processItemStep = createStep({
|
|
503
|
+
id: "process-item",
|
|
504
|
+
inputSchema: z.object({ value: z.number() }),
|
|
505
|
+
outputSchema: z.object({ processed: z.number() }),
|
|
506
|
+
execute: async ({ inputData }) => ({
|
|
507
|
+
processed: inputData.value * 2
|
|
508
|
+
})
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
const aggregateStep = createStep({
|
|
512
|
+
id: "aggregate",
|
|
513
|
+
// Input is an array of outputs from foreach
|
|
514
|
+
inputSchema: z.array(z.object({ processed: z.number() })),
|
|
515
|
+
outputSchema: z.object({ total: z.number() }),
|
|
516
|
+
execute: async ({ inputData }) => ({
|
|
517
|
+
// Sum all processed values
|
|
518
|
+
total: inputData.reduce((sum, item) => sum + item.processed, 0)
|
|
519
|
+
})
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
export const testWorkflow = createWorkflow({
|
|
523
|
+
id: "foreach-aggregate-example",
|
|
524
|
+
inputSchema: z.array(z.object({ value: z.number() })),
|
|
525
|
+
outputSchema: z.object({ total: z.number() })
|
|
526
|
+
})
|
|
527
|
+
.foreach(processItemStep)
|
|
528
|
+
.then(aggregateStep) // Receives the full array from foreach
|
|
529
|
+
.commit();
|
|
530
|
+
|
|
531
|
+
// When executed with [{ value: 1 }, { value: 2 }, { value: 3 }]
|
|
532
|
+
// After foreach: [{ processed: 2 }, { processed: 4 }, { processed: 6 }]
|
|
533
|
+
// After aggregate: { total: 12 }
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
You can also use `.map()` to transform the array output:
|
|
537
|
+
|
|
538
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
539
|
+
export const testWorkflow = createWorkflow({...})
|
|
540
|
+
.foreach(processItemStep)
|
|
541
|
+
.map(async ({ inputData }) => ({
|
|
542
|
+
// Transform the array into a different structure
|
|
543
|
+
values: inputData.map(item => item.processed),
|
|
544
|
+
count: inputData.length
|
|
545
|
+
}))
|
|
546
|
+
.then(nextStep)
|
|
547
|
+
.commit();
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
#### Chaining multiple `.foreach()` calls
|
|
551
|
+
|
|
552
|
+
When you chain `.foreach()` calls, each operates on the array output of the previous step. This is useful when each item in your array needs to be transformed by multiple steps in sequence.
|
|
553
|
+
|
|
554
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
555
|
+
const chunkStep = createStep({
|
|
556
|
+
id: "chunk",
|
|
557
|
+
// Takes a document, returns an array of chunks
|
|
558
|
+
inputSchema: z.object({ content: z.string() }),
|
|
559
|
+
outputSchema: z.array(z.object({ chunk: z.string() })),
|
|
560
|
+
execute: async ({ inputData }) => {
|
|
561
|
+
// Split document into chunks
|
|
562
|
+
const chunks = inputData.content.match(/.{1,100}/g) || [];
|
|
563
|
+
return chunks.map(chunk => ({ chunk }));
|
|
564
|
+
}
|
|
565
|
+
});
|
|
566
|
+
|
|
567
|
+
const embedStep = createStep({
|
|
568
|
+
id: "embed",
|
|
569
|
+
// Takes a single chunk, returns embedding
|
|
570
|
+
inputSchema: z.object({ chunk: z.string() }),
|
|
571
|
+
outputSchema: z.object({ embedding: z.array(z.number()) }),
|
|
572
|
+
execute: async ({ inputData }) => ({
|
|
573
|
+
embedding: [/* vector embedding */]
|
|
574
|
+
})
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
// For a single document that produces multiple chunks:
|
|
578
|
+
export const singleDocWorkflow = createWorkflow({
|
|
579
|
+
id: "single-doc-rag",
|
|
580
|
+
inputSchema: z.object({ content: z.string() }),
|
|
581
|
+
outputSchema: z.array(z.object({ embedding: z.array(z.number()) }))
|
|
582
|
+
})
|
|
583
|
+
.then(chunkStep) // Returns array of chunks
|
|
584
|
+
.foreach(embedStep) // Process each chunk -> array of embeddings
|
|
585
|
+
.commit();
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
For processing multiple documents where each produces multiple chunks, you have options:
|
|
589
|
+
|
|
590
|
+
**Option 1: Process all documents in a single step with batching control**
|
|
591
|
+
|
|
592
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
593
|
+
const downloadAndChunkStep = createStep({
|
|
594
|
+
id: "download-and-chunk",
|
|
595
|
+
inputSchema: z.array(z.string()), // Array of URLs
|
|
596
|
+
outputSchema: z.array(z.object({ chunk: z.string(), source: z.string() })),
|
|
597
|
+
execute: async ({ inputData: urls }) => {
|
|
598
|
+
// Control batching/parallelization within the step
|
|
599
|
+
const allChunks = [];
|
|
600
|
+
for (const url of urls) {
|
|
601
|
+
const content = await fetch(url).then(r => r.text());
|
|
602
|
+
const chunks = content.match(/.{1,100}/g) || [];
|
|
603
|
+
allChunks.push(...chunks.map(chunk => ({ chunk, source: url })));
|
|
604
|
+
}
|
|
605
|
+
return allChunks;
|
|
606
|
+
}
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
export const multiDocWorkflow = createWorkflow({...})
|
|
610
|
+
.then(downloadAndChunkStep) // Returns flat array of all chunks
|
|
611
|
+
.foreach(embedStep, { concurrency: 10 }) // Embed each chunk in parallel
|
|
612
|
+
.commit();
|
|
613
|
+
```
|
|
614
|
+
|
|
615
|
+
**Option 2: Use foreach for documents, aggregate chunks, then foreach for embeddings**
|
|
616
|
+
|
|
617
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
618
|
+
const downloadStep = createStep({
|
|
619
|
+
id: "download",
|
|
620
|
+
inputSchema: z.string(), // Single URL
|
|
621
|
+
outputSchema: z.object({ content: z.string(), source: z.string() }),
|
|
622
|
+
execute: async ({ inputData: url }) => ({
|
|
623
|
+
content: await fetch(url).then(r => r.text()),
|
|
624
|
+
source: url
|
|
625
|
+
})
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
const chunkDocStep = createStep({
|
|
629
|
+
id: "chunk-doc",
|
|
630
|
+
inputSchema: z.object({ content: z.string(), source: z.string() }),
|
|
631
|
+
outputSchema: z.array(z.object({ chunk: z.string(), source: z.string() })),
|
|
632
|
+
execute: async ({ inputData }) => {
|
|
633
|
+
const chunks = inputData.content.match(/.{1,100}/g) || [];
|
|
634
|
+
return chunks.map(chunk => ({ chunk, source: inputData.source }));
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
|
|
638
|
+
export const multiDocWorkflow = createWorkflow({
|
|
639
|
+
id: "multi-doc-rag",
|
|
640
|
+
inputSchema: z.array(z.string()), // Array of URLs
|
|
641
|
+
outputSchema: z.array(z.object({ embedding: z.array(z.number()) }))
|
|
642
|
+
})
|
|
643
|
+
.foreach(downloadStep, { concurrency: 5 }) // Download docs in parallel
|
|
644
|
+
.foreach(chunkDocStep) // Chunk each doc -> array of chunk arrays
|
|
645
|
+
.map(async ({ inputData }) => {
|
|
646
|
+
// Flatten nested arrays: [[chunks], [chunks]] -> [chunks]
|
|
647
|
+
return inputData.flat();
|
|
648
|
+
})
|
|
649
|
+
.foreach(embedStep, { concurrency: 10 }) // Embed all chunks
|
|
650
|
+
.commit();
|
|
651
|
+
```
|
|
652
|
+
|
|
653
|
+
**Key points about chaining `.foreach()`:**
|
|
654
|
+
- Each `.foreach()` operates on the array from the previous step
|
|
655
|
+
- If a step inside `.foreach()` returns an array, the output becomes an array of arrays
|
|
656
|
+
- Use `.map()` with `.flat()` to flatten nested arrays when needed
|
|
657
|
+
- For complex RAG pipelines, Option 1 (handling batching in a single step) often provides better control
|
|
658
|
+
|
|
659
|
+
#### Nested workflows inside foreach
|
|
660
|
+
|
|
661
|
+
The step after `.foreach()` only executes after all iterations complete. If you need to run multiple sequential operations per item, use a nested workflow instead of chaining multiple `.foreach()` calls. This keeps all operations for each item together and makes the data flow clearer.
|
|
662
|
+
|
|
663
|
+
```typescript title="src/mastra/workflows/test-workflow.ts" showLineNumbers copy
|
|
664
|
+
// Define a workflow that processes a single document
|
|
665
|
+
const processDocumentWorkflow = createWorkflow({
|
|
666
|
+
id: "process-document",
|
|
667
|
+
inputSchema: z.object({ url: z.string() }),
|
|
668
|
+
outputSchema: z.object({
|
|
669
|
+
embeddings: z.array(z.array(z.number())),
|
|
670
|
+
metadata: z.object({ url: z.string(), chunkCount: z.number() })
|
|
671
|
+
})
|
|
672
|
+
})
|
|
673
|
+
.then(downloadStep) // Download the document
|
|
674
|
+
.then(chunkStep) // Split into chunks
|
|
675
|
+
.then(embedChunksStep) // Embed all chunks for this document
|
|
676
|
+
.then(formatResultStep) // Format the final output
|
|
677
|
+
.commit();
|
|
678
|
+
|
|
679
|
+
// Use the nested workflow inside foreach
|
|
680
|
+
export const batchProcessWorkflow = createWorkflow({
|
|
681
|
+
id: "batch-process-documents",
|
|
682
|
+
inputSchema: z.array(z.object({ url: z.string() })),
|
|
683
|
+
outputSchema: z.array(z.object({
|
|
684
|
+
embeddings: z.array(z.array(z.number())),
|
|
685
|
+
metadata: z.object({ url: z.string(), chunkCount: z.number() })
|
|
686
|
+
}))
|
|
687
|
+
})
|
|
688
|
+
.foreach(processDocumentWorkflow, { concurrency: 3 })
|
|
689
|
+
.commit();
|
|
690
|
+
|
|
691
|
+
// Each document goes through all 4 steps before the next document starts (with concurrency: 1)
|
|
692
|
+
// With concurrency: 3, up to 3 documents process their full pipelines in parallel
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
**Why use nested workflows:**
|
|
696
|
+
- **Better parallelism**: With `concurrency: N`, multiple items run their full pipelines simultaneously. Chained `.foreach().foreach()` processes all items through step 1, waits, then all through step 2 - nested workflows let each item progress independently
|
|
697
|
+
- All steps for one item complete together before results are collected
|
|
698
|
+
- Cleaner than multiple `.foreach()` calls which create nested arrays
|
|
699
|
+
- Each nested workflow execution is independent with its own data flow
|
|
700
|
+
- Easier to test and reuse the per-item logic separately
|
|
701
|
+
|
|
702
|
+
**How it works:**
|
|
703
|
+
1. The parent workflow passes each array item to an instance of the nested workflow
|
|
704
|
+
2. Each nested workflow runs its full step sequence for that item
|
|
705
|
+
3. With `concurrency > 1`, multiple nested workflows execute in parallel
|
|
706
|
+
4. The nested workflow's final output becomes one element in the result array
|
|
707
|
+
5. After all nested workflows complete, the next step in the parent receives the full array
|
|
708
|
+
|
|
709
|
+
## Choosing the right pattern
|
|
710
|
+
|
|
711
|
+
Use this section as a reference for selecting the appropriate control flow method.
|
|
712
|
+
|
|
713
|
+
### Quick reference
|
|
714
|
+
|
|
715
|
+
| Method | Purpose | Input | Output | Concurrency |
|
|
716
|
+
|--------|---------|-------|--------|-------------|
|
|
717
|
+
| `.then(step)` | Sequential processing | `T` | `U` | N/A (one at a time) |
|
|
718
|
+
| `.parallel([a, b])` | Different operations on same input | `T` | `{ a: U, b: V }` | All run simultaneously |
|
|
719
|
+
| `.foreach(step)` | Same operation on each array item | `T[]` | `U[]` | Configurable (default: 1) |
|
|
720
|
+
| `.branch([...])` | Conditional path selection | `T` | `{ selectedStep: U }` | Only one branch runs |
|
|
721
|
+
|
|
722
|
+
### `.parallel()` vs `.foreach()`
|
|
723
|
+
|
|
724
|
+
**Use `.parallel()` when you have one input that needs different processing:**
|
|
725
|
+
|
|
726
|
+
```typescript
|
|
727
|
+
// Same user data processed differently in parallel
|
|
728
|
+
workflow
|
|
729
|
+
.parallel([validateStep, enrichStep, scoreStep])
|
|
730
|
+
.then(combineResultsStep)
|
|
731
|
+
```
|
|
732
|
+
|
|
733
|
+
**Use `.foreach()` when you have many inputs that need the same processing:**
|
|
734
|
+
|
|
735
|
+
```typescript
|
|
736
|
+
// Multiple URLs each processed the same way
|
|
737
|
+
workflow
|
|
738
|
+
.foreach(downloadStep, { concurrency: 5 })
|
|
739
|
+
.then(aggregateStep)
|
|
740
|
+
```
|
|
741
|
+
|
|
742
|
+
### When to use nested workflows
|
|
743
|
+
|
|
744
|
+
**Inside `.foreach()`** - when each array item needs multiple sequential steps:
|
|
745
|
+
|
|
746
|
+
```typescript
|
|
747
|
+
// Each document goes through a full pipeline
|
|
748
|
+
const processDocWorkflow = createWorkflow({...})
|
|
749
|
+
.then(downloadStep)
|
|
750
|
+
.then(parseStep)
|
|
751
|
+
.then(embedStep)
|
|
752
|
+
.commit();
|
|
753
|
+
|
|
754
|
+
workflow.foreach(processDocWorkflow, { concurrency: 3 })
|
|
755
|
+
```
|
|
756
|
+
|
|
757
|
+
This is cleaner than chaining `.foreach().foreach()`, which creates nested arrays.
|
|
758
|
+
|
|
759
|
+
**Inside `.parallel()`** - when a parallel branch needs its own multi-step pipeline:
|
|
760
|
+
|
|
761
|
+
```typescript
|
|
762
|
+
const pipelineA = createWorkflow({...}).then(step1).then(step2).commit();
|
|
763
|
+
const pipelineB = createWorkflow({...}).then(step3).then(step4).commit();
|
|
764
|
+
|
|
765
|
+
workflow.parallel([pipelineA, pipelineB])
|
|
766
|
+
```
|
|
767
|
+
|
|
768
|
+
### Chaining patterns
|
|
769
|
+
|
|
770
|
+
| Pattern | What happens | Common use case |
|
|
771
|
+
|---------|--------------|-----------------|
|
|
772
|
+
| `.then().then()` | Sequential steps | Simple pipelines |
|
|
773
|
+
| `.parallel().then()` | Run in parallel, then combine | Fan-out/fan-in |
|
|
774
|
+
| `.foreach().then()` | Process all items, then aggregate | Map-reduce |
|
|
775
|
+
| `.foreach().foreach()` | Creates array of arrays | Avoid - use nested workflow or `.map()` with `.flat()` |
|
|
776
|
+
| `.foreach(workflow)` | Full pipeline per item | Multi-step processing per array item |
|
|
777
|
+
|
|
778
|
+
### Synchronization: when does the next step run?
|
|
779
|
+
|
|
780
|
+
Both `.parallel()` and `.foreach()` are synchronization points. The next step in the workflow only executes after all parallel branches or all array iterations have completed.
|
|
781
|
+
|
|
782
|
+
```typescript
|
|
783
|
+
workflow
|
|
784
|
+
.parallel([stepA, stepB, stepC]) // All 3 run simultaneously
|
|
785
|
+
.then(combineStep) // Waits for ALL 3 to finish before running
|
|
786
|
+
.commit();
|
|
787
|
+
|
|
788
|
+
workflow
|
|
789
|
+
.foreach(processStep, { concurrency: 5 }) // Up to 5 items process at once
|
|
790
|
+
.then(aggregateStep) // Waits for ALL items to finish before running
|
|
791
|
+
.commit();
|
|
792
|
+
```
|
|
793
|
+
|
|
794
|
+
This means:
|
|
795
|
+
- `.parallel()` collects all branch outputs into an object, then passes it to the next step
|
|
796
|
+
- `.foreach()` collects all iteration outputs into an array, then passes it to the next step
|
|
797
|
+
- There is no way to "stream" results to the next step as they complete
|
|
798
|
+
|
|
799
|
+
### Concurrency behavior
|
|
800
|
+
|
|
801
|
+
| Method | Behavior |
|
|
802
|
+
|--------|----------|
|
|
803
|
+
| `.then()` | Sequential - one step at a time |
|
|
804
|
+
| `.parallel()` | All branches run simultaneously (no limit option) |
|
|
805
|
+
| `.foreach()` | Controlled via `{ concurrency: N }` - default is 1 (sequential) |
|
|
806
|
+
| Nested workflow in `.foreach()` | Respects parent's concurrency setting |
|
|
807
|
+
|
|
808
|
+
**Performance tip:** For I/O-bound operations in `.foreach()`, increase concurrency to process items in parallel:
|
|
809
|
+
|
|
810
|
+
```typescript
|
|
811
|
+
// Process up to 10 items simultaneously
|
|
812
|
+
workflow.foreach(fetchDataStep, { concurrency: 10 })
|
|
813
|
+
```
|
|
814
|
+
|
|
469
815
|
## Loop management
|
|
470
816
|
|
|
471
817
|
Loop conditions can be implemented in different ways depending on how you want the loop to end. Common patterns include checking values returned in `inputData`, setting a maximum number of iterations, or aborting execution when a limit is reached.
|