create_file
Create new files with specified content and encoding in your project directory. Set file paths, write content, and control overwrite behavior for organized file management.
Instructions
Create a new file with specified content
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | File path relative to working directory | |
| content | Yes | Content to write to the file | |
| encoding | No | File encoding (default: utf8) | utf8 |
| overwrite | No | Overwrite if file already exists |
Implementation Reference
- server.js:507-540 (handler)The handler function that implements the create_file tool logic: destructures args, resolves full path, checks if file exists and handles overwrite, ensures parent directory exists, writes the content to the file, and returns success message.async handleCreateFile(args) { const { path: filePath, content, encoding = 'utf8', overwrite = false } = args; const fullPath = path.resolve(this.workingDirectory, filePath); try { // Check if file exists try { await fs.access(fullPath); if (!overwrite) { throw new Error('File already exists. Set overwrite=true to replace it.'); } } catch (error) { // File doesn't exist, which is what we want } // Ensure directory exists const dir = path.dirname(fullPath); await fs.mkdir(dir, { recursive: true }); // Write file await fs.writeFile(fullPath, content, encoding); return { content: [ { type: 'text', text: `File created successfully: ${filePath}`, }, ], }; } catch (error) { throw new McpError(ErrorCode.InternalError, `Failed to create file: ${error.message}`); } }
- server.js:181-204 (schema)Input schema defining the parameters for the create_file tool: path (required), content (required), encoding (optional), overwrite (optional).inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, content: { type: 'string', description: 'Content to write to the file', }, encoding: { type: 'string', description: 'File encoding (default: utf8)', default: 'utf8', }, overwrite: { type: 'boolean', description: 'Overwrite if file already exists', default: false, }, }, required: ['path', 'content'], },
- server.js:179-205 (registration)Tool registration in the ListTools response: defines name, description, and input schema for client discovery.name: 'create_file', description: 'Create a new file with specified content', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'File path relative to working directory', }, content: { type: 'string', description: 'Content to write to the file', }, encoding: { type: 'string', description: 'File encoding (default: utf8)', default: 'utf8', }, overwrite: { type: 'boolean', description: 'Overwrite if file already exists', default: false, }, }, required: ['path', 'content'], }, },
- server.js:473-474 (registration)Dispatcher registration in CallToolRequestSchema handler: switch case that routes 'create_file' calls to the handleCreateFile method.case 'create_file': return await this.handleCreateFile(args);