ssh_download_file
Download a file from a remote server to a local path via SFTP, with optional directory creation and connection selection.
Instructions
Download a file from the remote server via SFTP
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| remotePath | Yes | Remote file path to download | |
| localPath | Yes | Local destination path | |
| connectionId | No | Connection ID to use | default |
| createDirs | No | Create local directories if they don't exist |
Implementation Reference
- index.js:717-774 (handler)The handleSSHDownloadFile method that executes the ssh_download_file tool logic. It retrieves an SSH connection, opens an SFTP session, reads a remote file via createReadStream, and writes it to the local filesystem.
async handleSSHDownloadFile(args) { const { remotePath, localPath, connectionId = 'default', createDirs = true } = args; const conn = this.connections.get(connectionId); if (!conn) { throw new Error(`No active connection found for ID: ${connectionId}`); } const absoluteLocalPath = resolve(localPath); return new Promise((resolve, reject) => { conn.sftp((err, sftp) => { if (err) { return reject(new Error(`SFTP error: ${err.message}`)); } const downloadFile = () => { const readStream = sftp.createReadStream(remotePath); let fileContent = Buffer.alloc(0); readStream.on('data', (chunk) => { fileContent = Buffer.concat([fileContent, chunk]); }); readStream.on('end', () => { try { writeFileSync(absoluteLocalPath, fileContent); resolve({ content: [ { type: 'text', text: `Successfully downloaded ${remotePath} to ${absoluteLocalPath}`, }, ], }); } catch (error) { reject(new Error(`Failed to write local file: ${error.message}`)); } }); readStream.on('error', (err) => { reject(new Error(`Download failed: ${err.message}`)); }); }; if (createDirs) { const localDir = dirname(absoluteLocalPath); try { mkdirSync(localDir, { recursive: true }); } catch (error) { // Ignore mkdir errors if directory already exists } } downloadFile(); }); }); } - index.js:226-252 (schema)The input schema definition for ssh_download_file, specifying remotePath (string, required), localPath (string, required), connectionId (string, default 'default'), and createDirs (boolean, default true).
{ name: 'ssh_download_file', description: 'Download a file from the remote server via SFTP', inputSchema: { type: 'object', properties: { remotePath: { type: 'string', description: 'Remote file path to download', }, localPath: { type: 'string', description: 'Local destination path', }, connectionId: { type: 'string', description: 'Connection ID to use', default: 'default', }, createDirs: { type: 'boolean', description: 'Create local directories if they don\'t exist', default: true, }, }, required: ['remotePath', 'localPath'], }, - index.js:37-39 (registration)The tool is registered in setupToolHandlers() via ListToolsRequestSchema handler and in the CallToolRequestSchema switch-case at lines 300-301.
setupToolHandlers() { this.server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [