Class FilesystemInterceptor
java.lang.Object
com.alibaba.cloud.ai.graph.agent.interceptor.ModelInterceptor
com.alibaba.cloud.ai.graph.agent.extension.interceptor.FilesystemInterceptor
- All Implemented Interfaces:
Interceptor
Filesystem interceptor that provides file system management capabilities to agents.
This interceptor adds filesystem tools to the agent: ls, read_file, write_file,
edit_file, glob, and grep. It enhances the system prompt to guide agents on using
these filesystem operations effectively.
Key Features:
- Pluggable backend system for file storage (local, state-based, composite)
- Path validation and security (prevents directory traversal)
- Custom tool descriptions support
- File metadata tracking (creation/modification timestamps)
Note: Large result eviction has been moved to
LargeResultEvictionInterceptor.
To enable automatic eviction of large tool results, use both interceptors together.
The interceptor automatically:
- Injects filesystem tools (ls, read_file, write_file, edit_file, glob, grep)
- Provides guidance on proper file path usage (absolute paths required)
- Helps agents explore and modify file systems systematically
Example:
FilesystemInterceptor interceptor = FilesystemInterceptor.builder()
.readOnly(false)
.build();
For automatic large result eviction:
FilesystemInterceptor fsInterceptor = FilesystemInterceptor.builder()
.build();
LargeResultEvictionInterceptor evictionInterceptor =
LargeResultEvictionInterceptor.builder()
.toolTokenLimitBeforeEvict(20000)
.excludeFilesystemTools()
.build();
// Use both interceptors in your agent
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for FilesystemInterceptor with comprehensive configuration options. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()getName()List<org.springframework.ai.tool.ToolCallback>getTools()Get tools provided by this interceptor.interceptModel(ModelRequest request, ModelCallHandler handler) Wrap a model call with custom logic.static StringvalidatePath(String path, List<String> allowedPrefixes) Validate and normalize file path for security.
-
Method Details
-
builder
-
validatePath
Validate and normalize file path for security. Prevents directory traversal attacks by checking for ".." and "~".- Parameters:
path- The path to validateallowedPrefixes- Optional list of allowed path prefixes- Returns:
- Normalized canonical path
- Throws:
IllegalArgumentException- if path is invalid
-
getTools
Description copied from class:ModelInterceptorGet tools provided by this interceptor. Interceptors can provide built-in tools that will be automatically added to the agent.- Overrides:
getToolsin classModelInterceptor- Returns:
- List of tools provided by this interceptor, empty list by default
-
getName
-
interceptModel
Description copied from class:ModelInterceptorWrap a model call with custom logic. Implementations can: - Modify the request before calling the handler - Call the handler multiple times (retry logic) - Modify the response after handler returns - Handle exceptions and provide fallbacks- Specified by:
interceptModelin classModelInterceptor- Parameters:
request- The model requesthandler- The next handler in the chain (or base handler)- Returns:
- The model response
-