Home / Class/ FilesystemFileSearchMiddleware Class — langchain Architecture

FilesystemFileSearchMiddleware Class — langchain Architecture

Architecture documentation for the FilesystemFileSearchMiddleware class in file_search.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  c970c63b_27bf_364e_6a46_1e5cc38ce38f["FilesystemFileSearchMiddleware"]
  7e138deb_72a0_c3b2_9372_a9429bc90819["file_search.py"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|defined in| 7e138deb_72a0_c3b2_9372_a9429bc90819
  99cb0385_d25f_ae58_a625_e8187615e060["__init__()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|method| 99cb0385_d25f_ae58_a625_e8187615e060
  b4e992f2_93ef_4642_9ddf_328ed58f534b["_validate_and_resolve_path()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|method| b4e992f2_93ef_4642_9ddf_328ed58f534b
  c95c91c6_daaf_119c_f394_b216ac96d04e["_ripgrep_search()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|method| c95c91c6_daaf_119c_f394_b216ac96d04e
  3f120244_d865_db15_009c_7c3c7671ebd6["_python_search()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|method| 3f120244_d865_db15_009c_7c3c7671ebd6
  dd8bd3f3_4ef2_0063_3646_d8a083bd6388["_format_grep_results()"]
  c970c63b_27bf_364e_6a46_1e5cc38ce38f -->|method| dd8bd3f3_4ef2_0063_3646_d8a083bd6388

Relationship Graph

Source Code

libs/langchain_v1/langchain/agents/middleware/file_search.py lines 87–382

class FilesystemFileSearchMiddleware(AgentMiddleware[AgentState[ResponseT], ContextT, ResponseT]):
    """Provides Glob and Grep search over filesystem files.

    This middleware adds two tools that search through local filesystem:

    - Glob: Fast file pattern matching by file path
    - Grep: Fast content search using ripgrep or Python fallback

    Example:
        ```python
        from langchain.agents import create_agent
        from langchain.agents.middleware import (
            FilesystemFileSearchMiddleware,
        )

        agent = create_agent(
            model=model,
            tools=[],  # Add tools as needed
            middleware=[
                FilesystemFileSearchMiddleware(root_path="/workspace"),
            ],
        )
        ```
    """

    def __init__(
        self,
        *,
        root_path: str,
        use_ripgrep: bool = True,
        max_file_size_mb: int = 10,
    ) -> None:
        """Initialize the search middleware.

        Args:
            root_path: Root directory to search.
            use_ripgrep: Whether to use `ripgrep` for search.

                Falls back to Python if `ripgrep` unavailable.
            max_file_size_mb: Maximum file size to search in MB.
        """
        self.root_path = Path(root_path).resolve()
        self.use_ripgrep = use_ripgrep
        self.max_file_size_bytes = max_file_size_mb * 1024 * 1024

        # Create tool instances as closures that capture self
        @tool
        def glob_search(pattern: str, path: str = "/") -> str:
            """Fast file pattern matching tool that works with any codebase size.

            Supports glob patterns like `**/*.js` or `src/**/*.ts`.

            Returns matching file paths sorted by modification time.

            Use this tool when you need to find files by name patterns.

            Args:
                pattern: The glob pattern to match files against.
                path: The directory to search in. If not specified, searches from root.

            Returns:
                Newline-separated list of matching file paths, sorted by modification
                time (most recently modified first). Returns `'No files found'` if no
                matches.
            """
            try:
                base_full = self._validate_and_resolve_path(path)
            except ValueError:
                return "No files found"

            if not base_full.exists() or not base_full.is_dir():
                return "No files found"

            # Use pathlib glob
            matching: list[tuple[str, str]] = []
            for match in base_full.glob(pattern):
                if match.is_file():
                    # Convert to virtual path
                    virtual_path = "/" + str(match.relative_to(self.root_path))
                    stat = match.stat()
                    modified_at = datetime.fromtimestamp(stat.st_mtime, tz=timezone.utc).isoformat()

Frequently Asked Questions

What is the FilesystemFileSearchMiddleware class?
FilesystemFileSearchMiddleware is a class in the langchain codebase, defined in libs/langchain_v1/langchain/agents/middleware/file_search.py.
Where is FilesystemFileSearchMiddleware defined?
FilesystemFileSearchMiddleware is defined in libs/langchain_v1/langchain/agents/middleware/file_search.py at line 87.

Analyze Your Own Codebase

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

Try Supermodel Free