editFile
Modify or create files by specifying the file path and new content using Claude Code MCP's file editing tool.
Instructions
Create or edit a file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| content | Yes | The new content for the file | |
| file_path | Yes | The absolute path to the file to edit |
Implementation Reference
- src/server/tools.ts:206-232 (registration)Registers the 'editFile' tool on the MCP server, including input schema (file_path and content), description, and inline handler that uses writeFile utility to update the file and returns appropriate response.server.tool( "editFile", "Create or edit a file", { file_path: z.string().describe("The absolute path to the file to edit"), content: z.string().describe("The new content for the file") }, async ({ file_path, content }) => { try { await writeFile(file_path, content); return { content: [{ type: "text", text: `File ${file_path} has been updated.` }] }; } catch (error) { return { content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }], isError: true }; } } );
- src/server/tools.ts:213-231 (handler)Inline handler function for the editFile tool. It writes the provided content to the specified file path using the writeFile helper and returns a success message or error response.async ({ file_path, content }) => { try { await writeFile(file_path, content); return { content: [{ type: "text", text: `File ${file_path} has been updated.` }] }; } catch (error) { return { content: [{ type: "text", text: error instanceof Error ? error.message : String(error) }], isError: true }; } }
- src/server/tools.ts:209-212 (schema)Zod schema defining the input parameters for the editFile tool: absolute file_path (string) and content (string).{ file_path: z.string().describe("The absolute path to the file to edit"), content: z.string().describe("The new content for the file") },
- src/utils/file.ts:112-121 (helper)Helper function that implements the core file editing logic by writing the given content to the specified file path using Node.js fs.promises.writeFile.export async function writeFile( filePath: string, content: string ): Promise<void> { try { await fs.writeFile(filePath, content, 'utf-8'); } catch (error) { throw error; } }