AgentSkillsCN

sandbox

为保障安全性,可在隔离的沙盒环境中执行命令。当您运行不受信任的代码、系统命令,或可能对宿主系统造成影响的操作时,可启用此技能。系统会自动根据命令类型,识别并选择合适的运行时环境(Python、Node、Rust、Go、Ruby等)。

SKILL.md
--- frontmatter
name: sandbox
description: Execute commands in isolated sandboxes for security. Use when running untrusted code, system commands, or operations that could affect the host system. Automatically detects the right runtime (Python, Node, Rust, Go, Ruby, etc.) from the command.

Sandbox Skill

Execute commands in secure, isolated containers using agentkernel. This provides hardware-level isolation to protect the host system from potentially dangerous operations.

When to Use This Skill

Use this skill when:

  • Running untrusted or generated code
  • Executing shell commands that could modify system state
  • Testing code in a clean environment
  • Running build/test commands that might have side effects
  • Executing code from external sources
  • Any operation where isolation is beneficial for security

Do NOT use this skill for:

  • Simple file reads/writes (use standard tools)
  • Git operations (use standard tools)
  • Operations that need access to host credentials or SSH keys

Instructions

Basic Usage

Run any command in an isolated sandbox:

bash
agentkernel run <command> [args...]

For compound commands with && or ||, wrap in sh -c:

bash
agentkernel run sh -c 'npm install && npm test'

The runtime is auto-detected from the command:

bash
# Python
agentkernel run python3 script.py
agentkernel run pip install package-name
agentkernel run pytest

# Node.js
agentkernel run node script.js
agentkernel run npm test
agentkernel run npx create-react-app my-app

# Rust
agentkernel run cargo build
agentkernel run cargo test

# Go
agentkernel run go build
agentkernel run go test ./...

# Ruby
agentkernel run ruby script.rb
agentkernel run bundle exec rspec

# And more: PHP, Java, C/C++, Elixir, Lua, Terraform...

Specifying an Image

Override the auto-detected image:

bash
agentkernel run --image ubuntu:24.04 apt-get update
agentkernel run --image postgres:16-alpine psql --version

Security Profiles

Control sandbox permissions with security profiles:

bash
# Default: moderate security (network enabled, no mounts)
agentkernel run npm test

# Restrictive: no network, read-only filesystem, all capabilities dropped
agentkernel run --profile restrictive python3 script.py

# Permissive: network, mounts, environment passthrough
agentkernel run --profile permissive cargo build

# Disable network access specifically
agentkernel run --no-network curl example.com  # Will fail
ProfileNetworkMount CWDMount HomePass EnvRead-only
permissiveYesYesYesYesNo
moderateYesNoNoNoNo
restrictiveNoNoNoNoYes

Keeping Sandboxes for Debugging

Keep the sandbox after execution:

bash
agentkernel run --keep npm test
# Later: agentkernel sandbox remove run-<id>

Persistent Sandboxes

For longer-running work:

bash
# Create and start
agentkernel sandbox create my-sandbox
agentkernel sandbox start my-sandbox

# Execute commands
agentkernel exec my-sandbox npm test
agentkernel exec my-sandbox python -m pytest

# Clean up
agentkernel sandbox stop my-sandbox
agentkernel sandbox remove my-sandbox

Supported Languages

LanguageCommandsDocker Image
Pythonpython, python3, pip, poetry, uv, pytestpython:3.12-alpine
Node.jsnode, npm, npx, yarn, pnpm, bunnode:22-alpine
Rustcargo, rustcrust:1.85-alpine
Gogo, gofmtgolang:1.23-alpine
Rubyruby, bundle, rails, rakeruby:3.3-alpine
Javajava, javac, mvn, gradleeclipse-temurin:21-alpine
PHPphp, composerphp:8.3-alpine
C/C++gcc, g++, make, cmakegcc:14-bookworm
.NETdotnetmcr.microsoft.com/dotnet/sdk:8.0
Terraformterraformhashicorp/terraform:1.10
Lualua, luajitnickblah/lua:5.4-alpine
Shellbash, sh, zshalpine:3.20

Security Notes

  • Sandboxes run in isolated containers (Docker or Podman)
  • On Linux with KVM, Firecracker microVMs provide hardware-level isolation
  • Three security profiles: permissive, moderate (default), restrictive
  • Use --profile restrictive for maximum isolation (no network, read-only fs)
  • Use --no-network to disable network access specifically
  • Host filesystem is NOT mounted by default (use permissive profile to enable)
  • Each sandbox gets a clean environment

Agent Compatibility Modes

Agentkernel supports compatibility modes for different AI agents. Each mode configures sandbox behavior optimally for that agent's needs:

bash
# Claude Code compatibility (default for this plugin)
agentkernel run --compatibility-mode claude python3 script.py

# Codex compatibility (stricter isolation)
agentkernel run --compatibility-mode codex python3 script.py

# Gemini compatibility (Docker-style)
agentkernel run --compatibility-mode gemini python3 script.py

Or configure in agentkernel.toml:

toml
[sandbox]
name = "my-project"

[agent]
preferred = "claude"
compatibility_mode = "claude"  # claude, codex, gemini, or native

[security]
profile = "moderate"  # Can override compatibility mode defaults

