Home / Class/ ClaudeBashToolMiddleware Class — langchain Architecture

ClaudeBashToolMiddleware Class — langchain Architecture

Architecture documentation for the ClaudeBashToolMiddleware class in bash.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f["ClaudeBashToolMiddleware"]
  12f8df34_8f5d_c490_cefe_1916d91746c6["ShellToolMiddleware"]
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f -->|extends| 12f8df34_8f5d_c490_cefe_1916d91746c6
  4ee92436_6007_4c4b_a692_e1f8844b6d29["bash.py"]
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f -->|defined in| 4ee92436_6007_4c4b_a692_e1f8844b6d29
  569f5259_25b8_711d_f06a_0304957af5a4["__init__()"]
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f -->|method| 569f5259_25b8_711d_f06a_0304957af5a4
  1665d404_339c_02de_43f1_1b62ec6e85af["wrap_model_call()"]
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f -->|method| 1665d404_339c_02de_43f1_1b62ec6e85af
  7cdff43e_2abe_51f5_072d_6a8ba73a6753["awrap_model_call()"]
  6c7e034b_a020_fa2e_b830_289ca1ffbe8f -->|method| 7cdff43e_2abe_51f5_072d_6a8ba73a6753

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/bash.py lines 19–81

class ClaudeBashToolMiddleware(ShellToolMiddleware):
    """Middleware that exposes Anthropic's native bash tool to models."""

    def __init__(
        self,
        workspace_root: str | None = None,
        *,
        startup_commands: tuple[str, ...] | list[str] | str | None = None,
        shutdown_commands: tuple[str, ...] | list[str] | str | None = None,
        execution_policy: Any | None = None,
        redaction_rules: tuple[Any, ...] | list[Any] | None = None,
        tool_description: str | None = None,
        env: dict[str, Any] | None = None,
    ) -> None:
        """Initialize middleware for Claude's native bash tool.

        Args:
            workspace_root: Base directory for the shell session.

                If omitted, a temporary directory is created.
            startup_commands: Optional commands executed after the session starts.
            shutdown_commands: Optional commands executed before session shutdown.
            execution_policy: Execution policy controlling timeouts and limits.
            redaction_rules: Optional redaction rules to sanitize output.
            tool_description: Optional override for tool description.
            env: Optional environment variables for the shell session.
        """
        super().__init__(
            workspace_root=workspace_root,
            startup_commands=startup_commands,
            shutdown_commands=shutdown_commands,
            execution_policy=execution_policy,
            redaction_rules=redaction_rules,
            tool_description=tool_description,
            tool_name=BASH_TOOL_NAME,
            shell_command=("/bin/bash",),
            env=env,
        )
        # Parent class now creates the tool with name "bash" via tool_name parameter

    def wrap_model_call(
        self,
        request: ModelRequest,
        handler: Callable[[ModelRequest], ModelResponse],
    ) -> ModelResponse:
        """Replace parent's shell tool with Claude's bash descriptor."""
        filtered = [
            t for t in request.tools if getattr(t, "name", None) != BASH_TOOL_NAME
        ]
        tools = [*filtered, {"type": BASH_TOOL_TYPE, "name": BASH_TOOL_NAME}]
        return handler(request.override(tools=tools))

    async def awrap_model_call(
        self,
        request: ModelRequest,
        handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
    ) -> ModelResponse:
        """Async: replace parent's shell tool with Claude's bash descriptor."""
        filtered = [
            t for t in request.tools if getattr(t, "name", None) != BASH_TOOL_NAME
        ]
        tools = [*filtered, {"type": BASH_TOOL_TYPE, "name": BASH_TOOL_NAME}]
        return await handler(request.override(tools=tools))

Frequently Asked Questions

What is the ClaudeBashToolMiddleware class?
ClaudeBashToolMiddleware is a class in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/bash.py.
Where is ClaudeBashToolMiddleware defined?
ClaudeBashToolMiddleware is defined in libs/partners/anthropic/langchain_anthropic/middleware/bash.py at line 19.
What does ClaudeBashToolMiddleware extend?
ClaudeBashToolMiddleware extends ShellToolMiddleware.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free