跳到主要内容

AgentCoreRuntimeSandbox

使用 InvokeAgentRuntimeCommandAWS Bedrock AgentCore Runtime 会话中执行 shell 命令。

如果 Agent 已在 AgentCore Runtime 中运行,并且你希望 Mastra Workspace 使用同一个运行时会话执行命令,请使用 AgentCoreRuntimeSandbox。有关接口的详细信息,请参阅 WorkspaceSandbox 接口

注意

AgentCoreRuntimeSandbox 仅支持一次性命令执行。它不支持后台进程管理、stdin 或文件系统挂载。AgentCore Code Interpreter 是另一项独立的 AWS 服务,不属于此 Provider。

安装
安装的直接链接

npm install @mastra/agentcore

使用方法
使用方法的直接链接

AgentCoreRuntimeSandbox 添加到 Workspace 并分配给 Agent:

src/mastra/agents/dev-agent.ts
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,
})

通过 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)
}

构造函数参数
构造函数参数的直接链接

agentRuntimeArn:

string
执行命令的 AgentCore Runtime ARN。

region?:

string
Bedrock AgentCore 客户端所用的 AWS 区域。未提供时使用 AWS SDK 的默认区域解析链。

runtimeSessionId?:

string
= 生成的 UUID
AgentCore Runtime 会话 ID。默认为生成的 UUID,满足 AgentCore Runtime 对会话 ID 长度的要求。

qualifier?:

string
= DEFAULT
Agent 运行时限定符或端点。

contentType?:

string
= application/json
为命令请求发送的 MIME 类型。

accept?:

string
= application/vnd.amazon.eventstream
命令事件流所用的 Accept 请求头。

commandTimeout?:

number
= 300000
默认命令超时时间(毫秒)。

stopSessionOnLifecycle?:

boolean
= false
stop()destroy() 是否应调用 StopRuntimeSession。默认为 false,因为 AgentCore Runtime 会话通常会与 Sandbox 实例之外的 Agent 调用共享。

stopClientToken?:

string
= 生成的 UUID
调用 StopRuntimeSession 时使用的客户端令牌。

client?:

BedrockAgentCoreClient
预配置的 AWS SDK 客户端。可用于自定义凭证、重试行为或测试。

instructions?:

string | ((opts) => string)
自定义说明,用于覆盖 getInstructions() 返回的默认说明。传入字符串可替换默认说明,传入函数可扩展默认说明。

属性
属性的直接链接

id:

string
此 Sandbox 实例使用的运行时会话 ID。

name:

'AgentCoreRuntimeSandbox'
便于阅读的名称。

provider:

'agentcore'
Provider 类型标识符。

status:

ProviderStatus
当前生命周期状态:'pending''starting''running''stopping''stopped''destroying''destroyed''error'

runtimeSessionId:

string
用于执行命令的 AgentCore Runtime 会话 ID。

agentRuntimeArn:

string
执行命令的 AgentCore Runtime ARN。

方法
方法的直接链接

命令执行
命令执行的直接链接

executeCommand(command, args?, options?)
executecommandcommand-args-options的直接链接

在 AgentCore Runtime 会话中运行一次性 shell 命令,并返回 stdout、stderr、退出代码和超时状态。

const result = await sandbox.executeCommand('npm', ['test'], {
cwd: '/workspace',
env: {
NODE_ENV: 'test',
},
timeout: 300_000,
})

返回值:Promise<CommandResult>

options.timeout 以毫秒为单位。AgentCore Runtime 接受 1 至 3600 秒的命令超时时间。Provider 会先将毫秒转换为秒,再发送请求。

生命周期
生命周期的直接链接

start()
start的直接链接

运行 Sandbox 生命周期的启动钩子。此 Provider 不会在 start() 期间创建 AgentCore Runtime 会话。

await sandbox.start()

stop()
stop的直接链接

仅当 stopSessionOnLifecycletrue 时停止 AgentCore Runtime 会话。

await sandbox.stop()

stopRuntimeSession()
stopruntimesession的直接链接

显式停止此 Sandbox 使用的 AgentCore Runtime 会话。

当 Sandbox 拥有该运行时会话且你希望直接清理它时,请使用此方法。destroy() 不会调用此方法,除非 stopSessionOnLifecycletrue,因为 AgentCore Runtime 会话可能会与 Workspace Sandbox 生命周期之外的 Agent 调用共享。

await sandbox.stopRuntimeSession()

destroy()
destroy的直接链接

销毁 Sandbox 实例。如果此实例拥有其 AWS SDK 客户端,destroy() 还会销毁该客户端。如果 stopSessionOnLifecycletrue,则调用 StopRuntimeSession

await sandbox.destroy()

元数据
元数据的直接链接

getInfo()
getinfo的直接链接

返回 Sandbox 状态和 AgentCore Runtime 元数据。

const info = await sandbox.getInfo()

返回值:Promise<SandboxInfo>

限制
限制的直接链接

AgentCoreRuntimeSandbox 遵循 AgentCore Runtime 命令执行语义:

  • 一次性命令:每条命令运行至完成或超时。
  • 无持久化 shell:shell 状态不会在命令之间保留。请将状态编码到每条命令中,例如 cd /workspace && npm test
  • 不支持后台进程:Provider 不公开 processes 管理器。
  • 交互式 stdin 不可用:运行时命令执行无法通过此 Provider 提供交互式 stdin 流。
  • 不支持挂载 Workspace 文件系统:此 Provider 不支持挂载 Workspace 文件系统。
  • 依赖容器的 Tool:命令只能使用 AgentCore Runtime 容器镜像中已安装的 Tool。