@mastra/mcp-docs-server 1.2.16-alpha.3 → 1.2.16-alpha.6
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/docs/agents/a2a.md +1 -1
- package/.docs/docs/agents/guardrails.md +11 -10
- package/.docs/docs/agents/overview.md +2 -0
- package/.docs/docs/agents/processors.md +8 -8
- package/.docs/docs/agents/structured-output.md +1 -1
- package/.docs/docs/agents/using-tools.md +51 -12
- package/.docs/docs/browser/overview.md +22 -3
- package/.docs/docs/capabilities/subagents.md +2 -2
- package/.docs/docs/datasets/running-experiments.md +2 -2
- package/.docs/docs/deployment/overview.md +3 -3
- package/.docs/docs/deployment/sandbox.md +1 -1
- package/.docs/docs/deployment/workers.md +5 -5
- package/.docs/docs/deployment/workflow-runners.md +1 -1
- package/.docs/docs/editor/overview.md +3 -1
- package/.docs/docs/evals/custom-scorers.md +1 -1
- package/.docs/docs/evals/multi-turn.md +1 -1
- package/.docs/docs/evals/overview.md +3 -3
- package/.docs/docs/getting-started/develop.md +1 -1
- package/.docs/docs/harness/agent-controller.md +2 -2
- package/.docs/docs/harness/overview.md +1 -1
- package/.docs/docs/long-running-agents/background-tasks.md +2 -2
- package/.docs/docs/long-running-agents/goals.md +3 -2
- package/.docs/docs/long-running-agents/signals.md +2 -2
- package/.docs/docs/mastra-platform/deploy.md +1 -1
- package/.docs/docs/mastra-platform/github.md +1 -1
- package/.docs/docs/mastra-platform/observability.md +1 -1
- package/.docs/docs/mastra-platform/overview.md +3 -3
- package/.docs/docs/mastra-platform/server.md +2 -2
- package/.docs/docs/mastra-platform/trace-intelligence.md +3 -3
- package/.docs/docs/memory/memory-processors.md +1 -1
- package/.docs/docs/memory/message-history.md +2 -2
- package/.docs/docs/memory/observational-memory.md +13 -13
- package/.docs/docs/memory/semantic-recall.md +1 -1
- package/.docs/docs/memory/working-memory.md +1 -1
- package/.docs/docs/observability/logging.md +1 -1
- package/.docs/docs/observability/metrics/querying.md +1 -1
- package/.docs/docs/observability/tracing/overview.md +3 -3
- package/.docs/docs/server/custom-adapters.md +2 -2
- package/.docs/docs/server/custom-api-routes.md +1 -1
- package/.docs/docs/server/mastra-client.md +1 -1
- package/.docs/docs/server/server-adapters.md +1 -1
- package/.docs/docs/studio/deployment.md +2 -2
- package/.docs/docs/studio/observability.md +1 -1
- package/.docs/docs/studio/overview.md +2 -0
- package/.docs/docs/workflows/dynamic-workflows.md +1 -1
- package/.docs/docs/workflows/scheduled-workflows.md +1 -1
- package/.docs/docs/workflows/suspend-and-resume.md +1 -1
- package/.docs/docs/workspace/overview.md +2 -1
- package/.docs/docs/workspace/search.md +1 -1
- package/.docs/docs/workspace/skills.md +1 -1
- package/.docs/integrations/browsers/agent-browser.md +2 -3
- package/.docs/integrations/browsers/firecrawl.md +0 -1
- package/.docs/integrations/browsers/stagehand.md +2 -3
- package/.docs/models/gateways/openrouter.md +1 -2
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/kilo.md +6 -7
- package/.docs/models/providers/nano-gpt.md +2 -1
- package/.docs/reference/acp/acp-agent.md +1 -1
- package/.docs/reference/agent-controller/session.md +1 -1
- package/.docs/reference/agents/channels.md +1 -1
- package/.docs/reference/agents/inngest-agent.md +1 -1
- package/.docs/reference/ai-sdk/overview.md +1 -1
- package/.docs/reference/auth/auth0.md +1 -1
- package/.docs/reference/auth/google.md +1 -1
- package/.docs/reference/browser/agent-browser.md +2 -2
- package/.docs/reference/browser/browser-viewer.md +1 -1
- package/.docs/reference/browser/stagehand-browser.md +2 -2
- package/.docs/reference/channels/channel-provider.md +1 -1
- package/.docs/reference/channels/slack-provider.md +1 -1
- package/.docs/reference/cli/mastra.md +5 -5
- package/.docs/reference/code-sdk/mount-agent-controller.md +1 -1
- package/.docs/reference/coding-agent/build-base-prompt.md +2 -1
- package/.docs/reference/configuration.md +3 -3
- package/.docs/reference/core/getAgentById.md +1 -1
- package/.docs/reference/core/getWorkflow.md +2 -2
- package/.docs/reference/core/mastra-class.md +1 -1
- package/.docs/reference/deployer/netlify.md +1 -1
- package/.docs/reference/deployer.md +1 -1
- package/.docs/reference/editor/prompt-blocks.md +1 -1
- package/.docs/reference/editor/tools.md +1 -1
- package/.docs/reference/evals/completeness.md +1 -1
- package/.docs/reference/evals/noise-sensitivity.md +1 -1
- package/.docs/reference/evals/summarization.md +1 -1
- package/.docs/reference/file-based-agents/config.md +1 -1
- package/.docs/reference/file-based-agents/skills.md +5 -5
- package/.docs/reference/file-based-agents/workspace.md +1 -1
- package/.docs/reference/index.md +1 -1
- package/.docs/reference/logging/pino-logger.md +1 -1
- package/.docs/reference/memory/observational-memory.md +12 -4
- package/.docs/reference/migrations/upgrade-to-v1/agent.md +3 -3
- package/.docs/reference/migrations/upgrade-to-v1/client.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/evals.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/mastra.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/mcp.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/overview.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/processors.md +1 -1
- package/.docs/reference/migrations/upgrade-to-v1/storage.md +5 -5
- package/.docs/reference/migrations/upgrade-to-v1/tools.md +1 -1
- package/.docs/reference/observability/tracing/bridges/datadog.md +1 -1
- package/.docs/reference/observability/tracing/bridges/otel.md +1 -1
- package/.docs/reference/observability/tracing/exporters/arize.md +1 -1
- package/.docs/reference/observability/tracing/exporters/cloud-exporter.md +2 -2
- package/.docs/reference/observability/tracing/exporters/mastra-platform-exporter.md +1 -1
- package/.docs/reference/observability/tracing/exporters/otel.md +1 -1
- package/.docs/reference/observability/tracing/exporters/sentry.md +1 -1
- package/.docs/reference/observability/tracing/processors/sensitive-data-filter.md +27 -3
- package/.docs/reference/processors/batch-parts-processor.md +1 -1
- package/.docs/reference/processors/language-detector.md +1 -2
- package/.docs/reference/processors/processor-interface.md +16 -16
- package/.docs/reference/processors/stream-error-retry-processor.md +2 -2
- package/.docs/reference/processors/token-cost-control.md +154 -0
- package/.docs/reference/rag/chunking-and-embedding.md +1 -1
- package/.docs/reference/rag/metadata-filters.md +1 -1
- package/.docs/reference/server/koa-adapter.md +1 -1
- package/.docs/reference/signals/webhook-signal-provider.md +1 -1
- package/.docs/reference/storage/retention.md +1 -1
- package/.docs/reference/tools/bedrock-kb-tool.md +1 -1
- package/.docs/reference/tools/mcp-client.md +3 -3
- package/.docs/reference/tools/mcp-server.md +5 -5
- package/.docs/reference/tools/task-tools.md +1 -1
- package/.docs/reference/tools/vector-query-tool.md +1 -1
- package/.docs/reference/vectors/chroma.md +1 -1
- package/.docs/reference/vectors/convex.md +1 -1
- package/.docs/reference/vectors/couchbase.md +1 -1
- package/.docs/reference/vectors/mongodb.md +2 -2
- package/.docs/reference/vectors/pg.md +1 -1
- package/.docs/reference/vectors/qdrant.md +2 -2
- package/.docs/reference/voice/overview.md +1 -1
- package/.docs/reference/voice/speech-to-speech.md +1 -1
- package/.docs/reference/voice/speech-to-text.md +1 -1
- package/.docs/reference/voice/voice.on.md +1 -1
- package/.docs/reference/voice/voice.updateConfig.md +1 -1
- package/.docs/reference/workspace/local-sandbox.md +1 -1
- package/.docs/reference/workspace/workspace-class.md +1 -1
- package/CHANGELOG.md +14 -0
- package/package.json +5 -5
- package/.docs/docs/browser/recording.md +0 -120
- package/.docs/reference/processors/cost-guard-processor.md +0 -115
|
@@ -93,7 +93,7 @@ USING snapshot::jsonb;
|
|
|
93
93
|
|
|
94
94
|
### Storage composition in `MastraCompositeStore`
|
|
95
95
|
|
|
96
|
-
`MastraCompositeStore` can now compose storage domains from different adapters. Use it when
|
|
96
|
+
`MastraCompositeStore` can now compose storage domains from different adapters. Use it when different purposes require different databases, such as PostgreSQL for memory and workflows with a specialized database for observability.
|
|
97
97
|
|
|
98
98
|
```typescript
|
|
99
99
|
import { MastraCompositeStore } from '@mastra/core/storage'
|
|
@@ -386,7 +386,7 @@ To migrate, use the memory store, remove format parameters, and update code to w
|
|
|
386
386
|
+ const saved = saveResult?.messages; // MastraDBMessage[]
|
|
387
387
|
```
|
|
388
388
|
|
|
389
|
-
### Vector store API from positional to
|
|
389
|
+
### Vector store API from positional arguments to arguments by name
|
|
390
390
|
|
|
391
391
|
All vector store methods now use an arguments object instead of positional arguments. This makes each value's purpose visible at the call site and lets method signatures evolve without relying on argument order.
|
|
392
392
|
|
|
@@ -430,7 +430,7 @@ To migrate, rename the methods and pass an arguments object.
|
|
|
430
430
|
|
|
431
431
|
### PGVector constructor from connection string to object
|
|
432
432
|
|
|
433
|
-
The PGVector constructor now requires object parameters instead of a connection string.
|
|
433
|
+
The PGVector constructor now requires object parameters instead of a connection string. Object parameters align the API across storage adapters.
|
|
434
434
|
|
|
435
435
|
To migrate, pass the connection string as an object property.
|
|
436
436
|
|
|
@@ -484,7 +484,7 @@ const pgStore = new PostgresStore({
|
|
|
484
484
|
|
|
485
485
|
### Score storage methods to `listScoresBy*` pattern
|
|
486
486
|
|
|
487
|
-
Score storage APIs have been renamed to follow the `listScoresBy*` pattern.
|
|
487
|
+
Score storage APIs have been renamed to follow the `listScoresBy*` pattern. The new names follow the broader API naming conventions.
|
|
488
488
|
|
|
489
489
|
To migrate, update method names from `getScores` to `listScoresByScorerId` and related variants.
|
|
490
490
|
|
|
@@ -500,7 +500,7 @@ To migrate, update method names from `getScores` to `listScoresByScorerId` and r
|
|
|
500
500
|
|
|
501
501
|
### Non-paginated storage functions
|
|
502
502
|
|
|
503
|
-
Non-paginated storage functions have been removed in favor of paginated versions. All list operations now use pagination, though you can fetch all records with `perPage: false`.
|
|
503
|
+
Non-paginated storage functions have been removed in favor of paginated versions. All list operations now use pagination, though you can fetch all records with `perPage: false`. Pagination aligns list operations across the API and prevents accidental loading of large datasets.
|
|
504
504
|
|
|
505
505
|
To migrate, use paginated methods via domain stores. For fetching all records, use `perPage: false`.
|
|
506
506
|
|
|
@@ -32,7 +32,7 @@ createTool({
|
|
|
32
32
|
|
|
33
33
|
### `createTool` context properties organization
|
|
34
34
|
|
|
35
|
-
Context properties in `createTool` are now organized into namespaces. Agent-specific properties are under `context.agent`, workflow-specific properties are under `context.workflow`, and MCP-specific properties are under `context.mcp`.
|
|
35
|
+
Context properties in `createTool` are now organized into namespaces. Agent-specific properties are under `context.agent`, workflow-specific properties are under `context.workflow`, and MCP-specific properties are under `context.mcp`. The namespaces organize these properties under a clearer API surface.
|
|
36
36
|
|
|
37
37
|
For tools that are executed inside an agent, access agent-specific properties through `context.agent`.
|
|
38
38
|
|
|
@@ -78,7 +78,7 @@ Force-flushes any buffered LLM Observability data to Datadog without shutting do
|
|
|
78
78
|
async shutdown(): Promise<void>
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
Force-finishes any APM spans that weren't properly closed
|
|
81
|
+
Force-finishes any APM spans that weren't properly closed and flushes pending LLM Observability data. It then disables the integration and clears all internal state.
|
|
82
82
|
|
|
83
83
|
## Usage examples
|
|
84
84
|
|
|
@@ -146,7 +146,7 @@ Tags are stored as a JSON-stringified array in the `mastra.tags` span attribute:
|
|
|
146
146
|
}
|
|
147
147
|
```
|
|
148
148
|
|
|
149
|
-
|
|
149
|
+
The format is compatible with all OTEL-compatible backends and collectors.
|
|
150
150
|
|
|
151
151
|
## Related
|
|
152
152
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# ArizeExporter
|
|
4
4
|
|
|
5
|
-
Sends Tracing data to Arize Phoenix
|
|
5
|
+
Sends Tracing data to Arize Phoenix or Arize AX, as well as any OpenTelemetry-compatible observability platform that supports OpenInference semantic conventions.
|
|
6
6
|
|
|
7
7
|
## Constructor
|
|
8
8
|
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
**Added in:** `@mastra/observability@1.8.0`. **Deprecated in `1.12.0`** in favor of [`MastraPlatformExporter`](https://mastra.ai/reference/observability/tracing/exporters/mastra-platform-exporter).
|
|
6
6
|
|
|
7
|
-
> **Deprecated:** `CloudExporter` is retained for backward compatibility and will be removed in a future major version. Use [`MastraPlatformExporter`](https://mastra.ai/reference/observability/tracing/exporters/mastra-platform-exporter) for new projects. Both classes share the same constructor, environment variables, and runtime behavior
|
|
7
|
+
> **Deprecated:** `CloudExporter` is retained for backward compatibility and will be removed in a future major version. Use [`MastraPlatformExporter`](https://mastra.ai/reference/observability/tracing/exporters/mastra-platform-exporter) for new projects. Both classes share the same constructor, environment variables, and runtime behavior. `CloudExporter` keeps its original `mastra-cloud-observability-exporter` exporter `name` and `CLOUD_EXPORTER_*` error IDs so monitoring rules built against it keep working.
|
|
8
8
|
|
|
9
9
|
Sends tracing spans, logs, metrics, scores, and feedback to the Mastra platform for online visualization and monitoring.
|
|
10
10
|
|
|
@@ -183,7 +183,7 @@ The exporter batches tracing spans, logs, metrics, scores, and feedback for effi
|
|
|
183
183
|
### Signal Processing
|
|
184
184
|
|
|
185
185
|
- `exportTracingEvent()` only exports `SPAN_ENDED` tracing events
|
|
186
|
-
- `onLogEvent()`, `onMetricEvent()`, `onScoreEvent()`, and `onFeedbackEvent()` buffer every event they receive for
|
|
186
|
+
- `onLogEvent()`, `onMetricEvent()`, `onScoreEvent()`, and `onFeedbackEvent()` buffer every event they receive for that signal type
|
|
187
187
|
- All supported signal batches are uploaded to their matching Cloud publish endpoints during `flush()` and `shutdown()`
|
|
188
188
|
|
|
189
189
|
## `MastraCloudSpanRecord`
|
|
@@ -199,7 +199,7 @@ The probe timer doesn't keep the Node.js process alive, so short-lived and serve
|
|
|
199
199
|
### Signal Processing
|
|
200
200
|
|
|
201
201
|
- `exportTracingEvent()` only exports `SPAN_ENDED` tracing events
|
|
202
|
-
- `onLogEvent()`, `onMetricEvent()`, `onScoreEvent()`, and `onFeedbackEvent()` buffer every event they receive for
|
|
202
|
+
- `onLogEvent()`, `onMetricEvent()`, `onScoreEvent()`, and `onFeedbackEvent()` buffer every event they receive for that signal type
|
|
203
203
|
- All supported signal batches are uploaded to their matching publish endpoints during `flush()` and `shutdown()`
|
|
204
204
|
|
|
205
205
|
## Span wire format
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# OtelExporter
|
|
4
4
|
|
|
5
|
-
Sends traces and logs to any OpenTelemetry-compatible observability platform. Traces use the standardized GenAI semantic conventions
|
|
5
|
+
Sends traces and logs to any OpenTelemetry-compatible observability platform. Traces use the standardized GenAI semantic conventions. Logs carry their original severity and message body and are correlated to traces via both the OTEL log record's native trace context and `mastra.traceId` / `mastra.spanId` attributes.
|
|
6
6
|
|
|
7
7
|
## Constructor
|
|
8
8
|
|
|
@@ -51,7 +51,7 @@ Force flushes any pending spans to Sentry without shutting down the exporter. Wa
|
|
|
51
51
|
async shutdown(): Promise<void>
|
|
52
52
|
```
|
|
53
53
|
|
|
54
|
-
Ends all active spans
|
|
54
|
+
Ends all active spans and clears internal state before closing the Sentry connection. Waits up to 2 seconds for pending data to be sent.
|
|
55
55
|
|
|
56
56
|
## Usage
|
|
57
57
|
|
|
@@ -57,7 +57,7 @@ new SensitiveDataFilter(options?: SensitiveDataFilterOptions)
|
|
|
57
57
|
|
|
58
58
|
**redactionToken** (`string`): Replacement value used for full redaction and short values under partial redaction. (Default: `'[REDACTED]'`)
|
|
59
59
|
|
|
60
|
-
**redactionStyle** (`'full' | 'partial'`): Controls
|
|
60
|
+
**redactionStyle** (`'full' | 'partial' | 'indexed'`): Controls how matched values are redacted. (Default: `'full'`)
|
|
61
61
|
|
|
62
62
|
```typescript
|
|
63
63
|
interface SensitiveDataFilterOptions {
|
|
@@ -66,7 +66,7 @@ interface SensitiveDataFilterOptions {
|
|
|
66
66
|
redactionStyle?: RedactionStyle
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
type RedactionStyle = 'full' | 'partial'
|
|
69
|
+
type RedactionStyle = 'full' | 'partial' | 'indexed'
|
|
70
70
|
```
|
|
71
71
|
|
|
72
72
|
## Redaction styles
|
|
@@ -101,6 +101,30 @@ const filter = new SensitiveDataFilter({
|
|
|
101
101
|
{ "apiKey": "sk-…789" }
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
+
### Indexed redaction
|
|
105
|
+
|
|
106
|
+
Indexed redaction replaces each unique value with a stable token derived from the first field name that matched it, for example `[APIKEY_1]`. The same value maps to the same token across the spans of a trace while the trace's mapping is retained, so redacted values stay correlatable without exposing the raw value. Later occurrences under other sensitive fields reuse the first token.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const filter = new SensitiveDataFilter({
|
|
110
|
+
redactionStyle: 'indexed',
|
|
111
|
+
})
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
```jsonc
|
|
115
|
+
// Before (two spans in the same trace)
|
|
116
|
+
[{ "apiKey": "sk-alice-key" }, { "apiKey": "sk-bob-key" }]
|
|
117
|
+
|
|
118
|
+
// After
|
|
119
|
+
[{ "apiKey": "[APIKEY_1]" }, { "apiKey": "[APIKEY_2]" }]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The mapping is scoped per trace. Numbering restarts for each trace, so tokens from different traces can't be linked.
|
|
123
|
+
|
|
124
|
+
State is bounded in two ways. The filter keeps mappings for the 1000 most recently used traces. The least recently used trace is evicted beyond that, and spans arriving for an evicted trace start a fresh mapping, so later values may receive new tokens. Each trace also tracks up to 1000 unique values. Once that cap is reached, already-tracked values keep their tokens and new values are redacted with `redactionToken`.
|
|
125
|
+
|
|
126
|
+
Non-string values are converted to strings before a token is assigned.
|
|
127
|
+
|
|
104
128
|
## Field matching
|
|
105
129
|
|
|
106
130
|
Field names are lowercased and stripped of non-alphanumeric characters before comparison:
|
|
@@ -165,7 +189,7 @@ Returns: `AnySpan`
|
|
|
165
189
|
|
|
166
190
|
#### `shutdown()`
|
|
167
191
|
|
|
168
|
-
Completes processor shutdown
|
|
192
|
+
Completes processor shutdown and clears the per-trace state used by indexed redaction.
|
|
169
193
|
|
|
170
194
|
```typescript
|
|
171
195
|
await filter.shutdown()
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# BatchPartsProcessor
|
|
4
4
|
|
|
5
|
-
The `BatchPartsProcessor` is an **output processor** that batches multiple stream parts together to reduce the frequency of emissions during streaming. This processor is useful for reducing network overhead
|
|
5
|
+
The `BatchPartsProcessor` is an **output processor** that batches multiple stream parts together to reduce the frequency of emissions during streaming. This processor is useful for reducing network overhead and consolidating small text chunks for users, while controlling emissions to optimize streaming performance.
|
|
6
6
|
|
|
7
7
|
## Usage example
|
|
8
8
|
|
|
@@ -40,7 +40,7 @@ const processor = new LanguageDetector({
|
|
|
40
40
|
|
|
41
41
|
**options.includeDetectionDetails** (`boolean`): Whether to include detailed detection info in logs
|
|
42
42
|
|
|
43
|
-
**options.translationQuality** (`'speed' | 'quality' | 'balanced'`):
|
|
43
|
+
**options.translationQuality** (`'speed' | 'quality' | 'balanced'`): Deprecated. This option previously selected prompt-level "Quality Level" guidance, but that behavior was removed when the detection and translation prompts were streamlined. The option now has no effect. For model-specific speed and quality controls, use providerOptions when your provider supports them.
|
|
44
44
|
|
|
45
45
|
**options.providerOptions** (`ProviderOptions`): Provider-specific options passed to the internal detection agent. Use this to control model behavior like reasoning effort for thinking models (e.g., { openai: { reasoningEffort: 'low' } })
|
|
46
46
|
|
|
@@ -74,7 +74,6 @@ export const agent = new Agent({
|
|
|
74
74
|
'Detect language and translate non-English content to English while preserving original intent',
|
|
75
75
|
minTextLength: 10,
|
|
76
76
|
includeDetectionDetails: true,
|
|
77
|
-
translationQuality: 'quality',
|
|
78
77
|
}),
|
|
79
78
|
],
|
|
80
79
|
})
|
|
@@ -78,17 +78,17 @@ Processor methods run at different points in the agent execution lifecycle:
|
|
|
78
78
|
└────────────────────────────────────────────────────────────────────┘
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
| Method | When it runs
|
|
82
|
-
| --------------------- |
|
|
83
|
-
| `processInput` | Once at the start, before the agentic loop
|
|
84
|
-
| `processInputStep` | At each step of the agentic loop, before each LLM call
|
|
85
|
-
| `processLLMRequest` | After LLM request conversion, before the provider call
|
|
86
|
-
| `processAPIError` | When an LLM API call fails
|
|
87
|
-
| `processOutputStream` | On each streaming chunk during LLM response
|
|
88
|
-
| `processLLMResponse` | After the LLM step completes and stream chunks are collected
|
|
89
|
-
| `processOutputStep` | After each LLM response, before tool execution
|
|
90
|
-
| `processToolResult` | Per tool, after
|
|
91
|
-
| `processOutputResult` | Once after generation completes
|
|
81
|
+
| Method | When it runs | Use case |
|
|
82
|
+
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------- |
|
|
83
|
+
| `processInput` | Once at the start, before the agentic loop | Validate/transform initial user input, add context |
|
|
84
|
+
| `processInputStep` | At each step of the agentic loop, before each LLM call | Transform messages between steps, handle tool results |
|
|
85
|
+
| `processLLMRequest` | After LLM request conversion, before the provider call | Rewrite the outbound `LanguageModelV2Prompt` for the current call without persisting changes |
|
|
86
|
+
| `processAPIError` | When an LLM API call fails | Inspect API rejections, optionally mutate state/messages, and request a retry |
|
|
87
|
+
| `processOutputStream` | On each streaming chunk during LLM response | Filter/modify streaming content, detect patterns in real-time |
|
|
88
|
+
| `processLLMResponse` | After the LLM step completes and stream chunks are collected | Capture or cache the full response, run post-call side effects paired with `processLLMRequest` |
|
|
89
|
+
| `processOutputStep` | After each LLM response, before tool execution | Validate output quality, implement guardrails with retry |
|
|
90
|
+
| `processToolResult` | Per tool, after a locally executed tool returns or a provider-executed result arrives, before the raw result is persisted to `messageList` | Inspect tool output and enforce security policies |
|
|
91
|
+
| `processOutputResult` | Once after generation completes | Post-process final response, log results |
|
|
92
92
|
|
|
93
93
|
## Interface definition
|
|
94
94
|
|
|
@@ -212,7 +212,7 @@ function getText(message: MastraDBMessage): string {
|
|
|
212
212
|
|
|
213
213
|
Key points:
|
|
214
214
|
|
|
215
|
-
- `message.content.parts` is the primary source. A single message can contain multiple parts
|
|
215
|
+
- `message.content.parts` is the primary source. A single message can contain multiple parts. Non-text parts include tool calls and tool results, plus file parts. Filter by `part.type === 'text'` before reading `part.text`.
|
|
216
216
|
- `message.content.content` is a flattened string kept for backward compatibility. Use it only as a fallback when `parts` is empty or missing.
|
|
217
217
|
- `message.content` itself is never a plain string on `MastraDBMessage`. Legacy `CoreMessage` shapes may be strings, but processors always receive `MastraDBMessage`.
|
|
218
218
|
|
|
@@ -365,7 +365,7 @@ System messages are **reset to their original values** at the start of each step
|
|
|
365
365
|
#### Use cases
|
|
366
366
|
|
|
367
367
|
- Dynamic model switching based on step number or context
|
|
368
|
-
- Disabling tools after a
|
|
368
|
+
- Disabling tools after a configured number of steps
|
|
369
369
|
- Dynamically adding or replacing tools based on conversation context
|
|
370
370
|
- Transforming message part types between providers (e.g., `reasoning` → `thinking` for Anthropic)
|
|
371
371
|
- Modifying messages based on step number or accumulated context
|
|
@@ -708,11 +708,11 @@ export class QualityGuardrail implements Processor {
|
|
|
708
708
|
|
|
709
709
|
### `processToolResult`
|
|
710
710
|
|
|
711
|
-
Processes a tool's result after
|
|
711
|
+
Processes a tool's result after a locally executed tool returns or a provider-executed result arrives, before the raw result is persisted to `messageList` or fed to the next LLM call. Symmetric with `processOutputStep`, which fires before tool execution. Use this method to enforce security policies on tool output. For example, scan for prompt injection or redact sensitive fields. Call `abort('reason', { retry: true })` to abort and retry the same step.
|
|
712
712
|
|
|
713
|
-
To replace the tool's result, mutate `messageList` in place via `messageList.updateToolInvocation`. The runtime re-reads the post-processor result from the message list and overwrites the downstream tool-result stream chunk before it
|
|
713
|
+
To replace the tool's result, mutate `messageList` in place via `messageList.updateToolInvocation`. The runtime re-reads the post-processor result from the message list and overwrites the downstream tool-result stream chunk before it's enqueued, so streaming clients see the processed value.
|
|
714
714
|
|
|
715
|
-
This method
|
|
715
|
+
This method doesn't fire when `tool.execute()` throws. It's called only for successful tool executions where a result is available.
|
|
716
716
|
|
|
717
717
|
```typescript
|
|
718
718
|
processToolResult?(args: ProcessToolResultArgs): ProcessorMessageResult;
|
|
@@ -49,7 +49,7 @@ For retryable errors with a `Retry-After` response header, the processor reads c
|
|
|
49
49
|
|
|
50
50
|
## Retry unknown errors
|
|
51
51
|
|
|
52
|
-
Set `retryUnknownErrors` to retry errors that don't match provider metadata
|
|
52
|
+
Set `retryUnknownErrors` to retry errors that don't match provider metadata or the built-in OpenAI matcher, including a custom matcher. Unknown-error retries use the processor-level `maxRetries` and `delayMs` values. Known authorization failures, including HTTP `401` and `403` responses, aren't retried:
|
|
53
53
|
|
|
54
54
|
```typescript
|
|
55
55
|
import { Agent } from '@mastra/core/agent'
|
|
@@ -105,7 +105,7 @@ export const agent = new Agent({
|
|
|
105
105
|
|
|
106
106
|
## Default OpenAI Responses matcher
|
|
107
107
|
|
|
108
|
-
`isRetryableOpenAIResponsesStreamError` matches OpenAI Responses stream error chunks with `type: 'error'` or `type: 'response.failed'`. It retries known transient OpenAI error codes
|
|
108
|
+
`isRetryableOpenAIResponsesStreamError` matches OpenAI Responses stream error chunks with `type: 'error'` or `type: 'response.failed'`. It retries known transient OpenAI error codes. As a fallback, it retries errors with explicit retry guidance such as `You can retry your request`.
|
|
109
109
|
|
|
110
110
|
`StreamErrorRetryProcessor` includes this matcher by default. You can also import it and reuse it in custom retry logic.
|
|
111
111
|
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
+
|
|
3
|
+
# TokenCostControl
|
|
4
|
+
|
|
5
|
+
The `TokenCostControl` enforces monetary cost limits across the agentic loop, blocking or warning when a configurable cost threshold is exceeded.
|
|
6
|
+
|
|
7
|
+
It uses `processInputStep` to check the cost limit before each LLM call. Cost data is queried from the observability storage APIs (`getMetricAggregate`) for all scopes. For all scopes except `run`, it aggregates cost across runs within a configurable time window (defaults to 7 days). For `run` scope, it queries cost for the current trace.
|
|
8
|
+
|
|
9
|
+
For token-based limits, use `TokenLimiterProcessor` instead.
|
|
10
|
+
|
|
11
|
+
> **Renamed from `CostGuardProcessor`.** The `CostGuardProcessor` export (and its `CostGuard*` option and detail types) remains available as a deprecated alias for the same class, including the `'token-cost-control'` processor id. Migrate imports to `TokenCostControl`.
|
|
12
|
+
|
|
13
|
+
Supports six scoping modes:
|
|
14
|
+
|
|
15
|
+
- **Run scope**: Tracks cost within a single agent run via trace ID
|
|
16
|
+
- **Resource scope** (default): Tracks cumulative cost per `resourceId` across runs
|
|
17
|
+
- **Thread scope**: Tracks cumulative cost per `threadId` across runs
|
|
18
|
+
- **User scope**: Tracks cumulative cost per `userId` across runs
|
|
19
|
+
- **Organization scope**: Tracks cumulative cost per `organizationId` across runs
|
|
20
|
+
- **Session scope**: Tracks cumulative cost per `sessionId` across runs
|
|
21
|
+
|
|
22
|
+
> **Approximate cost control.** Cost data is persisted asynchronously via buffered exporters in the observability pipeline. Fast-running agents may exceed the configured limit before metrics are available for query. Treat `maxCost` as an approximate threshold that fast-running agents may exceed.
|
|
23
|
+
|
|
24
|
+
> **Agent attribution only.** Cost is attributed via the `entityType: 'agent'` metric filter. Model calls made outside an agent run (for example, direct model usage in workflow steps) have no agent parent span and aren't counted by this guard.
|
|
25
|
+
|
|
26
|
+
## Usage example
|
|
27
|
+
|
|
28
|
+
Track cumulative cost per resource (default scope):
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { TokenCostControl } from '@mastra/core/processors'
|
|
32
|
+
|
|
33
|
+
const tokenCostControl = new TokenCostControl({
|
|
34
|
+
maxCost: 1.0,
|
|
35
|
+
})
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Track cumulative cost per thread with a 24-hour window and a soft warning at 80% of the limit:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { TokenCostControl } from '@mastra/core/processors'
|
|
42
|
+
|
|
43
|
+
const tokenCostControl = new TokenCostControl({
|
|
44
|
+
maxCost: 5.0,
|
|
45
|
+
scope: 'thread',
|
|
46
|
+
window: '24h',
|
|
47
|
+
warnAtPercent: 80,
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Use a per-tier budget by passing a function as `maxCost`:
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { TokenCostControl } from '@mastra/core/processors'
|
|
55
|
+
|
|
56
|
+
const tokenCostControl = new TokenCostControl({
|
|
57
|
+
maxCost: requestContext => (requestContext?.get('tier') === 'pro' ? 10.0 : 1.0),
|
|
58
|
+
scope: 'user',
|
|
59
|
+
})
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Attach to an agent with an `onViolation` callback and a per-provider/model breakdown:
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { Agent } from '@mastra/core/agent'
|
|
66
|
+
import { TokenCostControl } from '@mastra/core/processors'
|
|
67
|
+
|
|
68
|
+
const tokenCostControl = new TokenCostControl({
|
|
69
|
+
maxCost: 5.0,
|
|
70
|
+
scope: 'resource',
|
|
71
|
+
window: '30d',
|
|
72
|
+
strategy: 'warn',
|
|
73
|
+
includeBreakdown: true,
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
tokenCostControl.onViolation = ({ detail }) => {
|
|
77
|
+
console.log(
|
|
78
|
+
`Cost ${detail.threshold} threshold for ${detail.scopeKey}: $${detail.usage}/$${detail.limit}`,
|
|
79
|
+
)
|
|
80
|
+
for (const entry of detail.breakdown ?? []) {
|
|
81
|
+
console.log(` ${entry.provider}/${entry.model}: $${entry.estimatedCost}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const agent = new Agent({
|
|
86
|
+
id: 'my-agent',
|
|
87
|
+
name: 'my-agent',
|
|
88
|
+
model: 'openai/gpt-5-nano',
|
|
89
|
+
inputProcessors: [tokenCostControl],
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
## Constructor parameters
|
|
94
|
+
|
|
95
|
+
**maxCost** (`number | ((requestContext?: RequestContext) => number)`): Maximum estimated cost allowed (e.g. 0.50 for $0.50 USD). A number must be finite and positive. A function is called with the request's RequestContext on every check, enabling per-tier or per-user budgets; if it returns anything other than a finite positive number, the check is skipped for that request (fail-open) and a warning is logged. This is an approximate limit due to metric persistence delays.
|
|
96
|
+
|
|
97
|
+
**scope** (`'run' | 'resource' | 'thread' | 'user' | 'organization' | 'session'`): Scope for cost tracking. 'run' tracks cost within the current agent run via trace ID. 'resource' tracks cumulative cost per resourceId across runs (default). 'thread' tracks cumulative cost per threadId across runs. 'user', 'organization', and 'session' track cumulative cost per userId, organizationId, and sessionId respectively, read from the plain RequestContext keys 'userId', 'organizationId', and 'sessionId'. All scopes require observability storage with getMetricAggregate support. (Default: `'resource'`)
|
|
98
|
+
|
|
99
|
+
**window** (`'1h' | '6h' | '24h' | '7d' | '30d' | '365d'`): Time window for cost aggregation for all scopes except 'run'. (Default: `'7d'`)
|
|
100
|
+
|
|
101
|
+
**strategy** (`'block' | 'warn'`): Strategy when the cost limit is exceeded. 'block' aborts with a TripWire error. 'warn' logs a warning and calls onViolation at most once per request, then allows the step to proceed. (Default: `'block'`)
|
|
102
|
+
|
|
103
|
+
**message** (`string`): Custom message template for the abort reason. Supports {usage} and {limit} placeholders. (Default: `'Cost control: estimated cost limit exceeded ({usage}/{limit})'`)
|
|
104
|
+
|
|
105
|
+
**warnAtPercent** (`number`): Optional soft threshold as a percentage of maxCost (exclusive 0-100, e.g. 80). When the estimated cost reaches this percentage of the limit but is still below it, a warning is logged and onViolation is called once per request with threshold: "soft", regardless of strategy. Never aborts the step.
|
|
106
|
+
|
|
107
|
+
**includeBreakdown** (`boolean`): When true, violations (soft and hard) include a per-provider/model cost breakdown queried via getMetricBreakdown. The breakdown is capped at the top 10 provider/model groups ranked by aggregated token volume, not by spend. The breakdown query runs only when a violation trips, never on the happy path. If the configured store does not support breakdown queries or the query fails, the violation fires without the breakdown field. (Default: `false`)
|
|
108
|
+
|
|
109
|
+
## Instance properties
|
|
110
|
+
|
|
111
|
+
**id** (`'token-cost-control'`): Processor identifier.
|
|
112
|
+
|
|
113
|
+
**name** (`'Token Cost Control'`): Processor display name.
|
|
114
|
+
|
|
115
|
+
**onViolation** (`(violation: ProcessorViolation) => void | Promise<void>`): Callback invoked when a cost violation is detected, regardless of strategy. For the warn strategy and for soft thresholds, the guard calls it with a TokenCostControlViolationDetail (usage, limit, threshold, and optional breakdown) at most once per request per threshold level. Errors thrown by the callback on this path are caught and logged through the Mastra logger. For the block strategy, the processor runner invokes it with the TripWire metadata as the detail (see Error behavior) and silently catches callback errors. Use for side effects like alerting, logging to external systems, or emailing users.
|
|
116
|
+
|
|
117
|
+
**processInputStep** (`(args: ProcessInputStepArgs) => Promise<void>`): Checks cumulative estimated cost against the resolved maxCost before each LLM call. Queries observability storage for cost data: run scope filters by trace ID, all other scopes filter by their respective IDs with a time window. Calls abort() when the limit is exceeded (block strategy) or logs a warning (warn strategy). Cost checks are approximate due to metric persistence delays.
|
|
118
|
+
|
|
119
|
+
## Error behavior
|
|
120
|
+
|
|
121
|
+
When the `block` strategy is active (default), `TokenCostControl` calls `abort()` with `retry: false` when the cost limit is exceeded. The TripWire metadata includes:
|
|
122
|
+
|
|
123
|
+
- `processorId`: `'token-cost-control'`
|
|
124
|
+
- `usage`: Current cumulative usage (`estimatedCost`, `costUnit`)
|
|
125
|
+
- `maxCost`: The resolved cost limit for the request
|
|
126
|
+
- `scope`: The active scope
|
|
127
|
+
- `scopeKey`: The scope identifier for non-run scopes (if applicable)
|
|
128
|
+
- `threshold`: Always `'hard'`, since only the hard limit aborts
|
|
129
|
+
- `breakdown`: Per-provider/model cost entries (only when `includeBreakdown` is enabled and the breakdown query succeeds)
|
|
130
|
+
|
|
131
|
+
With the `warn` strategy, the hard-limit warning and `onViolation` callback fire at most once per request. Subsequent steps in the same request proceed without repeating the warning.
|
|
132
|
+
|
|
133
|
+
Numbers interpolated into violation messages are normalized to at most 6 decimal places, so messages never contain float precision artifacts.
|
|
134
|
+
|
|
135
|
+
## Scoping behavior
|
|
136
|
+
|
|
137
|
+
| Scope | Tracks across runs | Filter | Requires context |
|
|
138
|
+
| -------------- | ------------------ | ------------------------------ | ---------------------------------------- |
|
|
139
|
+
| `run` | No | `traceId` from current span | Tracing context (automatic) |
|
|
140
|
+
| `resource` | Yes | `resourceId` + time window | `resourceId` in `RequestContext` |
|
|
141
|
+
| `thread` | Yes | `threadId` + time window | `threadId` in `RequestContext` |
|
|
142
|
+
| `user` | Yes | `userId` + time window | `userId` key in `RequestContext` |
|
|
143
|
+
| `organization` | Yes | `organizationId` + time window | `organizationId` key in `RequestContext` |
|
|
144
|
+
| `session` | Yes | `sessionId` + time window | `sessionId` key in `RequestContext` |
|
|
145
|
+
|
|
146
|
+
All scopes require observability storage with `getMetricAggregate` support. If the Mastra instance doesn't have observability storage configured, an error is thrown at registration time.
|
|
147
|
+
|
|
148
|
+
For `run` scope, the processor reads the trace ID from the current span's tracing context. If no tracing context is available, the check is skipped (fail-open).
|
|
149
|
+
|
|
150
|
+
For all other scopes, if the required context ID is missing at runtime, the check is skipped. Observability query failures are handled with a fail-open strategy: if a query fails, a warning is logged through the Mastra logger and the step proceeds.
|
|
151
|
+
|
|
152
|
+
> **The `user`, `organization`, and `session` scopes require annotated traces.** These scopes match metric records by their `userId`, `organizationId`, and `sessionId` fields, which are populated from span metadata on the trace (for example, via tracing options metadata). If your traces don't carry the matching metadata, these scopes match zero records and the guard never trips. Setting the RequestContext key alone isn't enough: both the RequestContext key (for scope resolution) and the span metadata (for cost attribution) must be present.
|
|
153
|
+
|
|
154
|
+
> **Note on metric persistence delay.** The observability pipeline uses buffered exporters that flush metrics asynchronously. A short delay exists between when an LLM call completes and when its cost metrics are available for query. During high-frequency agent execution, the cost control may not detect a limit breach until one or more steps after the actual cost exceeded the threshold.
|
|
@@ -127,7 +127,7 @@ const { embeddings } = await embedMany({
|
|
|
127
127
|
})
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
-
> **Vector Database Compatibility:** When storing embeddings, the vector database index
|
|
130
|
+
> **Vector Database Compatibility:** When storing embeddings, configure the vector database index to match the output size of your embedding model to avoid errors or data corruption from mismatched dimensions.
|
|
131
131
|
|
|
132
132
|
## Example: Complete pipeline
|
|
133
133
|
|
|
@@ -201,7 +201,7 @@ const results = await store.query({
|
|
|
201
201
|
### Amazon S3 Vectors
|
|
202
202
|
|
|
203
203
|
- Equality values must be primitives (string/number/boolean). `null`/`undefined`, arrays, objects, and Date aren't allowed for equality. Range operators accept numbers or Date (Dates are normalized to epoch ms).
|
|
204
|
-
- `$in`/`$nin` require **non-empty arrays of primitives
|
|
204
|
+
- `$in`/`$nin` require **non-empty arrays of primitives**. Date elements are allowed and normalized to epoch ms. **Array equality** isn't supported.
|
|
205
205
|
- Implicit AND is canonicalized (`{a:1,b:2}` → `{$and:[{a:1},{b:2}]`). Logical operators must contain field conditions and use non-empty arrays. They may appear only at the root or within other logical operators (not inside field values).
|
|
206
206
|
- Keys listed in `nonFilterableMetadataKeys` at index creation are stored but not filterable. This setting is immutable.
|
|
207
207
|
- $exists requires a boolean value.
|
|
@@ -76,7 +76,7 @@ app.listen(3000, () => {
|
|
|
76
76
|
|
|
77
77
|
## Error handling
|
|
78
78
|
|
|
79
|
-
The Koa adapter propagates errors from route handlers up through Koa's middleware chain, following Koa's standard error handling pattern.
|
|
79
|
+
The Koa adapter propagates errors from route handlers up through Koa's middleware chain, following Koa's standard error handling pattern. You can use regular Koa error-handling middleware:
|
|
80
80
|
|
|
81
81
|
```typescript
|
|
82
82
|
const app = new Koa()
|
|
@@ -100,7 +100,7 @@ Returns: `boolean`
|
|
|
100
100
|
|
|
101
101
|
#### `handleWebhook(request)`
|
|
102
102
|
|
|
103
|
-
Process an incoming webhook request. Extracts the resource ID from the payload
|
|
103
|
+
Process an incoming webhook request. Extracts the resource ID from the payload and finds matching subscriptions. It builds a notification for each subscription and calls `notify()`.
|
|
104
104
|
|
|
105
105
|
```typescript
|
|
106
106
|
const result = await webhookProvider.handleWebhook({
|
|
@@ -234,7 +234,7 @@ const storage = new MongoDBStore({
|
|
|
234
234
|
|
|
235
235
|
`prune()` deletes rows but doesn't shrink the database file. On SQLite/libSQL the freed pages go on a freelist and are reused by future writes, so the file stops growing: for most users this alone solves the unbounded-growth problem.
|
|
236
236
|
|
|
237
|
-
Handing that free space back to the OS is a separate concern that Mastra doesn't manage. If you specifically need to shrink the file, run the underlying database's compaction (for example `VACUUM` on self-hosted libSQL) yourself in a maintenance window. A full `VACUUM` locks the file and needs roughly twice the file size in free disk. On PostgreSQL, autovacuum reclaims dead tuples for reuse automatically
|
|
237
|
+
Handing that free space back to the OS is a separate concern that Mastra doesn't manage. If you specifically need to shrink the file, run the underlying database's compaction (for example `VACUUM` on self-hosted libSQL) yourself in a maintenance window. A full `VACUUM` locks the file and needs roughly twice the file size in free disk. On PostgreSQL, autovacuum reclaims dead tuples for reuse automatically. A manual `VACUUM FULL` is only needed if you must return disk to the OS.
|
|
238
238
|
|
|
239
239
|
For MongoDB, deleted documents are reused by future insertions. To reclaim disk space, run [`db.runCommand({ compact: "collection_name" })`](https://www.mongodb.com/docs/manual/reference/command/compact/) during a maintenance window.
|
|
240
240
|
|
|
@@ -65,7 +65,7 @@ The tool returns an object with:
|
|
|
65
65
|
| ---------- | ------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
|
|
66
66
|
| `content` | `string` | The text content of the retrieved passage. |
|
|
67
67
|
| `source` | `string \| undefined` | The source URI when Bedrock provides one. Agentic retrieval only includes this field when the result metadata contains `_source_uri`. |
|
|
68
|
-
| `score` | `number \| undefined` | The relevance score returned by standard retrieval
|
|
68
|
+
| `score` | `number \| undefined` | The relevance score returned by standard retrieval, which the agentic API omits from result items. |
|
|
69
69
|
| `metadata` | `Record<string, unknown>` | Additional metadata from the retrieval result. |
|
|
70
70
|
|
|
71
71
|
## Retrieval Modes
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# MCPClient
|
|
4
4
|
|
|
5
|
-
The `MCPClient` class provides a way to manage multiple MCP server connections and their tools in a Mastra application. It handles connection lifecycle
|
|
5
|
+
The `MCPClient` class provides a way to manage multiple MCP server connections and their tools in a Mastra application. It handles connection lifecycle and tool namespacing while providing access to tools across all configured servers.
|
|
6
6
|
|
|
7
7
|
## Constructor
|
|
8
8
|
|
|
@@ -248,7 +248,7 @@ const res = await agent.stream(prompt, {
|
|
|
248
248
|
|
|
249
249
|
Returns every server's tools as plain, serializable definitions, grouped by server name and keyed by the server's own tool name (without the `serverName_toolName` namespacing that `listTools()` applies).
|
|
250
250
|
|
|
251
|
-
Unlike `listTools()`, the result contains no functions or references to a live client, so it can be passed through `JSON.stringify` and cached in Redis
|
|
251
|
+
Unlike `listTools()`, the result contains no functions or references to a live client, so it can be passed through `JSON.stringify` and cached in Redis or a database, as well as a build artifact. Each definition holds the data from the MCP `tools/list` response (name, description, input schema, output schema, annotations, and `_meta`), plus the server name, version, and instructions captured at discovery time.
|
|
252
252
|
|
|
253
253
|
```typescript
|
|
254
254
|
const definitions = await mcp.listToolDefinitions()
|
|
@@ -991,7 +991,7 @@ try {
|
|
|
991
991
|
|
|
992
992
|
Concurrent `authenticate()` calls for the same server join the pending flow. Different servers authenticate independently. With valid stored tokens the call reconnects without opening a browser.
|
|
993
993
|
|
|
994
|
-
Hosts that drive the flow themselves can capture the authorization code with the exported `createOAuthCallbackServer` helper, which binds a one-shot loopback server
|
|
994
|
+
Hosts that drive the flow themselves can capture the authorization code with the exported `createOAuthCallbackServer` helper, which binds a one-shot loopback server and validates the OAuth `state` parameter before resolving with the code. It creates a plain HTTP server, so it's only for local loopback redirects. Web applications that use an HTTPS redirect URL must host their own callback endpoint and drive the provider directly rather than using this helper:
|
|
995
995
|
|
|
996
996
|
```typescript
|
|
997
997
|
import { createOAuthCallbackServer, getCallbackUrlCandidates } from '@mastra/mcp'
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
# MCPServer
|
|
4
4
|
|
|
5
|
-
The `MCPServer` class provides the functionality to expose your existing Mastra tools and Agents as a Model Context Protocol (MCP) server.
|
|
5
|
+
The `MCPServer` class provides the functionality to expose your existing Mastra tools and Agents as a Model Context Protocol (MCP) server. Any MCP client, such as Cursor, Windsurf, or Claude Desktop, can connect to these capabilities and make them available to an agent.
|
|
6
6
|
|
|
7
7
|
Note that if you only need to use your tools or agents directly within your Mastra application, you don't necessarily need to create an MCP server. This API is specifically for exposing your Mastra tools and agents to _external_ MCP clients.
|
|
8
8
|
|
|
@@ -95,7 +95,7 @@ The constructor accepts an `MCPServerConfig` object with the following propertie
|
|
|
95
95
|
|
|
96
96
|
A powerful feature of `MCPServer` is its ability to automatically expose your Mastra Agents as callable tools. When you provide agents in the `agents` property of the configuration:
|
|
97
97
|
|
|
98
|
-
- **Tool Naming**: Each agent is converted into a tool
|
|
98
|
+
- **Tool Naming**: Each agent is converted into a tool with the name `ask_<agentKey>`, where `<agentKey>` is the key you used for that agent in the `agents` object. For instance, if you configure `agents: { myAgentKey: myAgentInstance }`, a tool with the name `ask_myAgentKey` will be created.
|
|
99
99
|
|
|
100
100
|
- **Tool Functionality**:
|
|
101
101
|
|
|
@@ -104,7 +104,7 @@ A powerful feature of `MCPServer` is its ability to automatically expose your Ma
|
|
|
104
104
|
- **Execution**: When this tool is called, it invokes the corresponding agent's `generate()` method with the provided `query`.
|
|
105
105
|
- **Output**: The direct result from the agent's `generate()` method is returned as the output of the tool.
|
|
106
106
|
|
|
107
|
-
- **Name collisions.** If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., a tool
|
|
107
|
+
- **Name collisions.** If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., a tool with the name `ask_myAgentKey` alongside an agent keyed as `myAgentKey`), the _explicitly defined tool will take precedence_. The agent won't be converted into a tool in this conflicting case, and a warning will be logged.
|
|
108
108
|
|
|
109
109
|
This makes it straightforward to allow MCP clients to interact with your agents using natural language queries, like any other tool.
|
|
110
110
|
|
|
@@ -116,7 +116,7 @@ The description for this generated tool will be: "Ask agent `<agent.name>` a que
|
|
|
116
116
|
|
|
117
117
|
For an agent to be converted into a tool, it **must** have a non-empty `description` string property set in its configuration when it was instantiated (e.g., `new Agent({ id: 'my-agent', name: 'myAgent', description: 'This agent does X.', ... })`). If an agent is passed to `MCPServer` with a missing or empty `description`, an error will be thrown when the `MCPServer` is instantiated, and server setup will fail.
|
|
118
118
|
|
|
119
|
-
|
|
119
|
+
Clients can use MCP to access your agents' generative capabilities and ask them questions directly.
|
|
120
120
|
|
|
121
121
|
### Accessing MCP Context in Tools
|
|
122
122
|
|
|
@@ -1240,7 +1240,7 @@ const customMiddleware = createOAuthMiddleware({
|
|
|
1240
1240
|
|
|
1241
1241
|
## Authentication context
|
|
1242
1242
|
|
|
1243
|
-
Tools can access request metadata via `context.mcp.extra` when using HTTP-based transports.
|
|
1243
|
+
Tools can access request metadata via `context.mcp.extra` when using HTTP-based transports. You can pass authentication info and user context, as well as custom data from your HTTP middleware to your MCP tools.
|
|
1244
1244
|
|
|
1245
1245
|
### How it works
|
|
1246
1246
|
|