Init commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
COMFYUI_URL=http://192.168.1.13:8188
|
||||
COMFYUI_TIMEOUT=300
|
||||
WORKFLOWS_DIR=/opt/hermes/MCP_comfyui/workflows
|
||||
COMFY_PROMPT_NODE=6
|
||||
COMFY_VISION_IMAGE_NODE=1
|
||||
COMFY_VISION_PROMPT_NODE=2
|
||||
ALFRED_RENDER_URL=http://127.0.0.1:54321/api/render
|
||||
@@ -0,0 +1,6 @@
|
||||
COMFYUI_URL=http://192.168.1.13:8188
|
||||
COMFYUI_TIMEOUT=300
|
||||
WORKFLOWS_DIR=/opt/hermes/MCP_comfyui/workflows
|
||||
COMFY_PROMPT_NODE=6
|
||||
COMFY_VISION_IMAGE_NODE=1
|
||||
COMFY_VISION_PROMPT_NODE=2
|
||||
@@ -0,0 +1,3 @@
|
||||
# MCP_comfyui
|
||||
|
||||
Copier `.env.example` vers `.env`, puis créer le venv et installer `requirements.txt`.
|
||||
@@ -0,0 +1,151 @@
|
||||
"""MCP ComfyUI pour Hermes avec transmission d'images vers l'interface Alfred.
|
||||
|
||||
Fonctionne avec un workflow API ComfyUI possédant un nœud de sauvegarde d'image.
|
||||
L'envoi HTTP à Alfred est indépendant de la réponse textuelle d'Hermes.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
from urllib.parse import urlencode
|
||||
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
load_dotenv(Path(__file__).with_name(".env"))
|
||||
|
||||
mcp = FastMCP("Alfred ComfyUI")
|
||||
BASE = os.getenv("COMFYUI_URL", "http://127.0.0.1:8188").rstrip("/")
|
||||
WF = Path(os.getenv("WORKFLOWS_DIR", str(Path(__file__).parent / "workflows")))
|
||||
ALFRED_RENDER_URL = os.getenv(
|
||||
"ALFRED_RENDER_URL", "http://127.0.0.1:54321/api/render"
|
||||
)
|
||||
ALFRED_RENDER_TOKEN = os.getenv("ALFRED_RENDER_TOKEN", "").strip()
|
||||
|
||||
|
||||
def submit(workflow: dict) -> str:
|
||||
response = requests.post(
|
||||
f"{BASE}/prompt",
|
||||
json={"prompt": workflow, "client_id": str(uuid.uuid4())},
|
||||
timeout=30,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return response.json()["prompt_id"]
|
||||
|
||||
|
||||
def wait(prompt_id: str) -> dict:
|
||||
deadline = time.monotonic() + int(os.getenv("COMFYUI_TIMEOUT", "300"))
|
||||
while time.monotonic() < deadline:
|
||||
response = requests.get(f"{BASE}/history/{prompt_id}", timeout=15)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
if prompt_id in result:
|
||||
return result[prompt_id]
|
||||
time.sleep(1)
|
||||
raise TimeoutError(f"ComfyUI timeout ({prompt_id})")
|
||||
|
||||
|
||||
def extract_images(history: dict) -> list[dict]:
|
||||
"""Construit les URL de /view depuis les sorties ComfyUI (SaveImage)."""
|
||||
images = []
|
||||
for node_id, output in history.get("outputs", {}).items():
|
||||
for image in output.get("images", []):
|
||||
filename = image.get("filename")
|
||||
if not filename:
|
||||
continue
|
||||
subfolder = image.get("subfolder", "")
|
||||
image_type = image.get("type", "output")
|
||||
query = urlencode(
|
||||
{
|
||||
"filename": filename,
|
||||
"subfolder": subfolder,
|
||||
"type": image_type,
|
||||
}
|
||||
)
|
||||
images.append(
|
||||
{
|
||||
"node_id": str(node_id),
|
||||
"filename": filename,
|
||||
"subfolder": subfolder,
|
||||
"type": image_type,
|
||||
"url": f"{BASE}/view?{query}",
|
||||
}
|
||||
)
|
||||
return images
|
||||
|
||||
|
||||
def show_on_alfred(image: dict, caption: str) -> tuple[bool, str | None]:
|
||||
"""Ne fait pas échouer la génération si Alfred est hors ligne."""
|
||||
headers = {}
|
||||
if ALFRED_RENDER_TOKEN:
|
||||
headers["X-Alfred-Render-Token"] = ALFRED_RENDER_TOKEN
|
||||
try:
|
||||
response = requests.post(
|
||||
ALFRED_RENDER_URL,
|
||||
json={
|
||||
"type": "image",
|
||||
"url": image["url"],
|
||||
"alt": "Image générée par ComfyUI",
|
||||
"caption": caption,
|
||||
},
|
||||
headers=headers,
|
||||
timeout=5,
|
||||
)
|
||||
response.raise_for_status()
|
||||
return True, None
|
||||
except requests.RequestException as exc:
|
||||
return False, str(exc)
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def generate_image(prompt: str) -> dict:
|
||||
"""Génère une image par ComfyUI et l'affiche automatiquement sur Alfred.
|
||||
|
||||
Utiliser cet outil lorsque l'utilisateur demande de créer/générer une image.
|
||||
Ne pas prétendre que l'image a été affichée si displayed_on_alfred est faux.
|
||||
"""
|
||||
prompt = str(prompt).strip()
|
||||
if not prompt:
|
||||
raise ValueError("Le prompt de génération est vide")
|
||||
|
||||
workflow = json.loads((WF / "text_to_image.json").read_text(encoding="utf-8"))
|
||||
prompt_node = os.getenv("COMFY_PROMPT_NODE", "6")
|
||||
workflow[prompt_node]["inputs"]["text"] = prompt
|
||||
|
||||
prompt_id = submit(workflow)
|
||||
history = wait(prompt_id)
|
||||
images = extract_images(history)
|
||||
if not images:
|
||||
raise RuntimeError(
|
||||
"ComfyUI a terminé sans image de sortie. "
|
||||
"Vérifie le nœud SaveImage du workflow."
|
||||
)
|
||||
|
||||
displayed, display_error = show_on_alfred(
|
||||
images[0], f"Image générée : {prompt}"
|
||||
)
|
||||
return {
|
||||
"ok": True,
|
||||
"prompt_id": prompt_id,
|
||||
"prompt": prompt,
|
||||
"images": images,
|
||||
"first_image_url": images[0]["url"],
|
||||
"displayed_on_alfred": displayed,
|
||||
"display_error": display_error,
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def analyze_image(image_path: str, question: str) -> dict:
|
||||
"""Analyse une image via le workflow VLM configuré (inchangé)."""
|
||||
workflow = json.loads((WF / "vision_analyze.json").read_text(encoding="utf-8"))
|
||||
workflow[os.getenv("COMFY_VISION_IMAGE_NODE", "1")]["inputs"]["image"] = image_path
|
||||
workflow[os.getenv("COMFY_VISION_PROMPT_NODE", "2")]["inputs"]["text"] = question
|
||||
return wait(submit(workflow))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mcp.run()
|
||||
@@ -0,0 +1,100 @@
|
||||
import os, json, time, uuid, requests
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
from dotenv import load_dotenv
|
||||
from mcp.server.fastmcp import FastMCP
|
||||
|
||||
load_dotenv()
|
||||
|
||||
mcp = FastMCP("Alfred ComfyUI")
|
||||
BASE = os.getenv("COMFYUI_URL", "http://127.0.0.1:8188").rstrip("/")
|
||||
WF = Path(os.getenv("WORKFLOWS_DIR", Path(__file__).parent / "workflows"))
|
||||
|
||||
|
||||
def submit(workflow):
|
||||
r = requests.post(
|
||||
BASE + "/prompt",
|
||||
json={
|
||||
"prompt": workflow,
|
||||
"client_id": str(uuid.uuid4())
|
||||
},
|
||||
timeout=30
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()["prompt_id"]
|
||||
|
||||
|
||||
def wait(pid):
|
||||
deadline = time.time() + int(os.getenv("COMFYUI_TIMEOUT", "300"))
|
||||
while time.time() < deadline:
|
||||
h = requests.get(f"{BASE}/history/{pid}", timeout=15).json()
|
||||
if pid in h:
|
||||
return h[pid]
|
||||
time.sleep(1)
|
||||
raise TimeoutError("ComfyUI timeout")
|
||||
|
||||
|
||||
def build_image_url(filename, subfolder="", img_type="output"):
|
||||
return (
|
||||
f"{BASE}/view?"
|
||||
f"filename={quote(str(filename))}"
|
||||
f"&subfolder={quote(str(subfolder))}"
|
||||
f"&type={quote(str(img_type))}"
|
||||
)
|
||||
|
||||
|
||||
def extract_images(history):
|
||||
images = []
|
||||
|
||||
outputs = history.get("outputs", {})
|
||||
for node_id, node_output in outputs.items():
|
||||
for img in node_output.get("images", []):
|
||||
filename = img.get("filename")
|
||||
subfolder = img.get("subfolder", "")
|
||||
img_type = img.get("type", "output")
|
||||
|
||||
if not filename:
|
||||
continue
|
||||
|
||||
images.append({
|
||||
"node_id": node_id,
|
||||
"filename": filename,
|
||||
"subfolder": subfolder,
|
||||
"type": img_type,
|
||||
"url": build_image_url(filename, subfolder, img_type)
|
||||
})
|
||||
|
||||
return images
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def generate_image(prompt: str):
|
||||
"""Generate an image with the locked text_to_image workflow."""
|
||||
w = json.loads((WF / "text_to_image.json").read_text())
|
||||
node = os.getenv("COMFY_PROMPT_NODE", "6")
|
||||
w[node]["inputs"]["text"] = prompt
|
||||
|
||||
history = wait(submit(w))
|
||||
images = extract_images(history)
|
||||
|
||||
return {
|
||||
"ok": True,
|
||||
"message": f"Image générée pour : {prompt}",
|
||||
"prompt": prompt,
|
||||
"images": images,
|
||||
"first_image_url": images[0]["url"] if images else None,
|
||||
"first_image_filename": images[0]["filename"] if images else None
|
||||
}
|
||||
|
||||
|
||||
@mcp.tool()
|
||||
def analyze_image(image_path: str, question: str):
|
||||
"""Analyze a camera/local image using the locked VLM workflow. Workflow node IDs are configured in .env."""
|
||||
w = json.loads((WF / "vision_analyze.json").read_text())
|
||||
w[os.getenv("COMFY_VISION_IMAGE_NODE", "1")]["inputs"]["image"] = image_path
|
||||
w[os.getenv("COMFY_VISION_PROMPT_NODE", "2")]["inputs"]["text"] = question
|
||||
return wait(submit(w))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
mcp.run()
|
||||
@@ -0,0 +1,3 @@
|
||||
mcp>=1.12
|
||||
requests>=2.32
|
||||
python-dotenv>=1.0
|
||||
@@ -0,0 +1 @@
|
||||
Exporter depuis ComfyUI les workflows API JSON et les nommer `text_to_image.json` et `vision_analyze.json`. Les IDs de nœuds sont configurables dans `.env`.
|
||||
@@ -0,0 +1,107 @@
|
||||
{
|
||||
"3": {
|
||||
"inputs": {
|
||||
"seed": 156680208700286,
|
||||
"steps": 20,
|
||||
"cfg": 8,
|
||||
"sampler_name": "euler",
|
||||
"scheduler": "normal",
|
||||
"denoise": 1,
|
||||
"model": [
|
||||
"4",
|
||||
0
|
||||
],
|
||||
"positive": [
|
||||
"6",
|
||||
0
|
||||
],
|
||||
"negative": [
|
||||
"7",
|
||||
0
|
||||
],
|
||||
"latent_image": [
|
||||
"5",
|
||||
0
|
||||
]
|
||||
},
|
||||
"class_type": "KSampler",
|
||||
"_meta": {
|
||||
"title": "KSampler"
|
||||
}
|
||||
},
|
||||
"4": {
|
||||
"inputs": {
|
||||
"ckpt_name": "absolutereality_v181.safetensors"
|
||||
},
|
||||
"class_type": "CheckpointLoaderSimple",
|
||||
"_meta": {
|
||||
"title": "Load Checkpoint"
|
||||
}
|
||||
},
|
||||
"5": {
|
||||
"inputs": {
|
||||
"width": 512,
|
||||
"height": 512,
|
||||
"batch_size": 1
|
||||
},
|
||||
"class_type": "EmptyLatentImage",
|
||||
"_meta": {
|
||||
"title": "Empty Latent Image"
|
||||
}
|
||||
},
|
||||
"6": {
|
||||
"inputs": {
|
||||
"text": "beautiful scenery nature glass bottle landscape, , purple galaxy bottle,",
|
||||
"clip": [
|
||||
"4",
|
||||
1
|
||||
]
|
||||
},
|
||||
"class_type": "CLIPTextEncode",
|
||||
"_meta": {
|
||||
"title": "CLIP Text Encode (Prompt)"
|
||||
}
|
||||
},
|
||||
"7": {
|
||||
"inputs": {
|
||||
"text": "text, watermark",
|
||||
"clip": [
|
||||
"4",
|
||||
1
|
||||
]
|
||||
},
|
||||
"class_type": "CLIPTextEncode",
|
||||
"_meta": {
|
||||
"title": "CLIP Text Encode (Prompt)"
|
||||
}
|
||||
},
|
||||
"8": {
|
||||
"inputs": {
|
||||
"samples": [
|
||||
"3",
|
||||
0
|
||||
],
|
||||
"vae": [
|
||||
"4",
|
||||
2
|
||||
]
|
||||
},
|
||||
"class_type": "VAEDecode",
|
||||
"_meta": {
|
||||
"title": "VAE Decode"
|
||||
}
|
||||
},
|
||||
"9": {
|
||||
"inputs": {
|
||||
"filename_prefix": "ComfyUI",
|
||||
"images": [
|
||||
"8",
|
||||
0
|
||||
]
|
||||
},
|
||||
"class_type": "SaveImage",
|
||||
"_meta": {
|
||||
"title": "Save Image"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
{
|
||||
"3": {
|
||||
"inputs": {
|
||||
"seed": 156680208700286,
|
||||
"steps": 20,
|
||||
"cfg": 8,
|
||||
"sampler_name": "euler",
|
||||
"scheduler": "normal",
|
||||
"denoise": 1,
|
||||
"model": [
|
||||
"4",
|
||||
0
|
||||
],
|
||||
"positive": [
|
||||
"6",
|
||||
0
|
||||
],
|
||||
"negative": [
|
||||
"7",
|
||||
0
|
||||
],
|
||||
"latent_image": [
|
||||
"5",
|
||||
0
|
||||
]
|
||||
},
|
||||
"class_type": "KSampler",
|
||||
"_meta": {
|
||||
"title": "KSampler"
|
||||
}
|
||||
},
|
||||
"4": {
|
||||
"inputs": {
|
||||
"ckpt_name": "absolutereality_v181.safetensors"
|
||||
},
|
||||
"class_type": "CheckpointLoaderSimple",
|
||||
"_meta": {
|
||||
"title": "Load Checkpoint"
|
||||
}
|
||||
},
|
||||
"5": {
|
||||
"inputs": {
|
||||
"width": 512,
|
||||
"height": 512,
|
||||
"batch_size": 1
|
||||
},
|
||||
"class_type": "EmptyLatentImage",
|
||||
"_meta": {
|
||||
"title": "Empty Latent Image"
|
||||
}
|
||||
},
|
||||
"6": {
|
||||
"inputs": {
|
||||
"text": "beautiful scenery nature glass bottle landscape, , purple galaxy bottle,",
|
||||
"clip": [
|
||||
"4",
|
||||
1
|
||||
]
|
||||
},
|
||||
"class_type": "CLIPTextEncode",
|
||||
"_meta": {
|
||||
"title": "CLIP Text Encode (Prompt)"
|
||||
}
|
||||
},
|
||||
"7": {
|
||||
"inputs": {
|
||||
"text": "text, watermark",
|
||||
"clip": [
|
||||
"4",
|
||||
1
|
||||
]
|
||||
},
|
||||
"class_type": "CLIPTextEncode",
|
||||
"_meta": {
|
||||
"title": "CLIP Text Encode (Prompt)"
|
||||
}
|
||||
},
|
||||
"8": {
|
||||
"inputs": {
|
||||
"samples": [
|
||||
"3",
|
||||
0
|
||||
],
|
||||
"vae": [
|
||||
"4",
|
||||
2
|
||||
]
|
||||
},
|
||||
"class_type": "VAEDecode",
|
||||
"_meta": {
|
||||
"title": "VAE Decode"
|
||||
}
|
||||
},
|
||||
"9": {
|
||||
"inputs": {
|
||||
"filename_prefix": "ComfyUI",
|
||||
"images": [
|
||||
"8",
|
||||
0
|
||||
]
|
||||
},
|
||||
"class_type": "SaveImage",
|
||||
"_meta": {
|
||||
"title": "Save Image"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user