Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Langchain ai Langchain Anthropic Bash Middleware

From Leeroopedia
Knowledge Sources
Domains Middleware, Anthropic, Shell Execution
Last Updated 2026-02-11 00:00 GMT

Overview

ClaudeBashToolMiddleware is agent middleware that exposes Anthropic's native bash tool to Claude models, replacing the generic shell tool descriptor with Claude's specialized bash_20250124 tool type.

Description

ClaudeBashToolMiddleware extends ShellToolMiddleware from langchain.agents.middleware.shell_tool to provide Anthropic-specific bash tool integration. It initializes a shell session (defaulting to /bin/bash) and intercepts model requests to replace the generic shell tool with Anthropic's native bash tool descriptor ({"type": "bash_20250124", "name": "bash"}). This enables Claude models to use their built-in bash tool format rather than a generic tool definition.

Usage

Import this middleware when building LangChain agents that use Anthropic Claude models and need to execute bash commands via Claude's native tool format.

Code Reference

Source Location

  • Repository: Langchain_ai_Langchain
  • File: libs/partners/anthropic/langchain_anthropic/middleware/bash.py
  • Lines: 1-85

Signature

class ClaudeBashToolMiddleware(ShellToolMiddleware):
    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: ...

    def wrap_model_call(
        self,
        request: ModelRequest,
        handler: Callable[[ModelRequest], ModelResponse],
    ) -> ModelResponse: ...

    async def awrap_model_call(
        self,
        request: ModelRequest,
        handler: Callable[[ModelRequest], Awaitable[ModelResponse]],
    ) -> ModelResponse: ...

Import

from langchain_anthropic.middleware.bash import ClaudeBashToolMiddleware

I/O Contract

Inputs

Name Type Required Description
workspace_root None No Base directory for the shell session. If omitted, a temporary directory is created.
startup_commands list[str] | str | None No Commands executed after the session starts.
shutdown_commands list[str] | str | None No Commands executed before session shutdown.
execution_policy None No Execution policy controlling timeouts and limits.
redaction_rules list[Any] | None No Redaction rules to sanitize output.
tool_description None No Override for the tool description.
env None No Environment variables for the shell session.

Outputs

Name Type Description
wrap_model_call return ModelResponse The model response after replacing the shell tool with Claude's native bash descriptor.
awrap_model_call return ModelResponse Async variant returning the same.

Constants

Constant Value Description
BASH_TOOL_TYPE "bash_20250124" The Anthropic-specific tool type identifier for the bash tool.
BASH_TOOL_NAME "bash" The name used for the bash tool.

Usage Examples

Basic Usage

from langchain_anthropic.middleware.bash import ClaudeBashToolMiddleware

# Initialize with a workspace directory
middleware = ClaudeBashToolMiddleware(
    workspace_root="/path/to/workspace",
    startup_commands=["cd /path/to/workspace"],
    env={"MY_VAR": "value"},
)

# Use with a LangChain agent that supports middleware

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment