write_file
Create or overwrite files with specified content in predefined directories using proper text encoding. Ensures controlled filesystem access for secure file management within the MCP Filesystem Server.
Instructions
Create a new file or completely overwrite an existing file with new content. Use with caution as it will overwrite existing files without warning. Handles text content with proper encoding. Only works within allowed directories.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | Content to write to the file | |
| encoding | No | File encoding | utf-8 |
| path | Yes | Path where to write the file |
Implementation Reference
- src/utils/tools.ts:139-170 (handler)Core handler function that implements the write_file tool logic: validates path and content size, creates parent directories, writes file using fs.writeFile, handles errors and metrics.export async function writeFile( args: z.infer<typeof WriteFileArgsSchema>, config: Config ): Promise<string> { const endMetric = metrics.startOperation('write_file') try { const validPath = await validatePath(args.path, config) // Check if content size exceeds limits if (config.security.maxFileSize > 0) { const contentSize = Buffer.byteLength(args.content, args.encoding as BufferEncoding) if (contentSize > config.security.maxFileSize) { metrics.recordError('write_file') throw new FileSizeError(args.path, contentSize, config.security.maxFileSize) } } // Create parent directory if needed const parentDir = path.dirname(validPath) await fs.mkdir(parentDir, { recursive: true }) // Write the file await fs.writeFile(validPath, args.content, args.encoding) await logger.debug(`Successfully wrote to file: ${validPath}`) endMetric() return `Successfully wrote to ${args.path}` } catch (error) { metrics.recordError('write_file') throw error } }
- src/utils/tools.ts:120-130 (schema)Zod schema defining input arguments for the write_file tool: path, content, and optional encoding.* Schema for write_file arguments */ export const WriteFileArgsSchema = z.object({ path: z.string().describe('Path where to write the file'), content: z.string().describe('Content to write to the file'), encoding: z .enum(['utf-8', 'utf8', 'base64']) .optional() .default('utf-8') .describe('File encoding'), })
- src/index.ts:254-261 (registration)Tool registration in the listTools response, defining name, description, and input schema for write_file.{ name: 'write_file', description: 'Create a new file or completely overwrite an existing file with new content. ' + 'Use with caution as it will overwrite existing files without warning. ' + 'Handles text content with proper encoding. Only works within allowed directories.', inputSchema: zodToJsonSchema(WriteFileArgsSchema) as ToolInput, },
- src/index.ts:440-454 (handler)Dispatcher handler in the main CallToolRequestSchema handler that parses arguments, calls the writeFile implementation, and formats the MCP response.case 'write_file': { const parsed = WriteFileArgsSchema.safeParse(a) if (!parsed.success) { throw new FileSystemError(`Invalid arguments for ${name}`, 'INVALID_ARGS', undefined, { errors: parsed.error.format(), }) } const result = await writeFile(parsed.data, config) endMetric() return { content: [{ type: 'text', text: result }], } }