ShellToolMiddleware Class — langchain Architecture
Architecture documentation for the ShellToolMiddleware class in shell_tool.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 12f8df34_8f5d_c490_cefe_1916d91746c6["ShellToolMiddleware"] 8485637b_14e5_e961_32ca_4c943c82ddfe["_SessionResources"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|extends| 8485637b_14e5_e961_32ca_4c943c82ddfe 5596bd8e_8eff_b33d_22f2_a520bcf8bea4["shell_tool.py"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|defined in| 5596bd8e_8eff_b33d_22f2_a520bcf8bea4 1e17bef5_412d_97e2_472a_783916293f12["__init__()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 1e17bef5_412d_97e2_472a_783916293f12 bc3cba16_0443_e869_4eca_9035a95d52c1["_normalize_commands()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| bc3cba16_0443_e869_4eca_9035a95d52c1 f27ed2d8_86da_12c6_ab4c_a5fd60f81155["_normalize_shell_command()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| f27ed2d8_86da_12c6_ab4c_a5fd60f81155 2f490c1b_918b_fe9e_66cd_de023f4c6824["_normalize_env()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 2f490c1b_918b_fe9e_66cd_de023f4c6824 cdceae02_f019_db48_7689_36cfb431ca7a["before_agent()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| cdceae02_f019_db48_7689_36cfb431ca7a 40090a0a_ae0c_790c_b01f_d8ce3e974d20["abefore_agent()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 40090a0a_ae0c_790c_b01f_d8ce3e974d20 4f686dc2_fd73_bcc5_e93c_9a63c706bdca["after_agent()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 4f686dc2_fd73_bcc5_e93c_9a63c706bdca 97a50ef4_b67b_dbba_ad40_c8d61a0c64da["aafter_agent()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 97a50ef4_b67b_dbba_ad40_c8d61a0c64da 1cb62d4e_980a_14c0_e2ea_6a4aa8fad2ae["_get_or_create_resources()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 1cb62d4e_980a_14c0_e2ea_6a4aa8fad2ae e68b2822_e3c3_68e3_054c_0383ef714d2c["_create_resources()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| e68b2822_e3c3_68e3_054c_0383ef714d2c 2c2391f7_7308_d4b1_0c1f_e7d0bdc07e08["_run_startup_commands()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| 2c2391f7_7308_d4b1_0c1f_e7d0bdc07e08 cde8c83b_84c4_2e51_ee6e_3b6db67397f5["_run_shutdown_commands()"] 12f8df34_8f5d_c490_cefe_1916d91746c6 -->|method| cde8c83b_84c4_2e51_ee6e_3b6db67397f5
Relationship Graph
Source Code
libs/langchain_v1/langchain/agents/middleware/shell_tool.py lines 489–873
class ShellToolMiddleware(AgentMiddleware[ShellToolState[ResponseT], ContextT, ResponseT]):
"""Middleware that registers a persistent shell tool for agents.
The middleware exposes a single long-lived shell session. Use the execution policy
to match your deployment's security posture:
* `HostExecutionPolicy` – full host access; best for trusted environments where the
agent already runs inside a container or VM that provides isolation.
* `CodexSandboxExecutionPolicy` – reuses the Codex CLI sandbox for additional
syscall/filesystem restrictions when the CLI is available.
* `DockerExecutionPolicy` – launches a separate Docker container for each agent run,
providing harder isolation, optional read-only root filesystems, and user
remapping.
When no policy is provided the middleware defaults to `HostExecutionPolicy`.
"""
state_schema = ShellToolState # type: ignore[assignment]
def __init__(
self,
workspace_root: str | Path | None = None,
*,
startup_commands: tuple[str, ...] | list[str] | str | None = None,
shutdown_commands: tuple[str, ...] | list[str] | str | None = None,
execution_policy: BaseExecutionPolicy | None = None,
redaction_rules: tuple[RedactionRule, ...] | list[RedactionRule] | None = None,
tool_description: str | None = None,
tool_name: str = SHELL_TOOL_NAME,
shell_command: Sequence[str] | str | None = None,
env: Mapping[str, Any] | None = None,
) -> None:
"""Initialize an instance of `ShellToolMiddleware`.
Args:
workspace_root: Base directory for the shell session.
If omitted, a temporary directory is created when the agent starts and
removed when it ends.
startup_commands: Optional commands executed sequentially after the session
starts.
shutdown_commands: Optional commands executed before the session shuts down.
execution_policy: Execution policy controlling timeouts, output limits, and
resource configuration.
Defaults to `HostExecutionPolicy` for native execution.
redaction_rules: Optional redaction rules to sanitize command output before
returning it to the model.
!!! warning
Redaction rules are applied post execution and do not prevent
exfiltration of secrets or sensitive data when using
`HostExecutionPolicy`.
tool_description: Optional override for the registered shell tool
description.
tool_name: Name for the registered shell tool.
Defaults to `"shell"`.
shell_command: Optional shell executable (string) or argument sequence used
to launch the persistent session.
Defaults to an implementation-defined bash command.
env: Optional environment variables to supply to the shell session.
Values are coerced to strings before command execution. If omitted, the
session inherits the parent process environment.
"""
super().__init__()
self._workspace_root = Path(workspace_root) if workspace_root else None
self._tool_name = tool_name
self._shell_command = self._normalize_shell_command(shell_command)
self._environment = self._normalize_env(env)
if execution_policy is not None:
self._execution_policy = execution_policy
else:
self._execution_policy = HostExecutionPolicy()
rules = redaction_rules or ()
self._redaction_rules: tuple[ResolvedRedactionRule, ...] = tuple(
rule.resolve() for rule in rules
)
Extends
Source
Frequently Asked Questions
What is the ShellToolMiddleware class?
ShellToolMiddleware is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/shell_tool.py.
Where is ShellToolMiddleware defined?
ShellToolMiddleware is defined in libs/langchain_v1/langchain/agents/middleware/shell_tool.py at line 489.
What does ShellToolMiddleware extend?
ShellToolMiddleware extends _SessionResources.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free