Compatibility Mode Differences

ModeNetwork PolicyProject MountAPI AccessIsolation
NativeAllow allNoNoneModerate
ClaudeDomain allowlistYesAnthropic APIModerate
CodexDomain allowlistYesOpenAI APIStrict
GeminiDomain allowlistYesGoogle APIModerate

Claude mode allows: api.anthropic.com, cdn.anthropic.com, common package registries. Codex mode allows: api.openai.com, cdn.openai.com, common package registries. Gemini mode allows: *.googleapis.com, common package registries.

All modes block cloud metadata endpoints (169.254.169.254, metadata.google.internal).

MCP Server Integration

This plugin provides an MCP (Model Context Protocol) server for direct tool integration. When enabled, the following MCP tools are available:

Command Execution

ToolDescription
sandbox_runRun a command in a temporary sandbox with security profile and compatibility mode
sandbox_createCreate a persistent sandbox
sandbox_execExecute a command in an existing sandbox
sandbox_listList all sandboxes
sandbox_removeRemove a sandbox

File Operations

ToolDescription
sandbox_file_writeWrite a file into a running sandbox
sandbox_file_readRead a file from a sandbox (supports binary via base64)

Lifecycle Management

ToolDescription
sandbox_startStart a stopped sandbox
sandbox_stopStop a running sandbox

sandbox_run Options

The sandbox_run tool accepts these parameters:

  • command (required): Command to execute (array of strings)
  • profile: Security profile (restrictive, moderate, permissive)
  • compatibility_mode: Agent mode (native, claude, codex, gemini)
  • network: Override network access (boolean)
  • cwd: Working directory inside sandbox
  • env: Environment variables (object)
  • timeout_ms: Command timeout in milliseconds (default: 30000)

Example MCP request:

json
{
  "method": "tools/call",
  "params": {
    "name": "sandbox_run",
    "arguments": {
      "command": ["python3", "-c", "print('hello')"],
      "compatibility_mode": "claude",
      "timeout_ms": 60000
    }
  }
}

The MCP server starts automatically when the plugin is loaded and provides these tools via JSON-RPC over stdio.

Daemon Mode (Fast Execution)

For high-performance scenarios, agentkernel can run a daemon that maintains a pool of pre-warmed sandboxes:

bash
# Start daemon with pre-warmed pool
agentkernel daemon start

# Check status
agentkernel daemon status  # Shows warm VMs per agent type

# Commands use warm pool automatically (~50ms vs ~500ms cold start)
agentkernel run python3 script.py

# Pre-warm for specific agent types
agentkernel daemon prewarm --mode claude

# Stop daemon
agentkernel daemon stop

The daemon supports per-agent pool configuration for optimal warm starts:

toml
# Example daemon config (future)
[daemon]
min_warm = 3
max_warm = 5

[daemon.agents.claude]
min_warm = 2
runtime = "python"

[daemon.agents.codex]
min_warm = 1
runtime = "python"

Claude Code Permission Configuration

Agentkernel MCP tools are designed to run safely in isolated sandboxes. You can configure Claude Code's approval behavior for these tools in your project's .claude/settings.json:

json
{
  "permissions": {
    "allow": [
      "mcp__agentkernel__sandbox_run",
      "mcp__agentkernel__sandbox_exec",
      "mcp__agentkernel__sandbox_list",
      "mcp__agentkernel__sandbox_file_read"
    ],
    "ask": [
      "mcp__agentkernel__sandbox_file_write",
      "mcp__agentkernel__sandbox_create"
    ],
    "deny": []
  }
}

Tool Risk Levels

ToolRisk LevelRecommendation
sandbox_listLowAuto-approve (read-only)
sandbox_file_readLowAuto-approve (read from sandbox only)
sandbox_runMediumAuto-approve (sandboxed execution)
sandbox_execMediumAuto-approve (sandboxed execution)
sandbox_file_writeMediumAsk (writes files into sandbox)
sandbox_createMediumAsk (creates persistent resources)
sandbox_startMediumAuto-approve (starts existing sandbox)
sandbox_stopMediumAuto-approve (stops sandbox)
sandbox_removeMediumAsk (removes sandbox)

All tools run in isolated sandboxes, so even "medium" risk tools cannot affect the host system. The ask recommendations are for visibility, not security.

MCP Server Configuration

Add agentkernel to your Claude Code MCP servers:

json
// .mcp.json (project-level)
{
  "mcpServers": {
    "agentkernel": {
      "command": "agentkernel",
      "args": ["mcp", "server"],
      "env": {}
    }
  }
}

Or globally in ~/.claude/mcp.json:

json
{
  "mcpServers": {
    "agentkernel": {
      "command": "agentkernel",
      "args": ["mcp", "server"]
    }
  }
}

Headless/Automation Mode

For CI/CD or automated workflows, use the --allowedTools flag:

bash
claude --allowedTools "mcp__agentkernel__*" "Run tests in sandbox"

Or configure in settings for persistent auto-approval of all agentkernel tools.

Error Handling

If a command fails, the sandbox is automatically cleaned up. Use --keep to preserve it for debugging:

bash
agentkernel run --keep failing-command
# Inspect: docker exec -it agentkernel-run-<id> sh
# Clean up: agentkernel sandbox remove run-<id>