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),
298 lines • 12.6 kB
JavaScript
import { existsSync } from "node:fs";
import { mkdir, readFile, writeFile } from "node:fs/promises";
import { homedir, platform } from "node:os";
import { dirname, join, isAbsolute, resolve } from "node:path";
import { parse as parseYaml, stringify as stringifyYaml } from "yaml";
import { config } from "../config.js";
import { resolveServerExtraModelConfig } from "./output-dir.js";
import { ValidationError } from "../utils/errors.js";
export const EXTRA_PATH_TARGETS = ["auto", "standalone", "desktop"];
const RESERVED_KEYS = new Set(["base_path", "is_default"]);
const SAFE_KEY_RE = /^[A-Za-z0-9_.-]+$/;
const CONTROL_RE = /[\x00\r\n]/;
function assertSafeKey(value, label) {
const trimmed = value.trim();
if (!trimmed)
throw new ValidationError(`${label} must be a non-empty string.`);
if (!SAFE_KEY_RE.test(trimmed)) {
throw new ValidationError(`${label} may contain only letters, numbers, dot, dash, and underscore: ${value}`);
}
return trimmed;
}
function assertPathValue(value) {
const trimmed = value.trim();
if (!trimmed)
throw new ValidationError("Path must be a non-empty string.");
if (CONTROL_RE.test(trimmed)) {
throw new ValidationError("Path must not contain NUL or newline characters.");
}
return trimmed;
}
function desktopConfigPath() {
const p = platform();
if (p === "win32") {
const root = process.env.APPDATA || join(homedir(), "AppData", "Roaming");
return join(root, "ComfyUI", "extra_models_config.yaml");
}
if (p === "darwin") {
return join(homedir(), "Library", "Application Support", "ComfyUI", "extra_models_config.yaml");
}
const root = process.env.XDG_CONFIG_HOME || join(homedir(), ".config");
return join(root, "ComfyUI", "extra_models_config.yaml");
}
function standaloneConfigPath() {
if (!config.comfyuiPath) {
throw new ValidationError("No local ComfyUI path is known. Set COMFYUI_PATH, set a default workspace, " +
"or pass config_path explicitly.");
}
return join(config.comfyuiPath, "extra_model_paths.yaml");
}
function resolveTargetPath(opts = {}) {
if (opts.configPath) {
return {
target: opts.target === "desktop" ? "desktop" : "standalone",
path: opts.configPath,
};
}
const target = opts.target ?? "auto";
if (target === "desktop")
return { target, path: desktopConfigPath() };
if (target === "standalone")
return { target, path: standaloneConfigPath() };
const desktop = desktopConfigPath();
if (existsSync(desktop))
return { target: "desktop", path: desktop };
return { target: "standalone", path: standaloneConfigPath() };
}
function splitPaths(value) {
if (typeof value === "string") {
return value
.split("\n")
.map((line) => line.trim())
.filter(Boolean);
}
if (Array.isArray(value)) {
return value
.filter((item) => typeof item === "string")
.map((line) => line.trim())
.filter(Boolean);
}
return [];
}
function readGroup(raw, name) {
if (!raw || typeof raw !== "object" || Array.isArray(raw))
return null;
const obj = raw;
const categories = [];
for (const [key, value] of Object.entries(obj)) {
if (RESERVED_KEYS.has(key))
continue;
const paths = splitPaths(value);
if (paths.length > 0)
categories.push({ category: key, paths });
}
return {
name,
base_path: typeof obj.base_path === "string" ? obj.base_path : undefined,
is_default: obj.is_default,
categories,
};
}
function parseConfig(text) {
if (!text.trim())
return {};
const parsed = parseYaml(text, { maxAliasCount: 50 });
if (parsed == null)
return {};
if (typeof parsed !== "object" || Array.isArray(parsed)) {
throw new ValidationError("Extra paths config must be a YAML object.");
}
return parsed;
}
async function readConfigFile(path) {
if (!existsSync(path))
return {};
return parseConfig(await readFile(path, "utf-8"));
}
function summarize(target, path, raw, extraNotes = []) {
const groups = [];
for (const [name, value] of Object.entries(raw)) {
const group = readGroup(value, name);
if (group)
groups.push(group);
}
const notes = [
...extraNotes,
// The generic "which file" hint only applies to the static-heuristic path; a
// server-resolved config (extraNotes present) already states the real file,
// so skip it to avoid contradicting that.
...(extraNotes.length > 0
? []
: [
target === "desktop"
? "Desktop uses extra_models_config.yaml in the OS ComfyUI app-data directory."
: "Standalone/manual installs use extra_model_paths.yaml in the ComfyUI root.",
]),
"Categories are generic ComfyUI search-path keys, so model folders and custom_nodes can both be represented when supported by the running ComfyUI build.",
"Restart ComfyUI after editing this file so startup path registration is rebuilt.",
];
return { target, path, exists: existsSync(path), groups, notes };
}
/**
* Detect the ComfyUI-Desktop auto-generated shared config, which carries a
* "do not edit manually" header and is regenerated by the Desktop app.
*/
function isDesktopGeneratedConfig(path) {
return /Comfy Desktop[\\/].*shared_model_paths\.ya?ml$/i.test(path);
}
/**
* Resolve the target config path, PREFERRING the file the running server was
* actually launched with (`--extra-model-paths-config`, read from /system_stats
* argv) when the caller didn't pin an explicit target/config_path. On ComfyUI
* Desktop the live file is `…\Comfy Desktop\shared_model_paths.yaml`, NOT the
* app-data `ComfyUI\extra_models_config.yaml` the static heuristic guesses — so
* without this, list_extra_paths reports fiction and add_extra_path is a silent
* no-op against a file the server never reads (issue #345). Returns the resolved
* target plus any warning notes to surface. Best-effort: falls back to the
* static resolveTargetPath when the server is unreachable.
*/
async function resolveTargetPathPreferServer(opts = {}) {
// Explicit config_path or an explicit non-auto target: honor the caller.
if (opts.configPath || (opts.target && opts.target !== "auto")) {
return { ...resolveTargetPath(opts), notes: [] };
}
const serverConfig = await resolveServerExtraModelConfig();
if (!serverConfig) {
return { ...resolveTargetPath(opts), notes: [] };
}
const notes = [
`Resolved from the running ComfyUI's --extra-model-paths-config launch flag (the file the live server actually reads): ${serverConfig}`,
];
// The static guess is only for a divergence diagnostic — never let it (e.g. a
// missing COMFYUI_PATH for the standalone fallback) break the real resolution.
try {
const staticGuess = resolveTargetPath(opts);
if (resolve(staticGuess.path) !== resolve(serverConfig)) {
notes.push(`NOTE: this differs from the path the static heuristic would have used (${staticGuess.path}). Editing that other file would be a silent no-op — the running server does not read it.`);
}
}
catch {
// No static guess available — the server-resolved path stands on its own.
}
const isDesktop = isDesktopGeneratedConfig(serverConfig) ||
/Comfy Desktop[\\/]|[\\/]ComfyUI[\\/]extra_models_config\.ya?ml$/i.test(serverConfig);
if (isDesktopGeneratedConfig(serverConfig)) {
notes.push("WARNING: this file is auto-generated by ComfyUI Desktop ('do not edit manually') and will be overwritten by the Desktop app. For a durable model path, place or symlink the models under the server's --base-directory models dir instead of editing this YAML.");
}
return { target: isDesktop ? "desktop" : "standalone", path: serverConfig, notes };
}
export async function listExtraPaths(opts = {}) {
const resolved = await resolveTargetPathPreferServer(opts);
const raw = await readConfigFile(resolved.path);
return summarize(resolved.target, resolved.path, raw, resolved.notes);
}
/**
* Enumerate every model directory declared in the active extra_model_paths /
* extra_models_config file — i.e. the same extra roots (often on other drives
* like E:\) that ComfyUI itself loads models from. Each entry pairs a category
* key with the absolute directory that serves it (paths are resolved against the
* group's base_path when relative). Returns [] when no config exists or no local
* ComfyUI path is known. Best-effort: never throws (a malformed/unreadable
* config yields []), so callers can treat extra roots as an additive search set.
*/
export async function getExtraModelRoots(opts = {}) {
let info;
try {
info = await listExtraPaths(opts);
}
catch {
return [];
}
const roots = [];
for (const group of info.groups) {
const base = group.base_path?.trim();
for (const category of group.categories) {
for (const p of category.paths) {
const dir = isAbsolute(p)
? resolve(p)
: base
? resolve(base, p)
: resolve(p);
roots.push({ category: category.category, dir, group: group.name });
}
}
}
return roots;
}
function ensureGroup(raw, name) {
const existing = raw[name];
if (existing && typeof existing === "object" && !Array.isArray(existing)) {
return existing;
}
const group = {};
raw[name] = group;
return group;
}
async function writeConfigFile(path, raw) {
await mkdir(dirname(path), { recursive: true });
await writeFile(path, stringifyYaml(raw, { lineWidth: 0 }), "utf-8");
}
export async function addExtraPath(opts) {
const resolved = await resolveTargetPathPreferServer(opts);
const groupName = assertSafeKey(opts.group ?? "comfyui_mcp", "Group");
const category = assertSafeKey(opts.category, "Category");
const nextPath = assertPathValue(opts.path);
if (RESERVED_KEYS.has(category)) {
throw new ValidationError(`"${category}" is a reserved config key, not a path category.`);
}
const raw = await readConfigFile(resolved.path);
const group = ensureGroup(raw, groupName);
if (opts.isDefault !== undefined && group.is_default === undefined) {
group.is_default = opts.isDefault;
}
const paths = splitPaths(group[category]);
const changed = !paths.includes(nextPath);
if (changed) {
paths.push(nextPath);
group[category] = paths.join("\n");
await writeConfigFile(resolved.path, raw);
}
const info = summarize(resolved.target, resolved.path, raw, resolved.notes);
return {
...info,
changed,
message: changed
? `Added ${nextPath} to ${groupName}.${category}. Restart ComfyUI to apply it.`
: `${nextPath} is already present in ${groupName}.${category}.`,
};
}
export async function removeExtraPath(opts) {
const resolved = await resolveTargetPathPreferServer(opts);
const groupName = assertSafeKey(opts.group ?? "comfyui_mcp", "Group");
const category = assertSafeKey(opts.category, "Category");
const removePath = assertPathValue(opts.path);
const raw = await readConfigFile(resolved.path);
const group = raw[groupName];
let changed = false;
if (group && typeof group === "object" && !Array.isArray(group)) {
const obj = group;
const remaining = splitPaths(obj[category]).filter((p) => p !== removePath);
changed = remaining.length !== splitPaths(obj[category]).length;
if (changed) {
if (remaining.length > 0)
obj[category] = remaining.join("\n");
else
delete obj[category];
await writeConfigFile(resolved.path, raw);
}
}
const info = summarize(resolved.target, resolved.path, raw, resolved.notes);
return {
...info,
changed,
message: changed
? `Removed ${removePath} from ${groupName}.${category}. Restart ComfyUI to apply it.`
: `${removePath} was not present in ${groupName}.${category}.`,
};
}
//# sourceMappingURL=extra-paths.js.map