45 lines
2.2 KiB
Python
45 lines
2.2 KiB
Python
# Minimal stdio client for Roblox Studio's built-in MCP server (StudioMCP.exe).
|
|
# Usage: python rbx.py <tool> <args.json | inline-json> -> prints text result
|
|
import json, os, subprocess, sys
|
|
|
|
BAT = os.path.join(os.environ["LOCALAPPDATA"], "Roblox", "mcp.bat")
|
|
tool = sys.argv[1]
|
|
raw = sys.argv[2] if len(sys.argv) > 2 else "{}"
|
|
if raw.endswith(".luau"): # python rbx.py execute_luau file.luau -> runs the file's code in Edit mode
|
|
args = {"code": open(raw, encoding="utf-8-sig").read(), "datamodel_type": sys.argv[3] if len(sys.argv) > 3 else "Edit"}
|
|
else:
|
|
args = json.load(open(raw, encoding="utf-8-sig")) if os.path.isfile(raw) else json.loads(raw)
|
|
|
|
p = subprocess.Popen(["cmd", "/c", BAT], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
|
|
def send(m): p.stdin.write((json.dumps(m) + "\n").encode()); p.stdin.flush()
|
|
def recv(i):
|
|
while True:
|
|
line = p.stdout.readline()
|
|
if not line: raise SystemExit("MCP server closed")
|
|
try: m = json.loads(line)
|
|
except ValueError: continue
|
|
if m.get("id") == i: return m
|
|
|
|
send({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-03-26", "capabilities": {},
|
|
"clientInfo": {"name": "cli", "version": "1"}}})
|
|
recv(1)
|
|
send({"jsonrpc": "2.0", "method": "notifications/initialized"})
|
|
import time
|
|
for n in range(15): # Studio connects to a fresh server process after a moment: retry until it does
|
|
send({"jsonrpc": "2.0", "id": 10 + n, "method": "tools/call", "params": {"name": tool, "arguments": args}})
|
|
r = recv(10 + n)
|
|
if "No Roblox Studio instances are connected" not in json.dumps(r): break
|
|
time.sleep(1)
|
|
p.stdin.close(); p.kill()
|
|
out = r.get("result", r.get("error"))
|
|
if isinstance(out, dict) and "content" in out:
|
|
for k, c in enumerate(out["content"]):
|
|
if c.get("type") == "image": # save screenshots next to this script
|
|
import base64
|
|
fn = os.path.join(os.path.dirname(os.path.abspath(__file__)), "capture_%d.png" % k)
|
|
open(fn, "wb").write(base64.b64decode(c["data"])); print("[image saved]", fn)
|
|
else:
|
|
print(c.get("text", "[%s]" % c.get("type")))
|
|
else:
|
|
print(json.dumps(out, ensure_ascii=False, indent=1))
|