|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module RubyLLM |
| 4 | + module Docker |
| 5 | + # MCP tool for copying files and directories from host to Docker containers. |
| 6 | + # |
| 7 | + # This tool provides the ability to copy files or entire directory trees from |
| 8 | + # the host filesystem into running Docker containers. It uses Docker's archive |
| 9 | + # streaming API to efficiently transfer files while preserving permissions and |
| 10 | + # directory structure. |
| 11 | + # |
| 12 | + # == ⚠️ SECURITY WARNING ⚠️ |
| 13 | + # |
| 14 | + # This tool can be dangerous as it allows: |
| 15 | + # - Reading arbitrary files from the host filesystem |
| 16 | + # - Writing files into container filesystems |
| 17 | + # - Potentially overwriting critical container files |
| 18 | + # - Escalating privileges if used with setuid/setgid files |
| 19 | + # - Exposing sensitive host data to containers |
| 20 | + # |
| 21 | + # Security recommendations: |
| 22 | + # - Validate source paths to prevent directory traversal |
| 23 | + # - Ensure containers run with minimal privileges |
| 24 | + # - Monitor file copy operations for sensitive paths |
| 25 | + # - Use read-only filesystems where possible |
| 26 | + # - Implement proper access controls on source files |
| 27 | + # |
| 28 | + # == Features |
| 29 | + # |
| 30 | + # - Copy individual files or entire directories |
| 31 | + # - Preserve file permissions and directory structure |
| 32 | + # - Optional ownership changes after copy |
| 33 | + # - Comprehensive error handling |
| 34 | + # - Support for both absolute and relative paths |
| 35 | + # |
| 36 | + # == Example Usage |
| 37 | + # |
| 38 | + # # Copy a configuration file |
| 39 | + # CopyToContainer.call( |
| 40 | + # server_context: context, |
| 41 | + # id: "web-server", |
| 42 | + # source_path: "/host/config/nginx.conf", |
| 43 | + # destination_path: "/etc/nginx/" |
| 44 | + # ) |
| 45 | + # |
| 46 | + # # Copy directory with ownership change |
| 47 | + # CopyToContainer.call( |
| 48 | + # server_context: context, |
| 49 | + # id: "app-container", |
| 50 | + # source_path: "/host/app/src", |
| 51 | + # destination_path: "/app/", |
| 52 | + # owner: "appuser:appgroup" |
| 53 | + # ) |
| 54 | + # |
| 55 | + # @see Docker::Container#archive_in_stream |
| 56 | + # @since 0.1.0 |
| 57 | + class CopyToContainer < RubyLLM::Tool |
| 58 | + description 'Copy a file or directory from the local filesystem into a running Docker container. ' \ |
| 59 | + 'The source path is on the local machine, and the destination path is inside the container.' |
| 60 | + |
| 61 | + input_schema( |
| 62 | + properties: { |
| 63 | + id: { |
| 64 | + type: 'string', |
| 65 | + description: 'Container ID or name' |
| 66 | + }, |
| 67 | + source_path: { |
| 68 | + type: 'string', |
| 69 | + description: 'Path to the file or directory on the local filesystem to copy' |
| 70 | + }, |
| 71 | + destination_path: { |
| 72 | + type: 'string', |
| 73 | + description: 'Path inside the container where the file/directory should be copied' |
| 74 | + }, |
| 75 | + owner: { |
| 76 | + type: 'string', |
| 77 | + description: 'Owner for the copied files (optional, e.g., "1000:1000" or "username:group")' |
| 78 | + } |
| 79 | + }, |
| 80 | + required: %w[id source_path destination_path] |
| 81 | + ) |
| 82 | + |
| 83 | + # Copy files or directories from host filesystem to a Docker container. |
| 84 | + # |
| 85 | + # This method creates a tar archive of the source path and streams it into |
| 86 | + # the specified container using Docker's archive API. The operation preserves |
| 87 | + # file permissions and directory structure. Optionally, ownership can be |
| 88 | + # changed after the copy operation completes. |
| 89 | + # |
| 90 | + # The source path must exist on the host filesystem and be readable by the |
| 91 | + # process running the MCP server. The destination path must be a valid path |
| 92 | + # within the container. |
| 93 | + # |
| 94 | + # @param id [String] container ID or name to copy files into |
| 95 | + # @param source_path [String] path to file/directory on host filesystem |
| 96 | + # @param destination_path [String] destination path inside container |
| 97 | + # @param server_context [Object] MCP server context (unused but required) |
| 98 | + # @param owner [String, nil] ownership specification (e.g., "user:group", "1000:1000") |
| 99 | + # |
| 100 | + # @return [RubyLLM::Tool::Response] success/failure message with operation details |
| 101 | + # |
| 102 | + # @raise [Docker::Error::NotFoundError] if container doesn't exist |
| 103 | + # @raise [StandardError] for file system or Docker API errors |
| 104 | + # |
| 105 | + # @example Copy configuration file |
| 106 | + # response = CopyToContainer.call( |
| 107 | + # server_context: context, |
| 108 | + # id: "nginx-container", |
| 109 | + # source_path: "/etc/nginx/sites-available/default", |
| 110 | + # destination_path: "/etc/nginx/sites-enabled/" |
| 111 | + # ) |
| 112 | + # |
| 113 | + # @example Copy directory with ownership |
| 114 | + # response = CopyToContainer.call( |
| 115 | + # server_context: context, |
| 116 | + # id: "app-container", |
| 117 | + # source_path: "/local/project", |
| 118 | + # destination_path: "/app/", |
| 119 | + # owner: "www-data:www-data" |
| 120 | + # ) |
| 121 | + # |
| 122 | + # @see Docker::Container#archive_in_stream |
| 123 | + # @see #add_to_tar |
| 124 | + def self.call(id:, source_path:, destination_path:, server_context:, owner: nil) |
| 125 | + container = Docker::Container.get(id) |
| 126 | + |
| 127 | + # Verify source path exists |
| 128 | + unless File.exist?(source_path) |
| 129 | + return RubyLLM::Tool::Response.new([{ |
| 130 | + type: 'text', |
| 131 | + text: "Source path not found: #{source_path}" |
| 132 | + }]) |
| 133 | + end |
| 134 | + |
| 135 | + # Create a tar archive of the source |
| 136 | + tar_io = StringIO.new |
| 137 | + tar_io.set_encoding('ASCII-8BIT') |
| 138 | + |
| 139 | + Gem::Package::TarWriter.new(tar_io) do |tar| |
| 140 | + add_to_tar(tar, source_path, File.basename(source_path)) |
| 141 | + end |
| 142 | + |
| 143 | + tar_io.rewind |
| 144 | + |
| 145 | + # Copy to container |
| 146 | + container.archive_in_stream(destination_path) do |
| 147 | + tar_io.read |
| 148 | + end |
| 149 | + |
| 150 | + # Optionally change ownership |
| 151 | + if owner |
| 152 | + chown_path = File.join(destination_path, File.basename(source_path)) |
| 153 | + container.exec(['chown', '-R', owner, chown_path]) |
| 154 | + end |
| 155 | + |
| 156 | + file_type = File.directory?(source_path) ? 'directory' : 'file' |
| 157 | + response_text = "Successfully copied #{file_type} from #{source_path} to #{id}:#{destination_path}" |
| 158 | + response_text += "\nOwnership changed to #{owner}" if owner |
| 159 | + |
| 160 | + RubyLLM::Tool::Response.new([{ |
| 161 | + type: 'text', |
| 162 | + text: response_text |
| 163 | + }]) |
| 164 | + rescue Docker::Error::NotFoundError |
| 165 | + RubyLLM::Tool::Response.new([{ |
| 166 | + type: 'text', |
| 167 | + text: "Container #{id} not found" |
| 168 | + }]) |
| 169 | + rescue StandardError => e |
| 170 | + RubyLLM::Tool::Response.new([{ |
| 171 | + type: 'text', |
| 172 | + text: "Error copying to container: #{e.message}" |
| 173 | + }]) |
| 174 | + end |
| 175 | + |
| 176 | + # Recursively add files and directories to a tar archive. |
| 177 | + # |
| 178 | + # This helper method builds a tar archive by recursively traversing |
| 179 | + # the filesystem starting from the given path. It preserves file |
| 180 | + # permissions and handles both files and directories appropriately. |
| 181 | + # |
| 182 | + # For directories, it creates directory entries in the tar and then |
| 183 | + # recursively processes all contained files and subdirectories. |
| 184 | + # For files, it reads the content and adds it to the tar with |
| 185 | + # preserved permissions. |
| 186 | + # |
| 187 | + # @param tar [Gem::Package::TarWriter] the tar writer instance |
| 188 | + # @param path [String] the filesystem path to add to the archive |
| 189 | + # @param archive_path [String] the path within the tar archive |
| 190 | + # |
| 191 | + # @return [void] |
| 192 | + # |
| 193 | + # @example Add single file |
| 194 | + # add_to_tar(tar_writer, "/host/file.txt", "file.txt") |
| 195 | + # |
| 196 | + # @example Add directory tree |
| 197 | + # add_to_tar(tar_writer, "/host/mydir", "mydir") |
| 198 | + # |
| 199 | + # @see Gem::Package::TarWriter#mkdir |
| 200 | + # @see Gem::Package::TarWriter#add_file_simple |
| 201 | + def self.add_to_tar(tar, path, archive_path) |
| 202 | + if File.directory?(path) |
| 203 | + # Add directory entry |
| 204 | + tar.mkdir(archive_path, File.stat(path).mode) |
| 205 | + |
| 206 | + # Add directory contents |
| 207 | + Dir.entries(path).each do |entry| |
| 208 | + next if ['.', '..'].include?(entry) |
| 209 | + |
| 210 | + full_path = File.join(path, entry) |
| 211 | + archive_entry_path = File.join(archive_path, entry) |
| 212 | + add_to_tar(tar, full_path, archive_entry_path) |
| 213 | + end |
| 214 | + else |
| 215 | + # Add file |
| 216 | + File.open(path, 'rb') do |file| |
| 217 | + tar.add_file_simple(archive_path, File.stat(path).mode, file.size) do |tar_file| |
| 218 | + IO.copy_stream(file, tar_file) |
| 219 | + end |
| 220 | + end |
| 221 | + end |
| 222 | + end |
| 223 | + end |
| 224 | + end |
| 225 | +end |
0 commit comments