Home / Class/ _StateClaudeFileToolMiddleware Class — langchain Architecture

_StateClaudeFileToolMiddleware Class — langchain Architecture

Architecture documentation for the _StateClaudeFileToolMiddleware class in anthropic_tools.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  2e6eda6f_54cd_9575_7c70_af4582d5a816["_StateClaudeFileToolMiddleware"]
  6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b["AgentMiddleware"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|extends| 6d9956c5_7c84_9ec3_58a3_b6b89f42fd0b
  5f8ab4b6_f98f_f653_1208_76332f94ecba["anthropic_tools.py"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|defined in| 5f8ab4b6_f98f_f653_1208_76332f94ecba
  90a2ca7d_b155_6823_a91f_f7c8d21facf6["__init__()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| 90a2ca7d_b155_6823_a91f_f7c8d21facf6
  e32af08e_ec7f_3004_1c73_0bf72d2c7d4c["wrap_model_call()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| e32af08e_ec7f_3004_1c73_0bf72d2c7d4c
  d7b72219_36f9_a45e_d607_eef6ad7437b8["awrap_model_call()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| d7b72219_36f9_a45e_d607_eef6ad7437b8
  c3f43648_3f51_8e4a_5764_178a6e56a520["_handle_view()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| c3f43648_3f51_8e4a_5764_178a6e56a520
  e84826c9_00c5_f813_57e2_12f7d728476b["_handle_create()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| e84826c9_00c5_f813_57e2_12f7d728476b
  982aeab6_41a3_dd0d_8f1a_0e99b1d5983c["_handle_str_replace()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| 982aeab6_41a3_dd0d_8f1a_0e99b1d5983c
  5aae4748_1c6a_3f83_f21b_69b5f122f59f["_handle_insert()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| 5aae4748_1c6a_3f83_f21b_69b5f122f59f
  26ab4ad7_e467_d101_2b57_df4f529196af["_handle_delete()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| 26ab4ad7_e467_d101_2b57_df4f529196af
  324ccafe_509d_5f07_fc49_bc08ec1c3146["_handle_rename()"]
  2e6eda6f_54cd_9575_7c70_af4582d5a816 -->|method| 324ccafe_509d_5f07_fc49_bc08ec1c3146

Relationship Graph

Source Code

libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py lines 159–569

class _StateClaudeFileToolMiddleware(AgentMiddleware):
    """Base class for state-based file tool middleware (internal)."""

    state_schema = AnthropicToolsState

    def __init__(
        self,
        *,
        tool_type: str,
        tool_name: str,
        state_key: str,
        allowed_path_prefixes: Sequence[str] | None = None,
        system_prompt: str | None = None,
    ) -> None:
        """Initialize.

        Args:
            tool_type: Tool type identifier.
            tool_name: Tool name.
            state_key: State key for file storage.
            allowed_path_prefixes: Optional list of allowed path prefixes.
            system_prompt: Optional system prompt to inject.
        """
        self.tool_type = tool_type
        self.tool_name = tool_name
        self.state_key = state_key
        self.allowed_prefixes = allowed_path_prefixes
        self.system_prompt = system_prompt

        # Create tool that will be executed by the tool node
        @tool(tool_name)
        def file_tool(
            runtime: ToolRuntime[None, AnthropicToolsState],
            command: str,
            path: str,
            file_text: str | None = None,
            old_str: str | None = None,
            new_str: str | None = None,
            insert_line: int | None = None,
            new_path: str | None = None,
            view_range: list[int] | None = None,
        ) -> Command | str:
            """Execute file operations on virtual file system.

            Args:
                runtime: Tool runtime providing access to state.
                command: Operation to perform.
                path: File path to operate on.
                file_text: Full file content for create command.
                old_str: String to replace for str_replace command.
                new_str: Replacement string for str_replace command.
                insert_line: Line number for insert command.
                new_path: New path for rename command.
                view_range: Line range `[start, end]` for view command.

            Returns:
                Command for state update or string result.
            """
            # Build args dict for handler methods
            args: dict[str, Any] = {"path": path}
            if file_text is not None:
                args["file_text"] = file_text
            if old_str is not None:
                args["old_str"] = old_str
            if new_str is not None:
                args["new_str"] = new_str
            if insert_line is not None:
                args["insert_line"] = insert_line
            if new_path is not None:
                args["new_path"] = new_path
            if view_range is not None:
                args["view_range"] = view_range

            # Route to appropriate handler based on command
            try:
                if command == "view":
                    return self._handle_view(args, runtime.state, runtime.tool_call_id)
                if command == "create":
                    return self._handle_create(
                        args, runtime.state, runtime.tool_call_id
                    )

Extends

Frequently Asked Questions

What is the _StateClaudeFileToolMiddleware class?
_StateClaudeFileToolMiddleware is a class in the langchain codebase, defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py.
Where is _StateClaudeFileToolMiddleware defined?
_StateClaudeFileToolMiddleware is defined in libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py at line 159.
What does _StateClaudeFileToolMiddleware extend?
_StateClaudeFileToolMiddleware extends AgentMiddleware.

Analyze Your Own Codebase

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

Try Supermodel Free