feat(filesystem): add copy_file tool#2012
Closed
nandsha wants to merge 3 commits intomodelcontextprotocol:mainfrom
Closed
feat(filesystem): add copy_file tool#2012nandsha wants to merge 3 commits intomodelcontextprotocol:mainfrom
nandsha wants to merge 3 commits intomodelcontextprotocol:mainfrom
Conversation
73e8965 to
2003e17
Compare
…ying Implements a new copy_file tool that addresses issue modelcontextprotocol#1581 by providing an efficient way to copy files and directories without requiring read/write operations for large files. ## Changes: - Add copy_file tool with support for both files and directories - Use fs.copyFile for files (optimized for large files) - Use fs.cp with recursive option for directories - Include proper validation and error handling - Update README documentation ## Implementation Details: - Validates both source and destination paths are within allowed directories - Checks if destination exists and fails appropriately - Follows existing tool patterns for consistency - Maintains security checks and path validation Fixes modelcontextprotocol#1581
2003e17 to
81cf93a
Compare
olaservo
requested changes
Jul 8, 2025
Member
olaservo
left a comment
There was a problem hiding this comment.
Thanks for the PR, left some comments. It could also make sense to group the copy tool code alongside the move_file tool.
| }; | ||
| } | ||
|
|
||
| case "copy_file": { |
Member
There was a problem hiding this comment.
This new tool needs to incorporate the same security fix that was added to the write_file tool, something like:
case "copy_file": {
const parsed = CopyFileArgsSchema.safeParse(args);
if (!parsed.success) {
throw new Error(`Invalid arguments for copy_file: ${parsed.error}`);
}
const validSourcePath = await validatePath(parsed.data.source);
const validDestPath = await validatePath(parsed.data.destination);
// Check if source exists and get stats
const sourceStats = await fs.stat(validSourcePath);
// Use atomic temp file + rename approach (like write_file)
const tempPath = `${validDestPath}.${randomBytes(16).toString('hex')}.tmp`;
try {
if (sourceStats.isDirectory()) {
// For directories, use fs.cp with options to prevent symlink following
await fs.cp(validSourcePath, tempPath, {
recursive: true,
dereference: false, // Don't follow symlinks
preserveTimestamps: true
});
} else {
// For files, copy to temp location first
await fs.copyFile(validSourcePath, tempPath);
}
// Atomic rename - this will fail if destination exists and won't follow symlinks
await fs.rename(tempPath, validDestPath);
} catch (error) {
// Cleanup temp file/directory
try {
const tempStats = await fs.stat(tempPath);
if (tempStats.isDirectory()) {
await fs.rm(tempPath, { recursive: true });
} else {
await fs.unlink(tempPath);
}
} catch {}
if ((error as NodeJS.ErrnoException).code === 'EEXIST') {
throw new Error(`Destination already exists: ${parsed.data.destination}`);
}
throw error;
}
return {
content: [{ type: "text", text: `Successfully copied ${parsed.data.source} to ${parsed.data.destination}` }],
};
}
The tldr; version of what needs to be addressed, based on the previous PR:
- Atomic Operation:
fs.rename()is atomic - either succeeds completely or fails completely - No Symlink Following:
fs.rename()doesn't follow symlinks at the destination - Fail-Safe: If destination exists (including symlinks),
fs.rename()fails withEEXIST - Race Condition Proof: No window between "check" and "action" - the action itself is the check
Member
|
Hey, thanks for the contribution! This hasn't been updated for a while, so I'm closing it to help us more easily track active contributions. If you're still interested in working on this, please feel free to reopen it. Thanks again for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Implements a new
copy_filetool in the filesystem server to enable efficient copying of files and directories without requiring read/write operations for large files.Server Details
Motivation and Context
Fixes #1581 - Currently, the agent has to read all the file content and write it to another location. For large files, this takes considerable time and memory. This implementation adds a dedicated copy tool that uses optimized native file operations.
How Has This Been Tested?
npm run buildBreaking Changes
No breaking changes. This is a new feature that adds functionality without modifying existing behavior.
Types of changes
Checklist
Additional context
Implementation Details:
fs.copyFilefor individual files (optimized for large files)fs.cpwith recursive option for directoriesKey Changes:
index.ts:
CopyFileArgsSchemafollowing existing schema patternscopy_filetool definition to the tools listcopy_filehandler with proper validation and error handlingREADME.md:
copy_filetool