-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcp_daz_server.py
More file actions
347 lines (303 loc) · 11.9 KB
/
mcp_daz_server.py
File metadata and controls
347 lines (303 loc) · 11.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import asyncio
import json
import logging
import os
import time
import subprocess
import sys
from uuid import uuid4
try:
import websockets
WEBSOCKETS_AVAILABLE = True
except ImportError:
WEBSOCKETS_AVAILABLE = False
"""
MCP DAZ Server — v1.3 (emergency protocol fix)
- JSON-RPC 2.0 over WebSocket
- Implements MCP control plane:
• initialize (FIXED)
• notifications/initialized (ADDED)
• tools/list
• tools/call
• resources/list (empty for now)
• prompts/list (empty for now)
- Tools exposed (snake_case to match Claude Desktop):
• load_scene(scene_path)
• set_pose(pose_path, figure_name[optional])
• render_scene(output_path, width[opt], height[opt])
- Backwards-compat: still accepts direct methods loadScene/setPose/render.
Config via ENV:
HOST=127.0.0.1
PORT=8765
DAZ_EXE=C:\\Program Files\\DAZ 3D\\DAZStudio4\\dazstudio.exe
DAZ_SCRIPT_PATH=C:\\Path\\To\\DazStudio\\Scripts
LOG_LEVEL=INFO (DEBUG/INFO/WARNING/ERROR)
"""
# === Config ===
HOST = os.getenv("HOST", "127.0.0.1")
PORT = int(os.getenv("PORT", "8765"))
DAZ_EXE = os.getenv("DAZ_EXE", r"C:\\Program Files\\DAZ 3D\\DAZStudio4\\dazstudio.exe")
DAZ_SCRIPT_PATH = os.getenv("DAZ_SCRIPT_PATH", r"C:\knosso\Daz\scripts")
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
# === Logging ===
# Configure logging to stderr to avoid interfering with stdio JSON-RPC communication
logging.basicConfig(
level=getattr(logging, LOG_LEVEL, logging.INFO),
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
stream=sys.stderr, # Use stderr instead of stdout for stdio mode
)
log = logging.getLogger("mcp-daz")
# === Tool Registry (for tools/list) ===
TOOLS = [
{
"name": "load_scene",
"description": "Load a DAZ Studio scene file",
"inputSchema": {
"type": "object",
"properties": {
"scene_path": {"type": "string", "description": "Path to the scene file to load"}
},
"required": ["scene_path"],
"additionalProperties": False,
},
},
{
"name": "set_pose",
"description": "Set a pose for the selected figure",
"inputSchema": {
"type": "object",
"properties": {
"pose_path": {"type": "string", "description": "Path to the pose file"},
"figure_name": {"type": "string", "description": "Name of the figure to apply pose to"},
},
"required": ["pose_path"],
"additionalProperties": False,
},
},
{
"name": "render_scene",
"description": "Render the current scene",
"inputSchema": {
"type": "object",
"properties": {
"output_path": {"type": "string", "description": "Path where to save the rendered image"},
"width": {"type": "integer", "description": "Render width in pixels"},
"height": {"type": "integer", "description": "Render height in pixels"},
},
"required": ["output_path"],
"additionalProperties": False,
},
},
{
"name": "read_scene",
"description": "Get list of items in the current DAZ scene",
"inputSchema": {"type": "object", "properties": {}},
},
{
"name": "list_content",
"description": "List available items in the DAZ Studio content library",
"inputSchema": {"type": "object", "properties": {}},
},
]
# === Config ===
CALL_TIMEOUT = int(os.getenv("CALL_TIMEOUT", "60")) # seconds; adjust as needed
# === Helpers ===
def _script_path(name: str):
return os.path.join(DAZ_SCRIPT_PATH, name)
def run_daz_script(script_name: str, *args):
"""
Launches DAZ script and returns payload with (rc, stdout, stderr, duration_ms).
Enforces CALL_TIMEOUT so the JSON-RPC call never hangs Claude.
"""
script = _script_path(script_name)
start = time.time()
cmd = [
DAZ_EXE,
"-noPrompt",
"-script", script,
]
for a in args:
cmd += ["-scriptArg", str(a)]
# Windows: hide console window
creationflags = 0x08000000 if os.name == "nt" else 0 # CREATE_NO_WINDOW
log.debug("Launching DAZ script: %s", {"script": script, "args": list(args), "cmd": cmd})
try:
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
creationflags=creationflags
)
out, err = proc.communicate(timeout=CALL_TIMEOUT)
rc = proc.returncode
except subprocess.TimeoutExpired:
# Ensure process doesn't linger; best effort kill:
try:
proc.kill()
except Exception:
pass
rc, out, err = 124, b"", b"timeout: DAZ script exceeded CALL_TIMEOUT"
except Exception as e:
rc, out, err = 1, b"", str(e).encode("utf-8")
duration_ms = int((time.time() - start) * 1000)
# Handle timeout case with specific JSON-RPC format
if rc == 124:
payload = {
"status": "error",
"reason": "timeout",
"timeout_s": CALL_TIMEOUT,
"stdout": out.decode("utf-8", "ignore").strip(),
"stderr": err.decode("utf-8", "ignore").strip(),
"duration_ms": duration_ms,
}
else:
payload = {
"status": "ok" if rc == 0 else "error",
"returncode": rc,
"stdout": out.decode("utf-8", "ignore").strip(),
"stderr": err.decode("utf-8", "ignore").strip(),
"duration_ms": duration_ms,
}
level = logging.INFO if rc == 0 else logging.ERROR
log.log(level, "DAZ script finished: %s", {"script": script_name, "rc": rc, "duration_ms": duration_ms})
return payload
# === JSON-RPC helpers ===
def rpc_result(req_id, result):
return json.dumps({"jsonrpc": "2.0", "id": req_id, "result": result})
def rpc_error(req_id, code, message):
return json.dumps({"jsonrpc": "2.0", "id": req_id, "error": {"code": code, "message": message}})
def rpc_notification(method, params=None):
msg = {"jsonrpc": "2.0", "method": method}
if params:
msg["params"] = params
return json.dumps(msg)
# === MCP method handlers ===
def handle_initialize(req_id, params):
"""Handle MCP initialize handshake"""
server_info = {
"name": "daz-studio-mcp",
"version": "1.3.0"
}
capabilities = {
"tools": {},
"resources": {},
"prompts": {}
}
protocol_version = params.get("protocolVersion", "2025-06-18")
result = {
"protocolVersion": protocol_version,
"capabilities": capabilities,
"serverInfo": server_info
}
return rpc_result(req_id, result)
def handle_tools_list(req_id):
return rpc_result(req_id, {"tools": TOOLS})
def handle_tools_call(req_id, params):
name = (params or {}).get("name")
args = (params or {}).get("arguments") or {}
if name == "load_scene":
scene_path = args.get("scene_path", "")
result = run_daz_script("load_scene.dsa", scene_path)
elif name == "set_pose":
pose_path = args.get("pose_path", "")
figure_name = args.get("figure_name", "")
result = run_daz_script("set_pose.dsa", pose_path, figure_name)
elif name == "render_scene":
output_path = args.get("output_path", "")
width = args.get("width", 800)
height = args.get("height", 600)
result = run_daz_script("render_scene.dsa", output_path, width, height)
elif name == "read_scene":
result = run_daz_script("read_scene.dsa")
elif name == "list_content":
result = run_daz_script("list_content.dsa")
else:
result = {"status": "error", "stderr": f"Unknown tool: {name}"}
# Normalize output
if result.get("status") == "ok":
payload = {"content": [{"type": "text", "text": json.dumps(result)}]}
else:
payload = {"error": {"message": result.get("stderr", "Unknown error"), "code": result.get("returncode", -1)}}
return rpc_result(req_id, payload)
async def handle_mcp_request(websocket, path):
log.info("Client connected.")
try:
async for message in websocket:
log.debug("Received: %s", message)
try:
req = json.loads(message)
req_id = req.get("id")
method = req.get("method")
params = req.get("params")
if method == "initialize":
response = handle_initialize(req_id, params or {})
elif method == "tools/list":
response = handle_tools_list(req_id)
elif method == "tools/call":
response = handle_tools_call(req_id, params)
elif method == "resources/list":
response = rpc_result(req_id, {"resources": []})
elif method == "prompts/list":
response = rpc_result(req_id, {"prompts": []})
else:
response = rpc_error(req_id, -32601, f"Method not found: {method}")
log.debug("Sending: %s", response)
await websocket.send(response)
except json.JSONDecodeError:
log.error("Invalid JSON received: %s", message)
await websocket.send(rpc_error(None, -32700, "Parse error"))
except Exception as e:
log.exception("Error handling request.")
await websocket.send(rpc_error(req_id, -32000, f"Server error: {e}"))
except websockets.exceptions.ConnectionClosedOK:
log.info("Client disconnected normally.")
except Exception as e:
log.exception("WebSocket error.")
async def main():
if not WEBSOCKETS_AVAILABLE:
log.error("websockets library not found. Please install it: pip install websockets")
sys.exit(1)
log.info(f"Starting MCP DAZ server on ws://{HOST}:{PORT}")
async with websockets.serve(handle_mcp_request, HOST, PORT):
await asyncio.Future() # Run forever
if __name__ == '__main__':
if '--stdio' in sys.argv:
log.info("MCP DAZ server started in stdio mode.")
log.info(f"DAZ_SCRIPT_PATH: {DAZ_SCRIPT_PATH}")
log.info(f"DAZ_EXE: {DAZ_EXE}")
while True:
try:
line = sys.stdin.readline()
if not line:
break
try:
req = json.loads(line.strip())
req_id = req.get("id")
method = req.get("method")
params = req.get("params")
if method == "initialize":
response = handle_initialize(req_id, params or {})
elif method == "tools/list":
response = handle_tools_list(req_id)
elif method == "tools/call":
response = handle_tools_call(req_id, params)
elif method == "resources/list":
response = rpc_result(req_id, {"resources": []})
elif method == "prompts/list":
response = rpc_result(req_id, {"prompts": []})
else:
response = rpc_error(req_id, -32601, f"Method not found: {method}")
sys.stdout.write(response + '\n')
sys.stdout.flush()
except json.JSONDecodeError:
log.error("Invalid JSON received: %s", line.strip())
sys.stdout.write(rpc_error(None, -32700, "Parse error") + '\n')
sys.stdout.flush()
except Exception as e:
log.exception("Error handling request in stdio mode.")
sys.stdout.write(rpc_error(req_id, -32000, f"Server error: {e}") + '\n')
sys.stdout.flush()
except Exception as e:
log.exception("Error in stdio loop: %s", e)
else:
asyncio.run(main())