create_file
Create a new file with specified content at a given path. Automatically generates parent directories if missing. Fails if the file already exists. Operates within allowed directory roots for secure file creation.
Instructions
Create a new file with specified content.
Args: path (str): File path to create (absolute or relative to allowed directories) content (str): UTF-8 text content to write to the file
Returns: str: Success message with created file path, or error message if failed
Note: - Fails if the file already exists - Creates parent directories if they don't exist - Path must be within allowed directory roots
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | ||
| path | Yes |
Implementation Reference
- main.py:206-230 (handler)Handler function for the 'create_file' tool, including decorator for registration. Resolves path safely, creates parents, writes content exclusively, returns success or human-readable error.@mcp.tool def create_file(path: str, content: str) -> str: """Create a new file with specified content. Args: path (str): File path to create (absolute or relative to allowed directories) content (str): UTF-8 text content to write to the file Returns: str: Success message with created file path, or error message if failed Note: - Fails if the file already exists - Creates parent directories if they don't exist - Path must be within allowed directory roots """ try: rp = _resolve(path) rp.parent.mkdir(parents=True, exist_ok=True) with rp.open("x", encoding="utf-8") as f: f.write(content) return f"Created {rp}" except Exception as e: return _human_error(e, "creating file")
- main.py:206-206 (registration)The @mcp.tool decorator registers the create_file function as an MCP tool, guarded by read-only mode check.@mcp.tool
- main.py:207-221 (schema)Input schema defined by type hints (path: str, content: str) and detailed docstring describing parameters, returns, and notes.def create_file(path: str, content: str) -> str: """Create a new file with specified content. Args: path (str): File path to create (absolute or relative to allowed directories) content (str): UTF-8 text content to write to the file Returns: str: Success message with created file path, or error message if failed Note: - Fails if the file already exists - Creates parent directories if they don't exist - Path must be within allowed directory roots """