-
Notifications
You must be signed in to change notification settings - Fork 9.1k
feat(filesystem): add copy_file tool #2012
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
feat(filesystem): add copy_file tool #2012
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
left a comment
There was a problem hiding this 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, left some comments. It could also make sense to group the copy tool code alongside the move_file tool.
| }; | ||
| } | ||
|
|
||
| case "copy_file": { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
|
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! |
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