download-file
Retrieve files from FTP servers by specifying the remote file path. This tool enables downloading documents, media, or data stored on FTP servers for local access and use.
Instructions
Download a file from the FTP server
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| remotePath | Yes | Path of the file on the FTP server |
Implementation Reference
- src/index.ts:72-95 (handler)The inline handler function for the 'download-file' tool. It calls ftpClient.downloadFile(remotePath), extracts the content, and returns it as a text response or an error message.async ({ remotePath }) => { try { const { content } = await ftpClient.downloadFile(remotePath); return { content: [ { type: "text", text: `File content of ${remotePath}:\n\n${content}` } ] }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error downloading file: ${error instanceof Error ? error.message : String(error)}` } ] }; } }
- src/index.ts:69-71 (schema)Zod input schema defining the 'remotePath' parameter as a string.{ remotePath: z.string().describe("Path of the file on the FTP server"), },
- src/index.ts:65-96 (registration)Registers the 'download-file' tool with the MCP server, providing name, description, schema, and handler.// Register download-file tool server.tool( "download-file", "Download a file from the FTP server", { remotePath: z.string().describe("Path of the file on the FTP server"), }, async ({ remotePath }) => { try { const { content } = await ftpClient.downloadFile(remotePath); return { content: [ { type: "text", text: `File content of ${remotePath}:\n\n${content}` } ] }; } catch (error) { return { isError: true, content: [ { type: "text", text: `Error downloading file: ${error instanceof Error ? error.message : String(error)}` } ] }; } } );
- src/ftp-client.ts:72-95 (helper)FtpClient.downloadFile method: connects to FTP, downloads file to temp location using basic-ftp, reads content as UTF-8 string, disconnects, returns path and content.async downloadFile(remotePath: string): Promise<{filePath: string, content: string}> { try { await this.connect(); // Create a unique local filename const tempFilePath = path.join(this.tempDir, `download-${Date.now()}-${path.basename(remotePath)}`); // Download the file await this.client.downloadTo(tempFilePath, remotePath); // Read the file content const content = fs.readFileSync(tempFilePath, 'utf8'); await this.disconnect(); return { filePath: tempFilePath, content }; } catch (error) { console.error("Download file error:", error); throw new Error(`Failed to download file: ${error instanceof Error ? error.message : String(error)}`); } }