create_directory
Create new directories or ensure existing ones are present, with options to automatically create parent directories and handle existing ones without errors.
Instructions
Create a new directory or ensure a directory exists.
Args:
path: Path to the directory
parents: Create parent directories if they don't exist
exist_ok: Don't raise an error if directory already exists
ctx: MCP context
Returns:
Success or error message
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| parents | No | ||
| exist_ok | No |
Implementation Reference
- mcp_filesystem/server.py:172-196 (handler)MCP tool handler for create_directory, decorated with @mcp.tool(), which registers the tool and defines its schema via args/docstring. Delegates to FileOperations.create_directory.@mcp.tool() async def create_directory( path: str, ctx: Context, parents: bool = True, exist_ok: bool = True, ) -> str: """Create a new directory or ensure a directory exists. Args: path: Path to the directory parents: Create parent directories if they don't exist exist_ok: Don't raise an error if directory already exists ctx: MCP context Returns: Success or error message """ try: components = get_components() await components["operations"].create_directory(path, parents, exist_ok) return f"Successfully created directory {path}" except Exception as e: return f"Error creating directory: {str(e)}"
- mcp_filesystem/operations.py:239-267 (helper)Core implementation of directory creation in FileOperations class using pathlib.Path.mkdir with path validation.async def create_directory( self, path: Union[str, Path], parents: bool = True, exist_ok: bool = True ) -> None: """Create a directory. Args: path: Path to the directory parents: Create parent directories if they don't exist exist_ok: Don't raise an error if directory already exists Raises: ValueError: If path is outside allowed directories PermissionError: If directory cannot be created """ abs_path, allowed = await self.validator.validate_path(path) if not allowed: raise ValueError(f"Path outside allowed directories: {path}") try: # Using partial to help mypy understand we're passing args to mkdir, not run_sync await anyio.to_thread.run_sync( partial(abs_path.mkdir, parents=parents, exist_ok=exist_ok) ) except FileExistsError: if not exist_ok: raise ValueError(f"Directory already exists: {path}") except PermissionError as e: raise ValueError(f"Cannot create directory: {e}")