Skip to main content
Glama
safurrier

MCP Filesystem Server

create_directory

Create or verify directory existence with options for parent directories and error suppression. Simplifies file system management on the MCP Filesystem Server.

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

Implementation Reference

  • MCP tool handler for 'create_directory' decorated with @mcp.tool(). Handles input parameters, calls the operations component, and returns success/error message.
    @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 in FileOperations class. Validates path security and performs directory creation using pathlib.Path.mkdir() with parents and exist_ok options.
    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