Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

edit_file tool #184

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Conversation

robotdad
Copy link

@robotdad robotdad commented Dec 2, 2024

Suggested implementation for an edit_file per #183

Description

Adds a edit_file tool to filesystem.

Server Details

  • Server: filesystem
  • Changes to: tools

Motivation and Context

Described in the issue, but write_file really isn't trustworthy for existing files.

How Has This Been Tested?

Claude Desktop, not a ton of interactions with it yet. I tried it on some code in a git repo on Windows which surfaced some line ending issues it can handle now.

Breaking Changes

No.

Types of changes

  • New MCP Server
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update

Checklist

  • I have read the MCP Protocol Documentation
  • My server follows MCP security best practices
  • I have updated the server's README accordingly
  • I have tested this with an LLM client
  • My code follows the repository's style guidelines
  • New and existing tests pass locally
  • I have added appropriate error handling
  • I have documented all environment variables and configuration options

Additional context

Copy link
Member

@jspahrsummers jspahrsummers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I like this idea, just left some comments with suggestions.

src/filesystem/README.md Outdated Show resolved Hide resolved
Comment on lines +243 to +269
if (edit.dryRun) {
// Create git-style diff preview
const preview = [
`@@ line ${lineNumber} @@`,
'<<<<<<< ORIGINAL',
edit.oldText,
'=======',
edit.newText,
'>>>>>>> MODIFIED'
].join('\n');

previews.push({
original: edit.oldText,
modified: edit.newText,
lineNumber,
preview
});
continue;
}

// Apply the edit
content = content.slice(0, pos) + edit.newText + content.slice(pos + edit.oldText.length);
}

if (edits.some(e => e.dryRun)) {
return previews;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since dryRun seems changes the logic and the return type, can we create two different functions instead? We can reuse common bits between them instead of if/else-ing most of the logic in one function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is still a good suggestion after the refactoring I did last night. I can take a look at this later as I'll need to rerun all of my tests after making this change.


const EditFileArgsSchema = z.object({
path: z.string(),
edits: z.array(EditOperation),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO dryRun makes more sense as an argument here, rather than per-operation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look at this while working on the other dry run suggestion, thanks.

@robotdad
Copy link
Author

robotdad commented Dec 3, 2024

@jspahrsummers thank you for the review and encouragement. I made some significant changes to the PR based on some testing last night. I have changed it to using a git style diff and substring matching informed by how Aider handles things. This made things a lot simpler and works a lot better with less special handling. I used Claude to generate a bunch of test documents of likely file types this would be used with described in TEST_DOCUMENTS.md, you can regenerate them using that document and the file system tool. TEST_RESULTS.md records findings and it worked really well, the edit_file tool was used for editing that document while testing other than the initial creation. I've recorded the limitations there and added them to the read me, but I don't think they are significant concerns for the reasons stated. (boils down to there may be issues with mixed tabs/spaces)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants