create_directory
Create a directory at a specified path using the MCP File System server. The tool ensures controlled directory management with input validation and security protocols.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | Directory path to create |
Input Schema (JSON Schema)
{
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"path": {
"description": "Directory path to create",
"type": "string"
}
},
"required": [
"path"
],
"type": "object"
}
Implementation Reference
- src/index.ts:96-104 (handler)The handler function that executes the create_directory tool: validates the path, creates directory recursively with fs.mkdir, and returns appropriate content response.async ({ path: dirPath }: { path: string }) => { try { const validPath = validatePath(dirPath); await fs.mkdir(validPath, { recursive: true }); return { content: [{ type: 'text', text: `Directory created: ${dirPath}` }] }; } catch (error: any) { return { content: [{ type: 'text', text: `Error creating directory: ${error.message}` }] }; } }
- src/index.ts:95-95 (schema)Input schema for create_directory tool using Zod: path as string.{ path: z.string().describe("Directory path to create") },
- src/index.ts:93-105 (registration)Registration of the create_directory tool on the MCP server.server.tool( "create_directory", { path: z.string().describe("Directory path to create") }, async ({ path: dirPath }: { path: string }) => { try { const validPath = validatePath(dirPath); await fs.mkdir(validPath, { recursive: true }); return { content: [{ type: 'text', text: `Directory created: ${dirPath}` }] }; } catch (error: any) { return { content: [{ type: 'text', text: `Error creating directory: ${error.message}` }] }; } } );
- src/index.ts:14-20 (helper)Shared helper function validatePath used in the handler to ensure the directory path is within allowed directories.function validatePath(filePath: string): string { const absolutePath = path.resolve(filePath); if (!allowedDirs.some(dir => absolutePath.startsWith(path.resolve(dir)))) { throw new Error(`Access denied: ${filePath} is not within allowed directories`); } return absolutePath; }