write_file
Create new files or replace existing ones by writing text content to specified paths. This tool handles file creation and overwriting with proper text encoding for PC file management.
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.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | The path where the file should be written | |
| content | Yes | The content to write to the file |
Implementation Reference
- src/index.ts:288-305 (handler)Handler for the 'write_file' tool. Parses arguments for file path and content, ensures the parent directory exists using fs.mkdir, writes the content to the file with UTF-8 encoding using fs.writeFile, and returns a success confirmation message.case "write_file": { const filePath = args.path as string; const content = args.content as string; // Ensure directory exists const dir = path.dirname(filePath); await fs.mkdir(dir, { recursive: true }); await fs.writeFile(filePath, content, "utf-8"); return { content: [ { type: "text", text: `Successfully wrote to ${filePath}`, }, ], }; }
- src/index.ts:37-50 (schema)Input schema for the 'write_file' tool defining required string parameters 'path' (file location) and 'content' (text to write). Used for validation in tool calls.inputSchema: { type: "object", properties: { path: { type: "string", description: "The path where the file should be written", }, content: { type: "string", description: "The content to write to the file", }, }, required: ["path", "content"], },
- src/index.ts:34-51 (registration)Registration of the 'write_file' tool in the TOOLS array. This Tool object (name, description, inputSchema) is returned by the ListTools handler, making the tool discoverable to MCP clients.{ 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.", inputSchema: { type: "object", properties: { path: { type: "string", description: "The path where the file should be written", }, content: { type: "string", description: "The content to write to the file", }, }, required: ["path", "content"], }, },