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),
179 lines • 8.6 kB
JavaScript
import { z } from "zod";
import { isLocalMode } from "../config.js";
import { installCustomNode, updateCustomNode, reinstallCustomNode, fixCustomNode, listInstalledNodes, syncNodeDependencies, } from "../services/node-management.js";
import { errorToToolResult } from "../utils/errors.js";
import { getComfyCliVersion, resolveComfyCliExecutable, shouldUseComfyCli } from "../services/comfy-cli.js";
/** Graceful "not supported remotely" tool result (no isError), matching the
* degrade-don't-throw pattern list_local_models uses. */
function remoteUnsupported(message) {
return { content: [{ type: "text", text: message }] };
}
function preferLocalComfyCli(explicit) {
if (explicit !== undefined)
return explicit;
if (!isLocalMode())
return false;
const executable = resolveComfyCliExecutable();
if (!executable)
return false;
return shouldUseComfyCli(undefined, true, executable, getComfyCliVersion());
}
const modeSchema = z
.enum(["remote", "local", "cache"])
.optional()
.describe("ComfyUI-Manager data source mode (default 'remote'): 'remote' fetches the live node list, 'local'/'cache' use bundled/cached data.");
const useCmCliSchema = z
.boolean()
.optional()
.describe("Force the official comfy-cli subprocess instead of the ComfyUI-Manager HTTP API. Local operations use comfy-cli by default; set false to force Manager HTTP. Requires a local ComfyUI install.");
const channelSchema = z
.string()
.optional()
.describe("ComfyUI-Manager channel name (default 'default').");
function formatInstalledNodes(nodes) {
if (nodes.length === 0)
return "No custom nodes installed.";
return nodes
.map((n, i) => {
const idParts = [
n.cnrId ? `cnr:${n.cnrId}` : null,
n.auxId ? `git:${n.auxId}` : null,
].filter(Boolean);
const idStr = idParts.length ? ` [${idParts.join(", ")}]` : "";
return (`${i + 1}. ${n.module}${idStr}\n` +
` version: ${n.version ?? "unknown"} | ${n.enabled ? "enabled" : "disabled"}`);
})
.join("\n");
}
export function registerNodeManagementTools(server) {
server.tool("install_custom_node", "Install a ComfyUI custom node pack by registry id, git URL, or name. Local installs prefer official comfy-cli when available; remote or CLI-unavailable installs use the ComfyUI-Manager HTTP API. A ComfyUI restart may be required.", {
id: z
.string()
.describe("Registry id, git URL, or node-pack name to install."),
source: z
.enum(["registry", "git", "auto"])
.optional()
.describe("How to interpret `id` (default 'auto', which detects git URLs vs registry ids)."),
version: z
.string()
.optional()
.describe("Version to install (e.g. 'latest', 'nightly', or a semver). For git installs, this is treated as a git ref unless `ref` is also provided. Registry installs default to 'latest'."),
ref: z
.string()
.optional()
.describe("Git ref (commit SHA, branch, or tag) to pin when installing a git URL. Overrides any ref parsed from the URL and any `version` value. Ignored for registry-id installs."),
mode: modeSchema,
channel: channelSchema,
useCmCli: useCmCliSchema,
}, async (args) => {
try {
const result = await installCustomNode({ ...args, useCmCli: preferLocalComfyCli(args.useCmCli) });
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
server.tool("update_custom_node", "Update an installed ComfyUI custom node pack, or pass 'all' to update every installed pack. Local operations prefer official comfy-cli; remote operations use Manager HTTP.", {
id: z
.string()
.describe("Registry id / module name to update, or 'all' for every pack."),
mode: modeSchema,
channel: channelSchema,
useCmCli: useCmCliSchema,
}, async (args) => {
try {
const result = await updateCustomNode({ ...args, useCmCli: preferLocalComfyCli(args.useCmCli) });
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
server.tool("reinstall_custom_node", "Reinstall a ComfyUI custom node pack. Local operations prefer official comfy-cli; remote operations use Manager HTTP. A ComfyUI restart may be required.", {
id: z.string().describe("Registry id / module name to reinstall."),
version: z
.string()
.optional()
.describe("Version to reinstall (default 'latest')."),
mode: modeSchema,
channel: channelSchema,
useCmCli: useCmCliSchema,
}, async (args) => {
try {
const result = await reinstallCustomNode({ ...args, useCmCli: preferLocalComfyCli(args.useCmCli) });
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
server.tool("fix_custom_node", "Repair a ComfyUI custom node pack's install and Python dependencies, or pass 'all' to repair every pack. Local operations prefer official comfy-cli; remote single-pack repairs use Manager HTTP.", {
id: z
.string()
.describe("Registry id / module name to repair, or 'all' for every pack."),
mode: modeSchema,
channel: channelSchema,
useCmCli: useCmCliSchema,
}, async (args) => {
// "all" repairs every pack via the cm-cli subprocess, which needs a local
// ComfyUI install. Single-pack repair uses the Manager HTTP API and works
// remotely, so only guard the "all" case here.
if (args.id.trim().toLowerCase() === "all" && !isLocalMode()) {
return remoteUnsupported("fix_custom_node id=\"all\" is not supported against a remote ComfyUI. " +
"Repairing every pack runs the cm-cli subprocess, which requires a " +
"local ComfyUI install (COMFYUI_PATH). Repair a single pack by id " +
"instead (that uses the ComfyUI-Manager HTTP API and works remotely).");
}
try {
const result = await fixCustomNode({ ...args, useCmCli: preferLocalComfyCli(args.useCmCli) });
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
server.tool("list_installed_nodes", "List installed ComfyUI custom node packs with their version and enabled/disabled state. Uses the ComfyUI-Manager HTTP API (works against remote instances); the cm-cli fallback returns names only.", {
mode: z
.enum(["default", "imported"])
.optional()
.describe("'default' lists all installed packs; 'imported' lists only those successfully imported this session."),
useCmCli: useCmCliSchema,
}, async (args) => {
try {
const nodes = await listInstalledNodes(args);
return {
content: [{ type: "text", text: formatInstalledNodes(nodes) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
server.tool("sync_node_dependencies", "Reconcile the Python dependencies of all installed custom node packs through official `comfy node restore-dependencies`. Requires a local ComfyUI install and comfy-cli.", {}, async () => {
if (!isLocalMode()) {
return remoteUnsupported("sync_node_dependencies is not supported against a remote ComfyUI. It " +
"runs the cm-cli restore-dependencies subprocess, which requires a " +
"local ComfyUI install (COMFYUI_PATH). Reconcile dependencies on the " +
"ComfyUI host instead.");
}
try {
const result = await syncNodeDependencies();
return {
content: [{ type: "text", text: JSON.stringify(result, null, 2) }],
};
}
catch (err) {
return errorToToolResult(err);
}
});
}
//# sourceMappingURL=node-management.js.map