download_image
Download images from a URL and save them to a specified path using the MCP server's image downloader functionality.
Instructions
Download an image from a URL to a specified path
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| outputPath | Yes | Path where to save the image | |
| url | Yes | URL of the image to download |
Implementation Reference
- src/index.ts:151-191 (handler)The main handler function that downloads the image from the given URL using axios, ensures the output directory exists, saves the image to the specified path, and returns success or error content.private async handleDownloadImage(args: DownloadImageArgs) { try { // Ensure output directory exists await fs.ensureDir(path.dirname(args.outputPath)); // Download image const response = await axios({ method: 'GET', url: args.url, responseType: 'arraybuffer', headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' }, timeout: 30000, }); // Save image await fs.writeFile(args.outputPath, response.data); return { content: [ { type: 'text', text: `Successfully downloaded image to ${args.outputPath}`, }, ], }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; console.error('Download error:', errorMessage); return { content: [ { type: 'text', text: `Failed to download image: ${errorMessage}`, }, ], isError: true, }; } }
- src/index.ts:15-18 (schema)TypeScript interface defining the input arguments for the download_image tool: url (string) and outputPath (string).interface DownloadImageArgs { url: string; outputPath: string; }
- src/index.ts:77-94 (registration)Tool registration in the listTools response, including name, description, and JSON input schema.{ name: 'download_image', description: 'Download an image from a URL to a specified path', inputSchema: { type: 'object', properties: { url: { type: 'string', description: 'URL of the image to download', }, outputPath: { type: 'string', description: 'Path where to save the image', }, }, required: ['url', 'outputPath'], }, },
- src/index.ts:53-60 (helper)Type guard function to validate if arguments match DownloadImageArgs structure.private isDownloadImageArgs(args: unknown): args is DownloadImageArgs { if (!args || typeof args !== 'object') return false; const a = args as Record<string, unknown>; return ( typeof a.url === 'string' && typeof a.outputPath === 'string' ); }
- src/index.ts:132-136 (registration)Dispatch case in CallToolRequestHandler that validates arguments and calls the download_image handler.case 'download_image': if (!this.isDownloadImageArgs(request.params.arguments)) { throw new McpError(ErrorCode.InvalidParams, 'Invalid arguments for download_image'); } return this.handleDownloadImage(request.params.arguments);