类 ShellCommandTool
- 所有已实现的接口:
AgentTool
Features: command whitelist, user approval callback, multiple command detection, timeout support (default 300s), platform-specific validation, customizable charset for decoding command output (default UTF-8).
Charset Support: The tool supports custom charset configuration for decoding command output streams (stdout/stderr). This is particularly useful when working with systems that use non-UTF-8 encodings (e.g., GBK, GB2312 for Chinese Windows systems). The charset can be configured at tool construction time or overridden per command execution.
Security Warning: new ShellCommandTool() allows arbitrary command execution.
For production, ALWAYS use whitelist: new ShellCommandTool(allowedCommands)
or with callback: new ShellCommandTool(allowedCommands, approvalCallback)
- 另请参阅:
-
构造器概要
构造器构造器说明ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback) Constructor with base directory, command whitelist, and approval callback.ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator) Constructor with base directory, command whitelist, approval callback, and custom validator.ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator, Charset charset) Full constructor with all configuration options including charset.ShellCommandTool(Set<String> allowedCommands) Constructor with command whitelist and approval callback.ShellCommandTool(Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator) Constructor with command whitelist, approval callback, and custom validator. -
方法概要
修饰符和类型方法说明booleanaddAllowedCommand(String command) Add a command to the whitelist in a thread-safe manner.reactor.core.publisher.Mono<ToolResultBlock> callAsync(ToolCallParam param) Execute the tool with the given parameters (asynchronous).voidClear all commands from the whitelist in a thread-safe manner.reactor.core.publisher.Mono<ToolResultBlock> executeShellCommand(String command, Integer timeout) Execute a shell command and return the return code, standard output, and standard error within tags.reactor.core.publisher.Mono<ToolResultBlock> executeShellCommand(String command, Integer timeout, Charset overrideCharset) Execute a shell command and return the return code, standard output, and standard error within tags.Get an unmodifiable view of the allowed commands.Get the approval callback function.Get the base directory for command execution.Get the charset used for decoding command output streams.Get the command validator.Gets the description of the tool.getName()Gets the name of the tool.Gets the parameters schema for this tool in JSON Schema format.booleanisCommandAllowed(String command) Check if a command is in the whitelist.booleanremoveAllowedCommand(String command) Remove a command from the whitelist in a thread-safe manner.从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait从接口继承的方法 io.agentscope.core.tool.AgentTool
getOutputSchema
-
构造器详细资料
-
ShellCommandTool
public ShellCommandTool() -
ShellCommandTool
-
ShellCommandTool
Constructor with command whitelist and approval callback.- 参数:
allowedCommands- Set of allowed command executablesapprovalCallback- Callback function to request user approval
-
ShellCommandTool
public ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback) Constructor with base directory, command whitelist, and approval callback.- 参数:
baseDir- Base directory for command execution (null to use current directory)allowedCommands- Set of allowed command executablesapprovalCallback- Callback function to request user approval
-
ShellCommandTool
public ShellCommandTool(Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator) Constructor with command whitelist, approval callback, and custom validator.- 参数:
allowedCommands- Set of allowed command executables (null to allow all commands)approvalCallback- Callback function to request user approvalcommandValidator- Custom command validator
-
ShellCommandTool
public ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator) Constructor with base directory, command whitelist, approval callback, and custom validator.Uses UTF-8 as the default charset for decoding command output.
- 参数:
baseDir- Base directory for command execution (null to use current directory)allowedCommands- Set of allowed command executables (null to allow all commands)approvalCallback- Callback function to request user approvalcommandValidator- Custom command validator
-
ShellCommandTool
public ShellCommandTool(String baseDir, Set<String> allowedCommands, Function<String, Boolean> approvalCallback, CommandValidator commandValidator, Charset charset) Full constructor with all configuration options including charset.This is the most comprehensive constructor that allows setting all available options. All other constructors delegate to this one with default values.
- 参数:
baseDir- Base directory for command execution (null to use current directory)allowedCommands- Set of allowed command executables (null to allow all commands)approvalCallback- Callback function to request user approvalcommandValidator- Custom command validator (null to use platform-specific default)charset- Charset used to decode command output streams (null to use UTF-8)
-
-
方法详细资料
-
getAllowedCommands
Get an unmodifiable view of the allowed commands. The returned set is thread-safe for reading but cannot be modified directly. UseaddAllowedCommand(String),removeAllowedCommand(String), orclearAllowedCommands()to modify the whitelist.- 返回:
- An unmodifiable view of the allowed command executables
-
addAllowedCommand
Add a command to the whitelist in a thread-safe manner.- 参数:
command- The command executable to add- 返回:
- true if the command was added, false if it was already present
-
removeAllowedCommand
Remove a command from the whitelist in a thread-safe manner.- 参数:
command- The command executable to remove- 返回:
- true if the command was removed, false if it was not present
-
clearAllowedCommands
public void clearAllowedCommands()Clear all commands from the whitelist in a thread-safe manner. -
isCommandAllowed
Check if a command is in the whitelist.- 参数:
command- The command executable to check- 返回:
- true if the command is whitelisted
-
getApprovalCallback
Get the approval callback function. Returns the callback that is used to request user approval for non-whitelisted commands.This method is useful for cloning ShellCommandTool instances with the same configuration.
- 返回:
- The approval callback function, or null if not configured
-
getCommandValidator
Get the command validator. Returns the validator used for command security validation.This method is useful for cloning ShellCommandTool instances with the same configuration.
- 返回:
- The command validator instance
-
getBaseDir
Get the base directory for command execution. Returns the directory where commands will be executed.This method is useful for cloning ShellCommandTool instances with a different base directory.
- 返回:
- The base directory path, or null if using current directory
-
getCharset
Get the charset used for decoding command output streams. Returns the charset used to decode stdout and stderr from executed commands.This method is useful for cloning ShellCommandTool instances with the same configuration, or for inspecting the current charset setting.
- 返回:
- The charset used for decoding command output (never null)
-
getName
从接口复制的说明:AgentToolGets the name of the tool.The name should be unique within a toolkit and follow snake_case convention for compatibility with various LLM providers.
-
getDescription
从接口复制的说明:AgentToolGets the description of the tool.The description should clearly explain what the tool does, when it should be used, and what kind of results it returns. This helps the LLM decide when to invoke the tool.
- 指定者:
getDescription在接口中AgentTool- 返回:
- The tool description (never null)
-
getParameters
从接口复制的说明:AgentToolGets the parameters schema for this tool in JSON Schema format.The schema defines the structure of the input parameters that this tool accepts. It should include:
- type: "object"
- properties: Map of parameter names to their schemas
- required: List of required parameter names
- 指定者:
getParameters在接口中AgentTool- 返回:
- Map representing the JSON Schema for tool parameters (never null)
-
callAsync
从接口复制的说明:AgentToolExecute the tool with the given parameters (asynchronous).This method accepts a
ToolCallParamobject containing all necessary context for tool execution, including:- toolUseBlock: Contains tool call ID and name for tracking
- input: Input parameters for the tool
- agent: The calling agent (may be null), provides access to agent context
-
executeShellCommand
public reactor.core.publisher.Mono<ToolResultBlock> executeShellCommand(String command, Integer timeout) Execute a shell command and return the return code, standard output, and standard error within tags.Uses the default charset configured for this tool instance.
Security features:
- Command whitelist validation - only whitelisted commands execute directly
- Multiple command detection - prevents command chaining attacks (&, |, ;)
- User approval callback - requests permission for non-whitelisted commands
- Platform-specific validation - different rules for Windows and Unix/Linux/macOS
- 参数:
command- The shell command to executetimeout- The maximum time (in seconds) allowed for the command to run (default: 300)- 返回:
- A ToolResultBlock containing the formatted output with returncode, stdout, and stderr
-
executeShellCommand
public reactor.core.publisher.Mono<ToolResultBlock> executeShellCommand(String command, Integer timeout, Charset overrideCharset) Execute a shell command and return the return code, standard output, and standard error within tags.Security features:
- Command whitelist validation - only whitelisted commands execute directly
- Multiple command detection - prevents command chaining attacks (&, |, ;)
- User approval callback - requests permission for non-whitelisted commands
- Platform-specific validation - different rules for Windows and Unix/Linux/macOS
- 参数:
command- The shell command to executetimeout- The maximum time (in seconds) allowed for the command to run (default: 300)overrideCharset- Optional charset to use for decoding output (null to use default)- 返回:
- A ToolResultBlock containing the formatted output with returncode, stdout, and stderr
-