Loading...
Loading...
Give an AG2 `Agent` the ability to run shell commands. Covers `SandboxShellTool` (client-side `subprocess` via `LocalEnvironment`, works with any provider) and the provider-native `ShellTool` (OpenAI Responses execution). Use when the user wants the Agent to execute commands, build/test code, manage files, or operate on a workspace. Always pair with sandboxing — `allowed`, `blocked`, `ignore`, or `readonly`.
npx skill4agent add ag2ai/ag2-skills ag2-shell-tool| Need | Use | Why |
|---|---|---|
| Works with any model provider; full control over what runs and where | | Client-side |
| Provider-managed sandbox (container, network policy) on OpenAI Responses | | Server-side execution. No local subprocess. |
SandboxShellToolSandboxShellToolfrom ag2 import Agent
from ag2.config import AnthropicConfig
from ag2.tools import SandboxShellTool
agent = Agent(
"coder",
"You write and run Python code.",
config=AnthropicConfig(model="claude-sonnet-4-6"),
tools=[SandboxShellTool()],
)
reply = await agent.ask("Write a hello world script and run it.")
print(await reply.content())SandboxShellToolAnthropicConfigOpenAIConfig(model="gpt-4.1")GeminiConfig(model="gemini-2.5-pro")ag2[<provider>]ag2-quickstartSandboxShellToolLocalEnvironment()ag2_sandbox_LocalEnvironmentfrom pathlib import Path
from ag2.tools import LocalEnvironment, SandboxShellTool
SandboxShellTool(LocalEnvironment("/tmp/my_project"))
SandboxShellTool(LocalEnvironment(Path("/tmp/my_project")))tool.workdirLocalEnvironmentLocalEnvironmentpathtimeoutmax_outputenv_varsSandboxShellToolallowedblockedignorereadonlyallowed>>>|;&&||`$(blockedignore"Access denied: <path>"subprocessfrom ag2.tools import LocalEnvironment, SandboxShellTool
sh = SandboxShellTool(
LocalEnvironment(
path="/tmp/my_project",
timeout=30,
max_output=50_000,
),
allowed=["python", "uv run", "git"],
blocked=["rm -rf", "curl", "wget"],
ignore=["**/.env", "*.key", "secrets/**"],
)catheadtaillsgrepfindgit loggit diffgit statusfrom ag2.tools import LocalEnvironment, SandboxShellTool
sh = SandboxShellTool(LocalEnvironment(path="/my/codebase"), readonly=True)allowed=[...]LocalEnvironment| Parameter | Default | Description |
|---|---|---|
| | Working dir. |
| | |
| | Per-command timeout in seconds (returns |
| | Max characters returned (truncated output is suffixed |
| | Extra env vars merged into each command |
SandboxShellTool| Parameter | Default | Description |
|---|---|---|
| | The backend. |
| | Whitelist of command prefixes. |
| | Blacklist of command prefixes (best-effort, not a security boundary) |
| | Gitignore-style path patterns; matches block the command |
| | When |
workdirask()from ag2.tools import LocalEnvironment, SandboxShellTool
sh = SandboxShellTool(LocalEnvironment(path="/tmp/counter_demo"))
agent = Agent("coder", "You manage files.", config=config, tools=[sh])
reply1 = await agent.ask("Create counter.txt with value 0")
reply2 = await reply1.ask("Increment the counter by 1")
reply3 = await reply2.ask("Read the counter and tell me the value")ShellToolShellToolbashUnsupportedToolErrorSandboxShellToolfrom ag2.config import OpenAIResponsesConfig
from ag2.tools import ShellTool
agent = Agent("devops", config=OpenAIResponsesConfig(model="gpt-4.1"), tools=[ShellTool()])from ag2.config import OpenAIResponsesConfig
from ag2.tools import ContainerAutoEnvironment, NetworkPolicy, ShellTool
agent = Agent(
"devops",
config=OpenAIResponsesConfig(model="gpt-4.1"),
tools=[
ShellTool(
environment=ContainerAutoEnvironment(
network_policy=NetworkPolicy(allowed_domains=["pypi.org"]),
),
),
],
)| Environment | Description |
|---|---|
| Provider-managed container with optional |
| Reference an existing container by ID |
SandboxShellToolShellTool | | |
|---|---|---|
| Execution | Client-side | Provider-side container |
| Provider support | Any provider | OpenAI Responses only |
| Environment control | Full ( | Limited (provider-dependent) |
| Local FS access | Yes (you choose what's exposed) | No |
| Network control | Via | OpenAI: |
| Import | | |
website/docs/user-guide/tools/local_shell.mdxSandboxShellToolLocalEnvironmentwebsite/docs/user-guide/tools/builtin_tools.mdx#shellShellToolapproval_required()ag2-hitlSandboxShellTool()allowedblockedreadonlyignore`cat secrets.key`blocked=["cat", "less"]blockedecho x; rm -rf ~blocked=["rm"]allowedreadonlyShellToolUnsupportedToolErrorSandboxShellTool/tmp/my_projecttempfile.mkdtemp(prefix="...")ShellToolSandboxShellToolpromptapproval_required()