write_file
Create new files or replace existing ones with specified content in allowed directories using proper text encoding. Use caution as it overwrites files without warning.
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 |
|---|---|---|---|
| path | Yes | Path where to write the file | |
| content | Yes | Content to write to the file | |
| encoding | No | File encoding | utf-8 |
Implementation Reference
- src/utils/tools.ts:139-170 (handler)The main handler function for the write_file tool. Validates the path, checks content size against limits, creates parent directory if needed, writes the file using fs.writeFile, and returns a 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:122-130 (schema)Zod schema defining the input arguments for the write_file tool: path, content, and optional encoding.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:255-261 (registration)Tool registration in the list_tools 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 (registration)Dispatch handler in the CallToolRequestSchema switch case that parses arguments with the schema and calls the writeFile handler function.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 }], } }