modify_file
Modify file content by replacing specified text strings to update code or configuration files programmatically.
Instructions
Modifies parts of a file using string replacements
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| path | Yes | ||
| replacements | Yes |
Implementation Reference
- Core handler function that validates the path, reads the file, applies string replacements, writes the modified content, and generates a unified diff.async def modify_file(self, path: str, replacements: dict[str, str]) -> str: path = await self.validate_path(path) content = await self.read_file(path) original = content for old, new in replacements.items(): content = content.replace(old, new) await self.write_file(path, content) return self.generate_diff(original, content)
- Pydantic BaseModel defining the input schema for the modify_file tool: path (str or Path) and replacements (dict of str to str). Used for validation and JSON schema generation.class FileModify(BaseModel): path: str | Path replacements: dict[str, str]
- src/mcp_server_code_assist/server.py:100-104 (registration)Registration of the 'modify_file' tool in the MCP server's list_tools() method, specifying name, description, and input schema.Tool( name=CodeAssistTools.MODIFY_FILE, description="Modifies parts of a file using string replacements", inputSchema=FileModify.model_json_schema(), ),
- Dispatch handler in the MCP server's call_tool() method that parses arguments into FileModify model and invokes the file_tools.modify_file implementation.case CodeAssistTools.MODIFY_FILE: model = FileModify(path=arguments["path"], replacements=arguments["replacements"]) result = await file_tools.modify_file(model.path, model.replacements) return [TextContent(type="text", text=result)]
- src/mcp_server_code_assist/server.py:22-22 (registration)Enum constant defining the tool name 'modify_file' in CodeAssistTools.MODIFY_FILE = "modify_file"