@mastra/mcp-docs-server 1.2.2 → 1.2.3-alpha.11
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/{harness → agent-controller}/modes.md +19 -19
- package/.docs/docs/agent-controller/overview.md +128 -0
- package/.docs/docs/agent-controller/session.md +143 -0
- package/.docs/docs/{harness → agent-controller}/subagents.md +12 -12
- package/.docs/docs/agent-controller/threads-and-state.md +141 -0
- package/.docs/docs/{harness → agent-controller}/tool-approvals.md +23 -23
- package/.docs/docs/agents/agent-approval.md +38 -0
- package/.docs/docs/agents/channels.md +47 -0
- package/.docs/docs/agents/heartbeats.md +211 -0
- package/.docs/docs/agents/signals.md +25 -1
- package/.docs/docs/observability/metrics/overview.md +2 -2
- package/.docs/models/gateways/openrouter.md +1 -2
- package/.docs/models/gateways/vercel.md +3 -1
- package/.docs/models/index.md +1 -1
- package/.docs/models/providers/anthropic.md +4 -5
- package/.docs/models/providers/chutes.md +17 -43
- package/.docs/models/providers/deepinfra.md +3 -2
- package/.docs/models/providers/friendli.md +1 -3
- package/.docs/models/providers/gmicloud.md +18 -15
- package/.docs/models/providers/huggingface.md +2 -1
- package/.docs/models/providers/inceptron.md +10 -8
- package/.docs/models/providers/llmgateway.md +3 -6
- package/.docs/models/providers/neuralwatt.md +9 -5
- package/.docs/models/providers/novita-ai.md +1 -1
- package/.docs/models/providers/nvidia.md +2 -1
- package/.docs/models/providers/siliconflow-cn.md +50 -78
- package/.docs/models/providers/siliconflow.md +52 -73
- package/.docs/models/providers/stepfun.md +1 -1
- package/.docs/models/providers/subconscious.md +71 -0
- package/.docs/models/providers/synthetic.md +8 -6
- package/.docs/models/providers/tinfoil.md +77 -0
- package/.docs/models/providers/xiaomi.md +2 -2
- package/.docs/models/providers.md +2 -0
- package/.docs/reference/{harness/harness-class.md → agent-controller/agent-controller-class.md} +106 -106
- package/.docs/reference/{harness → agent-controller}/session.md +97 -91
- package/.docs/reference/agents/channels.md +3 -1
- package/.docs/reference/agents/listSuspendedRuns.md +91 -0
- package/.docs/reference/channels/channel-provider.md +65 -0
- package/.docs/reference/channels/slack-provider.md +226 -0
- package/.docs/reference/client-js/agents.md +21 -0
- package/.docs/reference/index.md +6 -2
- package/.docs/reference/observability/metrics/automatic-metrics.md +2 -2
- package/.docs/reference/pubsub/lease-provider.md +131 -0
- package/.docs/reference/pubsub/redis-streams.md +10 -2
- package/CHANGELOG.md +42 -0
- package/package.json +4 -4
- package/.docs/docs/harness/overview.md +0 -128
- package/.docs/docs/harness/session.md +0 -143
- package/.docs/docs/harness/threads-and-state.md +0 -141
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Modes
|
|
2
2
|
|
|
3
|
-
Modes define the different behaviors
|
|
3
|
+
Modes define the different behaviors an AgentController can run. Each mode layers its own instructions and tool overrides on top of a shared backing agent, so the same agent can act as a planner in one mode and an executor in another. The AgentController keeps exactly one mode active at a time, carries the thread and state across switches, and handles the transition between them.
|
|
4
4
|
|
|
5
|
-
Every
|
|
5
|
+
Every AgentController needs at least one mode — the `modes` array is required, and the AgentController throws at construction if it's empty. A single-purpose AgentController still defines one mode; multiple modes are how you give a session more than one behavior to switch between.
|
|
6
6
|
|
|
7
7
|
A mode supplies three things that change how the agent behaves:
|
|
8
8
|
|
|
@@ -14,13 +14,13 @@ Because every mode shares the same backing agent, thread, and state, switching m
|
|
|
14
14
|
|
|
15
15
|
## Quickstart
|
|
16
16
|
|
|
17
|
-
Import the `
|
|
17
|
+
Import the `AgentController` class and create a new instance with your agent and modes:
|
|
18
18
|
|
|
19
19
|
```typescript
|
|
20
|
-
import {
|
|
20
|
+
import { AgentController } from '@mastra/core/agent-controller'
|
|
21
21
|
import { myAgent } from './agents'
|
|
22
22
|
|
|
23
|
-
const
|
|
23
|
+
const agentController = new AgentController({
|
|
24
24
|
id: 'multi-mode',
|
|
25
25
|
agent: myAgent,
|
|
26
26
|
modes: [
|
|
@@ -34,7 +34,7 @@ const harness = new Harness({
|
|
|
34
34
|
],
|
|
35
35
|
})
|
|
36
36
|
|
|
37
|
-
await
|
|
37
|
+
await agentController.init()
|
|
38
38
|
```
|
|
39
39
|
|
|
40
40
|
## Defining modes
|
|
@@ -42,9 +42,9 @@ await harness.init()
|
|
|
42
42
|
Each mode requires an `id`. When a top-level `agent` is provided, modes layer instructions and tool overrides on the shared agent. Each mode can also specify a `defaultModelId` to bootstrap model selection:
|
|
43
43
|
|
|
44
44
|
```typescript
|
|
45
|
-
import {
|
|
45
|
+
import { AgentController } from '@mastra/core/agent-controller'
|
|
46
46
|
|
|
47
|
-
const
|
|
47
|
+
const agentController = new AgentController({
|
|
48
48
|
id: 'multi-mode',
|
|
49
49
|
agent: myAgent,
|
|
50
50
|
modes: [
|
|
@@ -102,7 +102,7 @@ Workspace tools use the same list as every other tool — reference them by thei
|
|
|
102
102
|
|
|
103
103
|
### Mode transitions
|
|
104
104
|
|
|
105
|
-
A mode can declare a `transitionsTo` target. When the `submit_plan` built-in tool runs in that mode, the
|
|
105
|
+
A mode can declare a `transitionsTo` target. When the `submit_plan` built-in tool runs in that mode, the AgentController transitions to the target mode on approval:
|
|
106
106
|
|
|
107
107
|
```typescript
|
|
108
108
|
const planMode = {
|
|
@@ -113,33 +113,33 @@ const planMode = {
|
|
|
113
113
|
}
|
|
114
114
|
```
|
|
115
115
|
|
|
116
|
-
On plan approval, the
|
|
116
|
+
On plan approval, the AgentController automatically switches to `build` mode. On rejection, the agent remains in `plan` mode to revise.
|
|
117
117
|
|
|
118
118
|
## Switching modes
|
|
119
119
|
|
|
120
|
-
The active mode lives on the Session, so call `
|
|
120
|
+
The active mode lives on the Session, so call `agentController.session.mode.switch()` to change it. The switch aborts any in-progress generation, saves the current model to the outgoing mode, and emits a `mode_changed` event. It then resolves the incoming mode's model and, when one resolves, applies it and emits a `model_changed` event:
|
|
121
121
|
|
|
122
122
|
```typescript
|
|
123
|
-
await
|
|
123
|
+
await agentController.session.mode.switch({ modeId: 'build' })
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
## Querying modes
|
|
127
127
|
|
|
128
|
-
The
|
|
128
|
+
The AgentController exposes the full mode catalog, while the Session tracks which mode is active. Use `agentController.listModes()` to read every configured mode, `agentController.session.mode.get()` for the active mode ID, and `agentController.session.mode.resolve()` for the active mode's full definition:
|
|
129
129
|
|
|
130
130
|
```typescript
|
|
131
131
|
// List all configured modes
|
|
132
|
-
const modes =
|
|
132
|
+
const modes = agentController.listModes()
|
|
133
133
|
|
|
134
134
|
// Get the current mode ID
|
|
135
|
-
const modeId =
|
|
135
|
+
const modeId = agentController.session.mode.get()
|
|
136
136
|
|
|
137
137
|
// Get the full mode object
|
|
138
|
-
const mode =
|
|
138
|
+
const mode = agentController.session.mode.resolve()
|
|
139
139
|
```
|
|
140
140
|
|
|
141
141
|
## Related
|
|
142
142
|
|
|
143
|
-
- [
|
|
144
|
-
- [Threads and state](https://mastra.ai/docs/
|
|
145
|
-
- [API reference](https://mastra.ai/reference/
|
|
143
|
+
- [AgentController overview](https://mastra.ai/docs/agent-controller/overview)
|
|
144
|
+
- [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state)
|
|
145
|
+
- [API reference](https://mastra.ai/reference/agent-controller/agent-controller-class)
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# AgentController overview
|
|
2
|
+
|
|
3
|
+
> **Beta:** The `AgentController` feature is in beta stage and subject to breaking changes in minor versions until it graduates from its beta status.
|
|
4
|
+
|
|
5
|
+
The AgentController is a session controller for building interactive agent applications. It handles the runtime concerns that sit between your UI and the agent loop: managing conversation threads, switching between agent modes, persisting state, gating tool execution with approvals, and coordinating subagents. You can focus on what your agent does rather than how to wire it together.
|
|
6
|
+
|
|
7
|
+
A AgentController exposes a [`Session`](https://mastra.ai/docs/agent-controller/session) — the per-conversation runtime state that tracks the active mode, model, thread binding, permission grants, follow-up queue, and token usage. The AgentController is the shared host; the Session is the conversation running inside it. In a multi-user host, the same AgentController can back many Sessions at once.
|
|
8
|
+
|
|
9
|
+
[Mastra Code](https://code.mastra.ai/) is the flagship AgentController implementation: A terminal-based coding agent with multi-model support, persistent conversations, and plan-then-execute workflows.
|
|
10
|
+
|
|
11
|
+
## What you can build
|
|
12
|
+
|
|
13
|
+
The AgentController gives you the runtime pieces to ship interactive agent applications. Each outcome below maps to a capability you can use today:
|
|
14
|
+
|
|
15
|
+
- **Resume a conversation exactly where the user left off.** Persistent [threads and state](https://mastra.ai/docs/agent-controller/threads-and-state) reload the active mode, model, and progress across restarts, so a coding agent or assistant picks up mid-task instead of starting over.
|
|
16
|
+
- **Gate destructive actions behind human approval.** [Tool approvals and permission policies](https://mastra.ai/docs/agent-controller/tool-approvals) let you require confirmation for risky operations like file writes or deployments, while trusted tools run automatically.
|
|
17
|
+
- **Move a task through distinct phases without losing context.** [Modes](https://mastra.ai/docs/agent-controller/modes) switch the agent's instructions, tools, and model on the same thread, so you can build a plan-then-execute coding agent or a research-then-draft assistant.
|
|
18
|
+
- **Let users pick the right model for each step.** Per-mode [model management](https://mastra.ai/reference/agent-controller/agent-controller-class) switches models at runtime and tracks usage, which powers copilot UIs where users trade speed for capability.
|
|
19
|
+
- **Delegate focused work to child agents.** [Subagents](https://mastra.ai/docs/agent-controller/subagents) run subtasks with constrained tools and can fork the parent conversation, so a research mode can spin off web search or code review without polluting the main thread.
|
|
20
|
+
- **Drive a live UI from agent activity.** The [event system](https://mastra.ai/docs/agent-controller/session) emits typed events and coalesced display snapshots, so your TUI or web app reflects message updates, mode changes, and pending approvals in real time.
|
|
21
|
+
- **Run long-lived autonomous agents.** Structured task lists, heartbeat handlers, and observational memory keep background task runners on track and let them learn across threads.
|
|
22
|
+
|
|
23
|
+
## When to use the AgentController
|
|
24
|
+
|
|
25
|
+
Use the AgentController when your application needs:
|
|
26
|
+
|
|
27
|
+
- Multiple agent modes that share one conversation thread (e.g., plan → build → review)
|
|
28
|
+
- A control layer between your UI and the agent loop (model switching, state persistence, thread management)
|
|
29
|
+
- Tool approval flows and permission policies for human-in-the-loop gating
|
|
30
|
+
- Subagent orchestration to delegate focused subtasks with constrained tools
|
|
31
|
+
- Session continuity with persistent threads, state, and observational memory across restarts
|
|
32
|
+
|
|
33
|
+
You could assemble all of this yourself on top of the [Agent class](https://mastra.ai/docs/agents/overview), which exposes the full agent loop, tools, and memory. The AgentController is an opinionated set of defaults that wires those pieces into one application style: an ongoing session where the agent acts as a collaborator rather than a one-shot endpoint. Reach for the Agent class directly when you want full control or a simple request-response call; reach for the AgentController when you want the collaborative-session model without building the runtime around it.
|
|
34
|
+
|
|
35
|
+
## Key capabilities
|
|
36
|
+
|
|
37
|
+
- **Session**: Per-conversation state — active thread, mode, model, grants, follow-ups, token usage, and the display snapshot — accessed through `agentController.session`. See [Session](https://mastra.ai/docs/agent-controller/session).
|
|
38
|
+
- **Modes**: Define distinct agent personalities (instructions, tools, model) and switch between them without losing conversation context. See [Modes](https://mastra.ai/docs/agent-controller/modes).
|
|
39
|
+
- **Threads and state**: Persist conversations and structured state across sessions, users, and mode switches. See [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state).
|
|
40
|
+
- **Subagents**: Spawn focused child agents with constrained tools for subtasks, optionally forking the parent conversation. See [Subagents](https://mastra.ai/docs/agent-controller/subagents).
|
|
41
|
+
- **Tool approvals and permissions**: Configure which tools require user confirmation, grant session-wide exceptions, and handle interactive tool suspension. See [Tool approvals](https://mastra.ai/docs/agent-controller/tool-approvals).
|
|
42
|
+
- **Model management**: Switch models per-mode at runtime, track usage, and resolve gateway-backed models through Mastra's [model router](https://mastra.ai/models).
|
|
43
|
+
- **Follow-ups and steering**: Queue messages while the agent is running, or inject mid-stream instructions to redirect the agent. Built on [signals](https://mastra.ai/docs/agents/signals).
|
|
44
|
+
- **Event system**: Subscribe to typed events (message updates, mode changes, tool approvals) or coalesced `AgentControllerDisplayState` snapshots to drive your UI. See [Events](https://mastra.ai/reference/agent-controller/agent-controller-class).
|
|
45
|
+
- **Observational memory**: Automatic summarization and reflection across threads for long-running agent sessions. See [Observational memory](https://mastra.ai/docs/memory/observational-memory).
|
|
46
|
+
|
|
47
|
+
## Quickstart
|
|
48
|
+
|
|
49
|
+
Import the `AgentController` class and create a new instance with an agent, storage backend, and modes:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { Agent } from '@mastra/core/agent'
|
|
53
|
+
import { AgentController } from '@mastra/core/agent-controller'
|
|
54
|
+
import { LibSQLStore } from '@mastra/libsql'
|
|
55
|
+
|
|
56
|
+
const agent = new Agent({
|
|
57
|
+
name: 'assistant',
|
|
58
|
+
instructions: 'Help the user plan and complete tasks.',
|
|
59
|
+
model: 'openai/gpt-5.5',
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
const agentController = new AgentController({
|
|
63
|
+
id: 'my-agent',
|
|
64
|
+
agent,
|
|
65
|
+
storage: new LibSQLStore({ url: 'file:./data.db' }),
|
|
66
|
+
modes: [
|
|
67
|
+
{
|
|
68
|
+
id: 'plan',
|
|
69
|
+
name: 'Plan',
|
|
70
|
+
metadata: { default: true },
|
|
71
|
+
instructions: 'Reason about changes before making them.',
|
|
72
|
+
},
|
|
73
|
+
{ id: 'build', name: 'Build', instructions: 'Implement the approved plan.' },
|
|
74
|
+
],
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
agentController.subscribe(event => {
|
|
78
|
+
if (event.type === 'message_update') {
|
|
79
|
+
console.log(event.message)
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
await agentController.init()
|
|
84
|
+
await agentController.selectOrCreateThread()
|
|
85
|
+
await agentController.sendMessage({ content: 'Hello!' })
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
> **Note:** Visit the [AgentController reference](https://mastra.ai/reference/agent-controller/agent-controller-class) for the full constructor parameters and method signatures.
|
|
89
|
+
|
|
90
|
+
## Architecture
|
|
91
|
+
|
|
92
|
+
The AgentController sits between your application layer and the underlying agent loop:
|
|
93
|
+
|
|
94
|
+
```text
|
|
95
|
+
┌───────────────────────────────────────┐
|
|
96
|
+
│ Your App (TUI/Web/API) │
|
|
97
|
+
└───────────────────────────────────────┘
|
|
98
|
+
│ commands ▲ events
|
|
99
|
+
▼ │
|
|
100
|
+
┌───────────────────────────────────────┐
|
|
101
|
+
│ AgentController │
|
|
102
|
+
│ Config · storage · threads │
|
|
103
|
+
│ permissions · subagents · events │
|
|
104
|
+
│ │
|
|
105
|
+
│ ┌─────────────────────────────────┐ │
|
|
106
|
+
│ │ agentController.session │ │
|
|
107
|
+
│ │ identity · thread · mode │ │
|
|
108
|
+
│ │ model · run · grants │ │
|
|
109
|
+
│ │ display state │ │
|
|
110
|
+
│ └─────────────────────────────────┘ │
|
|
111
|
+
└───────────────────────────────────────┘
|
|
112
|
+
│
|
|
113
|
+
▼
|
|
114
|
+
┌───────────────────────────────────────┐
|
|
115
|
+
│ Agent + Memory + Tools │
|
|
116
|
+
└───────────────────────────────────────┘
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Your app sends commands — send a message, switch mode, approve a tool call — and receives typed events such as `message_update` and `tool_approval_required`. The AgentController manages the lifecycle internally: persisting threads, routing to the correct mode agent, enforcing permissions, and emitting events as state changes.
|
|
120
|
+
|
|
121
|
+
## Next steps
|
|
122
|
+
|
|
123
|
+
- [Session](https://mastra.ai/docs/agent-controller/session): The per-conversation state on an AgentController
|
|
124
|
+
- [Modes](https://mastra.ai/docs/agent-controller/modes): Define and switch between agent personalities
|
|
125
|
+
- [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state): Manage persistent conversations
|
|
126
|
+
- [Subagents](https://mastra.ai/docs/agent-controller/subagents): Delegate focused subtasks
|
|
127
|
+
- [Tool approvals and permissions](https://mastra.ai/docs/agent-controller/tool-approvals): Human-in-the-loop gating
|
|
128
|
+
- [API reference](https://mastra.ai/reference/agent-controller/agent-controller-class): Full constructor and method docs
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# Session
|
|
2
|
+
|
|
3
|
+
A [`Session`](https://mastra.ai/reference/agent-controller/session) holds the live state of an AgentController: who the session is for, which thread is active, the selected mode and model, permission grants, queued follow-ups, token usage, application state, and the display snapshot. You reach it through `agentController.session`.
|
|
4
|
+
|
|
5
|
+
## What the Session tracks
|
|
6
|
+
|
|
7
|
+
The Session is the live state of a single user's interaction with the AgentController. It answers "what is true right now": who the session belongs to, which thread is currently bound, which mode and model are selected, what the user has approved, what's queued or running, the application state, and the snapshot to render. A session has one active thread at a time but can list, switch between, and clone many threads over its lifetime. Anything that describes the current state lives here; the AgentController owns the shared infrastructure every session runs on.
|
|
8
|
+
|
|
9
|
+
Because each session has its own identity and state, one AgentController can serve many users at once — each backed by its own Session — without one session's mode, grants, or active thread leaking into another.
|
|
10
|
+
|
|
11
|
+
The rest of this page walks through the Session's state. Each part is a focused sub-object on `agentController.session`:
|
|
12
|
+
|
|
13
|
+
- **Who and where**: [Identity](#identity) sets the stable session `id`, `ownerId`, and resource the session belongs to, and [Thread](#thread) tracks the currently bound thread.
|
|
14
|
+
- **How the agent behaves**: [Mode and model](#mode-and-model) controls which agent profile and model are active, and [Permission grants](#permission-grants) records what the user has approved to run without prompting.
|
|
15
|
+
- **What's happening right now**: [Run state](#run-state) reflects the in-flight generation, and [Follow-ups](#follow-ups) holds messages queued to send when it finishes.
|
|
16
|
+
- **What you store and render**: [State](#state) holds your application's structured data, and [Display state](#display-state) is the single snapshot your UI renders from.
|
|
17
|
+
|
|
18
|
+
The AgentController performs actions that change this state — switching threads, modes, or models — because those operations coordinate shared infrastructure and emit events. The Session is where you read the result. The sections below note this split where it matters.
|
|
19
|
+
|
|
20
|
+
## Identity
|
|
21
|
+
|
|
22
|
+
`session.identity` holds three stable identifiers for the conversation: a session `id`, an `ownerId`, and the resource ID. Threads are scoped to a resource ID, so this is what groups a conversation's threads together:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
const sessionId = agentController.session.identity.getId()
|
|
26
|
+
const ownerId = agentController.session.identity.getOwnerId()
|
|
27
|
+
const resourceId = agentController.session.identity.getResourceId()
|
|
28
|
+
const threadId = agentController.session.thread.getId()
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The session `id` and `ownerId` are stable for the life of the session — they don't change when the resource ID is switched. They mirror the `id` and `ownerId` fields on `SessionRecord` in storage, so storage layers can key sessions by a stable identifier rather than the mutable resource ID.
|
|
32
|
+
|
|
33
|
+
The resource ID is set when creating a session via [`agentController.createSession()`](https://mastra.ai/reference/agent-controller/agent-controller-class) and defaults to the agentController `id`. See [Resource IDs](https://mastra.ai/docs/agent-controller/threads-and-state) for how it scopes threads.
|
|
34
|
+
|
|
35
|
+
## Thread
|
|
36
|
+
|
|
37
|
+
`session.thread` owns the active thread binding and read access to threads and messages. The AgentController performs lifecycle transitions; the Session reads the result:
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
// Lifecycle transitions live on the AgentController
|
|
41
|
+
await agentController.switchThread({ threadId: 'thread-abc123' })
|
|
42
|
+
|
|
43
|
+
// Reads live on the Session
|
|
44
|
+
const threads = await agentController.session.thread.list()
|
|
45
|
+
const messages = await agentController.session.thread.listActiveMessages()
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
See [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state) for the full lifecycle.
|
|
49
|
+
|
|
50
|
+
## Mode and model
|
|
51
|
+
|
|
52
|
+
The AgentController defines the available modes in `config.modes`. The Session tracks which mode and model are _currently_ selected:
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// Switch mode (aborts the run, emits events)
|
|
56
|
+
await agentController.session.mode.switch({ modeId: 'build' })
|
|
57
|
+
|
|
58
|
+
// Read the current selection (Session state)
|
|
59
|
+
const modeId = agentController.session.mode.get()
|
|
60
|
+
const mode = agentController.session.mode.resolve()
|
|
61
|
+
const modelId = agentController.session.model.get()
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
See [Modes](https://mastra.ai/docs/agent-controller/modes) for how modes carry their own model.
|
|
65
|
+
|
|
66
|
+
## Permission grants
|
|
67
|
+
|
|
68
|
+
The AgentController owns permission _policy_ — which categories or tools require approval. The Session owns the _grants_ a user makes during the conversation. A grant lets a tool run for the rest of the session without prompting again:
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
// Grant a category or tool for the rest of the session
|
|
72
|
+
agentController.session.grantCategory('edit')
|
|
73
|
+
agentController.session.grantTool('mastra_workspace_execute_command')
|
|
74
|
+
|
|
75
|
+
// Inspect current grants
|
|
76
|
+
const grants = agentController.session.getGrants()
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Grants are intentionally in-memory and reset when the session restarts. See [Tool approvals](https://mastra.ai/docs/agent-controller/tool-approvals) for the approval flow.
|
|
80
|
+
|
|
81
|
+
## Run state
|
|
82
|
+
|
|
83
|
+
`session.run` tracks the in-flight generation: whether a run is active, its run and trace IDs, and the abort signal. Use it to reflect run status in your UI:
|
|
84
|
+
|
|
85
|
+
```typescript
|
|
86
|
+
if (agentController.session.run.isRunning()) {
|
|
87
|
+
// show a stop button
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Abort the active run (AgentController orchestration clears related state)
|
|
91
|
+
agentController.abort()
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
## Follow-ups
|
|
95
|
+
|
|
96
|
+
A user may want to add to the conversation while the agent is still working. Instead of dropping or interrupting those messages, the Session queues them on `session.followUps` and sends them when the current run finishes:
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
const queued = agentController.session.followUps.count()
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Queue a follow-up with `agentController.followUp({ content })`. To redirect the agent mid-run instead of waiting, use `agentController.steer({ content })`. Both build on [signals](https://mastra.ai/docs/agents/signals).
|
|
103
|
+
|
|
104
|
+
## State
|
|
105
|
+
|
|
106
|
+
`session.state` holds the conversation's application state — structured values that agents and the UI share, such as model preferences, feature flags, or progress. The AgentController defines the shape with a `stateSchema`; the Session owns the live snapshot, validates updates against the schema, and emits `state_changed` on every write:
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const state = agentController.session.state.get()
|
|
110
|
+
await agentController.session.state.set({ theme: 'light' })
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
See [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state) for defining a schema.
|
|
114
|
+
|
|
115
|
+
## Display state
|
|
116
|
+
|
|
117
|
+
A conversation emits many fine-grained events: tokens streaming in, tools starting and finishing, approvals pending, tasks updating, token usage climbing. Subscribing to each event type and reassembling the current picture yourself is tedious and error-prone. `session.displayState` does that work for you.
|
|
118
|
+
|
|
119
|
+
It's a reducer-maintained snapshot. The Session keeps one `AgentControllerDisplayState` object and folds every agentController event into it as it happens, so the snapshot always reflects the latest state. It captures everything a UI needs to render: whether the agent is running and what it's streaming, which tools and subagents are active, anything waiting on the user such as approvals, the current task list, and running totals like token usage and queued follow-ups.
|
|
120
|
+
|
|
121
|
+
Because it's a single object, you can drive an entire UI from one place: read the current snapshot with `get()`, and re-render whenever the AgentController emits `display_state_changed` (fired after every other event):
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const snapshot = agentController.session.displayState.get()
|
|
125
|
+
|
|
126
|
+
// Re-render from the snapshot on every change
|
|
127
|
+
agentController.subscribe(event => {
|
|
128
|
+
if (event.type === 'display_state_changed') {
|
|
129
|
+
render(agentController.session.displayState.get())
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
This is the recommended pattern for most UIs. Subscribe to individual typed events only when you need to react to a specific transition rather than re-render the whole view.
|
|
135
|
+
|
|
136
|
+
> **Note:** Visit the [Session reference](https://mastra.ai/reference/agent-controller/session) for the full list of sub-objects and method signatures.
|
|
137
|
+
|
|
138
|
+
## Related
|
|
139
|
+
|
|
140
|
+
- [AgentController overview](https://mastra.ai/docs/agent-controller/overview)
|
|
141
|
+
- [Threads and state](https://mastra.ai/docs/agent-controller/threads-and-state)
|
|
142
|
+
- [Tool approvals](https://mastra.ai/docs/agent-controller/tool-approvals)
|
|
143
|
+
- [Session reference](https://mastra.ai/reference/agent-controller/session)
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
# Subagents
|
|
2
2
|
|
|
3
|
-
Subagents let a parent agent delegate focused tasks to child agents with constrained tools and instructions. The
|
|
3
|
+
Subagents let a parent agent delegate focused tasks to child agents with constrained tools and instructions. The AgentController auto-generates a `subagent` built-in tool that the parent model can call to spawn any configured subagent type. Reach for a subagent when a task needs a different toolset or instruction set than the parent agent, when you want to isolate a subtask's messages from the parent thread, or when the parent should delegate to a cheaper or faster model for specific work.
|
|
4
4
|
|
|
5
5
|
## Quickstart
|
|
6
6
|
|
|
7
|
-
Define subagent types in the
|
|
7
|
+
Define subagent types in the AgentController constructor:
|
|
8
8
|
|
|
9
9
|
```typescript
|
|
10
10
|
import { Agent } from '@mastra/core/agent'
|
|
11
|
-
import {
|
|
11
|
+
import { AgentController } from '@mastra/core/agent-controller'
|
|
12
12
|
|
|
13
13
|
const agent = new Agent({
|
|
14
14
|
name: 'assistant',
|
|
@@ -16,7 +16,7 @@ const agent = new Agent({
|
|
|
16
16
|
model: 'openai/gpt-5.5',
|
|
17
17
|
})
|
|
18
18
|
|
|
19
|
-
const
|
|
19
|
+
const agentController = new AgentController({
|
|
20
20
|
id: 'with-subagents',
|
|
21
21
|
agent,
|
|
22
22
|
modes: [{ id: 'default', name: 'Default', metadata: { default: true } }],
|
|
@@ -46,7 +46,7 @@ The parent agent can then call the auto-generated `subagent` tool with a `task`
|
|
|
46
46
|
|
|
47
47
|
When you define a subagent, write its `description` and `instructions` to be clear and narrow. The `description` is what the parent model reads to decide _when_ to delegate, so state the single job the subagent is for and, by implication, what it's not for — "Reads files and gathers context without making changes" tells the parent to reach for it during exploration and to handle edits elsewhere. The `instructions` then keep the child agent inside that job. A subagent scoped to one task with a focused toolset stays predictable and is easier for the parent to route to; a broadly described subagent invites the parent to over-delegate and blurs the boundary between types. Pair the narrow scope with `allowedWorkspaceTools` so the subagent can only do what its description promises.
|
|
48
48
|
|
|
49
|
-
For the full list of subagent configuration options, see [`
|
|
49
|
+
For the full list of subagent configuration options, see [`AgentController` reference](https://mastra.ai/reference/agent-controller/agent-controller-class).
|
|
50
50
|
|
|
51
51
|
## Forked subagents
|
|
52
52
|
|
|
@@ -73,7 +73,7 @@ Or per-invocation via the `subagent` tool's `forked` input parameter, which over
|
|
|
73
73
|
|
|
74
74
|
Because a fork reuses the parent agent to keep the prompt prefix stable, the subagent's own definition is mostly set aside:
|
|
75
75
|
|
|
76
|
-
- Memory must be configured on the
|
|
76
|
+
- Memory must be configured on the AgentController, since forking clones the parent thread. Without it, the fork call returns an error.
|
|
77
77
|
- The fork runs with the parent agent's instructions, tools, and model. The subagent definition's own `instructions`, `tools`, and `defaultModelId` are ignored, and a per-invocation `modelId` is ignored too.
|
|
78
78
|
- Only the subagent definition's `description` still matters — it's what the parent model reads to decide when to delegate.
|
|
79
79
|
- Fork threads are tagged with `metadata.forkedSubagent === true` and hidden from `session.thread.list()` by default.
|
|
@@ -85,20 +85,20 @@ Set a global or per-type subagent model at runtime:
|
|
|
85
85
|
|
|
86
86
|
```typescript
|
|
87
87
|
// Global subagent model
|
|
88
|
-
await
|
|
88
|
+
await agentController.session.subagents.model.set({ modelId: 'anthropic/claude-sonnet-4-6' })
|
|
89
89
|
|
|
90
90
|
// Per-type override
|
|
91
|
-
await
|
|
91
|
+
await agentController.session.subagents.model.set({
|
|
92
92
|
modelId: 'anthropic/claude-haiku-4-5',
|
|
93
93
|
agentType: 'explore',
|
|
94
94
|
})
|
|
95
95
|
|
|
96
96
|
// Read current model
|
|
97
|
-
const modelId =
|
|
97
|
+
const modelId = agentController.session.subagents.model.get({ agentType: 'explore' })
|
|
98
98
|
```
|
|
99
99
|
|
|
100
100
|
## Related
|
|
101
101
|
|
|
102
|
-
- [
|
|
103
|
-
- [Tool approvals and permissions](https://mastra.ai/docs/
|
|
104
|
-
- [API reference](https://mastra.ai/reference/
|
|
102
|
+
- [AgentController overview](https://mastra.ai/docs/agent-controller/overview)
|
|
103
|
+
- [Tool approvals and permissions](https://mastra.ai/docs/agent-controller/tool-approvals)
|
|
104
|
+
- [API reference](https://mastra.ai/reference/agent-controller/agent-controller-class)
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# Threads and state
|
|
2
|
+
|
|
3
|
+
Threads and state are how an AgentController conversation survives beyond a single exchange. A **thread** is the persistent record of a conversation — its messages and metadata, saved to storage so a user can close the app and resume the same conversation later, or switch between several conversations. **State** is structured data attached to the conversation — values like model preferences, feature flags, or progress — that agents and your UI read and write as the conversation runs.
|
|
4
|
+
|
|
5
|
+
The two work together: the thread is the message history, and state is the shared scratchpad alongside it. Both persist across mode switches, model changes, and restarts, so nothing is lost when a user switches from plan mode to build mode or reopens the app the next day.
|
|
6
|
+
|
|
7
|
+
Thread _lifecycle_ transitions — create, switch, clone, delete — live on the AgentController because they coordinate the shared thread lock and emit events. The active thread binding and thread/message _reads_ live on the [`Session`](https://mastra.ai/docs/agent-controller/session) as `agentController.session.thread`.
|
|
8
|
+
|
|
9
|
+
## Threads
|
|
10
|
+
|
|
11
|
+
A thread holds one conversation's full message history. The AgentController binds the Session to one active thread at a time; messages you send and the agent's replies are appended to that thread and saved to storage. Threads let users resume a past conversation, keep several conversations side by side, or branch one into alternatives.
|
|
12
|
+
|
|
13
|
+
### Creating and selecting threads
|
|
14
|
+
|
|
15
|
+
On startup, call `selectOrCreateThread()` to resume the most recent thread or create a new one:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
await agentController.init()
|
|
19
|
+
const thread = await agentController.selectOrCreateThread()
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Create a thread explicitly with a title:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
const thread = await agentController.createThread({ title: 'New conversation' })
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Switching threads
|
|
29
|
+
|
|
30
|
+
Switch to an existing thread. The agentController aborts any in-progress generation, acquires a lock on the new thread, and emits a `thread_changed` event:
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
await agentController.switchThread({ threadId: 'thread-abc123' })
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Listing threads
|
|
37
|
+
|
|
38
|
+
List threads for the current resource. Forked subagent threads are hidden by default:
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
const threads = await agentController.session.thread.list()
|
|
42
|
+
|
|
43
|
+
// Include all resources
|
|
44
|
+
const allThreads = await agentController.session.thread.list({ allResources: true })
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### Cloning threads
|
|
48
|
+
|
|
49
|
+
Clone a thread to create a branch of the conversation. The agentController copies all messages and switches to the clone:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const cloned = await agentController.cloneThread({ title: 'Alternative approach' })
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Thread locking
|
|
56
|
+
|
|
57
|
+
Pass a `threadLock` to the AgentController constructor to prevent concurrent access from multiple processes. The lock is acquired before any thread operation and released on switch or delete:
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const agentController = new AgentController({
|
|
61
|
+
id: 'my-agent',
|
|
62
|
+
threadLock: {
|
|
63
|
+
acquire: async threadId => {
|
|
64
|
+
/* acquire lock or throw */
|
|
65
|
+
},
|
|
66
|
+
release: async threadId => {
|
|
67
|
+
/* release lock */
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
})
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## State
|
|
74
|
+
|
|
75
|
+
Where a thread stores the conversation's messages, state stores structured values that describe the conversation but aren't messages — model preferences, feature flags, UI settings, or progress markers. Agents can read and update state during a run, and your UI can react to changes, so state is how the agent and the interface stay in sync on shared facts. You define its shape with a schema, and every update is validated against that schema before it's applied.
|
|
76
|
+
|
|
77
|
+
### Defining a state schema
|
|
78
|
+
|
|
79
|
+
Pass a `stateSchema` (Standard JSON Schema) to validate state and extract defaults:
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
import { Agent } from '@mastra/core/agent'
|
|
83
|
+
import { AgentController } from '@mastra/core/agent-controller'
|
|
84
|
+
import { z } from 'zod'
|
|
85
|
+
|
|
86
|
+
const agent = new Agent({
|
|
87
|
+
name: 'assistant',
|
|
88
|
+
instructions: 'Help the user manage a stateful session.',
|
|
89
|
+
model: 'openai/gpt-5.5',
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
const agentController = new AgentController({
|
|
93
|
+
id: 'stateful-agent',
|
|
94
|
+
agent,
|
|
95
|
+
modes: [{ id: 'default', name: 'Default', metadata: { default: true } }],
|
|
96
|
+
stateSchema: z.object({
|
|
97
|
+
currentModelId: z.string().optional(),
|
|
98
|
+
theme: z.enum(['light', 'dark']).default('dark'),
|
|
99
|
+
}),
|
|
100
|
+
})
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
### Reading and writing state
|
|
104
|
+
|
|
105
|
+
State is owned by the [`Session`](https://mastra.ai/docs/agent-controller/session) as `agentController.session.state`:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
// Read the current state snapshot
|
|
109
|
+
const state = agentController.session.state.get()
|
|
110
|
+
|
|
111
|
+
// Update state — validates against schema and emits state_changed
|
|
112
|
+
await agentController.session.state.set({ theme: 'light' })
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
State changes emit a `state_changed` event with the new state and the set of changed keys.
|
|
116
|
+
|
|
117
|
+
## Resource IDs
|
|
118
|
+
|
|
119
|
+
Threads are scoped to a resource ID, which groups a conversation's threads by project, user, or workspace. Set it on the AgentController constructor; it defaults to the agentController `id` when omitted:
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const agentController = new AgentController({
|
|
123
|
+
id: 'my-agent',
|
|
124
|
+
resourceId: 'project-xyz',
|
|
125
|
+
})
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The resource ID is part of a conversation's identity, so you read it from the Session:
|
|
129
|
+
|
|
130
|
+
```typescript
|
|
131
|
+
const resourceId = agentController.session.identity.getResourceId()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
The session also has a stable `id` and `ownerId` (read with `session.identity.getId()` and `session.identity.getOwnerId()`). Unlike the resource ID, these don't change when you switch resources — see [Session identity](https://mastra.ai/docs/agent-controller/session) for details.
|
|
135
|
+
|
|
136
|
+
## Related
|
|
137
|
+
|
|
138
|
+
- [AgentController overview](https://mastra.ai/docs/agent-controller/overview)
|
|
139
|
+
- [Session](https://mastra.ai/docs/agent-controller/session)
|
|
140
|
+
- [Modes](https://mastra.ai/docs/agent-controller/modes)
|
|
141
|
+
- [API reference](https://mastra.ai/reference/agent-controller/agent-controller-class)
|