Skip to main content
Glama

generate_image

Create custom images using AI models by providing a text prompt. Specify dimensions, model type, and quality enhancements to generate and download tailored visuals on demand.

Instructions

使用Pollinations.ai生成图像

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
enhanceNo提高图像质量(应用增强滤镜)
heightNo图像高度(像素)
modelNo要使用的模型(如flux、variation等)flux
nologoNo设置为true可去除水印
privateNo设置为true可使图像私有
promptYes图像描述提示词
safeNo启用安全过滤(过滤不适内容)
seedNo随机种子值(用于生成一致的图像)
widthNo图像宽度(像素)

Implementation Reference

  • The core handler function for the 'generate_image' tool. It validates input arguments, constructs the image generation URL for Pollinations.ai, performs a HEAD request to verify accessibility, provides prompt feedback, and returns the image URL and parameters or error message.
    private async handleGenerateImage(args: any) { try { if (!this.isValidGenerateImageArgs(args)) { throw this.handleValidationError('无效的图像生成参数'); } const { prompt, width = 1024, height = 1024, seed, model = 'flux', nologo = true, enhance = false, safe = false, private: isPrivate = false } = args; // 检查提示词是否为英文 const isMainlyEnglish = this.isMainlyEnglish(prompt); const isConcise = prompt.length <= 200; let promptFeedback = ''; if (!isMainlyEnglish) { promptFeedback += '提示:Pollinations.ai对英文提示词的理解更好,建议使用英文编写提示词。\n'; } if (!isConcise) { promptFeedback += '提示:提示词过长可能影响生成效果,建议保持简短精确(建议不超过200字符)。\n'; } // 构建Pollinations URL(使用官方路径格式) let imageUrl = `${this.baseUrl}/prompt/${encodeURIComponent(prompt)}?width=${width}&height=${height}`; if (seed !== undefined) { imageUrl += `&seed=${seed}`; } if (model) { imageUrl += `&model=${model}`; } if (nologo) { imageUrl += `&nologo=true`; } // 添加新参数支持 if (enhance) { imageUrl += `&enhance=true`; } if (safe) { imageUrl += `&safe=true`; } if (isPrivate) { imageUrl += `&private=true`; } // 验证URL是否有效 try { // 发送HEAD请求检查URL是否可访问(不下载完整图像) await axios.head(imageUrl); } catch (error) { // 处理API错误 const pollinationsError = this.handleApiError(error); // 特殊处理安全过滤错误 if (pollinationsError.statusCode === 400 && safe) { throw new PollinationsError( '内容被安全过滤拦截,请修改提示词后重试', PollinationsErrorType.VALIDATION_ERROR, 400 ); } throw pollinationsError; } const response = { content: [ { type: 'text', text: JSON.stringify({ url: imageUrl, prompt, width, height, seed, model, nologo, enhance, safe, private: isPrivate }, null, 2), }, ], }; // 如果有提示反馈,添加到响应中 if (promptFeedback) { response.content.unshift({ type: 'text', text: promptFeedback }); } return response; } 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, }; } }
  • Input schema for the generate_image tool as registered in MCP ListTools response, defining all parameters with types, descriptions, defaults, and required fields.
    inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: '图像描述提示词', }, width: { type: 'number', description: '图像宽度(像素)', default: 1024, }, height: { type: 'number', description: '图像高度(像素)', default: 1024, }, seed: { type: 'number', description: '随机种子值(用于生成一致的图像)', }, model: { type: 'string', description: '要使用的模型(如flux、variation等)', default: 'flux', }, nologo: { type: 'boolean', description: '设置为true可去除水印', default: true, }, enhance: { type: 'boolean', description: '提高图像质量(应用增强滤镜)', default: false, }, safe: { type: 'boolean', description: '启用安全过滤(过滤不适内容)', default: false, }, private: { type: 'boolean', description: '设置为true可使图像私有', default: false, }, }, required: ['prompt'], },
  • src/index.ts:155-207 (registration)
    The generate_image tool registration in the MCP ListToolsRequestSchema handler, including name, description, and input schema.
    { name: 'generate_image', description: '使用Pollinations.ai生成图像', inputSchema: { type: 'object', properties: { prompt: { type: 'string', description: '图像描述提示词', }, width: { type: 'number', description: '图像宽度(像素)', default: 1024, }, height: { type: 'number', description: '图像高度(像素)', default: 1024, }, seed: { type: 'number', description: '随机种子值(用于生成一致的图像)', }, model: { type: 'string', description: '要使用的模型(如flux、variation等)', default: 'flux', }, nologo: { type: 'boolean', description: '设置为true可去除水印', default: true, }, enhance: { type: 'boolean', description: '提高图像质量(应用增强滤镜)', default: false, }, safe: { type: 'boolean', description: '启用安全过滤(过滤不适内容)', default: false, }, private: { type: 'boolean', description: '设置为true可使图像私有', default: false, }, }, required: ['prompt'], }, },
  • Type guard function for validating generate_image input arguments matching the schema.
    private isValidGenerateImageArgs(args: any): args is { prompt: string; width?: number; height?: number; seed?: number; model?: string; nologo?: boolean; enhance?: boolean; safe?: boolean; private?: boolean; } { return ( typeof args === 'object' && args !== null && typeof args.prompt === 'string' && (args.width === undefined || typeof args.width === 'number') && (args.height === undefined || typeof args.height === 'number') && (args.seed === undefined || typeof args.seed === 'number') && (args.model === undefined || typeof args.model === 'string') && (args.nologo === undefined || typeof args.nologo === 'boolean') && (args.enhance === undefined || typeof args.enhance === 'boolean') && (args.safe === undefined || typeof args.safe === 'boolean') && (args.private === undefined || typeof args.private === 'boolean') ); }
  • src/index.ts:268-282 (registration)
    Tool dispatch registration in CallToolRequestSchema handler, routing 'generate_image' calls to the handleGenerateImage method.
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => { switch (request.params.name) { case 'generate_image': return this.handleGenerateImage(request.params.arguments); case 'download_image': return this.handleDownloadImage(request.params.arguments); case 'generate_text': return this.handleGenerateText(request.params.arguments); default: throw new McpError( ErrorCode.MethodNotFound, `未知工具: ${request.params.name}` ); } });

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