check_command_exists
Verify the existence of a specific Unix command on the system. Provides immediate confirmation for command availability, enabling efficient troubleshooting and execution planning.
Instructions
Check if a command exists on the system.
Args: command: The command to check
Returns: Information about whether the command exists
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| command | Yes |
Implementation Reference
- unix_manual_server.py:311-358 (handler)The main handler function for the 'check_command_exists' MCP tool. It validates the command name, resolves its path using get_command_path, checks existence, and attempts to fetch version information using safe_execute with --version, -V, or version subcommands.@mcp.tool() def check_command_exists(command: str) -> str: """ Check if a command exists on the system. Args: command: The command to check Returns: Information about whether the command exists """ logger.info(f"Checking if command exists: '{command}'") command_name = command.strip().split()[0] logger.debug(f"Extracted command name: {command_name}") if not re.match(r'^[a-zA-Z0-9_\.-]+$', command_name): logger.warning(f"Invalid command name: '{command_name}'") return f"Invalid command name: '{command_name}'" command_path = get_command_path(command_name) if command_path: logger.info(f"Command '{command_name}' exists at {command_path}") # Try --version logger.debug(f"Trying --version for {command_name}") version_result = safe_execute([command_path, "--version"], timeout=5) if version_result and version_result.returncode < 2 and version_result.stdout.strip(): logger.debug(f"Got version info using --version for {command_name}") return f"Command '{command_name}' exists at {command_path}.\nVersion information: {version_result.stdout.strip()}" # Try -V (some commands use this for version) logger.debug(f"Trying -V for {command_name}") version_result = safe_execute([command_path, "-V"], timeout=5) if version_result and version_result.returncode < 2 and version_result.stdout.strip(): logger.debug(f"Got version info using -V for {command_name}") return f"Command '{command_name}' exists at {command_path}.\nVersion information: {version_result.stdout.strip()}" # Try version logger.debug(f"Trying version subcommand for {command_name}") version_result = safe_execute([command_path, "version"], timeout=5) if version_result and version_result.returncode < 2 and version_result.stdout.strip(): logger.debug(f"Got version info using version subcommand for {command_name}") return f"Command '{command_name}' exists at {command_path}.\nVersion information: {version_result.stdout.strip()}" return f"Command '{command_name}' exists on this system at {command_path}." else: logger.warning(f"Command '{command_name}' does not exist or is not in the PATH") return f"Command '{command_name}' does not exist or is not in the PATH."
- unix_manual_server.py:26-48 (helper)Helper function used by check_command_exists to resolve the full absolute path of the command using the user's login shell and 'command -v'.def get_command_path(command): """Get the full absolute path to a command by filtering shell output.""" logger.debug(f"Searching for command path: {command}") try: # Use the user's shell (defaulting to /bin/zsh) with login to load the full environment. user_shell = os.environ.get('SHELL', '/bin/zsh') logger.debug(f"Using shell: {user_shell}") result = subprocess.run( [user_shell, "-l", "-c", f"command -v {command} 2>/dev/null"], capture_output=True, text=True ) # Process the output line by line and return the first line that is a valid absolute path. for line in result.stdout.splitlines(): if re.match(r'^/', line): path = line.strip() logger.debug(f"Found command path: {path}") return path logger.warning(f"Command not found: {command}") return None except subprocess.SubprocessError as e: logger.error(f"Error finding command path for {command}: {str(e)}") return None
- unix_manual_server.py:50-72 (helper)Helper function used by check_command_exists to safely execute version checks (--version, -V, version) with timeout and without shell interpretation.def safe_execute(cmd_args, timeout=10): """Safely execute a command directly (not through shell) and return its output.""" logger.debug(f"Executing command: {cmd_args} with timeout={timeout}") try: # Execute command directly without shell result = subprocess.run( cmd_args, capture_output=True, text=True, timeout=timeout, shell=False # Explicitly set shell=False for security ) logger.debug(f"Command exit code: {result.returncode}") # Add debug output to see first 100 chars of stdout if result.stdout: logger.debug(f"Command stdout first 100 chars: {result.stdout[:100].replace('\n', '\\n')}") return result except subprocess.TimeoutExpired: logger.warning(f"Command timed out after {timeout} seconds: {cmd_args}") return None except (subprocess.SubprocessError, FileNotFoundError, OSError) as e: logger.error(f"Error executing command {cmd_args}: {str(e)}") return None