Skip to main content
Glama

Execute Lua

lua_exec
Destructive

Run 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

NameRequiredDescriptionDefault
jitNoObsolete. 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`.
codeYesLua code. Use `return` to return values (several allowed).
depthNoCut tables deeper than this (default: no cut).
limitNoCut tables longer than this (default: no cut).
realmNoLua 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).
budgetNoOptional 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.
fieldsNoKeep 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.
instanceNoOptional. Instance identifier (see instances_list). If present and different from the local instance, the call is relayed by the coordinator to that instance.
chunknameNoChunk 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_sNoHow 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_stringNoCut strings longer than this (default: no cut).

Output Schema

NameRequiredDescriptionDefault
nYes
fileNo
nextNo
noteNo
outputNo
valuesYes
warningNo
budget_sNo
chunknameNo
next_callNo
projectedNo
truncatedNo
generationNo
answered_byNo
chunk_bytesNo
duration_msNo
fields_noteNo
truncated_noteNo
version_warningNo
blocked_console_commandsNo

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.3.7

TDQS

A4.5/5.0
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

Annotations only declare destructiveHint=true and openWorldHint=false; the description adds substantially beyond that: no time limit unless budget, an infinite loop freezes the game and yields main_thread_timeout/game_terminate, result_too_large above ~1M values or 64 MiB, generation changes on every map load, and the blocked_console_commands limitation. It stops short of describing permission or realm availability prerequisites in depth (realm_unavailable/client_locked are only listed as error codes), so it is not perfect.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness3/5

Is the description appropriately sized, front-loaded, and free of redundancy?

It is front-loaded with the execution model and return shape, and the error list is useful, but the paragraph is very long and the blocked_console_commands passage is a dense multi-clause aside that could be trimmed. Several sentences restate what the schema and output schema already document (values, n, output, duration_ms).

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For an 11-parameter, destructive, open-world-false tool with an output schema, the definition covers the execution model, argument pitfalls, size limits, failure modes and sibling routing. Nothing an agent needs in order to call it safely is missing.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is already 100%, so baseline is 3, but the description adds real meaning beyond the schema: that `jit` is obsolete and only disables `budget`, that an unbudgeted infinite loop freezes the game, and that `generation` should be compared with map_load's result. It adds little to `fields`, `limit`, `depth` and `max_string`, which the schema already explains.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description states a specific verb and resource ("Executes Lua code in a realm under xpcall + debug.traceback, at full speed") and immediately names the sibling that handles the file-based case (lua_exec_file / lua_job {path}), so an agent can distinguish it without opening any schema.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines5/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

It routes explicitly: pasted code goes here, code already in a file goes to lua_exec_file or lua_job {path}, and protected convars should be set via convar_set or console_exec then read back. The realm default caveat (menu in client mode has no game libraries) tells the agent which argument to choose for game code.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.