From b00b8ef63c47115cf097dde41e583b84170a1e58 Mon Sep 17 00:00:00 2001 From: Markus Fritsche Date: Fri, 17 Apr 2026 15:47:17 +0000 Subject: [PATCH] =?UTF-8?q?Add=20edit=5Ffile=20tool=20=E2=80=94=20Claude?= =?UTF-8?q?=20Code=20Edit=20semantics?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- server.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/server.lua b/server.lua index f0f85e5..c2c4b18 100644 --- a/server.lua +++ b/server.lua @@ -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 = "." } },