mastra-agent-approval-hitl
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Pause Mastra agent tool calls for a human: pre-execution approval with requireApproval and requireToolApproval, approve or decline with approveToolCall, declineToolCall, and their generate variants, runtime suspension with suspend() plus suspendSchema and resumeSchema, resumeStream and resumeGenerate, automatic resumption from the next chat message, askUserTool, listSuspendedRuns after a restart, and approvals that bubble up through supervisor agents. Use this skill whenever the user wants a person to confirm, reject, or supply data before an agent action runs, such as deleting records, sending email, issuing refunds, or spending money. Triggers on human-in-the-loop, HITL, tool approval, requireApproval, requireToolApproval, beforeApproval, tool-call-approval, tool-call-suspended, approveToolCall, declineToolCall, resumeStream, resumeGenerate, suspend, autoResumeSuspendedTools, listSuspendedRuns, snapshot not found. Also triggers for confirmation dialogs and approval UIs.
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-modelsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 3 are distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 62
All of this uses snapshots.
- Very specific: line 136
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and the installed Agent types.
- Very specific: line 140
Reject early without a human: when policy can decide: beforeApproval on the tool returns { proceed: false, output }.
- Very specific: line 143
Make it restart-safe: Use memory thread and resource ids, then listSuspendedRuns({ threadId, resourceId }) in the handler that receives the …
- Distinctive: line 162
After a restart, rediscover the run from storage:
- Distinctive: line 182
"snapshot not found": No storage on the Mastra instance.
- Very specific: line 186
Using the subagent's tool call id at the supervisor: Use the chunk's toolCallId (the delegation call).
- Very specific: line 193
Inferring "answered" from tool output: Use the tool-call-resumed chunk (payload.kind is 'approval' or 'suspension').
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Agent Approval and Human-in-the-Loop
Pause an agent's tool calls for a human decision or answer, then continue the same run.
When to use
- Destructive or irreversible tools: deletes, emails, payments, refunds
- Costly tools where a human should check the arguments first
- Tools that discover mid-run that they need a confirmation or extra data
- Asking the user a clarifying question from inside a run (
askUserTool) - Approval UIs that must survive a page refresh, restart, or load balancer
- Supervisor agents whose subagents call gated tools
When NOT to use
- Writing the tool itself, schemas, and
execute(usemastra-tools) - Blocking calls by policy with no human (use
mastra-tools:hooks.beforeToolCall, ormastra-processors-guardrails) - Suspending workflow steps for human input (use
mastra-workflow-suspend-resume) - Supervisor and subagent design (use
mastra-multi-agent) - Rendering approval cards with AI SDK
useChat()(usemastra-agentic-ui) - Configuring the storage that holds snapshots (use
mastra-storage) - Scoring whether a call is risky with a decision model (use
mastra-classifier)
Required inputs
- Which tools need approval: always, by name pattern, or by runtime condition
- Whether the human approves a fixed call or supplies data (approval vs
suspend()) - The approval channel: stream UI,
generate()API, webhook, or chat message - Whether approvals must survive restarts (persistent storage, memory thread ids)
- Whether a supervisor agent sits above the tool's agent
The framework: two ways to pause
| Pre-execution approval | Runtime suspension | |
| Set by | requireApproval: true on the tool, or requireToolApproval on the call | suspend() inside execute |
| Pauses | Before execute runs | Wherever execute calls suspend() |
| Stream chunk | tool-call-approval | tool-call-suspended |
generate() | finishReason: 'suspended' | finishReason: 'suspended' |
| Continue (stream) | approveToolCall / declineToolCall | resumeStream(resumeData, { runId }) |
| Continue (generate) | approveToolCallGenerate / declineToolCallGenerate | resumeGenerate(resumeData, { runId }) |
| Payload | toolCallId, toolName, args | Your suspendSchema shape |
| Auto-resume from chat | No | Yes, with autoResumeSuspendedTools |
All of this uses snapshots. Configure a storage provider on the Mastra instance, or you get "snapshot not found".
Approval
import { Agent } from '@mastra/core/agent'
import { createTool } from '@mastra/core/tools'
import { z } from 'zod'
const deleteTool = createTool({
id: 'delete-record',
description: 'Delete a record by ID',
inputSchema: z.object({ id: z.string() }),
outputSchema: z.object({ deleted: z.boolean() }),
requireApproval: true,
execute: async ({ id }) => {
await db.delete(id)
return { deleted: true }
},
})
const agent = new Agent({
id: 'my-agent',
name: 'My Agent',
instructions: 'You manage records.',
model: 'openai/gpt-6-luna',
tools: { deleteTool },
})
const stream = await agent.stream('Delete record abc-123')
for await (const chunk of stream.fullStream) {
if (chunk.type === 'tool-call-approval') {
const { toolCallId, toolName, args } = chunk.payload
const approved = await agent.approveToolCall({ runId: stream.runId, toolCallId })
for await (const c of approved.textStream) process.stdout.write(c)
}
}Decline with a reason so the model adjusts instead of retrying blindly:
await agent.declineToolCall({ runId: stream.runId, toolCallId, reason: 'Ask the user for their own email instead' })Suspension
const weatherTool = createTool({
id: 'get-weather',
description: 'Gets the weather for a city',
inputSchema: z.object({ location: z.string().optional() }),
suspendSchema: z.object({ question: z.string() }),
resumeSchema: z.object({ location: z.string() }),
execute: async ({ location }, context) => {
const { resumeData, suspend } = context?.agent ?? {}
const city = resumeData?.location ?? location
if (!city) {
return suspend?.({ question: 'Which city would you like the weather for?' })
}
return await fetchWeather(city)
},
})
// later: await agent.resumeStream({ location: 'London' }, { runId })suspend() does not throw. Always return it, or the code after it runs before the tool pauses.
Workflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and the installedAgenttypes. Otherwise fetchhttps://mastra.ai/docs/agents/human-in-the-loop.mdandhttps://mastra.ai/reference/agents/listSuspendedRuns.md. Trust these over memory. - Configure storage on
new Mastra({ storage }). Use a persistent store if approvals can outlive the process (seemastra-storage). - Pick the mechanism per tool. Fixed confirmation:
requireApproval: true. Needs user data or a conditional check:suspend()withsuspendSchemaandresumeSchema. Open question:askUserTool. - Add per-call gating if needed.
requireToolApproval: truegates everything. A function({ toolName, args, requestContext, workspace }) => booleangates selectively. - Reject early without a human when policy can decide:
beforeApprovalon the tool returns{ proceed: false, output }. - Handle the pause in the caller:
tool-call-approval/tool-call-suspendedchunks, orfinishReason: 'suspended'fromgenerate(). - Continue the run with the matching method and the
runId. PasstoolCallIdwhenever several calls can be pending. - Make it restart-safe. Use memory thread and resource ids, then
listSuspendedRuns({ threadId, resourceId })in the handler that receives the decision. - Enable chat-driven resume with
autoResumeSuspendedTools: true(memory andresumeSchemarequired) for conversational suspend flows. - Test approve, decline, and resume paths in Studio and after a server restart.
Deep dive: supervisors and restarts
When a subagent's tool needs approval or suspends, the pause bubbles up to the top supervisor, through any depth. The chunk's toolCallId is the supervisor's delegation call. Pass that id to approve, decline, or resume. Read delegatedToolName and delegatedToolCallId to label which subagent tool paused.
for await (const chunk of stream.fullStream) {
if (chunk.type === 'tool-call-approval' || chunk.type === 'tool-call-suspended') {
console.log('Paused:', chunk.payload.delegatedToolName, chunk.payload.delegatedToolCallId)
await supervisorAgent.approveToolCall({ runId: stream.runId, toolCallId: chunk.payload.toolCallId })
}
}After a restart, rediscover the run from storage:
const { runs } = await agent.listSuspendedRuns({ threadId: 'thread-123', resourceId: 'user-456' })
const run = runs[0]
const toolCall = run?.toolCalls[0]
if (run && toolCall) {
const stream = toolCall.requiresApproval
? await agent.approveToolCall({ runId: run.runId, toolCallId: toolCall.toolCallId })
: await agent.resumeStream({ location: 'San Francisco' }, { runId: run.runId })
}listSuspendedRuns() only returns runs owned by the agent you call it on. A supervisor sees its outer run. HTTP: GET /agents/:agentId/suspended-runs. Client SDK: agent.listSuspendedRuns().
Failure patterns
- "snapshot not found". No storage on the Mastra instance. Add one.
- Approvals lost after deploy or refresh. In-memory storage drops snapshots. Use a persistent store and
listSuspendedRuns(). - Code runs after
suspend(). It does not throw. Usereturn suspend?.({...}). - Wrong tool resumed. Without
toolCallId, the most recent suspended call resumes. Pass it, especially for supervisors. - Using the subagent's tool call id at the supervisor. Use the chunk's
toolCallId(the delegation call).delegatedToolCallIdis for display only. - Expecting auto-resume to approve.
autoResumeSuspendedToolsonly resumessuspend()flows. Approval-gated tools still need an explicit approve or decline. - Auto-resume does nothing. Missing memory, a different thread or resource id, or no
resumeSchemaon the tool. - Function
requireToolApprovalon a durable or stored agent. Functions cannot be serialized, so every call requires approval. Use a boolean there. requireApproval: falseon a tool to opt out. It does not overriderequireToolApproval. A tool-level function is authoritative.- Approved arguments drift before execution. Fingerprint
toolNameandargsat review time and check it inhooks.beforeToolCall(seereferences/examples.md). - Client tools inside a subagent. Subagents cannot run them. Use
suspend()instead, which propagates to the supervisor. - Inferring "answered" from tool output. Use the
tool-call-resumedchunk (payload.kindis'approval'or'suspension').
Output format
A finished HITL delivery contains:
- Gated tools with
requireApprovalorsuspend()plussuspendSchemaandresumeSchema - Storage configured on the Mastra instance, persistent for production
- A caller or route that detects the pause and exposes approve, decline, or resume actions
- Restart-safe resume through
listSuspendedRuns()or the client SDK - Decline reasons passed back to the model
- Tests or Studio runs covering approve, decline, resume, and restart
Reference files
references/api.md- Flags, chunk payloads, approve, decline, and resume methods,listSuspendedRuns()types.references/examples.md-generate()approval API, conditional approval, argument-bound approvals, conversational auto-resume, supervisor approval,askUserTool.references/checklist.md- HITL production checklist.
Live docs
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

