Skip to main content
Glama

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

NameRequiredDescriptionDefault
pathYes

Input Schema (JSON Schema)

{ "properties": { "path": { "title": "Path", "type": "string" } }, "required": [ "path" ], "type": "object" }

Implementation Reference

  • 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
  • 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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/DynamicEndpoints/PowerShell-Exec-MCP-Server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server