Implementation:Langchain ai Langchain Anthropic Tools Middleware
| Knowledge Sources | |
|---|---|
| Domains | LLM Framework, Anthropic, Agent Middleware, Tool Calling |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
The anthropic_tools.py module provides client-side middleware implementations of Anthropic's text editor and memory tools, supporting both LangGraph state-based and filesystem-based virtual file systems for agentic workflows.
Description
This module implements the AgentMiddleware pattern for Anthropic's native text editor tool (text_editor_20250728) and memory tool (memory_20250818). It provides four public middleware classes -- two state-based (using LangGraph state for storage) and two filesystem-based (using the local filesystem). Each middleware intercepts model calls to inject Anthropic's schema-less tool descriptors and provides tool implementations that handle file operations (view, create, str_replace, insert, delete, rename) on the chosen storage backend.
Usage
Import the desired middleware class and pass it to create_agent from the LangChain agents module. Use state-based middleware for ephemeral in-conversation file storage, or filesystem-based middleware for persistent local file operations.
Code Reference
Source Location
- Repository: Langchain_ai_Langchain
- File:
libs/partners/anthropic/langchain_anthropic/middleware/anthropic_tools.py - Lines: 1-1171
Signature
class AnthropicToolsState(AgentState):
text_editor_files: NotRequired[Annotated[dict[str, FileData], files_reducer]]
memory_files: NotRequired[Annotated[dict[str, FileData], files_reducer]]
class StateClaudeTextEditorMiddleware(_StateClaudeFileToolMiddleware):
def __init__(self, *, allowed_path_prefixes: Sequence[str] | None = None) -> None: ...
class StateClaudeMemoryMiddleware(_StateClaudeFileToolMiddleware):
def __init__(self, *, allowed_path_prefixes: Sequence[str] | None = None,
system_prompt: str = MEMORY_SYSTEM_PROMPT) -> None: ...
class FilesystemClaudeTextEditorMiddleware(_FilesystemClaudeFileToolMiddleware):
def __init__(self, *, root_path: str, allowed_prefixes: list[str] | None = None,
max_file_size_mb: int = 10) -> None: ...
class FilesystemClaudeMemoryMiddleware(_FilesystemClaudeFileToolMiddleware):
def __init__(self, *, root_path: str, allowed_prefixes: list[str] | None = None,
max_file_size_mb: int = 10,
system_prompt: str = MEMORY_SYSTEM_PROMPT) -> None: ...
Import
from langchain_anthropic.middleware.anthropic_tools import (
StateClaudeTextEditorMiddleware,
StateClaudeMemoryMiddleware,
FilesystemClaudeTextEditorMiddleware,
FilesystemClaudeMemoryMiddleware,
AnthropicToolsState,
)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| allowed_path_prefixes / allowed_prefixes | None | No | Restricts file operations to paths starting with these prefixes. Memory middleware defaults to ["/memories"]
|
| root_path | str | Yes (filesystem only) | Root directory for filesystem-based file operations |
| max_file_size_mb | int | No | Maximum file size in MB for filesystem operations (default: 10) |
| system_prompt | str | No | System prompt injected for memory middleware (default: MEMORY_SYSTEM_PROMPT)
|
Outputs
| Name | Type | Description |
|---|---|---|
| Command | langgraph.types.Command |
State update commands containing ToolMessage results and file state changes
|
| str | str | Error messages for invalid operations |
Architecture
Class Hierarchy
AgentMiddleware
|
+-- _StateClaudeFileToolMiddleware (internal base)
| +-- StateClaudeTextEditorMiddleware
| +-- StateClaudeMemoryMiddleware
|
+-- _FilesystemClaudeFileToolMiddleware (internal base)
+-- FilesystemClaudeTextEditorMiddleware
+-- FilesystemClaudeMemoryMiddleware
Tool Operations
Both storage backends support the same set of file commands:
| Command | Description | Required Args |
|---|---|---|
view |
View file content with line numbers, or list directory contents | path
|
create |
Create or overwrite a file | path, file_text
|
str_replace |
Replace first occurrence of a string in a file | path, old_str, new_str
|
insert |
Insert text at a specific line number | path, insert_line, new_str
|
delete |
Delete a file | path
|
rename |
Rename/move a file | old_path, new_path
|
State Schema
The AnthropicToolsState TypedDict extends AgentState with two annotated fields that use the custom files_reducer for merging updates:
text_editor_files: Virtual file system for text editor toolsmemory_files: Virtual file system for memory tools
The files_reducer function handles incremental updates where None values represent file deletions.
Security
Path validation is enforced via _validate_path (state-based) and _validate_and_resolve_path (filesystem-based):
- Rejects path traversal attempts (
..,~) - Enforces allowed path prefix restrictions
- Filesystem variant ensures resolved paths stay within
root_path - Filesystem variant checks file size limits before reading
Usage Examples
Basic Usage
from langchain.agents import create_agent
from langchain_anthropic.middleware.anthropic_tools import (
StateClaudeTextEditorMiddleware,
StateClaudeMemoryMiddleware,
)
# State-based (ephemeral, in LangGraph state)
agent = create_agent(
model=model,
tools=[],
middleware=[
StateClaudeTextEditorMiddleware(),
StateClaudeMemoryMiddleware(),
],
)
Filesystem-Based Usage
from langchain.agents import create_agent
from langchain_anthropic.middleware.anthropic_tools import (
FilesystemClaudeTextEditorMiddleware,
FilesystemClaudeMemoryMiddleware,
)
# Filesystem-based (persistent on disk)
agent = create_agent(
model=model,
tools=[],
middleware=[
FilesystemClaudeTextEditorMiddleware(root_path="/workspace"),
FilesystemClaudeMemoryMiddleware(root_path="/workspace"),
],
)