Skip to main content
Glama

editFile

Create or modify files by specifying a path and content, enabling direct file operations through the Claude Code MCP server.

Instructions

Create or edit a file

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
file_pathYesThe absolute path to the file to edit
contentYesThe new content for the file

Implementation Reference

  • The handler function for the 'editFile' tool that takes file_path and content, writes the content to the file using writeFile utility, and returns a success 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
        };
      }
    }
  • Zod input schema defining parameters for the 'editFile' tool: 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")
    },
  • Registration of the 'editFile' tool on the MCP server using server.tool(), including description, schema, and inline handler.
    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
          };
        }
      }
    );
  • Supporting utility function writeFile that performs the file system write operation 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;
      }
    }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/auchenberg/claude-code-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server