AgentCoreRuntimeSandbox
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 does not support background process management, stdin, or filesystem mounts. AgentCore Code Interpreter is a separate AWS service and is not part of this provider.
InstallationDirect link to Installation
- npm
- pnpm
- Yarn
- Bun
npm install @mastra/agentcore
pnpm add @mastra/agentcore
yarn add @mastra/agentcore
bun add @mastra/agentcore
UsageDirect link to Usage
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({
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 parametersDirect link to Constructor parameters
agentRuntimeArn:
region?:
runtimeSessionId?:
qualifier?:
contentType?:
accept?:
commandTimeout?:
stopSessionOnLifecycle?:
stopClientToken?:
client?:
instructions?:
PropertiesDirect link to Properties
id:
name:
provider:
status:
runtimeSessionId:
agentRuntimeArn:
MethodsDirect link to Methods
Command executionDirect link to Command execution
executeCommand(command, args?, options?)Direct link to 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.
LifecycleDirect link to Lifecycle
start()Direct link to start
Runs the sandbox lifecycle start hook. This provider does not create an AgentCore Runtime session during start().
await sandbox.start()
stop()Direct link to stop
Stops the AgentCore Runtime session only when stopSessionOnLifecycle is true.
await sandbox.stop()
stopRuntimeSession()Direct link to 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() does not 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()Direct link to 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()
MetadataDirect link to Metadata
getInfo()Direct link to getinfo
Returns sandbox status and AgentCore Runtime metadata.
const info = await sandbox.getInfo()
Returns: Promise<SandboxInfo>.
LimitationsDirect link to Limitations
AgentCoreRuntimeSandbox follows AgentCore Runtime command execution semantics:
- One-shot commands: Each command runs to completion or timeout.
- No persistent shell: Shell state does not carry over between commands. Encode state in each command, for example
cd /workspace && npm test. - Background processes are not supported: The provider does not expose a
processesmanager. - Interactive stdin is unavailable: Runtime command execution does not provide an interactive stdin stream through this provider.
- Workspace filesystem mounts are unsupported: Workspace filesystem mounting is not supported by this provider.
- Container-dependent tools: Commands can only use tools installed in the AgentCore Runtime container image.