@el4cteo/rbx-studio-mcp 0.7.2 → 0.7.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ --!strict
2
+ -- One temporary client relay lifecycle for input and execution.
3
+ local Players = game:GetService("Players")
4
+ local RunService = game:GetService("RunService")
5
+ local Dispatch = require(script.Parent.Dispatch)
6
+ local ClientRelay = {}
7
+ function ClientRelay.playerFor(name: string?): Player
8
+ if not RunService:IsRunning() then
9
+ Dispatch.fail("NOT_RUNNING", "Client access needs a running playtest; this is an edit session.")
10
+ end
11
+ if not RunService:IsServer() then
12
+ Dispatch.fail("WRONG_CONTEXT", "Address the playtest server's studioId for client access.")
13
+ end
14
+ local players = Players:GetPlayers()
15
+ if #players == 0 then
16
+ Dispatch.fail(
17
+ "NO_PLAYER",
18
+ "No player is in this session.",
19
+ "Client access needs a running playtest. Use `playtest op=\"play\"`, "
20
+ .. "then address this at the playtest's studioId."
21
+ )
22
+ end
23
+ if name == nil or name == "" then
24
+ if #players > 1 then
25
+ Dispatch.fail(
26
+ "AMBIGUOUS_PLAYER",
27
+ string.format("%d players are in this session.", #players),
28
+ "Name one with `player`."
29
+ )
30
+ end
31
+ return players[1]
32
+ end
33
+ for _, player in players do
34
+ if player.Name == name then
35
+ return player
36
+ end
37
+ end
38
+ Dispatch.fail("NO_PLAYER", string.format("No player named %q is in this session.", name))
39
+ return players[1]
40
+ end
41
+
42
+
43
+ function ClientRelay.run(player: Player, name: string, source: string, attributes: { [string]: any }, timeout: number, prepare: ((LocalScript) -> ())?, ready: (() -> ())?): { [string]: any }
44
+ local gui = player:FindFirstChildOfClass("PlayerGui")
45
+ if not gui then Dispatch.fail("NO_PLAYER", player.Name .. " has no PlayerGui yet.") end
46
+ local relay = Instance.new("LocalScript")
47
+ local connection: RBXScriptConnection? = nil
48
+ local answer: any = nil
49
+ local ok, failure = pcall(function()
50
+ relay.Name = name
51
+ relay:SetAttribute("MCPClientRelay", true)
52
+ relay.Source = source
53
+ for key, value in attributes do relay:SetAttribute(key, value) end
54
+ local remote = Instance.new("RemoteEvent")
55
+ remote.Name = "Report"
56
+ remote.Parent = relay
57
+ if prepare then prepare(relay) end
58
+ local started = false
59
+ connection = remote.OnServerEvent:Connect(function(from, payload)
60
+ if from ~= player or answer ~= nil or typeof(payload) ~= "table" then return end
61
+ -- A capture can wait until its client relay is ready before starting
62
+ -- the server window. Existing single-response relays are unchanged.
63
+ if ready and payload.ready == true then
64
+ if started then return end
65
+ started = true
66
+ local prepared, reason = pcall(ready)
67
+ if not prepared then
68
+ answer = { ok = false, reason = tostring(reason) }
69
+ return
70
+ end
71
+ remote:FireClient(player, true)
72
+ else
73
+ answer = payload
74
+ end
75
+ end)
76
+ relay.Parent = gui
77
+ local deadline = os.clock() + timeout
78
+ while answer == nil and os.clock() < deadline and player.Parent == Players do task.wait(0.05) end
79
+ end)
80
+ if connection then connection:Disconnect() end
81
+ relay:Destroy()
82
+ if not ok then Dispatch.fail("CLIENT_FAILED", tostring(failure)) end
83
+ if answer == nil then
84
+ Dispatch.fail("NO_ACK", string.format("The client did not respond within %gs (or disconnected).", timeout), "The temporary relay was removed; code may have made changes before timing out.")
85
+ end
86
+ return answer
87
+ end
88
+ return ClientRelay
@@ -22,9 +22,11 @@
22
22
 
23
23
  local ScriptEditorService = game:GetService("ScriptEditorService")
24
24
  local Selection = game:GetService("Selection")
25
+ local RunService = game:GetService("RunService")
25
26
  local ServerStorage = game:GetService("ServerStorage")
26
27
 
27
28
  local Config = require(script.Parent.Config)
29
+ local Playtest = require(script.Parent.handlers.Playtest)
28
30
  local Console = require(script.Parent.Console)
29
31
  local Net = require(script.Parent.Net)
30
32
  local Secret = require(script.Parent.Secret)
@@ -251,6 +253,30 @@ define({
251
253
  end,
252
254
  })
253
255
 
256
+ define({
257
+ name = "playtests",
258
+ usage = "playtests [on|off]",
259
+ summary = "allow or block MCP playtests; ON permits, OFF enforces",
260
+ run = function(args)
261
+ local wanted = args[1]
262
+ if #args > 1 or (wanted ~= nil and wanted ~= "on" and wanted ~= "off") then
263
+ Console.log("error", "usage: playtests [on|off]")
264
+ return
265
+ end
266
+ if wanted ~= nil then Playtest.setAllowed(wanted == "on") end
267
+ Console.log("info", if Playtest.isAllowed() then "Playtests: ON" else "Playtests: OFF")
268
+ if wanted == "off" then
269
+ -- Persist the lock BEFORE stopping, because stopping can destroy this VM.
270
+ -- The bridge sees other connected test sessions; the client view cannot
271
+ -- make HTTP calls and stops its own session locally instead.
272
+ if not (RunService:IsClient() and not RunService:IsServer()) then
273
+ task.spawn(function() remote("playtests", { "off" }, "playtests off") end)
274
+ end
275
+ Playtest.stopLocal()
276
+ end
277
+ end,
278
+ })
279
+
254
280
  define({
255
281
  name = "autoopen",
256
282
  usage = "autoopen [on|off]",
@@ -9,7 +9,7 @@
9
9
 
10
10
  local Config = {}
11
11
 
12
- Config.PLUGIN_VERSION = "0.7.2"
12
+ Config.PLUGIN_VERSION = "0.7.5"
13
13
 
14
14
  -- Fingerprint of plugin/src, stamped in by scripts/build-plugin.mjs. The server
15
15
  -- computes the same hash from its own copy of the sources and compares, so a
@@ -0,0 +1,161 @@
1
+ --!strict
2
+ -- Shared bounded execution results in the VM that calls run.
3
+ local LogService = game:GetService("LogService")
4
+ local Serialize = require(script.Parent.Serialize)
5
+ -- A chunk that prints in a loop would otherwise return a response no context
6
+ -- window can hold.
7
+ local MAX_OUTPUT_LINES = 200
8
+ local MAX_RETURN_VALUES = 10
9
+
10
+ -- Returned tables are walked rather than counted, but not without limit: a
11
+ -- chunk that returns a whole config tree, or one with a cycle in it, must not be
12
+ -- able to produce a response nothing can read.
13
+ local MAX_TABLE_ENTRIES = 50
14
+ local MAX_TABLE_DEPTH = 4
15
+
16
+ local function describe(value: any, depth: number?, seen: { [any]: boolean }?): any
17
+ local level = depth or 0
18
+ local visited = seen or {}
19
+ local kind = typeof(value)
20
+
21
+ if kind == "function" or kind == "thread" then
22
+ return string.format("<%s>", kind)
23
+ end
24
+ if kind ~= "table" then
25
+ return Serialize.value(value)
26
+ end
27
+
28
+ local source = value :: { [any]: any }
29
+ -- Named separately from the depth cap, because they mean opposite things to
30
+ -- a reader. The depth cap says "there is more below this"; a cycle says
31
+ -- "this is the table you are already inside". Reporting the second as the
32
+ -- first printed `t.self.self.self` as three distinct nested tables, which
33
+ -- reads as real structure that does not exist.
34
+ if visited[source] then
35
+ return "<circular reference>"
36
+ end
37
+ if level >= MAX_TABLE_DEPTH then
38
+ return "<nested table>"
39
+ end
40
+ visited[source] = true
41
+
42
+ local count = 0
43
+ for _ in source do
44
+ count += 1
45
+ end
46
+
47
+ -- Arrays keep their shape so they read as lists on the other side; anything
48
+ -- with non-sequential keys becomes a map with stringified keys.
49
+ if count == #source then
50
+ local list: { any } = {}
51
+ for index, item in ipairs(source) do
52
+ if index > MAX_TABLE_ENTRIES then
53
+ table.insert(list, string.format("<%d more>", count - MAX_TABLE_ENTRIES))
54
+ break
55
+ end
56
+ table.insert(list, describe(item, level + 1, visited))
57
+ end
58
+ -- Cleared on the way out so the same table appearing twice side by side
59
+ -- still renders twice; only a table containing itself is a cycle.
60
+ visited[source] = nil
61
+ return list
62
+ end
63
+
64
+ local map: { [string]: any } = {}
65
+ local shown = 0
66
+ for key, item in source do
67
+ shown += 1
68
+ if shown > MAX_TABLE_ENTRIES then
69
+ map["..."] = string.format("<%d more>", count - MAX_TABLE_ENTRIES)
70
+ break
71
+ end
72
+ map[tostring(key)] = describe(item, level + 1, visited)
73
+ end
74
+ visited[source] = nil
75
+ return map
76
+ end
77
+
78
+ local Runtime = {}
79
+ Runtime.describe = describe
80
+ function Runtime.run(chunk: () -> ...any, timeout: number?): { [string]: any }
81
+ -- Captured rather than inferred: a chunk's prints are usually the point, and
82
+ -- reading them back from the log afterwards would also pick up whatever else
83
+ -- the place logged in the meantime.
84
+ local output: { { [string]: any } } = {}
85
+ local connection = LogService.MessageOut:Connect(function(message, messageType)
86
+ if #output < MAX_OUTPUT_LINES then
87
+ table.insert(output, {
88
+ message = message,
89
+ level = if messageType == Enum.MessageType.MessageError
90
+ then "error"
91
+ elseif messageType == Enum.MessageType.MessageWarning then "warning"
92
+ else "print",
93
+ })
94
+ end
95
+ end)
96
+
97
+ local started = os.clock()
98
+ local results: any = nil
99
+ if timeout then
100
+ local worker = task.spawn(function() results = table.pack(pcall(chunk :: any)) end)
101
+ local deadline = started + timeout
102
+ while results == nil and os.clock() < deadline do task.wait() end
103
+ if results == nil then
104
+ task.cancel(worker)
105
+ results = table.pack(false, "CLIENT_TIMEOUT: execution exceeded " .. tostring(timeout) .. "s")
106
+ end
107
+ else
108
+ results = table.pack(pcall(chunk :: () -> ...any))
109
+ end
110
+ local elapsed = os.clock() - started
111
+
112
+ --[[
113
+ MessageOut is deferred: `print` returns before the event fires, so
114
+ disconnecting straight after the chunk finished captured nothing at all.
115
+ A short settle picks the lines up.
116
+
117
+ It is bounded and stops as soon as the flow stops, rather than waiting a
118
+ fixed period, so a chunk that logged nothing costs almost no time and one
119
+ that logged plenty is not truncated.
120
+ ]]
121
+ local settleDeadline = os.clock() + 0.5
122
+ local seen = #output
123
+ local quietFrames = 0
124
+ while os.clock() < settleDeadline and quietFrames < 3 do
125
+ task.wait()
126
+ if #output == seen then
127
+ quietFrames += 1
128
+ else
129
+ seen = #output
130
+ quietFrames = 0
131
+ end
132
+ end
133
+
134
+ connection:Disconnect()
135
+
136
+ if not results[1] then
137
+ return {
138
+ ok = false,
139
+ error = tostring(results[2]),
140
+ output = output,
141
+ milliseconds = math.floor(elapsed * 1000 + 0.5),
142
+ }
143
+ end
144
+
145
+ local returned: { any } = {}
146
+ for index = 2, math.min(results.n, MAX_RETURN_VALUES + 1) do
147
+ -- A nil return is written out rather than dropped. `table.insert` of nil
148
+ -- does nothing, so `return a, nil, b` came back as two values and every
149
+ -- one after the nil shifted into the wrong position.
150
+ local value = results[index]
151
+ table.insert(returned, if value == nil then "nil" else describe(value))
152
+ end
153
+
154
+ return {
155
+ ok = true,
156
+ returned = returned,
157
+ output = output,
158
+ milliseconds = math.floor(elapsed * 1000 + 0.5),
159
+ }
160
+ end
161
+ return Runtime
@@ -288,6 +288,9 @@ local DESCRIBERS: { [string]: Describer } = {
288
288
  local name = leaf(params.path)
289
289
  return if name ~= nil then "Clear breakpoints in " .. name else "Clear all breakpoints"
290
290
  end,
291
+ ["debug.remotes"] = function()
292
+ return "Observe playtest RemoteEvent traffic"
293
+ end,
291
294
  ["debug.snapshots"] = function()
292
295
  return "Read what the breakpoints caught"
293
296
  end,
@@ -0,0 +1,143 @@
1
+ --!strict
2
+ -- Passive, bounded RemoteEvent observations shared by the server and client.
3
+ local Paths = require(script.Parent.Paths)
4
+ local Trace = {}
5
+ local MAX_EVENTS = 1000 -- per direction; disconnect at the cap, never keep raw payloads
6
+ local MAX_REMOTES = 128
7
+ local MAX_ROWS = 20
8
+ local MAX_VISITS = 10000
9
+
10
+ local function brief(value: string, limit: number): string
11
+ local clipped = string.sub(value, 1, limit)
12
+ -- ASCII keeps invalid UTF-8, controls and JSON escaping from expanding output.
13
+ clipped = string.gsub(clipped, "[^ -~]", "?")
14
+ return clipped .. (if #value > limit then "..." else "")
15
+ end
16
+
17
+ local function pathOf(remote: Instance): string
18
+ local path = Paths.of(remote)
19
+ if #path <= 256 then return path end
20
+ -- Keep Unicode instance names valid at the byte boundary.
21
+ local ok, boundary = pcall(utf8.offset, path, 0, 257)
22
+ return string.sub(path, 1, if ok and boundary then boundary - 1 else 256) .. "..."
23
+ end
24
+
25
+ local function shape(value: any, depth: number): string
26
+ local kind = typeof(value)
27
+ if kind == "string" then return "string:" .. string.format("%q", brief(value, 32)) end
28
+ if kind == "number" or kind == "boolean" or kind == "nil" then return kind .. ":" .. tostring(value) end
29
+ if kind == "Instance" then return "Instance<" .. value.ClassName .. ">" end
30
+ if kind ~= "table" then return kind end
31
+ if depth >= 2 then return "table{...}" end
32
+ local parts = {}
33
+ local key, item = next(value)
34
+ for _ = 1, 4 do
35
+ if key == nil then break end
36
+ local label = if typeof(key) == "string" then brief(key, 20) else typeof(key)
37
+ table.insert(parts, label .. "=" .. shape(item, depth + 1))
38
+ key, item = next(value, key)
39
+ end
40
+ if key ~= nil then table.insert(parts, "...") end
41
+ return brief("table{" .. table.concat(parts, ",") .. "}", 160)
42
+ end
43
+
44
+ function Trace.arguments(...: any): string
45
+ local parts = {}
46
+ local count = select("#", ...)
47
+ for index = 1, math.min(count, 6) do
48
+ table.insert(parts, shape(select(index, ...), 0))
49
+ end
50
+ if count > 6 then table.insert(parts, "...") end
51
+ return brief("(" .. table.concat(parts, ", ") .. ")", 192)
52
+ end
53
+
54
+ -- Returns an idempotent stop function; its deadline also cleans up if the caller fails.
55
+ function Trace.start(root: Instance, player: Player, client: boolean, seconds: number, excluded: Instance?): () -> { [string]: any }
56
+ local connections: { RBXScriptConnection } = {}
57
+ local watched: { [Instance]: boolean } = {}
58
+ local rows: { any } = {}
59
+ local byRemote: { [Instance]: any } = {}
60
+ local events, remotes, visits = 0, 0, 0
61
+ local omitted, scanLimited, eventLimited = false, false, false
62
+ local skipped = 0
63
+ local active = true
64
+ local started = os.clock()
65
+ local elapsed = 0
66
+ local timer: thread? = nil
67
+ local function stop(): { [string]: any }
68
+ if active then
69
+ active = false
70
+ elapsed = math.max(os.clock() - started, 0.001)
71
+ for _, connection in connections do connection:Disconnect() end
72
+ if timer then task.cancel(timer); timer = nil end
73
+ for _, row in rows do row.callsPerSecond = math.round(row.count / elapsed * 100) / 100 end
74
+ end
75
+ return { items = rows, events = events, seconds = elapsed, watched = remotes,
76
+ eventLimitReached = eventLimited, scanLimitReached = scanLimited, rowsOmitted = omitted, skipped = skipped }
77
+ end
78
+ local function record(remote: Instance, ...: any)
79
+ if not active or (remote ~= root and not remote:IsDescendantOf(root)) then return end
80
+ events += 1
81
+ local row = byRemote[remote]
82
+ if row == nil and #rows < MAX_ROWS then
83
+ row = { path = pathOf(remote), direction = if client then "server -> client" else "client -> server",
84
+ player = brief(player.Name, 64), count = 0, samples = {} }
85
+ byRemote[remote] = row
86
+ table.insert(rows, row)
87
+ end
88
+ if row then
89
+ row.count += 1
90
+ if #row.samples < 2 then
91
+ local sample = Trace.arguments(...)
92
+ if row.samples[1] ~= sample then table.insert(row.samples, sample) end
93
+ end
94
+ else omitted = true end
95
+ if events >= MAX_EVENTS then eventLimited = true; stop() end
96
+ end
97
+ local function attach(remote: Instance)
98
+ if not active or watched[remote] or not remote:IsA("RemoteEvent") then return end
99
+ if excluded and (remote == excluded or remote:IsDescendantOf(excluded)) then return end
100
+ -- Ignore all temporary bridge traffic, including overlapping MCP calls.
101
+ local ancestor: Instance? = remote.Parent
102
+ while ancestor and ancestor ~= game do
103
+ if ancestor:GetAttribute("MCPClientRelay") == true then return end
104
+ ancestor = ancestor.Parent
105
+ end
106
+ if remotes >= MAX_REMOTES then scanLimited = true; return end
107
+ watched[remote] = true
108
+ remotes += 1
109
+ if client then
110
+ table.insert(connections, (remote :: RemoteEvent).OnClientEvent:Connect(function(...) record(remote, ...) end))
111
+ else
112
+ table.insert(connections, (remote :: RemoteEvent).OnServerEvent:Connect(function(from, ...)
113
+ if from == player then record(remote, ...) end
114
+ end))
115
+ end
116
+ end
117
+ local ok, failure = pcall(function()
118
+ -- Subscribe first, then scan so remotes created during discovery are not missed.
119
+ table.insert(connections, root.DescendantAdded:Connect(function(object)
120
+ if visits >= MAX_VISITS then scanLimited = true; return end
121
+ visits += 1
122
+ local attached = pcall(attach, object)
123
+ if not attached then skipped += 1 end
124
+ end))
125
+ local pending = { root }
126
+ while #pending > 0 and visits < MAX_VISITS do
127
+ local object = table.remove(pending) :: Instance
128
+ visits += 1
129
+ attach(object)
130
+ local readable, children = pcall(object.GetChildren, object)
131
+ if not readable then skipped += 1; continue end
132
+ for _, child in children do
133
+ if visits + #pending >= MAX_VISITS then scanLimited = true; break end
134
+ table.insert(pending, child)
135
+ end
136
+ end
137
+ if #pending > 0 then scanLimited = true end
138
+ timer = task.delay(seconds, function() timer = nil; stop() end)
139
+ end)
140
+ if not ok then stop(); error(failure, 0) end
141
+ return stop
142
+ end
143
+ return Trace