write_file
Create or overwrite files with specified content and encoding in allowed directories. Ensures secure file operations through 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)The core handler function that executes the write_file tool logic: validates path and content size, creates parent directories if needed, writes the file using fs.writeFile, and returns success message.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 the input parameters 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)Registration of the write_file tool in the ListTools response, including name, description, and converted input schema.{ 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)MCP server dispatch handler for write_file: parses input arguments using the schema and delegates to the core writeFile implementation.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 }], } }