ssh_download_file
Download files from a remote server via SSH by specifying the connection ID, remote file path, and local save location.
Instructions
Download a file from the remote server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| connectionId | Yes | ID of an active SSH connection | |
| localPath | Yes | Path where the file should be saved locally | |
| remotePath | Yes | Path to the file on the remote server |
Implementation Reference
- src/index.ts:509-562 (handler)The main handler function that downloads a file from the remote SSH server to local using SFTP.private async handleSSHDownload(params: any) { const { connectionId, remotePath, localPath } = params; // Check if the connection exists if (!this.connections.has(connectionId)) { return { content: [{ type: "text", text: `No active SSH connection with ID: ${connectionId}` }], isError: true }; } const { conn } = this.connections.get(connectionId)!; try { // Expand tilde if present in the local path const expandedLocalPath = localPath.replace(/^~/, os.homedir()); // Ensure the directory exists const dir = path.dirname(expandedLocalPath); if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } // Get SFTP client const sftp: any = await new Promise((resolve, reject) => { conn.sftp((err: Error | undefined, sftp: any) => { if (err) { reject(new Error(`Failed to initialize SFTP: ${err.message}`)); } else { resolve(sftp); } }); }); // Download the file await new Promise((resolve, reject) => { sftp.fastGet(remotePath, expandedLocalPath, (err: Error | undefined) => { if (err) { reject(new Error(`Failed to download file: ${err.message}`)); } else { resolve(true); } }); }); return { content: [{ type: "text", text: `Successfully downloaded ${remotePath} to ${expandedLocalPath}` }] }; } catch (error: any) { return { content: [{ type: "text", text: `File download failed: ${error.message}` }], isError: true }; }
- src/index.ts:120-140 (schema)Input schema definition for the ssh_download_file tool in server capabilities.ssh_download_file: { description: "Download a file from the remote server", inputSchema: { type: "object", properties: { connectionId: { type: "string", description: "ID of an active SSH connection" }, remotePath: { type: "string", description: "Path to the file on the remote server" }, localPath: { type: "string", description: "Path where the file should be saved locally" } }, required: ["connectionId", "remotePath", "localPath"] } },
- src/index.ts:231-241 (schema)Input schema for ssh_download_file in the ListTools response handler.name: 'ssh_download_file', description: 'Download a file from the remote server', inputSchema: { type: 'object', properties: { connectionId: { type: 'string', description: 'ID of an active SSH connection' }, remotePath: { type: 'string', description: 'Path to the file on the remote server' }, localPath: { type: 'string', description: 'Path where the file should be saved locally' } }, required: ['connectionId', 'remotePath', 'localPath'] }
- src/index.ts:282-283 (registration)Registration of the ssh_download_file handler in the tool call switch statement.case 'ssh_download_file': return this.handleSSHDownload(request.params.arguments);