MCP: add a replace tool for targeted edits
The MCP Sandbox Interface's only way to change a file is write_file, so an agent has to send back the whole file to change one line. That costs tokens, it can mangle unrelated lines (tabs turned into spaces will break a Makefile), and it silently overwrites any changes made on the host after the agent read the file.
Gemini CLI, which MSI is modeled on, has replace for this. Its model-assisted edit correction can't run inside an MCP server, but a plain version can:
replace(file_path, old_string, new_string, expected_replacements = 1)- Fail if
old_stringisn't found, or if the number of matches isn'texpected_replacements. - Otherwise, replace the matches and write the file.
Failing on a mismatch also protects against stale reads: if the file changed since the agent last read it, the edit fails instead of overwriting.
Claude Code's built-in Edit tool works the same way. Without an equivalent, following the docs' advice to swap the client's built-in file tools for Lima's makes editing noticeably worse.
Source: lima-vm/lima