generate_image
Generate images from text prompts using ComfyUI's default txt2img workflow. Control output with prompt, negative prompt, dimensions, steps, CFG scale, seed, and checkpoint.
Instructions
Generate an image from a text prompt using ComfyUI's default txt2img workflow. Returns one or more image URLs served directly by the ComfyUI instance.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| prompt | Yes | Text prompt describing the image to generate | |
| negative_prompt | No | What to avoid in the image | |
| width | No | Image width in pixels | |
| height | No | Image height in pixels | |
| steps | No | Number of diffusion steps | |
| cfg | No | CFG / prompt adherence (1-30) | |
| seed | No | Seed for reproducibility | |
| checkpoint | No | Checkpoint filename (defaults to COMFYUI_DEFAULT_CKPT) |
Implementation Reference
- src/tools/generate.ts:84-101 (handler)The async handler function that executes the 'generate_image' tool logic. Calls client.generate() with prompt, negative_prompt, width, height, steps, cfg, seed, and checkpoint, then returns image URLs.
async (args) => { const result = await client.generate({ prompt: args.prompt, negativePrompt: args.negative_prompt, width: args.width, height: args.height, steps: args.steps, cfg: args.cfg, seed: args.seed, checkpoint: args.checkpoint, }); return textResult( `Generated ${result.images.length} image(s) (prompt_id: ${result.promptId}):`, result.images, ); }, ); - src/tools/generate.ts:6-44 (schema)Zod schema defining input parameters for the 'generate_image' tool: prompt (string), negative_prompt (optional string), width (64-2048, default 1024), height (64-2048, default 1024), steps (1-150, default 25), cfg (1-30, default 7), seed (optional int), checkpoint (optional string).
const generateImageSchema = { prompt: z .string() .min(1) .describe("Text prompt describing the image to generate"), negative_prompt: z.string().optional().describe("What to avoid in the image"), width: z .number() .int() .min(64) .max(2048) .default(1024) .describe("Image width in pixels"), height: z .number() .int() .min(64) .max(2048) .default(1024) .describe("Image height in pixels"), steps: z .number() .int() .min(1) .max(150) .default(25) .describe("Number of diffusion steps"), cfg: z .number() .min(1) .max(30) .default(7) .describe("CFG / prompt adherence (1-30)"), seed: z.number().int().optional().describe("Seed for reproducibility"), checkpoint: z .string() .optional() .describe("Checkpoint filename (defaults to COMFYUI_DEFAULT_CKPT)"), }; - src/tools/generate.ts:76-83 (registration)Registration of the 'generate_image' tool via server.tool() with name 'generate_image' and description. Part of the registerGenerateTools() function called from src/server.ts:42.
export function registerGenerateTools( server: McpServer, client: ComfyUIClient, ): void { server.tool( "generate_image", "Generate an image from a text prompt using ComfyUI's default txt2img workflow. Returns one or more image URLs served directly by the ComfyUI instance.", generateImageSchema, - src/server.ts:42-52 (registration)Call site where registerGenerateTools() is invoked with the MCP server and ComfyUIClient instance to register the 'generate_image' tool.
registerGenerateTools(s, client); registerRefineTool(s, client); registerUpscaleTool(s, client); registerModelTools(s, client); registerImageTools(s, client); registerConditioningTools(s, client); registerTemplateTools(s, client, templateStore); return s; }; return { client, buildServer }; } - src/comfyui/client.ts:47-59 (helper)The generate() method on ComfyUIClient that the handler calls. It builds a txt2img workflow via the txt2img() helper and runs it via runWorkflow().
async generate(params: GenerateParams): Promise<GenerateResult> { const workflow = txt2img({ prompt: params.prompt, negativePrompt: params.negativePrompt ?? "", width: params.width ?? 1024, height: params.height ?? 1024, steps: params.steps ?? 25, cfg: params.cfg ?? 7, seed: params.seed ?? Math.floor(Math.random() * 2 ** 32), checkpoint: params.checkpoint ?? DEFAULT_CHECKPOINT, }); return this.runWorkflow(workflow); } - src/comfyui/workflows.ts:30-72 (helper)The txt2img() workflow builder that creates the ComfyUI node graph (KSampler, CheckpointLoaderSimple, EmptyLatentImage, CLIPTextEncode, VAEDecode, SaveImage) used by the 'generate_image' tool.
export function txt2img(params: Txt2ImgParams): Workflow { return { "3": { class_type: "KSampler", inputs: { seed: params.seed, steps: params.steps, cfg: params.cfg, sampler_name: "euler", scheduler: "normal", denoise: 1, model: ["4", 0], positive: ["6", 0], negative: ["7", 0], latent_image: ["5", 0], }, }, "4": { class_type: "CheckpointLoaderSimple", inputs: { ckpt_name: params.checkpoint }, }, "5": { class_type: "EmptyLatentImage", inputs: { width: params.width, height: params.height, batch_size: 1 }, }, "6": { class_type: "CLIPTextEncode", inputs: { text: params.prompt, clip: ["4", 1] }, }, "7": { class_type: "CLIPTextEncode", inputs: { text: params.negativePrompt, clip: ["4", 1] }, }, "8": { class_type: "VAEDecode", inputs: { samples: ["3", 0], vae: ["4", 2] }, }, "9": { class_type: "SaveImage", inputs: { filename_prefix: "comfyui-mcp", images: ["8", 0] }, }, }; }