AcpAgent class
The AcpAgent class wraps an Agent Client Protocol (ACP)-compatible coding agent as a Mastra subagent. Use it when a parent Mastra agent should delegate repository inspection and code edits. It can also delegate other ACP-backed tasks to the subagent.
If you want the parent agent to call the ACP agent as a tool instead, use createACPTool().
Usage exampleUsage example的直接链接
Register an ACP-compatible coding agent in a parent agent's agents map:
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.6-sol',
agents: {
codeAgent,
},
})
For Claude Code, ACP support is provided by the @agentclientprotocol/claude-agent-acp bridge package. Configure the ACP agent command to run the bridge, then select a Claude model after session creation:
import { AcpAgent } from '@mastra/acp'
export const claudeCodeAgent = new AcpAgent({
id: 'claude-code-agent',
name: 'Claude Code Agent',
description: 'Use Claude Code through ACP.',
command: 'npx',
args: ['@agentclientprotocol/claude-agent-acp'],
cwd: process.cwd(),
model: 'claude-sonnet-4-6',
})
Constructor parametersConstructor parameters的直接链接
id:
name?:
id.description:
command:
args?:
env?:
cwd?:
session?:
cwd or process.cwd() and an empty MCP server list.initialize?:
authMethodId?:
persistSession?:
false to stop the process after each prompt completes.onPermissionRequest?:
createClient?:
extMethod and extNotification handlers. See Extension methods.workspace?:
Workspace backed by LocalFilesystem at cwd or process.cwd().model?:
session/set_model method.PropertiesProperties的直接链接
id:
name:
description:
connection:
MethodsMethods的直接链接
GenerationGeneration的直接链接
generate(messages, options?)generatemessages-options的直接链接
Sends the prompt to the ACP agent and buffers text chunks from the response before returning a Mastra subagent generate result.
const result = await codeAgent.generate('Inspect the repository and summarize the test setup')
console.log(result.text)
stream(messages, options?)streammessages-options的直接链接
Sends the prompt to the ACP agent and returns a Mastra subagent stream result. ACP agent_message_chunk updates are emitted as Mastra text-delta chunks.
const result = await codeAgent.stream('Refactor the selected module and explain each change')
for await (const chunk of result.fullStream) {
if (chunk.type === 'text-delta') {
process.stdout.write(chunk.payload.text)
}
}
resumeGenerate() and resumeStream() aren't supported and throw an error when called.
Model managementModel management的直接链接
getAvailableModels()getavailablemodels的直接链接
Starts the ACP process if needed and returns the model list advertised by the ACP session.
const models = await codeAgent.getAvailableModels()
// [{ modelId: 'claude-sonnet-4-6', name: 'Claude Sonnet' }, ...]
setModel(modelId)setmodelmodelid的直接链接
Selects a model for the active ACP session. If the ACP agent advertises available models, the model ID must match one of them.
await codeAgent.setModel('claude-sonnet-4-6')
Session lifecycleSession lifecycle的直接链接
AcpAgent starts the configured command on first use and initializes the ACP client. It then creates an ACP session. By default, persistSession is true, so the process and session stay alive across generate(), stream(), getAvailableModels(), and setModel() calls.
Set persistSession: false when each prompt should run in a fresh ACP process:
import { AcpAgent } from '@mastra/acp'
export const codeAgent = new AcpAgent({
id: 'code-agent',
description: 'Run one isolated ACP coding task',
command: 'acp-agent',
args: ['--stdio'],
cwd: process.cwd(),
persistSession: false,
})
With persistSession: false, @mastra/acp stops the ACP process after each prompt completes.
Workspace integrationWorkspace integration的直接链接
ACP file operations go through Mastra's Workspace abstraction. If you don't pass workspace, @mastra/acp creates a Workspace backed by LocalFilesystem and uses cwd or process.cwd() as the filesystem base path.
Pass a custom Workspace when the ACP agent should read and write through a specific filesystem implementation:
import { AcpAgent } from '@mastra/acp'
import { LocalFilesystem, Workspace } from '@mastra/core/workspace'
const workspace = new Workspace({
filesystem: new LocalFilesystem({
basePath: process.cwd(),
}),
})
export const codeAgent = new AcpAgent({
id: 'code-agent',
description: 'Run coding tasks in a controlled workspace',
command: 'acp-agent',
args: ['--stdio'],
workspace,
})
Use cwd and workspace together when the ACP process should start in one directory but file operations should use an explicitly configured workspace root.
Permission handlingPermission handling的直接链接
ACP agents may ask the client to choose a permission option before they continue. By default, AcpAgent selects the first option returned by the ACP agent, or cancels when no option is available.
Pass onPermissionRequest to inspect the request and return your own permission response:
import { AcpAgent } from '@mastra/acp'
export const codeAgent = new AcpAgent({
id: 'code-agent',
description: 'Use an ACP-compatible coding agent',
command: 'acp-agent',
args: ['--stdio'],
async onPermissionRequest(request) {
const allowOption = request.options.find(option => option.name === 'Allow')
if (!allowOption) {
return { outcome: { outcome: 'cancelled' } }
}
return {
outcome: {
outcome: 'selected',
optionId: allowOption.optionId,
},
}
},
})
Use this callback to enforce local policy or inspect the permission title. It can also route the decision to your own approval flow.