execute_command
Run commands securely with timeouts and output limits, preventing shell injection and memory issues.
Instructions
Execute a command safely with timeout and output limits.
This tool executes commands without shell injection vulnerabilities. Commands are parsed into argument lists and executed directly via subprocess without shell=True.
Args: command: Command to execute (e.g., "python --version", "npm test") working_directory: Working directory for execution (default: current) timeout_ms: Timeout in milliseconds (default: 30000, max: 300000) capture_output: Whether to capture stdout/stderr (default: True) ctx: MCP context for logging (optional)
Returns: ExecuteCommandOutput with stdout, stderr, exit code, and timing
Raises: ValueError: If input validation fails SanitizedError: If command execution fails
Security: - No shell=True (prevents shell injection) - Command parsed with shlex.split (safe parsing) - Mandatory timeout enforcement - Output size limits (prevents memory exhaustion) - Path validation for working directory - Sanitized error messages (no information leakage)
Examples: >>> result = await execute_command("python --version") >>> print(result.stdout) # "Python 3.11.5" >>> print(result.exit_code) # 0
>>> result = await execute_command(
... "npm test",
... working_directory="./myproject",
... timeout_ms=60000
... )
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes | ||
| timeout_ms | No | ||
| capture_output | No | ||
| working_directory | No |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| stderr | Yes | Standard error from the command | |
| stdout | Yes | Standard output from the command | |
| exit_code | Yes | Exit code returned by the command (0 typically means success) | |
| timed_out | Yes | Whether the command was terminated due to timeout | |
| output_truncated | Yes | Whether output was truncated due to size limit | |
| execution_time_ms | Yes | Time taken to execute command in milliseconds |