Skip to main content
Glama

download_image

Download AI-generated images from Pollinations.ai to your local device by providing the image URL and specifying the output path. Simplify saving and accessing AI-created visuals.

Instructions

下载Pollinations.ai生成的图像到本地文件

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
output_pathNo保存图像的路径(包括文件名)image.jpg
urlYes要下载的图像URL

Implementation Reference

  • The primary handler function that validates arguments, downloads the image using axios, ensures the output directory exists, writes the file using fs, verifies the save, and returns success/error content blocks.
    private async handleDownloadImage(args: any) { try { if (!this.isValidDownloadImageArgs(args)) { throw this.handleValidationError('无效的图像下载参数'); } // 获取参数 let { url, output_path = 'image.jpg' } = args; // 使用相对路径保存图像到当前工作目录 output_path = path.resolve(process.cwd(), output_path); console.error(`图像将保存到: ${output_path}`); try { // 确保输出目录存在 const dirname = path.dirname(output_path); if (!fs.existsSync(dirname)) { console.error(`创建目录: ${dirname}`); fs.mkdirSync(dirname, { recursive: true }); } } catch (fsError) { throw this.handleFileSystemError(fsError, '创建目录'); } // 下载图像 console.error(`开始下载图像: ${url}`); let response; try { response = await axios.get(url, { responseType: 'arraybuffer' }); } catch (downloadError) { throw this.handleApiError(downloadError); } // 写入文件 try { console.error(`写入文件: ${output_path}`); fs.writeFileSync(output_path, Buffer.from(response.data, 'binary')); } catch (writeError) { throw this.handleFileSystemError(writeError, '写入文件'); } // 验证文件是否写入成功 try { if (fs.existsSync(output_path)) { const fileSize = fs.statSync(output_path).size; console.error(`文件成功写入: ${output_path}, 大小: ${fileSize} 字节`); return { content: [ { type: 'text', text: JSON.stringify({ success: true, message: `图像已下载到 ${output_path}`, size: fileSize, path: output_path }, null, 2), }, ], }; } else { throw new PollinationsError( `文件写入失败: ${output_path}`, PollinationsErrorType.FILE_SYSTEM_ERROR ); } } catch (verifyError) { if (verifyError instanceof PollinationsError) { throw verifyError; } else { throw this.handleFileSystemError(verifyError, '验证文件'); } } } catch (error) { // 处理所有错误 let pollinationsError: PollinationsError; if (error instanceof PollinationsError) { pollinationsError = error; } else { pollinationsError = this.handleApiError(error); } return { content: [ { type: 'text', text: pollinationsError.toUserFriendlyMessage(), }, ], isError: true, }; } }
  • Type guard function that validates the input arguments for the download_image tool, ensuring 'url' is string and 'output_path' is optional string.
    private isValidDownloadImageArgs(args: any): args is { url: string; output_path?: string; } { return ( typeof args === 'object' && args !== null && typeof args.url === 'string' && (args.output_path === undefined || typeof args.output_path === 'string') ); }
  • JSON Schema definition for the input parameters of the download_image tool as registered in the ListTools response.
    inputSchema: { type: 'object', properties: { url: { type: 'string', description: '要下载的图像URL', }, output_path: { type: 'string', description: '保存图像的路径(包括文件名)', default: 'image.jpg', }, }, required: ['url'], },
  • src/index.ts:208-226 (registration)
    Registration of the download_image tool in the ListToolsRequestSchema handler, including name, description, and input schema.
    { name: 'download_image', description: '下载Pollinations.ai生成的图像到本地文件', inputSchema: { type: 'object', properties: { url: { type: 'string', description: '要下载的图像URL', }, output_path: { type: 'string', description: '保存图像的路径(包括文件名)', default: 'image.jpg', }, }, required: ['url'], }, },
  • src/index.ts:272-273 (registration)
    Switch case in CallToolRequestSchema handler that routes download_image calls to the handleDownloadImage method.
    case 'download_image': return this.handleDownloadImage(request.params.arguments);

Other Tools

Related Tools

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/bendusy/pollinations-mcp'

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