Skip to main content
Glama

rewrite_file

Replace entire file content with new text to update code or documentation quickly. Specify file path and new content to rewrite files completely.

Instructions

Rewrites entire file content

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pathYes
contentYes

Implementation Reference

  • Core implementation of the rewrite_file tool: validates path, reads original content, overwrites with new content, generates and returns unified diff.
    async def rewrite_file(self, path: str, content: str) -> str: path = await self.validate_path(path) original = await self.read_file(path) if path.exists() else "" await self.write_file(path, content) return self.generate_diff(original, content)
  • Pydantic model defining the input schema (path and content) for the rewrite_file tool, used in tool registration.
    class FileRewrite(BaseModel): path: str | Path content: str
  • MCP tool registration in list_tools(): defines name, description, and input schema for rewrite_file.
    ), Tool( name=CodeAssistTools.REWRITE_FILE, description="Rewrites entire file content", inputSchema=FileRewrite.model_json_schema(),
  • MCP server call_tool handler: parses arguments into FileRewrite model and delegates to file_tools.rewrite_file.
    case CodeAssistTools.REWRITE_FILE: model = FileRewrite(path=arguments["path"], content=arguments["content"]) result = await file_tools.rewrite_file(model.path, model.content) return [TextContent(type="text", text=result)]
  • Static helper method used by rewrite_file (and modify_file) to generate unified diff output.
    @staticmethod def generate_diff(original: str, modified: str) -> str: diff = difflib.unified_diff(original.splitlines(keepends=True), modified.splitlines(keepends=True), fromfile="original", tofile="modified") return "".join(diff)

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/abhishekbhakat/mcp_server_code_assist'

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