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
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| content | Yes |
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
- src/mcp_server_code_assist/server.py:104-108 (registration)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)