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),

111 lines • 4.59 kB
// Read and write the user's Claude Code MCP-server config (~/.claude.json), so // the panel orchestrator's background agent uses the SAME MCP servers the user's // normal `claude` session does — and can ADD new ones programmatically (e.g. // connect the CivitAI MCP on demand), persisting them to the user's real config. // // MCP servers live at the top level of ~/.claude.json under `mcpServers`, keyed // by name; each value is a Claude Agent SDK server config (stdio | sse | http). // We deliberately EXCLUDE any entry that looks like a comfyui-mcp instance — the // orchestrator injects its own bridge-safe comfyui server, and a second one // would fight for the panel bridge port. import { existsSync, readFileSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { join } from "node:path"; import { logger } from "../utils/logger.js"; /** Path to the user's Claude config. Overridable for tests. */ export function claudeJsonPath() { return process.env.COMFYUI_MCP_CLAUDE_JSON || join(homedir(), ".claude.json"); } function readClaudeJson() { const p = claudeJsonPath(); if (!existsSync(p)) return {}; try { const parsed = JSON.parse(readFileSync(p, "utf-8")); return parsed && typeof parsed === "object" ? parsed : {}; } catch (err) { logger.warn(`[user-mcp] could not parse ${p}: ${err instanceof Error ? err.message : String(err)}`); return {}; } } function serversObject(cfg) { const m = cfg.mcpServers; return m && typeof m === "object" ? m : {}; } /** * Would this server fight our own comfyui server for the bridge port? True for * anything named "comfyui" or whose config invokes comfyui-mcp. */ export function isConflictingServer(name, cfg) { if (name.toLowerCase() === "comfyui") return true; const s = JSON.stringify(cfg ?? "").toLowerCase(); return s.includes("comfyui-mcp"); } /** * The user's configured MCP servers (user scope), minus anything that conflicts * with our injected comfyui server. This is what the panel agent inherits. */ export function readUserMcpServers() { const servers = serversObject(readClaudeJson()); const out = {}; for (const [name, cfg] of Object.entries(servers)) { if (isConflictingServer(name, cfg)) continue; out[name] = cfg; } return out; } const NAME_RE = /^[\w.-]+$/; /** * Add (or replace) an MCP server in the user's Claude config, persisting it so * both the panel agent (after a reload) and the user's normal session see it. * Refuses conflicting names so we can't accidentally clobber the bridge. */ export function addUserMcpServer(name, config) { if (!NAME_RE.test(name)) { throw new Error(`Invalid MCP server name "${name}" (use letters, digits, dot, dash, underscore).`); } if (isConflictingServer(name, config)) { throw new Error(`Refusing to add "${name}" — it conflicts with the panel's own comfyui server.`); } const cfg = readClaudeJson(); const servers = serversObject(cfg); servers[name] = config; cfg.mcpServers = servers; writeFileSync(claudeJsonPath(), JSON.stringify(cfg, null, 2)); } /** * Write a secret onto an existing MCP server (a header for http/sse, or an env * var for stdio), persisting to ~/.claude.json. The caller passes the raw secret * straight from the secure input; it is never logged or returned anywhere. */ export function setUserMcpServerSecret(target, value) { const cfg = readClaudeJson(); const servers = serversObject(cfg); const server = servers[target.server]; if (!server || typeof server !== "object") { throw new Error(`MCP server "${target.server}" is not configured — add it first with panel_add_mcp.`); } const bag = target.kind === "header" ? "headers" : "env"; const s = server; const existing = s[bag] && typeof s[bag] === "object" ? s[bag] : {}; existing[target.key] = `${target.prefix ?? ""}${value}`; s[bag] = existing; servers[target.server] = s; cfg.mcpServers = servers; writeFileSync(claudeJsonPath(), JSON.stringify(cfg, null, 2)); } /** Remove an MCP server from the user's config. Returns false if absent. */ export function removeUserMcpServer(name) { const cfg = readClaudeJson(); const servers = serversObject(cfg); if (!(name in servers)) return false; delete servers[name]; cfg.mcpServers = servers; writeFileSync(claudeJsonPath(), JSON.stringify(cfg, null, 2)); return true; } //# sourceMappingURL=user-mcp-config.js.map