ag2-shell-tool
Skill12 starsChanged 8 days agoApache-2.0View on GitHub
Give an AG2 `Agent` the ability to run shell commands or code. Covers `SandboxShellTool` (client-side, any provider) with execution backends `LocalEnvironment`, `DockerEnvironment`, `DaytonaEnvironment`, `TenkiEnvironment`; command filtering and restricted mode (`allowed`, `blocked`, `ignore`, `readonly`); `AnthropicBashTool` (Anthropic's `bash_20250124`, executed locally by AG2); the provider-hosted `ShellTool` (OpenAI Responses only, needs `ContainerAutoEnvironment` / `ContainerReferenceEnvironment` + `NetworkPolicy`); and `SandboxCodeTool` (`run_code`). Use when the agent should execute commands, build/test code, or operate on a workspace. For provider-run code execution (`CodeExecutionTool`) and other built-in tools see `ag2-use-builtin-tools`; to gate commands behind human approval see `ag2-hitl`.
- Reads credentials
- Installs packages
1 other project uses a version of this.
Adopted by
Most recently taken up by
- ag2ai/build-with-ag2their own version, first seen Oct 9, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- ag2ai/build-with-ag2★ 252same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository27 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- ag2-a2aSkill
- ag2-acpSkill
- ag2-add-custom-toolSkill
- ag2-ag-uiSkill
- ag2-eval-comparisonSkill
- ag2-evaluationSkill
- ag2-hitlSkill
- ag2-knowledge-and-memorySkill
- ag2-liveSkill
- ag2-mcpSkill
- ag2-middlewareSkill
- ag2-multimodal-inputSkill
- ag2-network-discussionSkill
- ag2-network-governanceSkill
- ag2-network-quickstartSkill
- ag2-network-tools-and-viewsSkill
- ag2-network-workflowSkill
- ag2-observers-and-alertsSkill
- ag2-overviewSkill
- ag2-quickstartSkill
- ag2-structured-outputSkill
- ag2-subagent-delegationSkill
- ag2-telemetrySkill
- ag2-testingSkill
- ag2-use-builtin-toolsSkill
The file
We split this skill into the rules it gives an agent
2 rules. About 0 are common, 0 are distinctive, and 2 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈0
Distinctive · estimate
This author's own choices. Worth a look.
2
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 20
All take the same backends (LocalEnvironment, DockerEnvironment, DaytonaEnvironment, TenkiEnvironment) except ShellTool.
- Very specific: line 39
No argument → LocalEnvironment(): a temp dir (prefix ag2_sandbox_) deleted at exit.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Shell tools
When to use — pick one deliberately
| Need | Use |
| Shell on any provider, you own where it runs | SandboxShellTool (default choice) |
| Claude using the bash tool it was trained on, executed on your side | AnthropicBashTool (Anthropic only) |
| Throwaway container hosted by OpenAI, no local access | ShellTool(environment=ContainerAutoEnvironment()) (OpenAI Responses, gpt-5.2+) |
A run_code(code, language) tool instead of raw shell | SandboxCodeTool(env) |
All take the same backends (LocalEnvironment, DockerEnvironment, DaytonaEnvironment, TenkiEnvironment) except ShellTool.
Recipe — SandboxShellTool
from ag2 import Agent
from ag2.config import OpenAIResponsesConfig
from ag2.tools import SandboxShellTool
agent = Agent(
"coder",
"You write and run Python code.",
config=OpenAIResponsesConfig(model="gpt-4.1-mini"), # any provider works
tools=[SandboxShellTool()],
)
reply = await agent.ask("Write a hello world script with python3 and run it.")
print(await reply.content())No argument → LocalEnvironment(): a temp dir (prefix ag2_sandbox_) deleted at exit. LocalEnvironment("/path") uses (and creates) that dir and keeps it. tool.workdir gives the resolved dir. Files persist across ask() / reply.ask() turns.
Lock it down
from ag2.tools import LocalEnvironment, SandboxShellTool
sh = SandboxShellTool(
LocalEnvironment("/srv/project", timeout=30, max_output=50_000, env_vars={"CI": "1"}),
allowed=["python", "uv run", "git log", "ls", "cat"], # prefix match, word by word
ignore=["**/.env", "*.key", "secrets/**"], # path words matching -> "Access denied: <path>"
)
ro = SandboxShellTool(LocalEnvironment("/srv/project"), readonly=True)Restricted mode (1.3): setting any of allowed, blocked, ignore, readonly splits the command into argv once and runs it without a shell. Pipes/redirects/chaining (|, >, ;, &&, …) are rejected with a clear error; globs (*.py), $VARS, ~, $(...) are passed literally; echo/pwd are the PATH binaries. With no filter set, commands run through sh -c with full syntax.
readonly=Trueallows onlycat head tail ls grep egrep fgrep wc du df diff stat which pwd echo printenv cut— nogit,find,file(they can write/exec). Needgit log? Pass your ownallowed=[...](it replaces the read-only set).blocked=["rm"]matches the program by basename (/bin/rmcaught) — but can't stopsh -c,env,xargs. Not a security boundary; useallowedor a container backend.
Isolated backends
from ag2.extensions.docker import DockerEnvironment # pip install "docker>=6.0.0,<8" (needs a Docker daemon)
from ag2.tools import SandboxCodeTool, SandboxShellTool
async with DockerEnvironment(image="python:3.12-slim", network_mode="none") as env:
agent = Agent("dev", config=config, tools=[SandboxShellTool(env), SandboxCodeTool(env)])
await agent.ask("Write hello.txt with the shell, then read it with Python.")
# container removed here; shell and run_code shared one container (workdir /workspace)DaytonaEnvironment (pip install "daytona>=0.171.0,<1", DAYTONA_API_KEY) and TenkiEnvironment (pip install "tenki>=1.4.0,<2", TENKI_API_KEY; workdir /home/tenki; use SandboxCodeTool(env.code_environment()) because its image has python3, not python) work the same way.
AnthropicBashTool and ShellTool
from ag2.config import AnthropicConfig, OpenAIResponsesConfig
from ag2.tools import AnthropicBashTool, ContainerAutoEnvironment, NetworkPolicy, ShellTool
claude = Agent("ops", config=AnthropicConfig(model="claude-haiku-4-5"),
tools=[AnthropicBashTool(LocalEnvironment("/srv/project"))]) # same allowed/blocked/ignore/readonly kwargs
hosted = Agent("ops", config=OpenAIResponsesConfig(model="gpt-5.2"),
tools=[ShellTool(environment=ContainerAutoEnvironment(
network_policy=NetworkPolicy(allowed_domains=["pypi.org"])))])Parameter tables, filter order, backend matrix, custom backends (SandboxFactory, WorkdirAware): references/shell_reference.md.
Common pitfalls
ShellTool()withoutenvironment=raisesClientExecutedShellUnsupportedError— passContainerAutoEnvironment()(or useSandboxShellTool). Hosted shell needs gpt-5.2 or later; older models return 400 "Tool 'shell' is not supported".- Wrong provider raises
UnsupportedToolError—ShellToolis OpenAI-only;AnthropicBashToolis Anthropic-only.SandboxShellToolworks everywhere. - Restricted mode surprises the model — it still writes
ls *.py | wc -land may conclude "0 files".SandboxShellTool's auto-description says "runs without a shell";AnthropicBashTool's fixed definition cannot, so say it in the agent prompt ("commands run without a shell: no pipes, redirects or globs"). - No filters on
LocalEnvironment= arbitrary commands on your machine (60 s timeout). Setallowed/readonly, or use Docker/Daytona/Tenki for real isolation. ignoreonly checks literal path words — a program that opens paths itself (sh -c,find) is not covered.- Shared hard-coded dirs race across parallel agents — use
tempfile.mkdtemp(). - Human approval before each command —
SandboxShellTool(env, middleware=[approval_required()])(from ag2.middleware import approval_required) plus an agenthitl_hook; seeag2-hitl.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

