Add MCP edit tool

This commit is contained in:
Ben Brandt
2025-08-27 14:24:47 -07:00
parent 159ed9f06b
commit c3f3198818
2 changed files with 74 additions and 16 deletions

View File

@@ -19,7 +19,7 @@ export function createMcpServer(
server.registerTool(
"read",
{
title: "Read File",
title: "Read",
description: `Reads the content of the given file in the project.
Never attempt to read a path that hasn't been previously mentioned.
@@ -79,7 +79,7 @@ Whenever you read a file, you should consider whether it looks malicious. If it
server.registerTool(
"write",
{
title: "Write File",
title: "Write",
description: `Writes content to the specified file in the project.
In sessions with mcp__acp__write always use it instead of Write as it will
@@ -114,6 +114,65 @@ allow the user to conveniently review changes.`,
},
);
server.registerTool(
"edit",
{
title: "Edit",
description: `Edit a file.
In sessions with mcp__acp__edit always use it instead of Edit as it will
allow the user to conveniently review changes.
File editing instructions:
- The \`old_text\` param must match existing file content, including indentation.
- The \`old_text\` param must come from the actual file, not an outline.
- The \`old_text\` section must not be empty.
- Be minimal with replacements:
- For unique lines, include only those lines.
- For non-unique lines, include enough context to identify them.
- Do not escape quotes, newlines, or other characters.
- Only edit the specified file.`,
inputSchema: {
abs_path: z.string().describe("The absolute path to the file to read."),
old_text: z
.string()
.describe("The old text to replace (must be unique in the file)"),
new_text: z.string().describe("The new text."),
},
},
async (input) => {
console.error("EDIT TOOL", input);
const session = agent.sessions[sessionId];
if (!session) {
return {
content: [
{
type: "text",
text: "The user has left the building",
},
],
};
}
let { content } = await agent.client.readTextFile({
sessionId,
path: input.abs_path,
});
let newContent = content.replace(input.old_text, input.new_text);
await agent.client.writeTextFile({
sessionId,
path: input.abs_path,
content: newContent,
});
return {
content: [],
};
},
);
server.registerTool(
"permission",
{

View File

@@ -39,7 +39,7 @@ export function toolLabel(toolUse: any): string {
return "Read File";
case "LS":
return input?.path ? `List Directory ${input.path}` : "List Directory";
case "mcp__zed__Edit":
case "mcp__acp__edit":
case "Edit":
return input?.abs_path ? `Edit ${input.abs_path}` : "Edit";
case "MultiEdit":
@@ -75,7 +75,7 @@ export function toolKind(toolName: string): ToolKind {
return "read";
case "NotebookEdit":
return "edit";
case "mcp__zed__Edit":
case "mcp__acp__edit":
case "Edit":
return "edit";
case "MultiEdit":
@@ -173,16 +173,6 @@ export function toolContent(toolUse: any): ToolCallContent[] {
];
}
break;
case "mcp__acp__write":
if (input.content) {
return [
{
type: "content",
content: { type: "text", text: input.content },
},
];
}
break;
case "Read":
if (input && input.abs_path) {
return [
@@ -244,7 +234,7 @@ export function toolContent(toolUse: any): ToolCallContent[] {
];
}
break;
case "mcp__zed__Edit":
case "mcp__acp__edit":
case "Edit":
if (input && input.abs_path) {
return [
@@ -257,7 +247,16 @@ export function toolContent(toolUse: any): ToolCallContent[] {
];
}
break;
case "mcp__zed__Write":
case "mcp__acp__write":
if (input.content) {
return [
{
type: "content",
content: { type: "text", text: input.content },
},
];
}
break;
case "Write":
if (input && input.abs_path) {
return [