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),
50 lines • 2.55 kB
JavaScript
import { createWorkflow } from "./workflow-composer.js";
import { DefaultsManager } from "./defaults-manager.js";
import { ValidationError } from "../utils/errors.js";
import { assertSafeInputFilename, assertSafeFilenamePrefix, } from "../utils/input-paths.js";
/** The ComfyUI-RMBG node class that does the matting. */
export const REMBG_NODE = "BiRefNetRMBG";
const DEFAULTABLE_KEYS = ["model", "filename_prefix"];
/**
* Build + enqueue a background-removal workflow (LoadImage → BiRefNetRMBG →
* SaveImage), returning a transparent cutout. Requires the ComfyUI-RMBG custom
* node pack; if we can confirm it's absent we throw an actionable error instead
* of enqueuing a graph that will fail at runtime.
*/
export async function removeBackground(args, deps) {
if (!args.image || !args.image.trim()) {
throw new ValidationError("image is required — the filename of an image already in ComfyUI's input dir " +
"(upload it first with upload_image (action:\"image\"), or stage an output with upload_image (action:\"stage\")).");
}
assertSafeInputFilename(args.image, "image");
if (deps.isNodeInstalled) {
const installed = await deps.isNodeInstalled(REMBG_NODE);
if (installed === false) {
throw new ValidationError(`The background-removal node "${REMBG_NODE}" (ComfyUI-RMBG) is not installed. ` +
"Install it with apply_manifest --path packs/wan-transparent/manifest.yaml, " +
"or install_custom_node id 'comfyui-rmbg'. The BiRefNet model auto-downloads " +
"into models/RMBG/BiRefNet/ on first run.");
}
}
const seed = {};
for (const key of DEFAULTABLE_KEYS) {
const v = args[key];
if (v !== undefined)
seed[key] = v;
}
const resolved = DefaultsManager.apply(seed);
// Validate the RESOLVED prefix (post-defaults) — a malicious default
// filename_prefix must not reach SaveImage unsanitized.
if (resolved.filename_prefix !== undefined) {
assertSafeFilenamePrefix(resolved.filename_prefix);
}
const workflow = createWorkflow("remove_background", {
image_path: args.image,
model: resolved.model,
filename_prefix: resolved.filename_prefix,
});
const model = workflow["2"]?.inputs.model ?? "BiRefNet_toonout";
const { prompt_id, queue_remaining, rejectedOutputs } = await deps.enqueue(workflow);
return { prompt_id, queue_remaining, rejectedOutputs, model };
}
//# sourceMappingURL=remove-background.js.map