create_file
Generates a new file at a specified path with given content, supporting customizable encoding and optional overwrite functionality on the Enhanced Directory Context MCP Server.
Instructions
Create a new file with specified content
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content to write to the file | |
| encoding | No | File encoding (default: utf8) | utf8 |
| overwrite | No | Overwrite if file already exists | |
| path | Yes | File path relative to working directory |
Input Schema (JSON Schema)
{
"properties": {
"content": {
"description": "Content to write to the file",
"type": "string"
},
"encoding": {
"default": "utf8",
"description": "File encoding (default: utf8)",
"type": "string"
},
"overwrite": {
"default": false,
"description": "Overwrite if file already exists",
"type": "boolean"
},
"path": {
"description": "File path relative to working directory",
"type": "string"
}
},
"required": [
"path",
"content"
],
"type": "object"
}
Implementation Reference
- server.js:507-540 (handler)Main execution logic for the 'create_file' tool: destructures args, resolves full path, checks existence and overwrite flag, creates parent directory if needed, writes content using fs.writeFile, returns success message or throws McpError.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)JSON Schema defining input parameters for create_file tool: path (string, required), content (string, required), encoding (string, default 'utf8'), overwrite (boolean, default false).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)Tool dispatch/registration in the CallToolRequestSchema switch statement: maps 'create_file' tool name to this.handleCreateFile handler.case 'create_file': return await this.handleCreateFile(args);
- server.js:178-205 (registration)Tool metadata registration: name, description, and full inputSchema included in the tools list returned by ListToolsRequestHandler.{ 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'], }, },