-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
81 lines (65 loc) · 2.51 KB
/
server.py
File metadata and controls
81 lines (65 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from mcp.server.fastmcp import FastMCP
from pathlib import Path
from typing import Dict
import src.loader as loader
import src.factory as factory
import sys
# Initialize FastMCP Server
mcp = FastMCP("ComfyUI")
# Global Registry
# Maps file_path -> WorkflowConfig
REGISTERED_CONFIGS: Dict[Path, loader.WorkflowConfig] = {}
def _generate_manifest() -> str:
"""Generates a text manifest of available workflows."""
if not REGISTERED_CONFIGS:
return "No workflows registered."
lines = ["# Available Workflows\n"]
for config in REGISTERED_CONFIGS.values():
lines.append(f"## {config.name}")
lines.append(f"_{config.description}_")
lines.append("### Parameters:")
for param in config.parameters:
req = "(Required)" if param.required else "(Optional)"
lines.append(f"- **{param.name}** ({param.type}): {param.description} {req}")
lines.append("\n---")
return "\n".join(lines)
@mcp.resource("comfy://list")
def list_resource() -> str:
"""Returns a list of available workflows and their parameters."""
return _generate_manifest()
@mcp.tool()
def list_available_workflows() -> str:
"""
Returns a list of all available workflows on this server.
Use this to discover what you can do.
"""
return _generate_manifest()
def register_workflows():
"""
Scans the workflows directory and registers them as MCP tools.
"""
global REGISTERED_CONFIGS
workflows_dir = Path(__file__).parent / "workflows"
# 1. Scan for valid configs
print(f"Scanning for workflows in {workflows_dir}...")
REGISTERED_CONFIGS = loader.scan_workflows(workflows_dir)
if not REGISTERED_CONFIGS:
print("No valid workflows found to register.")
return
# Register each config as a tool
for file_path, config in REGISTERED_CONFIGS.items():
try:
# Create the dynamic function
tool_func = factory.create_dynamic_tool(config, file_path)
# Register with FastMCP
# add_tool inspects the function signature and docstring
mcp.add_tool(tool_func)
print(f"Registered Tool: {config.name} (from {file_path.name})")
except Exception as e:
print(f"Failed to register tool for {file_path.name}: {e}")
# Register tools on module import (Essential for SSE/mcp run mode)
register_workflows()
if __name__ == "__main__":
print("Starting ComfyUI MCP Server...")
# Run the server
mcp.run()