Skip to main content
Glama

edit_file

Replace specific text in files by finding exact matches and substituting with new content. Use this tool to modify file contents through precise text replacement operations.

Instructions

Edit a file by replacing specific text. The old_text must match exactly (including whitespace).

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYesPath to the file
old_textYesText to find and replace (must match exactly)
new_textYesText to replace it with

Implementation Reference

  • The core handler function for the 'edit_file' tool. It resolves the file path, reads the content, verifies the old_text exists, replaces it with new_text using string.replace (all occurrences), writes the file back, and returns a success message.
    async editFile(filePath, oldText, newText) { const resolvedPath = this.resolvePath(filePath); let content = await fs.readFile(resolvedPath, 'utf-8'); if (!content.includes(oldText)) { throw new Error(`Text not found in file: ${oldText.substring(0, 50)}...`); } content = content.replace(oldText, newText); await fs.writeFile(resolvedPath, content, 'utf-8'); return { content: [ { type: 'text', text: `File edited successfully: ${resolvedPath}`, }, ], }; }
  • Input schema definition for the 'edit_file' tool, defining the required parameters: path, old_text, and new_text.
    inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file', }, old_text: { type: 'string', description: 'Text to find and replace (must match exactly)', }, new_text: { type: 'string', description: 'Text to replace it with', }, }, required: ['path', 'old_text', 'new_text'], },
  • index.js:79-100 (registration)
    Registration of the 'edit_file' tool in the ListTools response handler, providing name, description, and schema.
    { name: 'edit_file', description: 'Edit a file by replacing specific text. The old_text must match exactly (including whitespace).', inputSchema: { type: 'object', properties: { path: { type: 'string', description: 'Path to the file', }, old_text: { type: 'string', description: 'Text to find and replace (must match exactly)', }, new_text: { type: 'string', description: 'Text to replace it with', }, }, required: ['path', 'old_text', 'new_text'], }, },
  • index.js:169-170 (registration)
    Dispatch case in the CallToolRequestSchema handler that routes 'edit_file' calls to the editFile method.
    case 'edit_file': return await this.editFile(args.path, args.old_text, args.new_text);

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/talentedmrweb/local-dev-bridge-mcp'

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