Execute Lua
lua_execRun Lua code in a GMod menu, client, or server realm and get back serialized return values, captured output, timing, and traceback errors for testing or debugging game state.
Instructions
Executes Lua code in a realm under xpcall + debug.traceback, at full speed (the LuaJIT compiler is left on; no time limit unless budget). Returns all serialized return values (values, n), captured print/Msg/MsgN/MsgC (output), duration_ms, chunk_bytes, chunkname and the realm's generation (it changes on every map load: compare it with map_load's result to know which state ran the code). Values come back whole: nothing is cut unless you pass depth, limit or max_string, and then the result says truncated: . A value too big to send (over a million values or 64 MiB of strings) fails with result_too_large; a whole _G is about 4.5 MB: return only what you need. Non-JSON types: {__type: Vector|Angle|Color|Entity|function|cycle|...}. Example: {"realm": "server", "code": "return #player.GetAll(), game.GetMap()"} -> {values: [1, "gm_construct"], n: 2, output: "", duration_ms, chunk_bytes, chunkname, generation}. Errors: lua_syntax_error, lua_error (with traceback), budget_exceeded, main_thread_timeout, result_too_large, realm_unavailable, client_locked. Code that is already in a file: lua_exec_file (or lua_job {path}), not its content pasted here. Some convars cannot be set from Lua (RunConsoleCommand is refused for protected ones, e.g. volume_sfx, fps_max, silently): when the code calls RunConsoleCommand with such a command the result lists it in blocked_console_commands with a warning (only RunConsoleCommand calls made by this code while it runs are seen; a command refused for another reason, e.g. a cheat convar, is not): use convar_set or console_exec, then read the value back.
Tool call template
Replace each placeholder with a value matching the input schema. Only required parameters are included; add optional parameters as needed.
{
"id": 1,
"jsonrpc": "2.0",
"method": "tools/call",
"params": {
"arguments": {
"code": "<code>"
},
"name": "lua_exec"
}
}Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| jit | No | Obsolete. gmod-mcp no longer turns the LuaJIT compiler off, neither for this code nor for the functions it defines (hooks and timers run at real speed, timings are real). jit: true now only ignores `budget`. | |
| code | Yes | Lua code. Use `return` to return values (several allowed). | |
| depth | No | Cut tables deeper than this (default: no cut). | |
| limit | No | Cut tables longer than this (default: no cut). | |
| realm | No | Lua state to run in. Default: menu in client mode, which has no game libraries (ents, util.TraceLine, players...): pass 'server' or 'client' for game code; they exist only once a map is loaded (map_load). On srcds: server (the default; no menu or client realm). | |
| budget | No | Optional time limit in seconds, best effort (none by default). It stops loops written in this code itself (error budget_exceeded), not a loop inside a compiled function it calls. Without it, an infinite loop freezes the game: the call fails with main_thread_timeout after timeout_s, status shows main_thread_stalled, and game_terminate ends the game. | |
| fields | No | Keep only these keys of each returned table (e.g. ["pass", "fails"]): the rest of a large result is dropped before it reaches you. Only returned tables are projected: when none is (a JSON string, a number), the result says so in fields_note. | |
| instance | No | Optional. Instance identifier (see instances_list). If present and different from the local instance, the call is relayed by the coordinator to that instance. | |
| chunkname | No | Chunk name: the source shown by debug.getinfo, errors and tracebacks, e.g. '@addons/my_addon/lua/my_addon/core.lua' for code that identifies its caller by file. Default: '=mcpagent:lua_exec', or the file's path. | |
| timeout_s | No | How long to wait for the game's answer (default 60, or budget + 10). Past it the call fails with main_thread_timeout; the code may still be running. | |
| max_string | No | Cut strings longer than this (default: no cut). |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
| n | Yes | ||
| file | No | ||
| next | No | ||
| note | No | ||
| output | No | ||
| values | Yes | ||
| warning | No | ||
| budget_s | No | ||
| chunkname | No | ||
| next_call | No | ||
| projected | No | ||
| truncated | No | ||
| generation | No | ||
| answered_by | No | ||
| chunk_bytes | No | ||
| duration_ms | No | ||
| fields_note | No | ||
| truncated_note | No | ||
| version_warning | No | ||
| blocked_console_commands | No |