UNPKG

comfyui-mcp

Version:

Local-first, agent-native control plane for ComfyUI — MCP server + autonomous sidebar agent that drives your live graph in natural language on ANY LLM: Claude/ChatGPT/Gemini on your subscription (no API key), free local models via Ollama (fully offline),

162 lines • 8.5 kB
import { z } from "zod"; import { getQueueSummary, getJobStatus, getQueuedWorkflow, moveQueuedJob, editQueuedJob, cancelRunningJobEscalating, cancelQueuedJob, clearAllQueued, } from "../services/queue-manager.js"; import { errorToToolResult } from "../utils/errors.js"; export function registerQueueManagementTools(server) { server.tool("get_queue", "Get the current ComfyUI execution queue: the job running now plus all pending jobs, each with its prompt_id and position. Read-only; requires a reachable ComfyUI server (works against local or remote --comfyui-url). By default this omits queued workflow payloads to keep output small; set include_workflows:true when you need to inspect or edit the exact pending payload. Use this before cancel_job (running), cancel_queued_job/clear_queue (pending), move_queued_job, or edit_queued_job.", { include_workflows: z .boolean() .optional() .describe("Include each running/pending job's workflow payload and extra_data. Can be large."), }, async (args) => { try { const summary = await getQueueSummary({ include_workflows: args.include_workflows }); return { content: [ { type: "text", text: JSON.stringify(summary, null, 2), }, ], }; } catch (err) { return errorToToolResult(err); } }); server.tool("get_queued_workflow", "Return the full workflow payload for one PENDING queue item by prompt_id. Read-only. This does not work for the currently running job because ComfyUI cannot safely edit a job after execution starts.", { prompt_id: z.string().describe("The prompt_id of a pending queue item."), }, async (args) => { try { const item = await getQueuedWorkflow(args.prompt_id); return { content: [{ type: "text", text: JSON.stringify(item, null, 2) }], }; } catch (err) { return errorToToolResult(err); } }); server.tool("move_queued_job", "Move a PENDING ComfyUI queue item to the front or back by removing it and re-enqueuing its saved workflow payload. The job receives a NEW prompt_id; the old prompt_id is removed. Running jobs cannot be moved.", { prompt_id: z.string().describe("The prompt_id of a pending queue item."), position: z.enum(["front", "back"]).describe("Where to requeue the job."), }, async (args) => { try { const result = await moveQueuedJob(args.prompt_id, args.position); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (err) { return errorToToolResult(err); } }); server.tool("edit_queued_job", "Edit a PENDING ComfyUI queue item by removing it and re-enqueuing an updated workflow. Provide either a complete replacement workflow or node_inputs patches keyed by node id. The job receives a NEW prompt_id; the old prompt_id is removed. Running jobs cannot be edited.", { prompt_id: z.string().describe("The prompt_id of a pending queue item."), workflow: z .record(z.string(), z.any()) .optional() .describe("Optional complete replacement API-format workflow. If omitted, the existing queued workflow is patched."), node_inputs: z .record(z.string(), z.record(z.string(), z.any())) .optional() .describe("Optional input patches keyed by node id, e.g. {\"3\":{\"steps\":30,\"cfg\":7}}."), position: z .enum(["front", "back"]) .optional() .describe("Where to requeue the edited job. Defaults to back."), }, async (args) => { try { const result = await editQueuedJob({ prompt_id: args.prompt_id, workflow: args.workflow, node_inputs: args.node_inputs, position: args.position, }); return { content: [{ type: "text", text: JSON.stringify(result, null, 2) }], }; } catch (err) { return errorToToolResult(err); } }); server.tool("get_job_status", "Check the status of ONE ComfyUI job by its prompt_id (the id returned by enqueue_workflow). Queries the connected ComfyUI server; requires it to be running. Returns JSON with running, pending, and done booleans, plus optional status_str, error details, and execution_stats from ComfyUI history once the job is done. Also returns text_outputs when the workflow contained text-preview nodes (Preview as Text, ShowText, …) — those produce no image file, so this is the ONLY way to read their result; report that text back to the user. Use get_queue to see the whole queue at once, and get_history for full output filenames.", { prompt_id: z.string().describe("The prompt ID returned by enqueue_workflow"), }, async (args) => { try { const status = await getJobStatus(args.prompt_id); return { content: [ { type: "text", text: JSON.stringify(status, null, 2), }, ], }; } catch (err) { return errorToToolResult(err); } }); server.tool("cancel_job", "Stop the CURRENTLY RUNNING ComfyUI job ROBUSTLY. Sends an interrupt, then WAITS and verifies the job actually stopped — ComfyUI only honors interrupts BETWEEN steps, so a long single step (e.g. a high-res video sampler) can ignore a plain cancel. If the interrupt isn't honored it escalates to freeing VRAM (POST /free) and re-checks; if it STILL won't die it reports the job as WEDGED and tells you to restart_comfyui (an HTTP cancel cannot kill a stuck step). Set clear_pending:true to also drop ALL pending jobs in the same call — the correct 'reset the queue' action, since cancelling alone leaves pending jobs that would run next. The partial result is discarded. Use cancel_queued_job to remove one specific PENDING job instead.", { prompt_id: z .string() .optional() .describe("Optional. If given, only interrupts the running job when its prompt_id matches; omit to interrupt whatever is currently running."), clear_pending: z .boolean() .optional() .describe("Also clear ALL pending jobs (recommended when resetting after a stuck/slow render, so a re-queue doesn't stack behind a backlog). Default false."), }, async (args) => { try { const result = await cancelRunningJobEscalating({ prompt_id: args.prompt_id, clear_pending: args.clear_pending, }); return { content: [{ type: "text", text: result.message }], ...(result.wedged ? { isError: true } : {}), }; } catch (err) { return errorToToolResult(err); } }); server.tool("cancel_queued_job", "Remove a specific pending job from the ComfyUI queue by prompt_id. Does not affect running jobs.", { prompt_id: z .string() .describe("The prompt_id of the pending job to remove from the queue"), }, async (args) => { try { await cancelQueuedJob(args.prompt_id); return { content: [ { type: "text", text: `Queued job ${args.prompt_id} removed successfully.`, }, ], }; } catch (err) { return errorToToolResult(err); } }); server.tool("clear_queue", "Clear all pending jobs from the ComfyUI queue. Does not affect the currently running job.", {}, async () => { try { await clearAllQueued(); return { content: [ { type: "text", text: "All pending queue items cleared successfully.", }, ], }; } catch (err) { return errorToToolResult(err); } }); } //# sourceMappingURL=queue-management.js.map