Skip to main content
Glama

download_file

Download files from any URL to a specified folder, enabling local storage of web content and resources.

Instructions

Download a file from a given URL to a specified folder.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
urlYesThe URL of the file to download.
destinationFolderYesThe destination folder (relative to the workspace directory) to save the file.

Implementation Reference

  • The core handler function that downloads the file from the given URL, ensures the destination folder exists, streams the content using fetch and pipeline, and returns the saved file path.
    private async downloadFile(url: string, destinationFolder: string): Promise<string> { const logsDir = path.join(process.cwd(), 'logs'); try { // Ensure the destination folder exists const fullDestinationPath = path.resolve(process.cwd(), destinationFolder); if (!fs.existsSync(fullDestinationPath)) { fs.mkdirSync(fullDestinationPath, { recursive: true }); } const response = await fetch(url); if (!response.ok) { throw new Error(`Failed to download file: ${response.statusText}`); } const filename = path.basename(new URL(url).pathname) || 'downloaded_file'; const filePath = path.join(fullDestinationPath, filename); const fileStream = fs.createWriteStream(filePath); if (response.body) { // Convert Web ReadableStream to Node.js Readable stream // Convert Web ReadableStream to Node.js Readable stream // For Node.js 18+, response.body is a ReadableStream (Web Streams API) // For pipeline, it needs to be a Node.js Readable stream. // A common way to bridge this is to convert the Web ReadableStream to an AsyncIterable // and then use Readable.from. However, directly piping is often possible. // If response.body is indeed a Web ReadableStream, it can often be directly piped. // If not, it needs to be converted. // Assuming response.body is a WHATWG ReadableStream, which is generally compatible with Node.js streams. await pipeline(Readable.fromWeb(response.body as any), fileStream); // Use Readable.fromWeb for conversion } else { throw new Error('Response body is null.'); } return filePath; // Return the path to the downloaded file } catch (error: any) { console.error('Error downloading file:', error); throw new Error(`Failed to download file: ${error.message}`); } }
  • src/index.ts:252-270 (registration)
    Registers the 'download_file' tool in the MCP ListTools handler, specifying its description and input schema for validation.
    { name: 'download_file', description: 'Download a file from a given URL to a specified folder.', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'The URL of the file to download.' }, destinationFolder: { type: 'string', description: 'The destination folder (relative to the workspace directory) to save the file.' } }, required: ['url', 'destinationFolder'], additionalProperties: false, }, },
  • TypeScript interface defining the structure of arguments expected by the download_file tool.
    interface DownloadFileArgs { url: string; destinationFolder: string; }
  • Helper function (type guard) to validate that tool arguments conform to DownloadFileArgs interface.
    const isValidDownloadFileArgs = (args: any): args is DownloadFileArgs => { if ( typeof args === 'object' && args !== null && typeof args.url === 'string' && typeof args.destinationFolder === 'string' ) { // Accept relative paths; resolution is handled in downloadFile (resolved against process.cwd()) return true; } return false; };
  • Dispatch handler in CallToolRequestSchema that validates arguments, invokes the downloadFile method, and returns success/error response in MCP format.
    } else if (toolName === 'download_file') { if (!isValidDownloadFileArgs(args)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid download_file arguments'); } const validatedArgs = args as DownloadFileArgs; try { const filePath = await this.downloadFile(validatedArgs.url, validatedArgs.destinationFolder); return { content: [{ type: 'text', text: `File downloaded successfully to: ${filePath}` }], }; } catch (error: any) { console.error('Error calling download_file:', error); return { content: [{ type: 'text', text: `Error downloading file: ${error.message}` }], isError: true, }; }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/rayss868/MCP-Web-Curl'

If you have feedback or need assistance with the MCP directory API, please join our Discord server