@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
|
@@ -53,6 +53,17 @@ yarn create mastra@beta
|
|
|
53
53
|
bun create mastra@beta
|
|
54
54
|
```
|
|
55
55
|
|
|
56
|
+
:::warning
|
|
57
|
+
|
|
58
|
+
Due to a [known issue with Bun](https://github.com/oven-sh/bun/issues/25314) you'll need to run these steps after creating the project:
|
|
59
|
+
|
|
60
|
+
- `bun add @mastra/server@beta`
|
|
61
|
+
- Delete your `node_modules` folder
|
|
62
|
+
- Delete your `bun.lock` file
|
|
63
|
+
- Run `bun install` to reinstall your packages
|
|
64
|
+
|
|
65
|
+
:::
|
|
66
|
+
|
|
56
67
|
</TabItem>
|
|
57
68
|
</Tabs>
|
|
58
69
|
|
|
@@ -177,7 +177,20 @@ To migrate, update any code that consumes branch outputs to handle optional valu
|
|
|
177
177
|
|
|
178
178
|
If your code depends on non-optional types, add runtime checks or provide default values when accessing branch outputs.
|
|
179
179
|
|
|
180
|
-
|
|
180
|
+
### `setState` is now async and the data passed is validated
|
|
181
|
+
|
|
182
|
+
The `setState` function is now async. The data passed is now validated against the `stateSchema` defined in the step.
|
|
183
|
+
The state data validation also uses the `validateInputs` flag to determine whether to validate the state data or not.
|
|
184
|
+
Also, when calling `setState`, you can now pass only the state data being updated, instead of adding the previous state spread (`...state`).
|
|
185
|
+
|
|
186
|
+
To migrate, update the `setState` function to be async.
|
|
187
|
+
|
|
188
|
+
```diff
|
|
189
|
+
- setState({ ...state, sharedCounter: state.sharedCounter + 1 });
|
|
190
|
+
+ await setState({ sharedCounter: state.sharedCounter + 1 });
|
|
191
|
+
+ // await setState({ ...state, sharedCounter: state.sharedCounter + 1 });
|
|
192
|
+
+ // this also works, as the previous state spread remains supported
|
|
193
|
+
```
|
|
181
194
|
|
|
182
195
|
## Removed
|
|
183
196
|
|
|
@@ -189,6 +202,32 @@ To migrate, use the standard `stream()`, `resumeStream()`, and `observeStream()`
|
|
|
189
202
|
|
|
190
203
|
See [`Run.stream()`](/reference/v1/streaming/workflows/stream), [`Run.resumeStream()`](/reference/v1/streaming/workflows/resumeStream), and [`Run.observeStream()`](/reference/v1/streaming/workflows/observeStream) for details.
|
|
191
204
|
|
|
205
|
+
:::tip[Codemod]
|
|
206
|
+
|
|
207
|
+
You can use Mastra's codemod CLI to update your code automatically:
|
|
208
|
+
|
|
209
|
+
```shell
|
|
210
|
+
npx @mastra/codemod@beta v1/workflow-stream-vnext .
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
:::
|
|
214
|
+
|
|
215
|
+
### `suspend` and `setState` are not available in step condition functions parameters
|
|
216
|
+
|
|
217
|
+
The `suspend` and `setState` functions are not available in step condition functions parameters.
|
|
218
|
+
|
|
219
|
+
To migrate, use the `suspend` function in the step execute function instead.
|
|
220
|
+
|
|
221
|
+
```diff
|
|
222
|
+
.dowhile(step, async ({ suspend, state, setState }) => {
|
|
223
|
+
- setState({...state, updatedState: "updated state"})
|
|
224
|
+
- await suspend({ reason: "Suspension reason" });
|
|
225
|
+
+ // Use the suspend/setState in the step execute function instead
|
|
226
|
+
});
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
This is the same for `dountil` and `branch` condition functions parameters.
|
|
230
|
+
|
|
192
231
|
### Legacy workflows export
|
|
193
232
|
|
|
194
233
|
The `./workflows/legacy` export path has been removed from `@mastra/core`. Legacy workflows are no longer supported.
|
|
@@ -131,6 +131,7 @@ Resource-scoped working memory requires specific storage adapters that support t
|
|
|
131
131
|
- **LibSQL** (`@mastra/libsql`)
|
|
132
132
|
- **PostgreSQL** (`@mastra/pg`)
|
|
133
133
|
- **Upstash** (`@mastra/upstash`)
|
|
134
|
+
- **MongoDB** (`@mastra/mongodb`)
|
|
134
135
|
|
|
135
136
|
## Custom Templates
|
|
136
137
|
|
|
@@ -129,6 +129,12 @@ Use the `--import` flag to ensure instrumentation loads before your application:
|
|
|
129
129
|
tsx --import ./instrumentation.ts ./src/index.ts
|
|
130
130
|
```
|
|
131
131
|
|
|
132
|
+
## Semantic Conventions
|
|
133
|
+
|
|
134
|
+
The OtelBridge exports Mastra spans using [OpenTelemetry Semantic Conventions for GenAI v1.38.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.38.0/docs/gen-ai). This includes standardized span names (`chat {model}`, `execute_tool {tool_name}`, etc.) and attributes (`gen_ai.usage.input_tokens`, `gen_ai.request.model`, etc.).
|
|
135
|
+
|
|
136
|
+
For details on span naming and attributes, see the [OpenTelemetry Exporter semantic conventions](/docs/v1/observability/tracing/exporters/otel#opentelemetry-semantic-conventions).
|
|
137
|
+
|
|
132
138
|
## Trace Hierarchy
|
|
133
139
|
|
|
134
140
|
With the OtelBridge, your traces maintain proper hierarchy across OTEL and Mastra boundaries:
|
|
@@ -160,6 +166,25 @@ Both services must have:
|
|
|
160
166
|
2. W3C Trace Context propagator enabled
|
|
161
167
|
3. Mastra with OtelBridge configured
|
|
162
168
|
|
|
169
|
+
## Using Tags
|
|
170
|
+
|
|
171
|
+
Tags help you categorize and filter traces in your OTEL backend. Add tags when executing agents or workflows:
|
|
172
|
+
|
|
173
|
+
```typescript
|
|
174
|
+
const result = await agent.generate({
|
|
175
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
176
|
+
tracingOptions: {
|
|
177
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Tags are exported as a JSON string in the `mastra.tags` span attribute for broad backend compatibility. Common use cases include:
|
|
183
|
+
|
|
184
|
+
- Environment labels: `"production"`, `"staging"`
|
|
185
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
186
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
187
|
+
|
|
163
188
|
## Troubleshooting
|
|
164
189
|
|
|
165
190
|
If traces aren't appearing or connecting as expected:
|
|
@@ -173,4 +198,3 @@ If traces aren't appearing or connecting as expected:
|
|
|
173
198
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
174
199
|
- [OpenTelemetry Exporter](/docs/v1/observability/tracing/exporters/otel) - For sending traces to OTEL backends
|
|
175
200
|
- [OtelBridge Reference](/reference/v1/observability/tracing/bridges/otel) - API documentation
|
|
176
|
-
- [OpenTelemetry GenAI Conventions](https://opentelemetry.io/docs/specs/semconv/gen-ai/)
|
|
@@ -203,6 +203,25 @@ Reserved fields such as `input`, `output`, `sessionId`, thread/user IDs, and Ope
|
|
|
203
203
|
|
|
204
204
|
This exporter implements the [OpenInference Semantic Conventions](https://github.com/Arize-ai/openinference/tree/main/spec) for generative AI applications, providing standardized trace structure across different observability platforms.
|
|
205
205
|
|
|
206
|
+
## Using Tags
|
|
207
|
+
|
|
208
|
+
Tags help you categorize and filter traces in Phoenix and Arize AX. Add tags when executing agents or workflows:
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
const result = await agent.generate({
|
|
212
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
213
|
+
tracingOptions: {
|
|
214
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
Tags appear as the `tag.tags` attribute following OpenInference conventions and can be used to filter and search traces. Common use cases include:
|
|
220
|
+
|
|
221
|
+
- Environment labels: `"production"`, `"staging"`
|
|
222
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
223
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
224
|
+
|
|
206
225
|
## Related
|
|
207
226
|
|
|
208
227
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
@@ -107,6 +107,35 @@ new LangfuseExporter({
|
|
|
107
107
|
});
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
+
## Prompt Linking
|
|
111
|
+
|
|
112
|
+
You can link LLM generations to prompts stored in [Langfuse Prompt Management](https://langfuse.com/docs/prompt-management). This enables version tracking and metrics for your prompts.
|
|
113
|
+
|
|
114
|
+
### Using the Helper (Recommended)
|
|
115
|
+
|
|
116
|
+
Use `withLangfusePrompt` with `buildTracingOptions` for the cleanest API:
|
|
117
|
+
|
|
118
|
+
```typescript title="src/agents/support-agent.ts"
|
|
119
|
+
import { Agent } from "@mastra/core/agent";
|
|
120
|
+
import { openai } from "@ai-sdk/openai";
|
|
121
|
+
import { buildTracingOptions } from "@mastra/observability";
|
|
122
|
+
import { withLangfusePrompt } from "@mastra/langfuse";
|
|
123
|
+
import { Langfuse } from "langfuse";
|
|
124
|
+
|
|
125
|
+
const langfuse = new Langfuse({
|
|
126
|
+
publicKey: process.env.LANGFUSE_PUBLIC_KEY!,
|
|
127
|
+
secretKey: process.env.LANGFUSE_SECRET_KEY!,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Fetch the prompt from Langfuse Prompt Management
|
|
131
|
+
const prompt = await langfuse.getPrompt("customer-support");
|
|
132
|
+
|
|
133
|
+
export const supportAgent = new Agent({
|
|
134
|
+
name: "support-agent",
|
|
135
|
+
instructions: prompt.prompt, // Use the prompt text from Langfuse
|
|
136
|
+
model: openai("gpt-4o"),
|
|
137
|
+
defaultGenerateOptions: {
|
|
138
|
+
tracingOptions: buildTracingOptions(withLangfusePrompt(prompt)),
|
|
110
139
|
## Using Tags
|
|
111
140
|
|
|
112
141
|
Tags help you categorize and filter traces in the Langfuse dashboard. Add tags when executing agents or workflows:
|
|
@@ -120,6 +149,39 @@ const result = await agent.generate({
|
|
|
120
149
|
});
|
|
121
150
|
```
|
|
122
151
|
|
|
152
|
+
The `withLangfusePrompt` helper automatically extracts `name`, `version`, and `id` from the Langfuse prompt object.
|
|
153
|
+
|
|
154
|
+
### Manual Fields
|
|
155
|
+
|
|
156
|
+
You can also pass manual fields if you're not using the Langfuse SDK:
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const tracingOptions = buildTracingOptions(
|
|
160
|
+
withLangfusePrompt({ name: "my-prompt", version: 1 }),
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
// Or with just an ID
|
|
164
|
+
const tracingOptions = buildTracingOptions(
|
|
165
|
+
withLangfusePrompt({ id: "prompt-uuid-12345" }),
|
|
166
|
+
);
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Prompt Object Fields
|
|
170
|
+
|
|
171
|
+
The prompt object supports these fields:
|
|
172
|
+
|
|
173
|
+
| Field | Type | Description |
|
|
174
|
+
|-------|------|-------------|
|
|
175
|
+
| `name` | string | The prompt name in Langfuse |
|
|
176
|
+
| `version` | number | The prompt version number |
|
|
177
|
+
| `id` | string | The prompt UUID for direct linking |
|
|
178
|
+
|
|
179
|
+
You can link prompts using either:
|
|
180
|
+
- `id` alone (the UUID uniquely identifies a prompt version)
|
|
181
|
+
- `name` + `version` together
|
|
182
|
+
- All three fields
|
|
183
|
+
|
|
184
|
+
When set on a `MODEL_GENERATION` span, the Langfuse exporter automatically links the generation to the corresponding prompt.
|
|
123
185
|
Tags appear in Langfuse's trace view and can be used to filter and search traces. Common use cases include:
|
|
124
186
|
|
|
125
187
|
- Environment labels: `"production"`, `"staging"`
|
|
@@ -131,3 +193,4 @@ Tags appear in Langfuse's trace view and can be used to filter and search traces
|
|
|
131
193
|
|
|
132
194
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
133
195
|
- [Langfuse Documentation](https://langfuse.com/docs)
|
|
196
|
+
- [Langfuse Prompt Management](https://langfuse.com/docs/prompt-management)
|
|
@@ -198,33 +198,26 @@ new OtelExporter({
|
|
|
198
198
|
|
|
199
199
|
## OpenTelemetry Semantic Conventions
|
|
200
200
|
|
|
201
|
-
The exporter follows [OpenTelemetry Semantic Conventions for GenAI](https://
|
|
201
|
+
The exporter follows [OpenTelemetry Semantic Conventions for GenAI v1.38.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.38.0/docs/gen-ai), ensuring compatibility with observability platforms:
|
|
202
202
|
|
|
203
203
|
### Span Naming
|
|
204
204
|
|
|
205
|
-
- **LLM Operations**: `chat {model}`
|
|
206
|
-
- **Tool Execution**: `
|
|
207
|
-
- **Agent Runs**: `
|
|
208
|
-
- **Workflow Runs**: `
|
|
205
|
+
- **LLM Operations**: `chat {model}`
|
|
206
|
+
- **Tool Execution**: `execute_tool {tool_name}`
|
|
207
|
+
- **Agent Runs**: `invoke_agent {agent_id}`
|
|
208
|
+
- **Workflow Runs**: `invoke_workflow {workflow_id}`
|
|
209
209
|
|
|
210
210
|
### Key Attributes
|
|
211
211
|
|
|
212
212
|
- `gen_ai.operation.name` - Operation type (chat, tool.execute, etc.)
|
|
213
|
-
- `gen_ai.
|
|
213
|
+
- `gen_ai.provider.name` - AI provider (openai, anthropic, etc.)
|
|
214
214
|
- `gen_ai.request.model` - Model identifier
|
|
215
|
+
- `gen_ai.input.messages` - Chat history provided to the model
|
|
216
|
+
- `gen_ai.output.messages` - Messages returned by the model
|
|
215
217
|
- `gen_ai.usage.input_tokens` - Number of input tokens
|
|
216
218
|
- `gen_ai.usage.output_tokens` - Number of output tokens
|
|
217
219
|
- `gen_ai.request.temperature` - Sampling temperature
|
|
218
|
-
- `gen_ai.response.finish_reasons` - Completion
|
|
219
|
-
|
|
220
|
-
## Buffering Strategy
|
|
221
|
-
|
|
222
|
-
The exporter buffers spans until a trace is complete:
|
|
223
|
-
|
|
224
|
-
1. Collects all spans for a trace
|
|
225
|
-
2. Waits 5 seconds after root span completes
|
|
226
|
-
3. Exports complete trace with preserved parent-child relationships
|
|
227
|
-
4. Ensures no orphaned spans
|
|
220
|
+
- `gen_ai.response.finish_reasons` - Completion reasons
|
|
228
221
|
|
|
229
222
|
## Protocol Selection Guide
|
|
230
223
|
|
|
@@ -264,11 +257,29 @@ Install the suggested package for your provider.
|
|
|
264
257
|
1. **Wrong protocol package**: Verify you installed the correct exporter for your provider
|
|
265
258
|
2. **Invalid endpoint**: Check endpoint format matches provider requirements
|
|
266
259
|
3. **Authentication failures**: Verify API keys and headers are correct
|
|
267
|
-
|
|
260
|
+
|
|
261
|
+
## Using Tags
|
|
262
|
+
|
|
263
|
+
Tags help you categorize and filter traces in your observability platform. Add tags when executing agents or workflows:
|
|
264
|
+
|
|
265
|
+
```typescript
|
|
266
|
+
const result = await agent.generate({
|
|
267
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
268
|
+
tracingOptions: {
|
|
269
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
Tags are exported as a JSON string in the `mastra.tags` span attribute for broad backend compatibility. Common use cases include:
|
|
275
|
+
|
|
276
|
+
- Environment labels: `"production"`, `"staging"`
|
|
277
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
278
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
268
279
|
|
|
269
280
|
## Related
|
|
270
281
|
|
|
271
282
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
272
|
-
- [OpenTelemetry Bridge](/docs/v1/observability/tracing/bridges/otel)
|
|
273
|
-
- [OpenTelemetry GenAI
|
|
283
|
+
- [OpenTelemetry Bridge](/docs/v1/observability/tracing/bridges/otel)
|
|
284
|
+
- [OpenTelemetry Semantic Conventions for GenAI v1.38.0](https://github.com/open-telemetry/semantic-conventions/tree/v1.38.0/docs/gen-ai)
|
|
274
285
|
- [OTEL Exporter Reference](/reference/v1/observability/tracing/exporters/otel)
|
|
@@ -101,6 +101,26 @@ new PosthogExporter({
|
|
|
101
101
|
});
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
+
## Using Tags
|
|
105
|
+
|
|
106
|
+
Tags help you categorize and filter traces in PostHog's AI analytics. Add tags when executing agents or workflows:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const result = await agent.generate({
|
|
110
|
+
messages: [{ role: "user", content: "Hello" }],
|
|
111
|
+
tracingOptions: {
|
|
112
|
+
tags: ["production", "experiment-v2", "user-request"],
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Tags are added as event properties where the tag name is the key and the value is set to `true`. In PostHog's trace view, filter by a tag using the `is set` filter (e.g., "production is set" shows all traces with the production tag). Common use cases include:
|
|
118
|
+
|
|
119
|
+
- Environment labels: `"production"`, `"staging"`
|
|
120
|
+
- Experiment tracking: `"experiment-v1"`, `"control-group"`
|
|
121
|
+
- Priority levels: `"priority-high"`, `"batch-job"`
|
|
122
|
+
- User segments: `"beta-user"`, `"enterprise"`
|
|
123
|
+
|
|
104
124
|
## Related
|
|
105
125
|
|
|
106
126
|
- [Tracing Overview](/docs/v1/observability/tracing/overview)
|
|
@@ -492,7 +492,12 @@ const result = await run.start({
|
|
|
492
492
|
#### How Tags Work
|
|
493
493
|
|
|
494
494
|
- **Root span only**: Tags are applied only to the root span of a trace (the agent run or workflow run span)
|
|
495
|
-
- **
|
|
495
|
+
- **Widely supported**: Tags are supported by most exporters for filtering and searching traces:
|
|
496
|
+
- **Braintrust** - Native `tags` field
|
|
497
|
+
- **Langfuse** - Native `tags` field on traces
|
|
498
|
+
- **ArizeExporter** - `tag.tags` OpenInference attribute
|
|
499
|
+
- **OtelExporter** - `mastra.tags` span attribute
|
|
500
|
+
- **OtelBridge** - `mastra.tags` span attribute
|
|
496
501
|
- **Combinable with metadata**: You can use both `tags` and `metadata` in the same `tracingOptions`
|
|
497
502
|
|
|
498
503
|
```ts showLineNumbers copy
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: chatRoute() | AI SDK"
|
|
3
|
+
description: API reference for chatRoute(), a function to create chat route handlers for streaming agent conversations in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# chatRoute()
|
|
9
|
+
|
|
10
|
+
Creates a chat route handler for streaming agent conversations using the AI SDK format. This function registers an HTTP `POST` endpoint that accepts messages, executes an agent, and streams the response back to the client in AI SDK-compatible format. You have to use it inside a [custom API route](/docs/v1/server-db/custom-api-routes).
|
|
11
|
+
|
|
12
|
+
Use [`handleChatStream()`](/reference/v1/ai-sdk/handle-chat-stream) if you need a framework-agnostic handler.
|
|
13
|
+
|
|
14
|
+
## Usage example
|
|
15
|
+
|
|
16
|
+
This example shows how to set up a chat route at the `/chat` endpoint that uses an agent with the ID `weatherAgent`.
|
|
17
|
+
|
|
18
|
+
```typescript title="src/mastra/index.ts" copy
|
|
19
|
+
import { Mastra } from "@mastra/core";
|
|
20
|
+
import { chatRoute } from "@mastra/ai-sdk";
|
|
21
|
+
|
|
22
|
+
export const mastra = new Mastra({
|
|
23
|
+
server: {
|
|
24
|
+
apiRoutes: [
|
|
25
|
+
chatRoute({
|
|
26
|
+
path: "/chat",
|
|
27
|
+
agent: "weatherAgent",
|
|
28
|
+
}),
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
You can also use dynamic agent routing based on an `agentId`. The URL `/chat/weatherAgent` will resolve to the agent with the ID `weatherAgent`.
|
|
35
|
+
|
|
36
|
+
```typescript title="src/mastra/index.ts" copy
|
|
37
|
+
import { Mastra } from "@mastra/core";
|
|
38
|
+
import { chatRoute } from "@mastra/ai-sdk";
|
|
39
|
+
|
|
40
|
+
export const mastra = new Mastra({
|
|
41
|
+
server: {
|
|
42
|
+
apiRoutes: [
|
|
43
|
+
chatRoute({
|
|
44
|
+
path: "/chat/:agentId",
|
|
45
|
+
}),
|
|
46
|
+
],
|
|
47
|
+
},
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Parameters
|
|
52
|
+
|
|
53
|
+
<PropertiesTable
|
|
54
|
+
content={[
|
|
55
|
+
{
|
|
56
|
+
name: "path",
|
|
57
|
+
type: "string",
|
|
58
|
+
description: "The route path (e.g., `/chat` or `/chat/:agentId`). Include `:agentId` for dynamic agent routing.",
|
|
59
|
+
isOptional: false,
|
|
60
|
+
defaultValue: "'/chat/:agentId'",
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "agent",
|
|
64
|
+
type: "string",
|
|
65
|
+
description: "The ID of the agent to use for this chat route. Required if the path doesn't include `:agentId`.",
|
|
66
|
+
isOptional: true,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "defaultOptions",
|
|
70
|
+
type: "AgentExecutionOptions",
|
|
71
|
+
description: "Default options passed to agent execution. These can include instructions, memory configuration, maxSteps, and other execution settings.",
|
|
72
|
+
isOptional: true,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: "sendStart",
|
|
76
|
+
type: "boolean",
|
|
77
|
+
description: "Whether to send start events in the stream.",
|
|
78
|
+
isOptional: true,
|
|
79
|
+
defaultValue: "true",
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "sendFinish",
|
|
83
|
+
type: "boolean",
|
|
84
|
+
description: "Whether to send finish events in the stream.",
|
|
85
|
+
isOptional: true,
|
|
86
|
+
defaultValue: "true",
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "sendReasoning",
|
|
90
|
+
type: "boolean",
|
|
91
|
+
description: "Whether to include reasoning steps in the stream.",
|
|
92
|
+
isOptional: true,
|
|
93
|
+
defaultValue: "false",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "sendSources",
|
|
97
|
+
type: "boolean",
|
|
98
|
+
description: "Whether to include source citations in the stream.",
|
|
99
|
+
isOptional: true,
|
|
100
|
+
defaultValue: "false",
|
|
101
|
+
},
|
|
102
|
+
]}
|
|
103
|
+
/>
|
|
104
|
+
|
|
105
|
+
## Additional configuration
|
|
106
|
+
|
|
107
|
+
You can use [`prepareSendMessagesRequest`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/use-chat#transport.default-chat-transport.prepare-send-messages-request) to customize the request sent to the chat route, for example to pass additional configuration to the agent:
|
|
108
|
+
|
|
109
|
+
```typescript
|
|
110
|
+
const { error, status, sendMessage, messages, regenerate, stop } = useChat({
|
|
111
|
+
transport: new DefaultChatTransport({
|
|
112
|
+
api: "http://localhost:4111/chat",
|
|
113
|
+
prepareSendMessagesRequest({ messages }) {
|
|
114
|
+
return {
|
|
115
|
+
body: {
|
|
116
|
+
messages,
|
|
117
|
+
// Pass memory config
|
|
118
|
+
memory: {
|
|
119
|
+
thread: "user-1",
|
|
120
|
+
resource: "user-1",
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
},
|
|
125
|
+
}),
|
|
126
|
+
});
|
|
127
|
+
```
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: handleChatStream() | AI SDK"
|
|
3
|
+
description: API reference for handleChatStream(), a framework-agnostic handler for streaming agent chat in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# handleChatStream()
|
|
9
|
+
|
|
10
|
+
Framework-agnostic handler for streaming agent chat in AI SDK-compatible format. Use this function directly when you need to handle chat streaming outside Hono or Mastra's own [apiRoutes](/docs/v1/server-db/custom-api-routes) feature.
|
|
11
|
+
|
|
12
|
+
`handleChatStream()` returns a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
13
|
+
|
|
14
|
+
Use [`chatRoute()`](/reference/v1/ai-sdk/chat-route) if you want to create a chat route inside a Mastra server.
|
|
15
|
+
|
|
16
|
+
## Usage example
|
|
17
|
+
|
|
18
|
+
Next.js App Router example:
|
|
19
|
+
|
|
20
|
+
```typescript title="app/api/chat/route.ts" copy
|
|
21
|
+
import { handleChatStream } from '@mastra/ai-sdk';
|
|
22
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
23
|
+
import { mastra } from '@/src/mastra';
|
|
24
|
+
|
|
25
|
+
export async function POST(req: Request) {
|
|
26
|
+
const params = await req.json();
|
|
27
|
+
const stream = await handleChatStream({
|
|
28
|
+
mastra,
|
|
29
|
+
agentId: 'weatherAgent',
|
|
30
|
+
params,
|
|
31
|
+
});
|
|
32
|
+
return createUIMessageStreamResponse({ stream });
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Parameters
|
|
37
|
+
|
|
38
|
+
<PropertiesTable
|
|
39
|
+
content={[
|
|
40
|
+
{
|
|
41
|
+
name: "mastra",
|
|
42
|
+
type: "Mastra",
|
|
43
|
+
description: "The Mastra instance containing registered agents.",
|
|
44
|
+
isOptional: false,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: "agentId",
|
|
48
|
+
type: "string",
|
|
49
|
+
description: "The ID of the agent to use for chat.",
|
|
50
|
+
isOptional: false,
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
name: "params",
|
|
54
|
+
type: "ChatStreamHandlerParams",
|
|
55
|
+
description: "Parameters for the chat stream, including messages and optional resume data.",
|
|
56
|
+
isOptional: false,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: "params.messages",
|
|
60
|
+
type: "UIMessage[]",
|
|
61
|
+
description: "Array of messages in the conversation.",
|
|
62
|
+
isOptional: false,
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: "params.resumeData",
|
|
66
|
+
type: "Record<string, any>",
|
|
67
|
+
description: "Data for resuming a suspended agent execution. Requires `runId` to be set.",
|
|
68
|
+
isOptional: true,
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
name: "params.runId",
|
|
72
|
+
type: "string",
|
|
73
|
+
description: "The run ID. Required when `resumeData` is provided.",
|
|
74
|
+
isOptional: true,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "params.requestContext",
|
|
78
|
+
type: "RequestContext",
|
|
79
|
+
description: "Request context to pass to the agent execution.",
|
|
80
|
+
isOptional: true,
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "defaultOptions",
|
|
84
|
+
type: "AgentExecutionOptions",
|
|
85
|
+
description: "Default options passed to agent execution. These are merged with params, with params taking precedence.",
|
|
86
|
+
isOptional: true,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: "sendStart",
|
|
90
|
+
type: "boolean",
|
|
91
|
+
description: "Whether to send start events in the stream.",
|
|
92
|
+
isOptional: true,
|
|
93
|
+
defaultValue: "true",
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
name: "sendFinish",
|
|
97
|
+
type: "boolean",
|
|
98
|
+
description: "Whether to send finish events in the stream.",
|
|
99
|
+
isOptional: true,
|
|
100
|
+
defaultValue: "true",
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: "sendReasoning",
|
|
104
|
+
type: "boolean",
|
|
105
|
+
description: "Whether to include reasoning steps in the stream.",
|
|
106
|
+
isOptional: true,
|
|
107
|
+
defaultValue: "false",
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: "sendSources",
|
|
111
|
+
type: "boolean",
|
|
112
|
+
description: "Whether to include source citations in the stream.",
|
|
113
|
+
isOptional: true,
|
|
114
|
+
defaultValue: "false",
|
|
115
|
+
},
|
|
116
|
+
]}
|
|
117
|
+
/>
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Reference: handleNetworkStream() | AI SDK"
|
|
3
|
+
description: API reference for handleNetworkStream(), a framework-agnostic handler for streaming network execution in AI SDK-compatible format.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
import PropertiesTable from "@site/src/components/PropertiesTable";
|
|
7
|
+
|
|
8
|
+
# handleNetworkStream()
|
|
9
|
+
|
|
10
|
+
Framework-agnostic handler for streaming network execution in AI SDK-compatible format. Use this function directly when you need to handle network streaming outside Hono or Mastra's own [apiRoutes](/docs/v1/server-db/custom-api-routes) feature.
|
|
11
|
+
|
|
12
|
+
`handleNetworkStream()` returns a `ReadableStream` that you can wrap with [`createUIMessageStreamResponse()`](https://ai-sdk.dev/docs/reference/ai-sdk-ui/create-ui-message-stream-response).
|
|
13
|
+
|
|
14
|
+
Use [`networkRoute()`](/reference/v1/ai-sdk/network-route) if you want to create a network route inside a Mastra server.
|
|
15
|
+
## Usage example
|
|
16
|
+
|
|
17
|
+
Next.js App Router example:
|
|
18
|
+
|
|
19
|
+
```typescript title="app/api/network/route.ts" copy
|
|
20
|
+
import { handleNetworkStream } from '@mastra/ai-sdk';
|
|
21
|
+
import { createUIMessageStreamResponse } from 'ai';
|
|
22
|
+
import { mastra } from '@/src/mastra';
|
|
23
|
+
|
|
24
|
+
export async function POST(req: Request) {
|
|
25
|
+
const params = await req.json();
|
|
26
|
+
const stream = await handleNetworkStream({
|
|
27
|
+
mastra,
|
|
28
|
+
agentId: 'routingAgent',
|
|
29
|
+
params,
|
|
30
|
+
});
|
|
31
|
+
return createUIMessageStreamResponse({ stream });
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Parameters
|
|
36
|
+
|
|
37
|
+
<PropertiesTable
|
|
38
|
+
content={[
|
|
39
|
+
{
|
|
40
|
+
name: "mastra",
|
|
41
|
+
type: "Mastra",
|
|
42
|
+
description: "The Mastra instance to use for agent lookup and execution.",
|
|
43
|
+
isOptional: false,
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: "agentId",
|
|
47
|
+
type: "string",
|
|
48
|
+
description: "The ID of the routing agent to execute as a network.",
|
|
49
|
+
isOptional: false,
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
name: "params",
|
|
53
|
+
type: "NetworkStreamHandlerParams",
|
|
54
|
+
description: "The request parameters containing messages and execution options. Includes `messages` (required) and any AgentExecutionOptions like `memory`, `maxSteps`, `runId`, etc.",
|
|
55
|
+
isOptional: false,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "defaultOptions",
|
|
59
|
+
type: "AgentExecutionOptions",
|
|
60
|
+
description: "Default options passed to agent execution. These are merged with params, with params taking precedence.",
|
|
61
|
+
isOptional: true,
|
|
62
|
+
},
|
|
63
|
+
]}
|
|
64
|
+
/>
|