generate_image
Convert text prompts into high-quality images using the Flux Schnell model, powered by the mcp-flux-schnell MCP server for streamlined text-to-image generation.
Instructions
Generate an image from a text prompt using Flux Schnell model
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | A text description of the image you want to generate. |
Implementation Reference
- src/index.ts:79-186 (handler)Handler for the 'generate_image' tool: validates inputs, checks env vars, calls Flux Schnell API via fetch, saves base64 image to file, returns file path.case "generate_image": { // Validate environment variables if (!FLUX_API_URL) { return { content: [ { type: "text", text: "Configuration Error: FLUX_API_URL environment variable is not set", }, ], }; } if (!FLUX_API_TOKEN) { return { content: [ { type: "text", text: "Configuration Error: FLUX_API_TOKEN environment variable is not set", }, ], }; } // Validate input parameters const validationResult = generateImageSchema.safeParse( request.params.arguments ); if (!validationResult.success) { return { content: [ { type: "text", text: `Input Error: ${validationResult.error.message}`, }, ], }; } const { prompt } = validationResult.data; const timestamp = new Date().getTime(); const filename = `flux-${timestamp}.png`; const directory = WORKING_DIR; const filepath = join(directory, filename); // Check if directory exists and create it if it doesn't if (!existsSync(directory)) { try { await mkdir(directory, { recursive: true }); } catch (error) { return { content: [ { type: "text", text: `Directory Error: Failed to create directory ${directory}: ${error instanceof Error ? error.message : 'Unknown error'}`, }, ], }; } } try { // Call Flux Schnell API const response = await fetch(FLUX_API_URL, { method: "POST", headers: { Authorization: `Bearer ${FLUX_API_TOKEN}`, "Content-Type": "application/json", }, body: JSON.stringify({ prompt }), }); if (!response.ok) { const errorText = await response.text(); return { content: [ { type: "text", text: `API Error: Flux API returned status ${response.status}: ${errorText}`, }, ], }; } const result = await response.json(); const base64Data = result.image.replace(/^data:image\/png;base64,/, ""); await writeFile(filepath, Buffer.from(base64Data, "base64")); return { content: [ { type: "text", text: `Image saved successfully:\nFilename: ${filename}\nPath: ${filepath}`, }, ], }; } catch (error: unknown) { const errorMessage = error instanceof Error ? error.message : "An unknown error occurred"; return { content: [ { type: "text", text: `Operation Error: Failed to generate or save image: ${errorMessage}`, }, ], }; } }
- src/index.ts:25-27 (schema)Zod schema for validating 'generate_image' tool input: requires a prompt string between 1-2048 chars.const generateImageSchema = z.object({ prompt: z.string().min(1).max(2048), });
- src/index.ts:49-70 (registration)Registration of 'generate_image' tool in ListToolsRequestSchema handler, including name, description, and inputSchema.return { tools: [ { name: "generate_image", description: "Generate an image from a text prompt using Flux Schnell model", inputSchema: { type: "object", properties: { prompt: { type: "string", minLength: 1, maxLength: 2048, description: "A text description of the image you want to generate.", }, }, required: ["prompt"], }, }, ], };