generate_image
Generate custom images from text descriptions using AI models, with options to control dimensions, style, privacy, and quality settings.
Instructions
使用Pollinations.ai生成图像
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | 图像描述提示词 | |
| width | No | 图像宽度(像素) | |
| height | No | 图像高度(像素) | |
| seed | No | 随机种子值(用于生成一致的图像) | |
| model | No | 要使用的模型(如flux、variation等) | flux |
| nologo | No | 设置为true可去除水印 | |
| enhance | No | 提高图像质量(应用增强滤镜) | |
| safe | No | 启用安全过滤(过滤不适内容) | |
| private | No | 设置为true可使图像私有 |
Implementation Reference
- src/index.ts:286-415 (handler)The primary handler for the 'generate_image' tool. Validates arguments, constructs the Pollinations.ai image generation URL with parameters like prompt, dimensions, model, etc., performs a HEAD request to verify accessibility, and returns the URL along with parameters or error messages.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, }; } }
- src/index.ts:532-556 (schema)Runtime validation function for 'generate_image' input arguments, matching the tool's inputSchema type definitions.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:156-207 (registration)Tool registration in the ListToolsRequestSchema handler, including name, description, and detailed inputSchema for 'generate_image'.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'], }, },
- src/index.ts:268-282 (registration)Registration of the callTool handler dispatch for 'generate_image' via switch case calling handleGenerateImage.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}` ); } });
- src/index.ts:418-433 (helper)Helper function to check if prompt is mainly English, used to provide feedback in the handler.private isMainlyEnglish(text: string): boolean { // 英文字符(包括空格和标点)的正则表达式 const englishRegex = /^[A-Za-z0-9\s.,;:'"!?()-]+$/; // 如果完全匹配英文字符,返回true if (englishRegex.test(text)) { return true; } // 否则计算非英文字符的比例 const nonEnglishChars = text.split('').filter(char => !char.match(/[A-Za-z0-9\s.,;:'"!?()-]/)).length; const totalChars = text.length; // 如果非英文字符少于20%,仍然视为主要是英文 return (nonEnglishChars / totalChars) < 0.2; }