Agent Client Protocol
Mastra supports the Agent Client Protocol (ACP) for running ACP-compatible coding agents from a Mastra agent. Use @mastra/acp to wrap a coding agent process as a Mastra tool or as a subagent.
ACP is useful for coding agents such as Claude Code, Amp, Codex, or any other executable that implements ACP over standard input and output.
When to use ACPDirect link to When to use ACP
- A Mastra agent should delegate code inspection, editing, or repository tasks to an external coding agent.
- An ACP-compatible agent process should stay alive across calls so it can keep session context.
- A parent agent needs real-time output from a coding agent while the task runs.
- An ACP-compatible agent needs permission prompts before it reads files, writes files, or runs actions.
- File access should go through Mastra's workspace abstraction instead of direct process-only file access.
How ACP worksDirect link to How ACP works
@mastra/acp starts the configured ACP agent command as a child process and communicates with it using newline-delimited JSON over standard input and output.
The flow is:
- Configure
command,args, and optional connection settings. @mastra/acpspawns the ACP agent process on first use.- The client sends ACP
initializeandsession/newrequests. - Mastra sends the user task to the ACP agent with
session/prompt. - The ACP agent streams session updates and message chunks back to Mastra.
- Mastra returns the buffered output, emits streaming chunks, or handles permission input.
- The ACP connection stops the process after the prompt when
persistSessionisfalse;AcpAgentcan keep a reusable process alive across calls by default.
During execution, the ACP client also handles permission requests and file operations. File reads and writes go through Mastra's Workspace, so the ACP agent operates inside the workspace you provide.
Getting startedDirect link to Getting started
Install @mastra/acp in a project that already uses @mastra/core. The package requires @mastra/core version 1.34.0 or later.
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/acp
pnpm add @mastra/acp
yarn add @mastra/acp
bun add @mastra/acp
@mastra/acp exports two APIs:
createACPTool(): Create a Mastra tool that sends ataskstring to an ACP agent and returns anoutputstring.AcpAgent: Wrap an ACP agent as a Mastra subagent withgenerate()andstream()support.
Use ACP as a subagentDirect link to Use ACP as a subagent
Use AcpAgent when a parent Mastra agent should delegate directly to an ACP-compatible coding agent as a subagent.
import { AcpAgent } from '@mastra/acp'
import { Agent } from '@mastra/core/agent'
const codeAgent = new AcpAgent({
id: 'code-agent',
name: 'Code Agent',
description: 'An ACP-compatible coding agent that can inspect and edit files',
command: 'acp-agent',
args: ['--stdio'],
cwd: process.cwd(),
})
export const codeSupervisor = new Agent({
id: 'code-supervisor',
name: 'Code Supervisor',
instructions: 'Delegate code editing tasks to the code-agent subagent.',
model: 'openai/gpt-5.5',
agents: {
codeAgent,
},
})
See the AcpAgent reference for all options, methods, and configuration.
Use ACP as a toolDirect link to Use ACP as a tool
Use createACPTool() when the parent Mastra agent should decide when to call the ACP agent as a tool.
import { createACPTool } from '@mastra/acp'
import { Agent } from '@mastra/core/agent'
const codeAgentTool = createACPTool({
id: 'code-agent',
description: 'Use an ACP-compatible coding agent to inspect and edit code',
command: 'acp-agent',
args: ['--stdio'],
cwd: process.cwd(),
})
export const codeSupervisor = new Agent({
id: 'code-supervisor',
name: 'Code Supervisor',
instructions: 'Use the code-agent tool when a task requires repository inspection or code edits.',
model: 'openai/gpt-5.5',
tools: {
codeAgentTool,
},
})
See the createACPTool() reference for all options and configuration.
Model selectionDirect link to Model selection
ACP agents may expose selectable models. Pass model in the ACP configuration to select a model after session creation, or use AcpAgent.getAvailableModels() and AcpAgent.setModel() to manage models at runtime.
See the AcpAgent model management methods for examples.
Session lifecycleDirect link to Session lifecycle
AcpAgent starts the configured command on first use and creates an ACP session. By default, persistSession is true, so the child process stays alive across calls. Set persistSession: false when each prompt should run in an isolated process.
See the AcpAgent session lifecycle section for details.
Permission handlingDirect link to Permission handling
ACP agents may ask the client to choose a permission option before they continue. By default, @mastra/acp selects the first option returned by the ACP agent. Pass onPermissionRequest when you need custom permission behavior.
See the createACPTool() permission handling section for a complete example.
Workspace integrationDirect link to Workspace integration
ACP file operations go through Mastra's workspace abstraction. AcpAgent can use a workspace option, and createACPTool() uses the current Mastra workspace from the tool execution context when one is available. Without a workspace, @mastra/acp falls back to a Workspace backed by LocalFilesystem at cwd or process.cwd().
See the AcpAgent workspace integration section for custom workspace examples.