generate_with_workflow
Execute a custom ComfyUI workflow by submitting its node graph JSON and receive the resulting image URLs. Supports any exported workflow like ControlNet or upscaling.
Instructions
Submit an arbitrary ComfyUI workflow (full node graph) and return the resulting image URLs. Use this when you need a custom workflow like ControlNet, upscaling, or a node graph exported from ComfyUI's 'Save (API Format)'.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| workflow | Yes | Complete ComfyUI workflow JSON (node graph as returned by ComfyUI's 'Save (API Format)' export) |
Implementation Reference
- src/tools/generate.ts:132-144 (handler)Handler function for the 'generate_with_workflow' tool. Takes a 'workflow' parameter (full ComfyUI node graph JSON), casts it to Workflow type, calls client.runWorkflow(), and returns image URLs in a text result.
server.tool( "generate_with_workflow", "Submit an arbitrary ComfyUI workflow (full node graph) and return the resulting image URLs. Use this when you need a custom workflow like ControlNet, upscaling, or a node graph exported from ComfyUI's 'Save (API Format)'.", generateWithWorkflowSchema, async (args) => { const workflow = args.workflow as Workflow; const result = await client.runWorkflow(workflow); return textResult( `Workflow submitted (prompt_id: ${result.promptId}), ${result.images.length} image(s):`, result.images, ); }, ); - src/tools/generate.ts:68-74 (schema)Input schema for 'generate_with_workflow'. Defines a single required parameter 'workflow' as a record of string to any (the ComfyUI workflow JSON node graph).
const generateWithWorkflowSchema = { workflow: z .record(z.string(), z.any()) .describe( "Complete ComfyUI workflow JSON (node graph as returned by ComfyUI's 'Save (API Format)' export)", ), }; - src/tools/generate.ts:76-145 (registration)Registration of the 'generate_with_workflow' tool via server.tool() inside registerGenerateTools(), which is called from src/server.ts line 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, 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, ); }, ); server.tool( "generate_variations", "Generate multiple variations of the same prompt by varying the seed. Useful for picking the best result or exploring a concept.", generateVariationsSchema, async (args) => { const startSeed = args.base_seed ?? Math.floor(Math.random() * 2 ** 32); const results = await Promise.all( Array.from({ length: args.count }, (_, i) => client.generate({ prompt: args.prompt, negativePrompt: args.negative_prompt, width: args.width, height: args.height, steps: args.steps, cfg: args.cfg, seed: startSeed + i, checkpoint: args.checkpoint, }), ), ); const urls = results.flatMap((r) => r.images); return textResult( `Generated ${args.count} variation(s) starting from seed ${startSeed}:`, urls, ); }, ); server.tool( "generate_with_workflow", "Submit an arbitrary ComfyUI workflow (full node graph) and return the resulting image URLs. Use this when you need a custom workflow like ControlNet, upscaling, or a node graph exported from ComfyUI's 'Save (API Format)'.", generateWithWorkflowSchema, async (args) => { const workflow = args.workflow as Workflow; const result = await client.runWorkflow(workflow); return textResult( `Workflow submitted (prompt_id: ${result.promptId}), ${result.images.length} image(s):`, result.images, ); }, ); } - src/comfyui/client.ts:61-68 (helper)The actual workflow execution engine. runWorkflow() submits the workflow JSON to ComfyUI's /prompt endpoint and waits for completion, returning extracted image URLs.
async runWorkflow(workflow: Workflow): Promise<GenerateResult> { const { prompt_id } = await this.submit(workflow); const entry = await this.waitForCompletion(prompt_id); return { promptId: prompt_id, images: extractImageUrls(entry, this.publicUrl), }; } - src/comfyui/types.ts:18-24 (helper)Type definitions: Workflow (Record<string, WorkflowNode>) and WorkflowNode interface used by the handler to type-check the incoming workflow argument.
export interface WorkflowNode { inputs: Record<string, unknown>; class_type: string; _meta?: Record<string, unknown>; } export type Workflow = Record<string, WorkflowNode>;