Repository navigation
Expand file tree
/
Copy pathmcp_proxy.py
More file actions
551 lines (475 loc) · 21.2 KB
/
Copy pathmcp_proxy.py
File metadata and controls
551 lines (475 loc) · 21.2 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
"""Bridge a stdio MCP client to PSD2Live's Streamable HTTP endpoint.
Use PSD2Live's HTTP endpoint directly whenever the host supports Streamable
HTTP. This dependency-free bridge is a compatibility fallback for stdio-only
hosts. It keeps stdout reserved for line-delimited JSON-RPC messages.
"""
from __future__ import annotations
import base64
import json
import os
import sys
import time
import urllib.error
import urllib.parse
import urllib.request
import uuid
try:
import winreg
except ImportError: # pragma: no cover - only available on Windows
winreg = None
DEFAULT_PORT = 23871
REQUEST_TIMEOUT_SECONDS = float(os.environ.get("PSD2LIVE_MCP_TIMEOUT", "60"))
TOKEN_ENV = "PSD2LIVE_MCP_TOKEN"
TRANSIENT_HTTP_STATUSES = {408, 425, 429, 502, 503, 504}
SAFE_PROTOCOL_METHODS = {
"initialize",
"ping",
"tools/list",
"resources/list",
"resources/read",
"resources/templates/list",
"notifications/initialized",
"notifications/cancelled",
}
# Read-only tools, used until the server's tools/list arrives and replaces this set
# with every tool it marks read-only or idempotent.
SAFE_PSD2LIVE_TOOLS = {
"workspace_inspect",
"workspace_list_operations",
"workspace_get_operation",
"history_list",
"job_get",
"job_list",
}
def _decode_java_preferences_value(value: str) -> str:
"""Decode the slash escaping used by Java Preferences on Windows."""
result: list[str] = []
index = 0
while index < len(value):
if value[index] == "/" and index + 1 < len(value):
index += 1
result.append("/" if value[index] == "/" else value[index].upper())
else:
result.append(value[index])
index += 1
return "".join(result)
def _read_saved(name: str) -> str | None:
"""Read a value PSD2Live saved in its Java preferences node (Windows only)."""
if winreg is None:
return None
try:
with winreg.OpenKey(
winreg.HKEY_CURRENT_USER,
r"Software\JavaSoft\Prefs\io\github\psd2live\agent",
) as key:
value, _ = winreg.QueryValueEx(key, name)
return _decode_java_preferences_value(value)
except OSError:
return None
def get_token() -> str | None:
return os.environ.get(TOKEN_ENV) or _read_saved("agent_mcp_bearer_token")
def require_local_endpoint(endpoint: str) -> str:
"""The proxy sends the bearer token. It only talks to this machine."""
parsed = urllib.parse.urlparse(endpoint)
host = (parsed.hostname or "").lower()
if parsed.scheme != "http" or host not in {"127.0.0.1", "localhost", "::1"}:
raise SystemExit("PSD2LIVE_MCP_ENDPOINT must be an http URL on 127.0.0.1, localhost, or ::1")
return endpoint
def get_endpoint() -> str:
endpoint = os.environ.get("PSD2LIVE_MCP_ENDPOINT")
if endpoint:
return require_local_endpoint(endpoint)
port = _read_saved("agent_mcp_port")
return f"http://127.0.0.1:{port if port and port.isdigit() else DEFAULT_PORT}/mcp"
ENDPOINT = get_endpoint()
def _is_retry_safe(message: object) -> bool:
if not isinstance(message, dict):
return False
method = message.get("method")
if method in SAFE_PROTOCOL_METHODS:
return True
if method != "tools/call":
return False
params = message.get("params")
return isinstance(params, dict) and params.get("name") in SAFE_PSD2LIVE_TOOLS
def _build_headers(
token: str,
session_id: str | None,
protocol_version: str | None,
) -> dict[str, str]:
headers = {
"Accept": "application/json, text/event-stream",
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
if session_id:
headers["Mcp-Session-Id"] = session_id
if protocol_version:
headers["MCP-Protocol-Version"] = protocol_version
return headers
def _write_message(message: str) -> None:
message = message.strip()
if message:
sys.stdout.write(message + "\n")
sys.stdout.flush()
def _write_json_payload(payload: str) -> None:
payload = payload.strip()
if not payload:
return
try:
parsed = json.loads(payload)
except json.JSONDecodeError as error:
sys.stderr.write(f"PSD2Live MCP returned invalid JSON: {error}\n")
return
_learn_safe_tools(parsed)
_write_message(json.dumps(parsed, separators=(",", ":"), ensure_ascii=False))
def _learn_safe_tools(message: object) -> None:
"""Writes repeat their request ID, so the server marks them idempotent and a retry recovers the result."""
result = message.get("result") if isinstance(message, dict) else None
tools = result.get("tools") if isinstance(result, dict) else None
if not isinstance(tools, list):
return
for tool in tools:
if not isinstance(tool, dict) or not isinstance(tool.get("name"), str):
continue
hints = tool.get("annotations")
if isinstance(hints, dict) and (hints.get("readOnlyHint") or hints.get("idempotentHint")):
SAFE_PSD2LIVE_TOOLS.add(tool["name"])
else:
SAFE_PSD2LIVE_TOOLS.discard(tool["name"])
def _write_http_body(body: str, content_type: str) -> None:
if "text/event-stream" not in content_type.lower():
_write_json_payload(body)
return
# Streamable HTTP may return one or more SSE events. A stdio client expects
# each JSON-RPC message as exactly one line without SSE framing.
event_data: list[str] = []
for line in body.splitlines() + [""]:
if line.startswith("data:"):
event_data.append(line[5:].lstrip())
elif not line and event_data:
_write_json_payload("\n".join(event_data))
event_data.clear()
def _write_rpc_error(request_id: object, message: str) -> None:
if request_id is None:
return
_write_message(
json.dumps(
{
"jsonrpc": "2.0",
"id": request_id,
"error": {"code": -32000, "message": message},
},
separators=(",", ":"),
ensure_ascii=False,
)
)
def _retry_delay(error: urllib.error.HTTPError | None = None) -> float:
if error is not None:
retry_after = error.headers.get("Retry-After")
if retry_after:
try:
return min(max(float(retry_after), 0.0), 2.0)
except ValueError:
pass
return 0.2
def _close_session(
session_id: str | None,
token: str,
protocol_version: str | None,
) -> None:
if not session_id:
return
request = urllib.request.Request(
ENDPOINT,
headers=_build_headers(token, session_id, protocol_version),
method="DELETE",
)
try:
with urllib.request.urlopen(request, timeout=min(REQUEST_TIMEOUT_SECONDS, 2.0)):
pass
except (urllib.error.HTTPError, urllib.error.URLError, TimeoutError, OSError):
# Session cleanup is best effort during process shutdown.
pass
def main() -> int:
token = get_token()
if not token:
sys.stderr.write(
"PSD2Live MCP token was not found. Start PSD2Live once or set "
f"{TOKEN_ENV} for this process.\n"
)
return 2
session_id: str | None = None
protocol_version: str | None = None
try:
for raw_line in sys.stdin:
line = raw_line.strip()
if not line:
continue
try:
message = json.loads(line)
except json.JSONDecodeError as error:
sys.stderr.write(f"Invalid JSON-RPC input: {error}\n")
continue
request_id = message.get("id") if isinstance(message, dict) else None
method = message.get("method") if isinstance(message, dict) else None
if method == "initialize":
# A fresh initialize starts a fresh HTTP session. Reusing a stale
# session header causes otherwise valid reconnects to fail.
session_id = None
params = message.get("params") or {}
if isinstance(params, dict):
value = params.get("protocolVersion")
if isinstance(value, str):
protocol_version = value
retry_safe = _is_retry_safe(message)
transient_retried = False
auth_refreshed = False
while True:
request = urllib.request.Request(
ENDPOINT,
data=line.encode("utf-8"),
headers=_build_headers(token, session_id, protocol_version),
method="POST",
)
try:
with urllib.request.urlopen(
request,
timeout=REQUEST_TIMEOUT_SECONDS,
) as response:
session_id = response.headers.get("Mcp-Session-Id", session_id)
body = response.read().decode("utf-8")
_write_http_body(body, response.headers.get("Content-Type", ""))
break
except urllib.error.HTTPError as error:
detail = error.read().decode("utf-8", errors="replace").strip()
# Authentication failed before a tool can run. Refresh once,
# and retry only when the credential actually changed.
if error.code in {401, 403} and not auth_refreshed:
refreshed_token = get_token()
auth_refreshed = True
if refreshed_token and refreshed_token != token:
token = refreshed_token
continue
if error.code == 404 and session_id and method != "initialize":
session_id = None
summary = (
"PSD2Live MCP session expired. Initialize a new MCP session, "
"then call workspace_inspect before resuming."
)
elif (
error.code in TRANSIENT_HTTP_STATUSES
and retry_safe
and not transient_retried
):
transient_retried = True
time.sleep(_retry_delay(error))
continue
else:
summary = f"PSD2Live MCP returned HTTP {error.code}"
if detail:
summary += f": {detail}"
if not retry_safe and error.code in TRANSIENT_HTTP_STATUSES:
summary += (
". The write commit state may be unknown; reconnect and check "
"workspace_inspect/history_list before retrying"
)
sys.stderr.write(summary + "\n")
_write_rpc_error(request_id, summary)
if method == "initialize":
return 1
break
except (urllib.error.URLError, TimeoutError, OSError) as error:
if retry_safe and not transient_retried:
transient_retried = True
time.sleep(_retry_delay())
continue
summary = f"Cannot reach PSD2Live MCP at {ENDPOINT}: {error}"
if not retry_safe:
summary += (
". The write commit state is unknown; reconnect and check "
"workspace_inspect/history_list before retrying"
)
sys.stderr.write(summary + "\n")
_write_rpc_error(request_id, summary)
if method == "initialize":
return 1
break
finally:
_close_session(session_id, token, protocol_version)
return 0
class McpError(RuntimeError):
"""An operation that answered ok false, or a JSON-RPC or HTTP failure."""
def __init__(self, message: str, detail: object = None):
super().__init__(message)
self.detail = detail
class Client:
"""Call PSD2Live operations from a script, over one Streamable HTTP session.
Works with both tool profiles: an operation listed as its own tool is called directly, any other through
workspace_call. Background operations are waited for. Images an operation returns come back as PNG bytes.
with Client() as p2l:
state = p2l.state()
p2l.edit("material_crop", id="ref", canvas_rect={...}, new_id="head")
data, images = p2l.call("view_render_model", {"scope": "canvas"})
"""
def __init__(self, endpoint: str | None = None, token: str | None = None, timeout: float = REQUEST_TIMEOUT_SECONDS):
self.endpoint = require_local_endpoint(endpoint) if endpoint else ENDPOINT
self.token = token or get_token()
if not self.token:
raise McpError(f"PSD2Live MCP token was not found. Start PSD2Live once or set {TOKEN_ENV}.")
self.timeout = timeout
self.session_id: str | None = None
self.protocol_version = "2025-03-26"
self._ids = 0
self._tools: dict[str, dict] | None = None
def __enter__(self) -> "Client":
return self
def __exit__(self, *_: object) -> None:
self.close()
def close(self) -> None:
_close_session(self.session_id, self.token, self.protocol_version)
self.session_id = None
def _post(self, message: dict) -> dict | None:
request = urllib.request.Request(
self.endpoint,
data=json.dumps(message).encode("utf-8"),
headers=_build_headers(self.token, self.session_id, self.protocol_version),
method="POST",
)
try:
with urllib.request.urlopen(request, timeout=self.timeout) as response:
self.session_id = response.headers.get("Mcp-Session-Id", self.session_id)
body = response.read().decode("utf-8")
content_type = response.headers.get("Content-Type", "")
except urllib.error.HTTPError as error:
raise McpError(f"PSD2Live MCP returned HTTP {error.code}: {error.read().decode('utf-8', 'replace').strip()}") from error
except (urllib.error.URLError, TimeoutError, OSError) as error:
raise McpError(f"Cannot reach PSD2Live MCP at {self.endpoint}: {error}") from error
if not body.strip():
return None
if "text/event-stream" not in content_type.lower():
return json.loads(body)
reply = None
for line in body.splitlines():
if line.startswith("data:") and line[5:].strip():
parsed = json.loads(line[5:].strip())
if isinstance(parsed, dict) and "id" in parsed:
reply = parsed
return reply
def _rpc(self, method: str, params: dict) -> dict:
if self.session_id is None and method != "initialize":
self._initialize()
self._ids += 1
reply = self._post({"jsonrpc": "2.0", "id": self._ids, "method": method, "params": params})
if reply is None:
raise McpError(f"No reply to {method}")
if "error" in reply:
raise McpError(f"{method} failed: {reply['error'].get('message')}", reply["error"])
return reply["result"]
def _initialize(self) -> None:
self._rpc("initialize", {"protocolVersion": self.protocol_version, "capabilities": {},
"clientInfo": {"name": "psd2live-script", "version": "1"}})
self._post({"jsonrpc": "2.0", "method": "notifications/initialized"})
def tools(self) -> set[str]:
return set(self._schemas())
def _schemas(self) -> dict[str, dict]:
"""Each tool's request properties, by tool name."""
if self._tools is None:
self._tools = {tool["name"]: tool.get("inputSchema", {}).get("properties", {}).get("request", {}).get("properties", {})
for tool in self._rpc("tools/list", {}).get("tools", [])}
return self._tools
def _tool(self, name: str, arguments: dict) -> tuple[dict, list[bytes]]:
result = self._rpc("tools/call", {"name": name, "arguments": arguments})
images = [base64.b64decode(part["data"]) for part in result.get("content", []) if part.get("type") == "image"]
data = result.get("structuredContent")
if data is None:
texts = [part["text"] for part in result.get("content", []) if part.get("type") == "text"]
try:
data = json.loads(texts[0]) if texts else {}
except json.JSONDecodeError:
data = {"ok": not result.get("isError"), "data": {"text": "\n".join(texts)}}
if result.get("isError") or not data.get("ok", True):
error = data.get("error", data)
raise McpError(f"{name}: {error.get('message', error) if isinstance(error, dict) else error}", error)
return data, images
def call(self, operation: str, request: dict | None = None, wait_ms: int | None = None) -> tuple[dict, list[bytes]]:
"""Run one operation; returns its data (a finished job's result for backgro
8C42
und operations) and images.
wait_ms is for background operations only; they are waited for until they end either way."""
request = dict(request or {})
if operation in self.tools():
fields = self._schemas()[operation]
if "workspace_call" not in self.tools():
# The full profile asks edits for their identity; the core profile fills it in.
if "request_id" in fields:
request.setdefault("request_id", f"script-{uuid.uuid4().hex}")
if "project_id" in fields and "project_id" not in request:
request["project_id"] = self.project()["project_id"]
data, images = self._tool(operation, {"request": request})
else:
arguments = {"operation": operation, "request": request}
if wait_ms is not None:
arguments["wait_ms"] = wait_ms
data, images = self._tool("workspace_call", arguments)
payload = data.get("data", data)
while isinstance(payload, dict) and str(payload.get("id", "")).startswith("job-") and "terminal" in payload and not payload["terminal"]:
arguments = {"request": {"id": payload["id"], "timeout_ms": 30000}}
tool = "job_wait"
if tool not in self.tools():
tool = "workspace_call"
arguments["operation"] = "job_wait"
data, more = self._tool(tool, arguments)
images += more
payload = data.get("data", data)
if isinstance(payload, dict) and payload.get("terminal") and str(payload.get("id", "")).startswith("job-"):
if payload.get("status") != "completed":
raise McpError(f"{operation} {payload.get('status')}: {payload.get('error', {}).get('message', '')}", payload)
payload = payload.get("result", payload)
return payload, images
def project(self) -> dict:
return self.call("workspace_inspect", {"scope": "project"})[0]
def state(self) -> str:
return self.project()["state"]
def edit(self, operation: str, **request: object) -> dict:
"""A document edit with the current state filled in; returns the result."""
request.setdefault("state", self.state())
return self.call(operation, request)[0]
def _cli(arguments: list[str]) -> int:
"""`call <operation> [json | @file] [--edit] [--out DIR]` and `operations`, for shells and scripts."""
out = None
if "--out" in arguments:
index = arguments.index("--out")
out = arguments[index + 1]
del arguments[index:index + 2]
edit = "--edit" in arguments
if edit:
arguments.remove("--edit")
with Client() as client:
if arguments[0] == "operations":
print("\n".join(sorted(client.tools())))
return 0
if len(arguments) < 2:
sys.stderr.write("Usage: mcp_proxy.py call <operation> [json | @file] [--edit] [--out DIR]\n")
return 2
raw = arguments[2] if len(arguments) > 2 else "{}"
request = json.load(open(raw[1:], encoding="utf-8")) if raw.startswith("@") else json.loads(raw)
try:
if edit:
request.setdefault("state", client.state())
data, images = client.call(arguments[1], request)
except McpError as error:
sys.stderr.write(f"{error}\n")
print(json.dumps(error.detail, ensure_ascii=False, indent=1))
return 1
print(json.dumps(data, ensure_ascii=False, indent=1))
for index, png in enumerate(images):
directory = out or os.getcwd()
os.makedirs(directory, exist_ok=True)
path = os.path.join(directory, f"{arguments[1]}_{int(time.time() * 1000)}_{index}.png")
with open(path, "wb") as file:
file.write(png)
print(f"image {path}")
return 0
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] in {"call", "operations"}:
raise SystemExit(_cli(sys.argv[1:]))
raise SystemExit(main())