@mastra/mcp-docs-server 1.3.13-alpha.8 → 1.3.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/docs/agents/code-mode.md +4 -0
- package/.docs/docs/agents/tools.md +10 -2
- package/.docs/docs/auth/custom-auth-provider.md +66 -0
- package/.docs/docs/auth/jwt.md +1 -1
- package/.docs/docs/connections/mcp.md +11 -71
- package/.docs/docs/evals/overview.md +4 -0
- package/.docs/docs/harness/durable-agents.md +36 -0
- package/.docs/docs/harness/schedules.md +1 -1
- package/.docs/docs/memory/multi-user-threads.md +57 -127
- package/.docs/docs/memory/observational-memory.md +196 -756
- package/.docs/docs/memory/overview.md +4 -0
- package/.docs/docs/memory/semantic-recall.md +2 -0
- package/.docs/docs/observability/feedback.md +5 -7
- package/.docs/docs/sandbox/overview.md +2 -0
- package/.docs/docs/sandbox/search.md +25 -0
- package/.docs/docs/server/mcp.md +315 -0
- package/.docs/docs/server/request-context.md +35 -0
- package/.docs/docs/studio/deployment.md +0 -1
- package/.docs/docs/studio/overview.md +4 -4
- package/.docs/docs/workflows/scheduled-workflows.md +26 -5
- package/.docs/integrations/deploy/temporal.md +74 -0
- package/.docs/integrations/sandboxes/docker.md +39 -0
- package/.docs/integrations/sandboxes/e2b.md +1 -1
- package/.docs/integrations/voice/openai.md +2 -2
- package/.docs/models/environment-variables.md +1 -1
- package/.docs/models/gateways/azure-openai.md +61 -2
- package/.docs/models/gateways/custom-gateways.md +74 -44
- package/.docs/models/gateways/requesty.md +221 -0
- package/.docs/models/gateways.md +7 -1
- package/.docs/models/index.md +0 -1
- package/.docs/models/providers/aihubmix.md +2 -0
- package/.docs/models/providers/amazon-bedrock.md +4 -0
- package/.docs/models/providers/azure.md +4 -0
- package/.docs/models/providers/cloudflare-ai-gateway.md +2 -0
- package/.docs/models/providers/cohere.md +2 -0
- package/.docs/models/providers/google-vertex.md +2 -0
- package/.docs/models/providers/ollama.md +2 -0
- package/.docs/models/providers/qvac.md +2 -0
- package/.docs/models/providers.md +0 -1
- package/.docs/reference/agents/generate.md +1 -1
- package/.docs/reference/agents/generateLegacy.md +7 -23
- package/.docs/reference/auth/jwt.md +26 -2
- package/.docs/reference/cli/mastra.md +74 -6
- package/.docs/reference/client-js/observability.md +13 -5
- package/.docs/reference/configuration.md +64 -0
- package/.docs/reference/connect/tools.md +59 -0
- package/.docs/reference/editor/mastra-editor.md +1 -1
- package/.docs/reference/index.md +1 -0
- package/.docs/reference/memory/observational-memory.md +350 -151
- package/.docs/reference/observability/feedback.md +57 -14
- package/.docs/reference/observability/tracing/advanced-queries.md +3 -1
- package/.docs/reference/observability/tracing/exporters/langfuse.md +24 -1
- package/.docs/reference/observability/tracing/exporters/mastra-platform-exporter.md +19 -16
- package/.docs/reference/observability/tracing/exporters/otel.md +1 -1
- package/.docs/reference/observability/tracing/interfaces.md +2 -2
- package/.docs/reference/observability/tracing/spans.md +7 -1
- package/.docs/reference/processors/processor-interface.md +23 -0
- package/.docs/reference/processors/semantic-recall-processor.md +2 -0
- package/.docs/reference/rag/chunk.md +1 -1
- package/.docs/reference/rag/graph-rag-guide.md +3 -1
- package/.docs/reference/rag/graph-rag.md +1 -1
- package/.docs/reference/rag/rerank.md +1 -1
- package/.docs/reference/rag/rerankWithScorer.md +1 -1
- package/.docs/reference/schedules/overview.md +25 -5
- package/.docs/reference/server/dispatch-route.md +124 -0
- package/.docs/reference/server/elysia-adapter.md +1 -1
- package/.docs/reference/server/express-adapter.md +17 -11
- package/.docs/reference/server/fastify-adapter.md +1 -1
- package/.docs/reference/server/hono-adapter.md +1 -1
- package/.docs/reference/server/invoke-route.md +10 -0
- package/.docs/reference/server/koa-adapter.md +1 -1
- package/.docs/reference/server/nestjs-adapter.md +1 -1
- package/.docs/reference/streaming/agents/stream.md +1 -1
- package/.docs/reference/tools/mcp-client.md +11 -1
- package/.docs/reference/workspace/factory-sandbox.md +20 -1
- package/.docs/reference/workspace/platform-sandbox.md +1 -1
- package/package.json +5 -5
- package/.docs/models/providers/requesty.md +0 -261
|
@@ -241,6 +241,80 @@ export const { createWorkflow, createStep } = init({
|
|
|
241
241
|
})
|
|
242
242
|
```
|
|
243
243
|
|
|
244
|
+
## Compatibility
|
|
245
|
+
|
|
246
|
+
`@mastra/temporal` is experimental. It supports a subset of the Mastra workflow API, and anything it can't compile fails when the worker builds, not at run time.
|
|
247
|
+
|
|
248
|
+
### Run APIs
|
|
249
|
+
|
|
250
|
+
| Method | Temporal support |
|
|
251
|
+
| ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
252
|
+
| `start()`, `startAsync()` | Starts a Temporal workflow execution |
|
|
253
|
+
| `resume()`, `resumeAsync()` | Signals the Temporal execution to resume a suspended step. Pass `step`, or omit it when only one step is suspended. Resuming by `label` isn't supported |
|
|
254
|
+
| `cancel()` | Cancels the Temporal execution |
|
|
255
|
+
| `stream()`, `streamLegacy()`, `resumeStream()` | Throws. Use `start()` or `startAsync()` instead |
|
|
256
|
+
| `restart()`, `timeTravel()`, `timeTravelStream()` | Throws. Time travel is tracked in [#24025](https://github.com/mastra-ai/mastra/issues/24025) |
|
|
257
|
+
| `observeStream()`, `watch()` | Not connected to Temporal. Runs emit no Mastra events |
|
|
258
|
+
|
|
259
|
+
Run state, step results, and events stay in Temporal and aren't written to Mastra storage, so Temporal runs don't appear in Studio's run history. Studio's workflow runner calls `stream()`, which throws for Temporal workflows. Use the [Temporal UI](https://docs.temporal.io/web-ui) to inspect runs. Projecting Temporal runs into Mastra APIs and Studio is tracked in [#24016](https://github.com/mastra-ai/mastra/issues/24016).
|
|
260
|
+
|
|
261
|
+
### Workflow methods
|
|
262
|
+
|
|
263
|
+
The worker build reads each workflow chain statically, so arguments must be resolvable at build time. A step can be an identifier or an inline `createStep()` call whose `id` is a string literal. Conditions for `.branch()`, `.dowhile()`, and `.dountil()` must be identifiers.
|
|
264
|
+
|
|
265
|
+
| Method | Supported arguments |
|
|
266
|
+
| -------------------------- | --------------------------------------------------------------------------------------------------- |
|
|
267
|
+
| `.then()` | A step or workflow |
|
|
268
|
+
| `.parallel()` | An array literal of steps or workflows |
|
|
269
|
+
| `.branch()` | An array literal of `[condition, step]` pairs |
|
|
270
|
+
| `.dowhile()`, `.dountil()` | A step and a condition |
|
|
271
|
+
| `.foreach()` | A step and an optional options object |
|
|
272
|
+
| `.sleep()` | A numeric literal or a function identifier |
|
|
273
|
+
| `.sleepUntil()` | A `new Date(...)` expression, a string or number literal, or a function identifier |
|
|
274
|
+
| `.map()` | An inline function or a top-level function identifier. Declarative object mappings aren't supported |
|
|
275
|
+
| `.commit()` | No arguments |
|
|
276
|
+
| Any other method | Not supported |
|
|
277
|
+
|
|
278
|
+
Condition functions and `.sleep()` or `.sleepUntil()` function identifiers are called as activities named after the identifier, but the plugin only generates activities for steps and `.map()` callbacks. Register these functions yourself in `Worker.create({ activities })`, or the run fails when it reaches them. These functions receive `inputData`, `runId`, `workflowId`, `resourceId`, and `requestContext` as a plain object. They don't receive `getInitData()`.
|
|
279
|
+
|
|
280
|
+
Nested workflows passed to `.then()` or `.parallel()` run as Temporal child workflows. Each one is registered as its own workflow type and appears as a separate execution in the Temporal UI. A suspended step inside a child workflow can't be resumed through the parent run.
|
|
281
|
+
|
|
282
|
+
An unsupported construct stops the worker build with an error that names the workflow and file:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
// Build error: .map() does not yet support declarative object mappings; use a callback mapping
|
|
286
|
+
workflow.map({ city: { step: fetchWeather, path: 'city' } })
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
### Step execution context
|
|
290
|
+
|
|
291
|
+
Each step runs as a Temporal activity. Its `execute` function receives:
|
|
292
|
+
|
|
293
|
+
- `inputData` and, when resuming, `resumeData`
|
|
294
|
+
- `runId`, `workflowId`, and `resourceId`
|
|
295
|
+
- `requestContext`
|
|
296
|
+
- `getInitData()`
|
|
297
|
+
- `suspend()`, except in `.foreach()` steps
|
|
298
|
+
- `mastra`, when the step's module can import the Mastra instance
|
|
299
|
+
|
|
300
|
+
Other `execute` parameters, such as `state`, `setState()`, `getStepResult()`, `bail()`, `abort()`, `writer`, and `tracingContext`, aren't available. Steps in a nested workflow receive the parent's `runId`, `resourceId`, and request context, and the nested workflow's own `workflowId`.
|
|
301
|
+
|
|
302
|
+
### Bundling and worker operation
|
|
303
|
+
|
|
304
|
+
On startup, `MastraPlugin` bundles your Mastra entry file and writes the generated code to `node_modules/.mastra/`:
|
|
305
|
+
|
|
306
|
+
- `workflow.mjs`: the workflow bundle, which runs in Temporal's deterministic workflow sandbox
|
|
307
|
+
- `activities.mjs`: the step and mapping code, which runs as regular Node.js activities
|
|
308
|
+
- `activity-bindings.json`: the map from step ids to activity exports
|
|
309
|
+
|
|
310
|
+
Keep I/O, clients, agents, tools, storage, and MCP connections inside step `execute` functions. The workflow bundle can't import Node.js builtins such as `node:fs`. When it does, the build fails with `Temporal workflow bundle cannot depend on Node.js builtin`, and you need to move that dependency into a step. Check the generated files in `node_modules/.mastra/` when a build or activity lookup fails.
|
|
311
|
+
|
|
312
|
+
A run started while no worker polls the task queue stays queued without an error. Before you send traffic, confirm that a worker is polling:
|
|
313
|
+
|
|
314
|
+
```bash
|
|
315
|
+
temporal task-queue describe --task-queue mastra
|
|
316
|
+
```
|
|
317
|
+
|
|
244
318
|
## Constraints and notes
|
|
245
319
|
|
|
246
320
|
- Workflow ids must be static string literals. The build-time transformer reads the literal value to derive Temporal workflow export names.
|
|
@@ -118,6 +118,45 @@ Markers tell a session what the image already ran: `.mastra-sandbox/repos/<repo>
|
|
|
118
118
|
|
|
119
119
|
If any entry's access or head can't be resolved, the whole resolution rejects and the sandbox falls back to its default template.
|
|
120
120
|
|
|
121
|
+
## Docker Cloud Sandboxes
|
|
122
|
+
|
|
123
|
+
`DockerSandboxesSandbox` runs commands in a Docker Cloud Sandboxes microVM instead of a local container. Supply an authenticated `@docker/sandboxes` client to access Docker Cloud Sandboxes.
|
|
124
|
+
|
|
125
|
+
```typescript
|
|
126
|
+
import { Sandboxes, pat } from '@docker/sandboxes'
|
|
127
|
+
import { Workspace } from '@mastra/core/workspace'
|
|
128
|
+
import { DockerSandboxesSandbox } from '@mastra/docker'
|
|
129
|
+
|
|
130
|
+
const client = new Sandboxes({
|
|
131
|
+
auth: pat({
|
|
132
|
+
username: process.env.DOCKER_USERNAME!,
|
|
133
|
+
personalAccessToken: process.env.DOCKER_PAT!,
|
|
134
|
+
}),
|
|
135
|
+
})
|
|
136
|
+
const workspace = new Workspace({
|
|
137
|
+
sandbox: new DockerSandboxesSandbox({
|
|
138
|
+
id: sessionId,
|
|
139
|
+
image: 'ubuntu:24.04',
|
|
140
|
+
environment: { NODE_ENV: 'production' },
|
|
141
|
+
client,
|
|
142
|
+
}),
|
|
143
|
+
})
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
The `id` is used as the remote sandbox display name for reconnection lookup, so a new instance with the same `id` reconnects to an existing microVM and restarts it if it was stopped. Each instance uses a separate creation idempotency key. Pass `sandboxId` to reconnect directly to a known remote sandbox.
|
|
147
|
+
|
|
148
|
+
Commands run through `sh -lc`, so command strings can use pipes, redirects and `&&`. Separate `args` are shell-quoted. `executeCommand` supports `timeout`, `abortSignal`, streaming `onStdout`/`onStderr` callbacks and `maxRetainedBytes` (default 1 MiB per stream). Calling `destroy()` deletes the remote sandbox.
|
|
149
|
+
|
|
150
|
+
**image** (`string`): Image used to create the cloud sandbox.
|
|
151
|
+
|
|
152
|
+
**id** (`string`): Stable logical identifier used as the remote display name for reconnection lookup. (Default: `Auto-generated`)
|
|
153
|
+
|
|
154
|
+
**sandboxId** (`string`): Remote sandbox name to reconnect to directly.
|
|
155
|
+
|
|
156
|
+
**environment** (`Record<string, string>`): Environment variables set on a newly created cloud sandbox.
|
|
157
|
+
|
|
158
|
+
**client** (`Sandboxes`): Authenticated Docker Cloud Sandboxes client. Supply one to access Docker Cloud Sandboxes; caller-owned clients are not closed on destroy().
|
|
159
|
+
|
|
121
160
|
## Constructor parameters
|
|
122
161
|
|
|
123
162
|
**id** (`string`): Unique identifier for this sandbox instance. Used for label-based reconnection. (Default: `Auto-generated`)
|
|
@@ -400,7 +400,7 @@ Private repositories build warm templates when `getRepositoryAccess` returns an
|
|
|
400
400
|
|
|
401
401
|
Only pass short-lived credentials — the value stays in the template definition until the next rebuild, so its expiry is what bounds the exposure. Note that the cloned repository contents become part of a team-visible template image. When the access call returns no credential the clone is tokenless: public repositories build fine, and a private repository's build degrades to the fallback, where the session's own runtime setup (with runtime-injected auth) does the clone.
|
|
402
402
|
|
|
403
|
-
Setup commands that need their own credentials — a registry token, a private index URL — take them through `buildEnv`, which accepts a record or an async resolver. Those values are part of the template's identity, so changing one produces a new template.
|
|
403
|
+
Setup commands that need their own credentials — a registry token, a private index URL — take them through `buildEnv`, which accepts a record or an async resolver. Those values are part of the template's identity, so changing one produces a new template. To keep values out of the template name, also pass a `buildEnvRevision`. The values still reach the build steps like the clone credential, but only the revision names the template. Change the revision whenever the values change.
|
|
404
404
|
|
|
405
405
|
`repoTemplateRef` computes the tagged ref for a spec without building anything — useful for pruning old sha tags with the E2B CLI.
|
|
406
406
|
|
|
@@ -155,7 +155,7 @@ const voice = new OpenAIRealtimeVoice()
|
|
|
155
155
|
// Or initialize with specific configuration
|
|
156
156
|
const voiceWithConfig = new OpenAIRealtimeVoice({
|
|
157
157
|
apiKey: 'your-openai-api-key',
|
|
158
|
-
model: 'gpt-
|
|
158
|
+
model: 'gpt-realtime-2.1-mini',
|
|
159
159
|
speaker: 'alloy', // Default voice
|
|
160
160
|
})
|
|
161
161
|
|
|
@@ -198,7 +198,7 @@ voice.connect()
|
|
|
198
198
|
|
|
199
199
|
#### Constructor options
|
|
200
200
|
|
|
201
|
-
**model** (`string`): The model ID to use for real-time voice interactions. (Default: `'gpt-
|
|
201
|
+
**model** (`string`): The model ID to use for real-time voice interactions. (Default: `'gpt-realtime-2.1-mini'`)
|
|
202
202
|
|
|
203
203
|
**apiKey** (`string`): OpenAI API key. Falls back to OPENAI_API_KEY environment variable.
|
|
204
204
|
|
|
@@ -164,7 +164,6 @@ List of required environment variables for each model provider and gateway suppo
|
|
|
164
164
|
| [Qiniu](https://mastra.ai/models/providers/qiniu-ai) | `qiniu-ai/*` | `QINIU_API_KEY` |
|
|
165
165
|
| [Reflection](https://mastra.ai/models/providers/reflection) | `reflection/*` | `REFLECTION_API_KEY` |
|
|
166
166
|
| [Regolo AI](https://mastra.ai/models/providers/regolo-ai) | `regolo-ai/*` | `REGOLO_API_KEY` |
|
|
167
|
-
| [Requesty](https://mastra.ai/models/providers/requesty) | `requesty/*` | `REQUESTY_API_KEY` |
|
|
168
167
|
| [routing.run](https://mastra.ai/models/providers/routing-run) | `routing-run/*` | `ROUTING_RUN_API_KEY` |
|
|
169
168
|
| [RunInfra](https://mastra.ai/models/providers/runinfra) | `runinfra/*` | `RUNINFRA_GATEWAY_KEY` |
|
|
170
169
|
| [Sakana AI](https://mastra.ai/models/providers/sakana) | `sakana/*` | `SAKANA_API_KEY` |
|
|
@@ -230,4 +229,5 @@ List of required environment variables for each model provider and gateway suppo
|
|
|
230
229
|
| [Neon](https://mastra.ai/models/gateways/neon) (Gateway) | `neon/*` | `NEON_AI_GATEWAY_BASE_URL`, `NEON_AI_GATEWAY_TOKEN` |
|
|
231
230
|
| [Netlify](https://mastra.ai/models/gateways/netlify) (Gateway) | `netlify/*` | `NETLIFY_TOKEN`, `NETLIFY_SITE_ID` |
|
|
232
231
|
| [OpenRouter](https://mastra.ai/models/gateways/openrouter) (Gateway) | `openrouter/*` | `OPENROUTER_API_KEY` |
|
|
232
|
+
| [Requesty](https://mastra.ai/models/gateways/requesty) (Gateway) | `requesty/*` | `REQUESTY_API_KEY` |
|
|
233
233
|
| [Vercel](https://mastra.ai/models/gateways/vercel) (Gateway) | `vercel/*` | `AI_GATEWAY_API_KEY` |
|
|
@@ -10,15 +10,44 @@ Unlike other providers that have fixed model names, Azure uses **deployment name
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
+
The Azure OpenAI gateway isn't registered by default. Register it on your Mastra instance, and register the agents that use it on the same instance. Otherwise `azure-openai/...` model IDs fail with `Could not find config for provider azure-openai`.
|
|
14
|
+
|
|
15
|
+
Reference your Azure deployment name in the agent's model ID:
|
|
16
|
+
|
|
13
17
|
```typescript
|
|
14
18
|
import { Agent } from "@mastra/core/agent";
|
|
15
19
|
|
|
16
|
-
const
|
|
20
|
+
export const myAgent = new Agent({
|
|
17
21
|
id: "my-agent",
|
|
18
22
|
name: "My Agent",
|
|
19
23
|
instructions: "You are a helpful assistant",
|
|
20
|
-
model: "azure-openai/my-gpt-5-4-deployment"
|
|
24
|
+
model: "azure-openai/my-gpt-5-4-deployment", // Use your Azure deployment name (autocompleted in dev mode)
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Register the gateway and the agent:
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Mastra } from "@mastra/core";
|
|
32
|
+
import { AzureOpenAIGateway } from "@mastra/core/llm";
|
|
33
|
+
import { myAgent } from "./agents/my-agent";
|
|
34
|
+
|
|
35
|
+
export const mastra = new Mastra({
|
|
36
|
+
gateways: {
|
|
37
|
+
"azure-openai": new AzureOpenAIGateway({
|
|
38
|
+
resourceName: "my-openai-resource",
|
|
39
|
+
apiKey: process.env.AZURE_API_KEY!,
|
|
40
|
+
deployments: ["my-gpt-5-4-deployment"],
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
agents: { myAgent },
|
|
21
44
|
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Get the agent from the Mastra instance and call it:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
const agent = mastra.getAgent("myAgent");
|
|
22
51
|
|
|
23
52
|
// Generate a response
|
|
24
53
|
const response = await agent.generate("Hello!");
|
|
@@ -30,6 +59,8 @@ for await (const chunk of stream) {
|
|
|
30
59
|
}
|
|
31
60
|
```
|
|
32
61
|
|
|
62
|
+
See [Configuration](#configuration) for other authentication and deployment discovery options.
|
|
63
|
+
|
|
33
64
|
Check [Azure OpenAI model availability](https://learn.microsoft.com/en-us/azure/ai-services/openai/concepts/models) for region-specific options.
|
|
34
65
|
|
|
35
66
|
## How Azure Deployments Work
|
|
@@ -218,6 +249,34 @@ Set `closeOnFinish: false` when you want to keep the socket open across follow-u
|
|
|
218
249
|
|
|
219
250
|
Do not send overlapping follow-up requests with `previous_response_id` on the same WebSocket transport. Mastra rejects overlapping continuation requests because Azure only keeps one in-flight response per connection. Wait for the active stream to finish before continuing the response chain.
|
|
220
251
|
|
|
252
|
+
### Strict structured output
|
|
253
|
+
|
|
254
|
+
The gateway sends [structured output](https://mastra.ai/docs/agents/structured-output) schemas and tool schemas without strict mode by default. Without strict mode the model can leave out keys, and Mastra then fails validation when a required key is missing.
|
|
255
|
+
|
|
256
|
+
Set `providerOptions.azure.strictJsonSchema: true` to have Azure enforce your schema:
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import { z } from "zod";
|
|
260
|
+
|
|
261
|
+
const response = await agent.generate("Summarize the ticket.", {
|
|
262
|
+
structuredOutput: {
|
|
263
|
+
schema: z.object({
|
|
264
|
+
summary: z.string(),
|
|
265
|
+
priority: z.enum(["low", "medium", "high"]).optional(),
|
|
266
|
+
}),
|
|
267
|
+
},
|
|
268
|
+
providerOptions: {
|
|
269
|
+
azure: { strictJsonSchema: true },
|
|
270
|
+
},
|
|
271
|
+
});
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
With strict mode on, Mastra prepares the schemas before sending them so Azure accepts them. Every key is listed as required, optional keys accept `null` and come back as `undefined`, and validation keywords such as `uniqueItems`, `pattern`, and `minimum` move into the field description. Mastra still validates the result against your original schema.
|
|
275
|
+
|
|
276
|
+
The setting covers the output schema and every tool in the request, because the gateway's AI SDK 5 Azure provider uses one flag for both. Azure strict mode can't express objects with free-form keys, such as `z.record()`, so keep strict mode off for agents whose output or tool schemas use them.
|
|
277
|
+
|
|
278
|
+
The `azure` key applies when `useResponsesAPI` is `true`. On the chat completions route, set `providerOptions.openai.strictJsonSchema` instead.
|
|
279
|
+
|
|
221
280
|
## Configuration Reference
|
|
222
281
|
|
|
223
282
|
| Option | Type | Required | Description |
|
|
@@ -15,16 +15,35 @@ Gateways handle provider-specific logic for accessing language models:
|
|
|
15
15
|
- URL construction for API endpoints
|
|
16
16
|
- Language model instance creation
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
## When to use custom gateways
|
|
19
19
|
|
|
20
|
-
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
20
|
+
Use a custom gateway to connect Mastra to open-weight models that you run yourself, such as gpt-oss, Llama, Gemma, Mistral, Qwen, or DeepSeek. Self-hosting fits when:
|
|
21
|
+
|
|
22
|
+
- Your industry's rules don't allow sending prompts or customer data to a third-party API (HIPAA, GDPR, finance, defense)
|
|
23
|
+
- You make enough model calls that per-token API pricing costs more than running your own GPUs
|
|
24
|
+
- You've fine-tuned an open-weight model on internal data and want your agents to use it
|
|
25
|
+
- You need a model that only changes when you upgrade it, so a provider update can't break your prompts
|
|
26
|
+
- Your agents run offline, on air-gapped networks, on edge hardware, or in the same data center as your app
|
|
27
|
+
|
|
28
|
+
The gateway lists the models behind your endpoint, so they get typed model IDs and appear in the Studio model picker. It can also resolve credentials on every call, and you can [limit the Studio model picker](#showing-only-custom-gateways-in-studio) to your own models.
|
|
29
|
+
|
|
30
|
+
To call a single endpoint with an OpenAI-compatible API, you don't need a gateway: set `url` in the model config instead. Mastra doesn't read an API key from environment variables for a `url` model, so also set `apiKey` when the endpoint requires authentication. See [Use local models with Mastra](https://mastra.ai/models).
|
|
24
31
|
|
|
25
32
|
## Creating a Custom Gateway
|
|
26
33
|
|
|
27
|
-
Implement `MastraModelGatewayInterface` for a plain object gateway, or extend `MastraModelGateway`
|
|
34
|
+
Implement `MastraModelGatewayInterface` for a plain object gateway, or extend the `MastraModelGateway` base class.
|
|
35
|
+
|
|
36
|
+
### Choose a plain object or a class
|
|
37
|
+
|
|
38
|
+
Both work the same way at runtime. The base class provides default `getId()`, `shouldEnable()`, `handlesModel()`, and `serializeForSpan()` methods, and Mastra applies the same defaults to a plain object that leaves them out.
|
|
39
|
+
|
|
40
|
+
Use a plain object when the gateway has fixed configuration and no state. Extend `MastraModelGateway` when the gateway:
|
|
41
|
+
|
|
42
|
+
- Takes options in its constructor, such as a base URL or API key
|
|
43
|
+
- Keeps private state, such as cached tokens
|
|
44
|
+
- Runs as several instances, such as one per region or version
|
|
45
|
+
|
|
46
|
+
The following example implements the interface with a plain object:
|
|
28
47
|
|
|
29
48
|
```typescript
|
|
30
49
|
import { type MastraModelGatewayInterface, type ProviderConfig } from '@mastra/core/llm';
|
|
@@ -50,35 +69,43 @@ const myPrivateGateway: MastraModelGatewayInterface = {
|
|
|
50
69
|
async getApiKey() {
|
|
51
70
|
return process.env.MY_API_KEY ?? '';
|
|
52
71
|
},
|
|
53
|
-
async resolveLanguageModel({ modelId, providerId, apiKey }) {
|
|
72
|
+
async resolveLanguageModel({ modelId, providerId, apiKey, headers }) {
|
|
54
73
|
return createOpenAICompatible({
|
|
55
74
|
name: providerId,
|
|
56
75
|
apiKey,
|
|
76
|
+
headers,
|
|
57
77
|
baseURL: 'https://api.myprovider.com/v1',
|
|
58
78
|
}).chatModel(modelId);
|
|
59
79
|
},
|
|
60
80
|
};
|
|
61
81
|
```
|
|
62
82
|
|
|
63
|
-
The following example extends
|
|
83
|
+
The following example extends `MastraModelGateway` and takes its ID, base URL, and API key as constructor options:
|
|
64
84
|
|
|
65
85
|
```typescript
|
|
66
|
-
import { MastraModelGateway, type ProviderConfig } from '@mastra/core/llm';
|
|
86
|
+
import { MastraModelGateway, type GatewayLanguageModel, type ProviderConfig } from '@mastra/core/llm';
|
|
67
87
|
import { createOpenAICompatible } from '@ai-sdk/openai-compatible-v5';
|
|
68
|
-
import type { LanguageModelV2 } from '@ai-sdk/provider-v5';
|
|
69
88
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
89
|
+
interface MyPrivateGatewayOptions {
|
|
90
|
+
id?: string;
|
|
91
|
+
baseURL?: string;
|
|
92
|
+
apiKey?: string;
|
|
93
|
+
}
|
|
74
94
|
|
|
75
|
-
|
|
95
|
+
class MyPrivateGateway extends MastraModelGateway {
|
|
96
|
+
readonly id: string;
|
|
76
97
|
readonly name = 'My Private Gateway';
|
|
77
98
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
99
|
+
private readonly baseURL: string;
|
|
100
|
+
private readonly apiKey?: string;
|
|
101
|
+
|
|
102
|
+
constructor(options: MyPrivateGatewayOptions = {}) {
|
|
103
|
+
super();
|
|
104
|
+
this.id = options.id ?? 'private';
|
|
105
|
+
this.baseURL = options.baseURL ?? 'https://api.myprovider.com/v1';
|
|
106
|
+
this.apiKey = options.apiKey;
|
|
107
|
+
}
|
|
108
|
+
|
|
82
109
|
async fetchProviders(): Promise<Record<string, ProviderConfig>> {
|
|
83
110
|
return {
|
|
84
111
|
'my-provider': {
|
|
@@ -86,57 +113,60 @@ class MyPrivateGateway extends MastraModelGateway {
|
|
|
86
113
|
models: ['openai/gpt-6.1-sol', 'anthropic/claude-sonnet-5-5'],
|
|
87
114
|
apiKeyEnvVar: 'MY_API_KEY',
|
|
88
115
|
gateway: this.id,
|
|
89
|
-
url:
|
|
116
|
+
url: this.baseURL,
|
|
90
117
|
},
|
|
91
118
|
};
|
|
92
119
|
}
|
|
93
120
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
* @param modelId - Full model ID (e.g., "private/my-provider/model-1")
|
|
97
|
-
* @param envVars - Environment variables (optional)
|
|
98
|
-
*/
|
|
99
|
-
buildUrl(modelId: string, envVars?: Record<string, string>): string {
|
|
100
|
-
return 'https://api.myprovider.com/v1';
|
|
121
|
+
buildUrl(): string {
|
|
122
|
+
return this.baseURL;
|
|
101
123
|
}
|
|
102
124
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
* @param modelId - Full model ID
|
|
106
|
-
*/
|
|
107
|
-
async getApiKey(modelId: string): Promise<string> {
|
|
108
|
-
const apiKey = process.env.MY_API_KEY;
|
|
125
|
+
async getApiKey(): Promise<string> {
|
|
126
|
+
const apiKey = this.apiKey ?? process.env.MY_API_KEY;
|
|
109
127
|
if (!apiKey) {
|
|
110
|
-
throw new Error(
|
|
128
|
+
throw new Error('Missing MY_API_KEY environment variable');
|
|
111
129
|
}
|
|
112
130
|
return apiKey;
|
|
113
131
|
}
|
|
114
132
|
|
|
115
|
-
|
|
116
|
-
* Create a language model instance
|
|
117
|
-
* @param args - Model ID, provider ID, and API key
|
|
118
|
-
*/
|
|
119
|
-
async resolveLanguageModel({
|
|
133
|
+
resolveLanguageModel({
|
|
120
134
|
modelId,
|
|
121
135
|
providerId,
|
|
122
136
|
apiKey,
|
|
137
|
+
headers,
|
|
123
138
|
}: {
|
|
124
139
|
modelId: string;
|
|
125
140
|
providerId: string;
|
|
126
141
|
apiKey: string;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
142
|
+
headers?: Record<string, string>;
|
|
143
|
+
}): GatewayLanguageModel {
|
|
130
144
|
return createOpenAICompatible({
|
|
131
145
|
name: providerId,
|
|
132
146
|
apiKey,
|
|
133
|
-
|
|
147
|
+
headers,
|
|
148
|
+
baseURL: this.baseURL,
|
|
134
149
|
supportsStructuredOutputs: true,
|
|
135
150
|
}).chatModel(modelId);
|
|
136
151
|
}
|
|
137
152
|
}
|
|
138
153
|
```
|
|
139
154
|
|
|
155
|
+
To run one instance per endpoint, give each instance its own `id`. Mastra keeps only the first gateway registered with a given ID, and the ID is the model ID prefix:
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
import { Mastra } from '@mastra/core';
|
|
159
|
+
|
|
160
|
+
const mastra = new Mastra({
|
|
161
|
+
gateways: {
|
|
162
|
+
privateEu: new MyPrivateGateway({ id: 'private-eu', baseURL: 'https://eu.api.myprovider.com/v1' }),
|
|
163
|
+
privateUs: new MyPrivateGateway({ id: 'private-us', baseURL: 'https://us.api.myprovider.com/v1' }),
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// Models: 'private-eu/my-provider/openai/gpt-6.1-sol' and 'private-us/my-provider/openai/gpt-6.1-sol'
|
|
168
|
+
```
|
|
169
|
+
|
|
140
170
|
### Gateway-owned authentication
|
|
141
171
|
|
|
142
172
|
Add `resolveAuth` when the gateway owns credential lookup. Mastra uses this hook before falling back to `getApiKey()`.
|
|
@@ -625,7 +655,7 @@ By default, **Studio** lists every external model provider (such as OpenAI, Anth
|
|
|
625
655
|
AUTO_BLOCK_EXTERNAL_PROVIDERS=true
|
|
626
656
|
```
|
|
627
657
|
|
|
628
|
-
When this variable is set to `true` or `1`, Mastra returns only the providers from gateways you register yourself. The static provider registry and the built-in gateways (`models.dev`, `netlify`, and `mastra`) are hidden from the model picker. This is useful when you route all model traffic through a single private gateway and don't want the other providers to appear.
|
|
658
|
+
When this variable is set to `true` or `1`, Mastra returns only the providers from gateways you register yourself. The static provider registry and the built-in gateways (`models.dev`, `netlify`, and `mastra`) are hidden from the model picker. This is useful when you route all model traffic through a single private gateway and don't want the other providers to appear. The filter applies to the server's `GET /agents/providers` endpoint, which Studio uses for the model picker, so API clients don't see the external providers either. It only changes which providers are listed. It doesn't stop agents from calling other providers in code.
|
|
629
659
|
|
|
630
660
|
With this variable set and no custom gateway registered, the model picker is empty. Register at least one custom gateway to expose its models.
|
|
631
661
|
|