Code Execution Backends
The stirrup.tools.code_backends module provides code execution backends.
CodeExecToolProvider (Base Class)
stirrup.tools.code_backends.CodeExecToolProvider
Bases: ToolProvider, ABC
Abstract base class for code execution tool providers.
CodeExecToolProvider is a ToolProvider that manages code execution environments (sandboxes, containers, local temp directories) and returns a code_exec Tool.
Subclasses must implement: - aenter(): Initialize environment and return the code_exec tool - aexit(): Cleanup the execution environment - run_command(): Execute a command and return raw result - read_file_bytes(): Read file content as bytes from the environment - write_file_bytes(): Write bytes to a file in the environment
Default implementations are provided for: - save_output_files(): Save files to local dir or another exec env (uses primitives) - upload_files(): Upload files from local or another exec env (uses primitives)
All code execution providers support an optional allowlist of command patterns. If provided, only commands matching at least one pattern are allowed. If None, all commands are allowed.
Usage with Agent
from stirrup.clients.chat_completions_client import ChatCompletionsClient
client = ChatCompletionsClient(model="gpt-5") agent = Agent( client=client, name="assistant", tools=[LocalCodeExecToolProvider(), CALCULATOR_TOOL], )
Initialize execution environment with optional command allowlist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allowed_commands
|
list[str] | None
|
Optional list of regex patterns. If provided, only commands matching at least one pattern are allowed. If None, all commands are allowed. |
None
|
Source code in src/stirrup/tools/code_backends/base.py
run_command
abstractmethod
async
run_command(
cmd: str, *, timeout: int = SHELL_TIMEOUT
) -> CommandResult
read_file_bytes
abstractmethod
async
Read file content as bytes from this execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path within this execution environment (relative or absolute within the env's working directory). |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
File contents as bytes. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file does not exist. |
RuntimeError
|
If execution environment not started. |
Source code in src/stirrup/tools/code_backends/base.py
write_file_bytes
abstractmethod
async
Write bytes to a file in this execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination path within this execution environment. |
required |
content
|
bytes
|
File contents to write. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If execution environment not started. |
Source code in src/stirrup/tools/code_backends/base.py
save_output_files
async
save_output_files(
paths: list[str],
output_dir: Path | str,
dest_env: CodeExecToolProvider | None = None,
) -> SaveOutputFilesResult
Save files from this execution environment to a destination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[str]
|
List of file paths in this execution environment to save. |
required |
output_dir
|
Path | str
|
Directory path to save files to. |
required |
dest_env
|
CodeExecToolProvider | None
|
If provided, output_dir is interpreted as a path within dest_env (cross-environment transfer). If None, output_dir is a local filesystem path. |
None
|
Returns:
| Type | Description |
|---|---|
SaveOutputFilesResult
|
SaveOutputFilesResult containing lists of saved files and any failures. |
Source code in src/stirrup/tools/code_backends/base.py
upload_files
async
upload_files(
*paths: Path | str,
source_env: CodeExecToolProvider | None = None,
dest_dir: str | None = None,
) -> UploadFilesResult
Upload files to this execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*paths
|
Path | str
|
File or directory paths to upload. If source_env is None, these are local filesystem paths. If source_env is provided, these are paths within source_env (cross-environment transfer). |
()
|
source_env
|
CodeExecToolProvider | None
|
If provided, paths are within source_env. If None, paths are local filesystem paths. |
None
|
dest_dir
|
str | None
|
Destination directory in this environment. If None, uses the environment's working directory. |
None
|
Returns:
| Type | Description |
|---|---|
UploadFilesResult
|
UploadFilesResult containing lists of uploaded files and any failures. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If execution environment not started. |
Source code in src/stirrup/tools/code_backends/base.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
get_code_exec_tool
get_code_exec_tool(
*,
name: str = "code_exec",
description: str | None = None,
) -> Tool[CodeExecutionParams, ToolUseCountMetadata]
Create a code execution tool for this environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name |
'code_exec'
|
description
|
str | None
|
Tool description |
None
|
Returns:
| Type | Description |
|---|---|
Tool[CodeExecutionParams, ToolUseCountMetadata]
|
Tool[CodeExecutionParams] that executes commands in this environment |
Source code in src/stirrup/tools/code_backends/base.py
get_view_image_tool
get_view_image_tool(
*,
name: str = "view_image",
description: str | None = None,
) -> Tool[ViewImageParams, ToolUseCountMetadata]
Create a view_image tool for this environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Tool name |
'view_image'
|
description
|
str | None
|
Tool description |
None
|
Returns:
| Type | Description |
|---|---|
Tool[ViewImageParams, ToolUseCountMetadata]
|
Tool[ViewImageParams, ToolUseCountMetadata] that views images in this environment |
Source code in src/stirrup/tools/code_backends/base.py
LocalCodeExecToolProvider
Executes code in an isolated temporary directory on the host machine.
stirrup.tools.code_backends.LocalCodeExecToolProvider
LocalCodeExecToolProvider(
*,
allowed_commands: list[str] | None = None,
temp_base_dir: Path | str | None = None,
description: str | None = None,
)
Bases: CodeExecToolProvider
Local code execution tool provider using an isolated temp directory.
Commands are executed with the temp directory as the working directory. An optional allowlist can restrict which commands are permitted.
Usage with Agent
from stirrup.clients.chat_completions_client import ChatCompletionsClient
client = ChatCompletionsClient(model="gpt-5") agent = Agent( client=client, name="assistant", tools=[LocalCodeExecToolProvider(), CALCULATOR_TOOL], )
async with agent.session(output_dir="./output") as session: await session.run("Run some Python code")
Standalone usage
provider = LocalCodeExecToolProvider()
async with provider as tool: # tool is a Tool instance for code execution result = await provider.run_command("python script.py") await provider.save_output_files(["output.txt"], "/path/to/output")
Initialize LocalCodeExecToolProvider configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allowed_commands
|
list[str] | None
|
Optional list of regex patterns. If provided, only commands matching at least one pattern are allowed. If None, all commands are allowed. |
None
|
temp_base_dir
|
Path | str | None
|
Optional base directory for creating the execution environment temp directory. If None, uses the system default temp directory. |
None
|
description
|
str | None
|
Optional description of the tool. If None, uses the default description. |
None
|
Source code in src/stirrup/tools/code_backends/local.py
_temp_base_dir
instance-attribute
_description
instance-attribute
_description = (
description
or "Execute a shell command in the execution environment. Returns exit code, stdout, and stderr as XML. Use `uv` to manage packages."
)
_check_absolute_paths
_check_absolute_paths(cmd: str) -> CommandResult | None
Check if command contains absolute paths that could escape the temp directory.
Returns:
| Type | Description |
|---|---|
CommandResult | None
|
CommandResult with error if absolute paths detected, None otherwise. |
Note
This check is specific to LocalCodeExecToolProvider since Docker and E2B providers are already sandboxed and absolute paths are safe within them.
Source code in src/stirrup/tools/code_backends/local.py
__aenter__
async
__aenter__() -> Tool[
CodeExecutionParams, ToolUseCountMetadata
]
Create temp directory and return the code_exec tool.
Source code in src/stirrup/tools/code_backends/local.py
__aexit__
async
__aexit__(
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: object,
) -> None
Cleanup the local execution environment.
Source code in src/stirrup/tools/code_backends/local.py
_resolve_and_validate_path
Resolve a path and validate it's within the temp directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path (relative or absolute within the temp dir). |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Resolved absolute Path. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If environment not started. |
ValueError
|
If path is outside temp directory. |
FileNotFoundError
|
If path does not exist (for reads). |
Source code in src/stirrup/tools/code_backends/local.py
read_file_bytes
async
Read file content as bytes from the temp directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path (relative or absolute within the temp dir). |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
File contents as bytes. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If environment not started. |
ValueError
|
If path is outside temp directory. |
FileNotFoundError
|
If file does not exist. |
Source code in src/stirrup/tools/code_backends/local.py
write_file_bytes
async
Write bytes to a file in the temp directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Destination path (relative or absolute within the temp dir). |
required |
content
|
bytes
|
File contents to write. |
required |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If environment not started. |
ValueError
|
If path is outside temp directory. |
Source code in src/stirrup/tools/code_backends/local.py
run_command
async
run_command(
cmd: str, *, timeout: int = SHELL_TIMEOUT
) -> CommandResult
Execute command in the temp directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
str
|
Shell command to execute (bash syntax). |
required |
timeout
|
int
|
Maximum time in seconds to wait for command completion. |
SHELL_TIMEOUT
|
Returns:
| Type | Description |
|---|---|
CommandResult
|
CommandResult with exit_code, stdout, stderr, and optional error info. |
Source code in src/stirrup/tools/code_backends/local.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | |
save_output_files
async
save_output_files(
paths: list[str],
output_dir: Path | str,
dest_env: CodeExecToolProvider | None = None,
) -> SaveOutputFilesResult
Move files from the temp directory to a destination.
When dest_env is None (local filesystem), files are MOVED (not copied) - originals are deleted from the execution environment. Existing files in output_dir are silently overwritten.
When dest_env is provided (cross-environment transfer), files are copied using the base class implementation via read/write primitives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
paths
|
list[str]
|
List of file paths in the execution environment (relative or absolute). Relative paths are resolved against the execution environment temp directory. |
required |
output_dir
|
Path | str
|
Directory path to save files to. |
required |
dest_env
|
CodeExecToolProvider | None
|
If provided, output_dir is interpreted as a path within dest_env (cross-environment transfer). If None, output_dir is a local filesystem path. |
None
|
Returns:
| Type | Description |
|---|---|
SaveOutputFilesResult
|
SaveOutputFilesResult containing lists of saved files and any failures. |
Source code in src/stirrup/tools/code_backends/local.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
upload_files
async
upload_files(
*paths: Path | str,
source_env: CodeExecToolProvider | None = None,
dest_dir: str | None = None,
) -> UploadFilesResult
Upload files to the execution environment.
When source_env is None (local filesystem), files are COPIED (not moved) - originals remain on the local filesystem. Directories are uploaded recursively, preserving their structure.
When source_env is provided (cross-environment transfer), files are copied using the base class implementation via read/write primitives.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*paths
|
Path | str
|
File or directory paths to upload. If source_env is None, these are local filesystem paths. If source_env is provided, these are paths within source_env. |
()
|
source_env
|
CodeExecToolProvider | None
|
If provided, paths are within source_env. If None, paths are local filesystem paths. |
None
|
dest_dir
|
str | None
|
Destination subdirectory within the temp directory. If None, files are placed directly in the temp directory. |
None
|
Returns:
| Type | Description |
|---|---|
UploadFilesResult
|
UploadFilesResult containing lists of uploaded files and any failures. |
Source code in src/stirrup/tools/code_backends/local.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
view_image
async
view_image(path: str) -> ImageContentBlock
Read and return an image file from the local execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path to image file (relative to temp directory, or absolute within it). |
required |
Returns:
| Type | Description |
|---|---|
ImageContentBlock
|
ImageContentBlock containing the image data. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If execution environment not started. |
FileNotFoundError
|
If file does not exist. |
ValueError
|
If path is outside temp directory, is a directory, or not a valid image. |
Source code in src/stirrup/tools/code_backends/local.py
DockerCodeExecToolProvider
Executes code in a Docker container.
Note
Requires pip install stirrup[docker] (or: uv add stirrup[docker])
from stirrup.tools.code_backends.docker import DockerCodeExecToolProvider
provider = DockerCodeExecToolProvider.from_image("python:3.12-slim")
E2BCodeExecToolProvider
Executes code in an E2B cloud sandbox.
Note
Requires pip install stirrup[e2b] (or: uv add stirrup[e2b]) and E2B_API_KEY environment variable.
from stirrup.tools.code_backends.e2b import E2BCodeExecToolProvider
provider = E2BCodeExecToolProvider()
Data Types
stirrup.tools.code_backends.CodeExecutionParams
Bases: BaseModel
Shell command to execute in the execution environment.
stirrup.tools.code_backends.CommandResult
dataclass
CommandResult(
exit_code: int,
stdout: str,
stderr: str,
error_kind: str | None = None,
advice: str | None = None,
)
Raw result from command execution (before formatting).