File Search Tools¶
The FileSearch tools provide file discovery and content search capabilities commonly needed in agent workflows: glob-based file finding, regex content search (grep), and directory tree display.
Class Overview¶
FileSearch- Three static methods covering the most common file-discovery operations:glob()- Find files by patterngrep()- Search file contents with regextree()- Display directory structure
Usage¶
Glob - Find Files by Pattern¶
from toolregistry_hub import FileSearch
# Find all Python files recursively
files = FileSearch.glob("**/*.py", root="/path/to/project")
# Non-recursive search in root only
files = FileSearch.glob("*.txt", root=".", recursive=False)
Results are sorted by modification time (most recent first), capped at 1000 results. Paths are relative to root.
Grep - Search File Contents¶
# Search for pattern in directory
results = FileSearch.grep("import os", path="/path/to/project")
# [{"file": "main.py", "line_number": 3, "content": "import os"}, ...]
# Search with file filter
results = FileSearch.grep(r"def\s+test_", path=".", file_pattern="*.py")
# Single file search
results = FileSearch.grep("TODO", path="src/main.py")
# Limit results
results = FileSearch.grep(".", path=".", max_results=10)
Each result is a dict with file (relative path), line_number (1-based), and content (stripped line).
Tree - Directory Structure¶
# Basic tree
print(FileSearch.tree("/path/to/project"))
# project/
# +-- src/
# | +-- main.py
# | +-- utils.py
# +-- tests/
# | +-- test_main.py
# +-- README.md
# Limit depth and filter files
print(FileSearch.tree(".", max_depth=2, file_pattern="*.py"))
# Show hidden files
print(FileSearch.tree(".", show_hidden=True))
Parameters¶
glob()¶
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern |
str |
required | Glob pattern (e.g. "**/*.py") |
root |
str |
"." |
Root directory to search from |
recursive |
bool |
True |
Whether ** matches subdirectories |
grep()¶
| Parameter | Type | Default | Description |
|---|---|---|---|
pattern |
str |
required | Regex pattern to search for |
path |
str |
"." |
File or directory to search in |
recursive |
bool |
True |
Search subdirectories |
file_pattern |
str \| None |
None |
Glob to filter files (e.g. "*.py") |
max_results |
int |
50 |
Maximum results (capped at 200) |
tree()¶
| Parameter | Type | Default | Description |
|---|---|---|---|
path |
str |
"." |
Root directory |
max_depth |
int |
3 |
Maximum depth to display |
show_hidden |
bool |
False |
Show hidden files/directories |
file_pattern |
str \| None |
None |
Glob to filter displayed files |
Safety Caps¶
- Glob: 1000 results max
- Grep: 200 results max
- Tree: 2000 entries max
MCP Server Endpoints¶
API Reference¶
toolregistry_hub.file_search.FileSearch ¶
File and content search tools.
glob
staticmethod
¶
Find files matching a glob pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern (e.g. |
required |
root
|
str
|
Root directory to search from. Defaults to current directory. |
'.'
|
recursive
|
bool
|
Whether |
True
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of matching file paths relative to root, sorted by |
list[str]
|
modification time (most recent first). Capped at 1000 results. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If root does not exist or is not a directory. |
Source code in toolregistry_hub/file_search.py
grep
staticmethod
¶
grep(pattern: str, path: str = '.', recursive: bool = True, file_pattern: str | None = None, max_results: int = 50) -> list[dict]
Search file contents using regex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Regex pattern to search for. |
required |
path
|
str
|
File or directory to search in. Defaults to current directory. |
'.'
|
recursive
|
bool
|
Whether to search subdirectories. Defaults to True. |
True
|
file_pattern
|
str | None
|
Optional glob to filter files (e.g. |
None
|
max_results
|
int
|
Maximum number of match results to return. Clamped to an internal cap of 200. |
50
|
Returns:
| Type | Description |
|---|---|
list[dict]
|
List of dicts, each with keys: |
list[dict]
|
|
list[dict]
|
|
list[dict]
|
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If path does not exist. |
error
|
If pattern is not a valid regex. |
Source code in toolregistry_hub/file_search.py
tree
staticmethod
¶
tree(path: str = '.', max_depth: int = 3, show_hidden: bool = False, file_pattern: str | None = None) -> str
Display directory tree structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Root directory. Defaults to current directory. |
'.'
|
max_depth
|
int
|
Maximum depth to display. Defaults to 3. |
3
|
show_hidden
|
bool
|
Whether to show hidden files/directories. |
False
|
file_pattern
|
str | None
|
Optional glob to filter displayed files. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Tree-formatted string representation of the directory. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If path does not exist or is not a directory. |
ValueError
|
If max_depth is less than 1. |