Add edit_file tool — Claude Code Edit semantics

Literal string replacement with uniqueness check. Fails if old_string
is not found or matches multiple times (unless replace_all=true).

Matches the Claude Code harness Edit tool so sibling lmcp clients get
the same behaviour they already expect for in-place patches.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-17 15:47:17 +00:00
parent c6efc8f685
commit b00b8ef63c
+54
View File
@@ -194,6 +194,60 @@ server:tool("write_file", "Write content to a file.", {
return string.format("Written %d bytes to %s", #a.content, a.path)
end)
server:tool("edit_file", "Replace exact text in a file (literal match). Fails unless old_string is unique, unless replace_all=true.", {
type = "object",
properties = {
path = { type = "string", description = "Path to file" },
old_string = { type = "string", description = "Exact text to replace (literal, no regex)" },
new_string = { type = "string", description = "Replacement text" },
replace_all = { type = "boolean", description = "Replace every occurrence (default: false)", default = false },
},
required = { "path", "old_string", "new_string" },
}, function(a)
if type(a.path) ~= "string" or a.path == "" then return "Error: path required" end
if type(a.old_string) ~= "string" then return "Error: old_string required" end
if type(a.new_string) ~= "string" then return "Error: new_string required" end
if a.old_string == "" then return "Error: old_string cannot be empty" end
if a.old_string == a.new_string then return "Error: new_string must differ from old_string" end
local f = io.open(a.path, 'rb')
if not f then return "Error: could not read " .. a.path end
local content = f:read('*a'); f:close()
local count, pos = 0, 1
while pos <= #content do
local i = content:find(a.old_string, pos, true)
if not i then break end
count = count + 1
pos = i + #a.old_string
end
if count == 0 then
return "Error: old_string not found in " .. a.path
end
if count > 1 and not a.replace_all then
return string.format("Error: old_string matches %d times in %s (use replace_all=true or provide more surrounding context to disambiguate)", count, a.path)
end
local parts, p, replaced = {}, 1, 0
while true do
local i = content:find(a.old_string, p, true)
if not i then break end
parts[#parts+1] = content:sub(p, i-1)
parts[#parts+1] = a.new_string
p = i + #a.old_string
replaced = replaced + 1
if not a.replace_all then break end
end
parts[#parts+1] = content:sub(p)
local w = io.open(a.path, 'wb')
if not w then return "Error: could not write " .. a.path end
w:write(table.concat(parts)); w:close()
return string.format("Edited %s: %d replacement(s)", a.path, replaced)
end)
server:tool("list_dir", "List directory contents.", {
type = "object",
properties = { path = { type = "string", default = "." } },