ensure_directory
Creates a directory if it doesn't exist and returns the absolute path, ensuring required folder structure for PowerShell automation tasks.
Instructions
Ensure directory exists and return absolute path.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes |
Input Schema (JSON Schema)
{
"properties": {
"path": {
"title": "Path",
"type": "string"
}
},
"required": [
"path"
],
"type": "object"
}
Implementation Reference
- src/server.py:306-315 (handler)The main handler function for the 'ensure_directory' tool. It normalizes the path, determines the directory path (handling files vs directories), creates the directory if it doesn't exist using os.makedirs, and returns the absolute path.@mcp.tool() def ensure_directory(path: str) -> str: """Ensure directory exists and return absolute path.""" abs_path = normalize_path(path) if os.path.splitext(abs_path)[1]: # If path has an extension dir_path = os.path.dirname(abs_path) else: dir_path = abs_path os.makedirs(dir_path, exist_ok=True) return abs_path
- src/server.py:296-304 (helper)Helper function used by ensure_directory to normalize input paths to absolute paths, handling relative paths and removing ./ or .\\ prefixes.def normalize_path(path: str) -> str: """Convert relative paths to absolute using current working directory.""" if not path: raise ValueError("Path cannot be empty") if path.startswith(('./','.\\')): path = path[2:] if not os.path.isabs(path): path = os.path.join(os.getcwd(), path) return os.path.abspath(path)