generate_image
Create images from text descriptions using Stable Diffusion through the Draw Things app, saving generated files to disk with customizable parameters for control.
Instructions
Generate an image from a text prompt using the Draw Things app. The image will be saved to disk and the file path returned.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | Text description of the image to generate | |
| negative_prompt | No | Elements to exclude from the generated image | |
| width | No | Width of the generated image in pixels (default: 512) | |
| height | No | Height of the generated image in pixels (default: 512) | |
| steps | No | Number of inference steps (default: 20) | |
| cfg_scale | No | Classifier-free guidance scale (default: 7.5) | |
| seed | No | Random seed for reproducibility (-1 for random) | |
| model | No | Model filename to use for generation (use list_models to see available models) | |
| output_path | No | Custom file path to save the generated image |
Implementation Reference
- src/tools/generate-image.ts:55-134 (handler)The main handler function that executes the generate_image tool logic, calling the DrawThingsClient to generate and save images based on parameters.export async function generateImage( client: DrawThingsClient, params: z.infer<typeof generateImageSchema> ): Promise<{ type: "text"; text: string }[]> { try { // Check if server is running first const status = await client.checkStatus(); if (!status.running) { return [ { type: "text", text: `Error: ${status.message}`, }, ]; } // Generate the image const response = await client.txt2img({ prompt: params.prompt, negative_prompt: params.negative_prompt, width: params.width, height: params.height, steps: params.steps, cfg_scale: params.cfg_scale, seed: params.seed, model: params.model, }); if (!response.images || response.images.length === 0) { return [ { type: "text", text: "Error: No images were generated", }, ]; } // Save the image(s) const savedPaths: string[] = []; for (let i = 0; i < response.images.length; i++) { const outputPath = params.output_path && response.images.length === 1 ? params.output_path : undefined; const path = await client.saveImage(response.images[i], outputPath); savedPaths.push(path); } return [ { type: "text", text: JSON.stringify( { success: true, message: `Generated ${savedPaths.length} image(s)`, files: savedPaths, prompt: params.prompt, parameters: { width: params.width || 512, height: params.height || 512, steps: params.steps || 20, cfg_scale: params.cfg_scale || 7.5, seed: params.seed ?? -1, }, }, null, 2 ), }, ]; } catch (error) { const message = error instanceof Error ? error.message : String(error); return [ { type: "text", text: `Error generating image: ${message}`, }, ]; } }
- src/tools/generate-image.ts:4-50 (schema)Zod schema defining the input parameters and validation for the generate_image tool.export const generateImageSchema = z.object({ prompt: z.string().describe("Text description of the image to generate"), negative_prompt: z .string() .optional() .describe("Elements to exclude from the generated image"), width: z .number() .int() .min(64) .max(2048) .optional() .describe("Width of the generated image in pixels (default: 512)"), height: z .number() .int() .min(64) .max(2048) .optional() .describe("Height of the generated image in pixels (default: 512)"), steps: z .number() .int() .min(1) .max(150) .optional() .describe("Number of inference steps (default: 20)"), cfg_scale: z .number() .min(1) .max(30) .optional() .describe("Classifier-free guidance scale (default: 7.5)"), seed: z .number() .int() .optional() .describe("Random seed for reproducibility (-1 for random)"), model: z .string() .optional() .describe("Model filename to use for generation (use list_models to see available models)"), output_path: z .string() .optional() .describe("Custom file path to save the generated image"), });
- src/index.ts:57-65 (registration)Registration of the generate_image tool on the MCP server, linking the name, description, schema, and handler.server.tool( "generate_image", generateImageDescription, generateImageSchema.shape, async (params) => { const result = await generateImage(client, params); return { content: result }; } );