Skip to main content
Glama
safurrier

MCP Filesystem Server

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
NameRequiredDescriptionDefault
pathYes
parentsNo
exist_okNo

Implementation Reference

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

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/safurrier/mcp-filesystem'

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