AgentCore
Executes shell commands in an AWS Bedrock AgentCore Runtime session by using InvokeAgentRuntimeCommand.
Use AgentCoreRuntimeSandbox when your agent already runs in AgentCore Runtime and you want Mastra workspace command execution to use the same runtime session. For interface details, see WorkspaceSandbox interface.
AgentCoreRuntimeSandbox only supports one-shot command execution. It doesn't support background process management, stdin, or filesystem mounts. AgentCore Code Interpreter is a separate AWS service and isn't part of this provider.
InstallationInstallation的直接链接
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/agentcore
pnpm add @mastra/agentcore
yarn add @mastra/agentcore
bun add @mastra/agentcore
UsageUsage的直接链接
Add an AgentCoreRuntimeSandbox to a workspace and assign it to an agent:
import { Agent } from '@mastra/core/agent'
import { Workspace } from '@mastra/core/workspace'
import { AgentCoreRuntimeSandbox } from '@mastra/agentcore'
const workspace = new Workspace({
sandbox: new AgentCoreRuntimeSandbox({
region: 'us-west-2',
agentRuntimeArn: process.env.AGENTCORE_RUNTIME_ARN!,
runtimeSessionId: '12345678-1234-1234-1234-123456789012',
}),
})
const agent = new Agent({
id: 'dev-agent',
name: 'dev-agent',
model: 'anthropic/claude-sonnet-4-6',
instructions: 'You are a helpful development assistant.',
workspace,
})
Run commands programmatically through the sandbox:
const result = await workspace.sandbox?.executeCommand?.('npm', ['test'], {
cwd: '/workspace',
env: {
NODE_ENV: 'test',
},
timeout: 300_000,
})
if (!result?.success) {
console.error(result?.stderr)
}
Constructor parametersConstructor parameters的直接链接
agentRuntimeArn:
region?:
runtimeSessionId?:
qualifier?:
contentType?:
accept?:
workingDirectory?:
cwd is given, applied as a shell cd prefix. A per-command cwd always wins. The path is shell-quoted, so ~ is not expanded — use an absolute path.commandTimeout?:
stopSessionOnLifecycle?:
stop() and destroy() should call StopRuntimeSession. Defaults to false because AgentCore Runtime sessions are often shared with agent invocations outside the sandbox instance.stopClientToken?:
StopRuntimeSession is called.client?:
instructions?:
getInstructions(). Pass a string to replace the defaults, or a function to extend them.PropertiesProperties的直接链接
id:
name:
provider:
status:
'pending', 'starting', 'running', 'stopping', 'stopped', 'destroying', 'destroyed', or 'error'.runtimeSessionId:
agentRuntimeArn:
MethodsMethods的直接链接
Command executionCommand execution的直接链接
executeCommand(command, args?, options?)executecommandcommand-args-options的直接链接
Runs a one-shot shell command in the AgentCore Runtime session and returns stdout, stderr, exit code, and timeout status.
const result = await sandbox.executeCommand('npm', ['test'], {
cwd: '/workspace',
env: {
NODE_ENV: 'test',
},
timeout: 300_000,
})
Returns: Promise<CommandResult>.
options.timeout is specified in milliseconds. AgentCore Runtime accepts command timeouts from 1 to 3600 seconds. The provider converts milliseconds to seconds before sending the request.
LifecycleLifecycle的直接链接
start()start的直接链接
Runs the sandbox lifecycle start hook. This provider doesn't create an AgentCore Runtime session during start().
await sandbox.start()
stop()stop的直接链接
Stops the AgentCore Runtime session only when stopSessionOnLifecycle is true.
await sandbox.stop()
stopRuntimeSession()stopruntimesession的直接链接
Explicitly stops the AgentCore Runtime session used by this sandbox.
Use this when the sandbox owns the runtime session and you want to clean it up directly. destroy() doesn't call this method unless stopSessionOnLifecycle is true, because AgentCore Runtime sessions can be shared with agent invocations outside the Workspace sandbox lifecycle.
await sandbox.stopRuntimeSession()
destroy()destroy的直接链接
Destroys the sandbox instance. If this instance owns its AWS SDK client, destroy() also destroys the client. If stopSessionOnLifecycle is true, it calls StopRuntimeSession.
await sandbox.destroy()
MetadataMetadata的直接链接
getInfo()getinfo的直接链接
Returns sandbox status and AgentCore Runtime metadata.
const info = await sandbox.getInfo()
Returns: Promise<SandboxInfo>.
LimitationsLimitations的直接链接
AgentCoreRuntimeSandbox follows AgentCore Runtime command execution semantics:
- One-shot commands: Each command runs to completion or timeout.
- No persistent shell: Shell state doesn't carry over between commands. Encode state in each command, for example
cd /workspace && npm test. - Background processes aren't supported: The provider doesn't expose a
processesmanager. - Interactive stdin is unavailable: Runtime command execution doesn't provide an interactive stdin stream through this provider.
- Workspace filesystem mounts are unsupported: Workspace filesystem mounting isn't supported by this provider.
- Container-dependent tools: Commands can only use tools installed in the AgentCore Runtime container image.