Skip to main content
Glama

Run a long Lua job

lua_job
Destructive

Runs Lua scripts as frame-by-frame coroutines so long scans, traces, and tests do not freeze the game while reporting progress.

Instructions

Runs a Lua script as a coroutine resumed once per frame, so long work (scans, batches of traces, multi-second tests) does not freeze the game and reports progress while it runs. The script MUST cooperate through the job object (global inside the chunk, also its first argument):

  • job:progress(done, total, message) or job:progress(message): forwarded to the client as MCP progress (and as the task status when run as a task).

  • job:step(): call it inside long loops; it yields only when this frame's time slice (slice_ms, default 8 ms) is used up.

  • job:yield() / job:frames(n) / job:sleep(seconds) / job:wait(fn, timeout): resume on a later frame.

  • job:report(value [, channel]): intermediate result, returned at the end and readable live with reports_read (channel "job:"). Each report is bounded: depth 8, 5000 entries per table, strings of 1 MiB (an item the serializer cannot take, over 20000 values, becomes a {__type="report_too_large"} marker); a cut part is marked truncated and counted in reports_truncated. At most 1000 reports are kept (more are counted in reports_dropped), 32 MiB of strings in all: put big data in the returned values or a file instead.

  • args (a global, same table as job.args): the args object of this call. job:elapsed(): seconds since start.

  • return value, ... ends the job: the values are the tool result. Realm: server by default (a map must be loaded); menu always exists in client mode. The code is code, or path (a file, found like lua_exec_file finds it). By default the call waits for the end and returns {job_id, values, n, reports, output (printed text), frames, resumes, cpu_ms, elapsed_s, generation, truncated?, concurrent_jobs?}; timeout_s (default 300) cancels a job that is still running. Errors: lua_syntax_error, lua_error (with traceback, reports and output so far), realm_reset (the map changed: the job died with its Lua state), cancelled, timeout. Globals the script assigns go to the realm's _G (they outlive the job and the next job sees them) unless fresh_env: true. async: true starts the job and returns {job_id, state, realm, generation} at once: poll it with lua_job_status (wait_s to wait), stop it with lua_job_cancel; two jobs can then run side by side, e.g. one per realm. A job started while another runs in the same realm is reported in concurrent_jobs: their timings and state mix. Returned values come back whole (no cut unless depth/limit/max_string; then the result says truncated). The LuaJIT compiler stays on (timings are real). A resume that never yields freezes the game: polling it then fails with main_thread_timeout (about 10 s) and status shows main_thread_stalled. resume_limit_s (optional, best effort) aborts a resume that runs that long without yielding, for loops written in the script itself. Cancelling the request (or the task) stops the coroutine at the next frame. The job dies with its realm (map change). Example: local hits = 0 for i = 1, 5000 do local tr = util.TraceLine({ start = Vector(0, 0, 64), endpos = VectorRand() * 4096 }) if tr.Hit then hits = hits + 1 end if i % 250 == 0 then job:progress(i, 5000, hits .. " hits") end job:step() end return { hits = hits } called as {"realm": "server", "code": "", "timeout_s": 120}.

Input Schema

NameRequiredDescriptionDefault
jitNoObsolete: the compiler is never turned off any more. jit: true now only ignores resume_limit_s.
argsNoArbitrary data exposed to the script as job.args.
codeNoLua source. Use the `job` object described above.
pathNoInstead of code: a file, absolute (e.g. in your git worktree) or relative (lua/mcpagent_tmp/, the mounted Lua folder if any, then the game folder, then lua/, gamemodes/, addons/*/lua/). Errors then point to the file. Prefer it to pasting a file's content into code.
asyncNotrue: start the job and return its id at once (poll with lua_job_status). Default false.
depthNoCut returned tables deeper than this (default: no cut).
limitNoCut returned tables longer than this (default: no cut).
realmNoRealm (default server).
fieldsNoKeep only these keys of each returned table (e.g. ["pass", "fails"]); fields_note says so when no returned value is a table.
instanceNoOptional. Instance identifier (see instances_list). If present and different from the local instance, the call is relayed by the coordinator to that instance.
slice_msNoTime per frame before job:step() yields (default 8).
chunknameNoChunk name shown by debug.getinfo and tracebacks (default: the file's path or '=mcpagent:job:<realm>').
fresh_envNoKeep the globals the script assigns in its own table instead of _G (no leak into later jobs; reads still see _G). Default false.
timeout_sNoCancel the job after this many seconds (default 300). 15 s or more: runs as an MCP task when the client supports it.
max_stringNoCut returned strings longer than this (default: no cut).
resume_limit_sNoOptional, best effort: abort a resume that runs this long without yielding (loops written in the script itself). None by default.

Output Schema

NameRequiredDescriptionDefault
nNo
nextNo
noteNo
realmNo
stateNo
cpu_msNo
framesNo
job_idYes
outputNo
valuesNo
reportsNo
resumesNo
warningNo
elapsed_sNo
next_callNo
projectedNo
truncatedNo
generationNo
answered_byNo
fields_noteNo
truncated_noteNo
concurrent_jobsNo
reports_droppedNo
version_warningNo
reports_truncatedNo
blocked_console_commandsNo

Schema Changelog

Changes observed during successful MCP inspections.

  1. First observedv0.3.7

TDQS

A4.6/5.0
Behavior5/5

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

Annotations only declare destructiveHint=true and openWorldHint=false; the description goes far beyond them, disclosing that globals leak into the realm's _G unless fresh_env is set, that the job dies with its realm on map change, that a non-yielding resume freezes the game and trips main_thread_timeout, and that concurrent jobs in one realm mix timings. It also enumerates the error taxonomy (lua_syntax_error, lua_error, realm_reset, cancelled, timeout) and the report size caps.

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

Conciseness4/5

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

It is front-loaded with purpose, then bulleted job API, realm, return shape, errors, async, and caveats, ending with a worked example, so it is navigable despite its length. Some material is redundant with the schema, such as the restatement of depth/limit/max_string truncation, and the example is long, but for a 16-parameter tool with this much hidden behavior the density is largely earned.

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?

Given 16 parameters, nested objects, an output schema, and a cooperative-coroutine execution model, the description covers the contract an agent needs: the mandatory job:step() cooperation rule, the return shape, error modes, cancellation, and realm lifetime. Nothing essential to calling it correctly is left unstated.

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 100%, so the baseline is 3, but the description adds real meaning the schema does not: args is exposed as a global identical to job.args, async:true pairs with lua_job_status/lua_job_cancel, timeout_s cancels a still-running job, and fresh_env controls global leakage. The interaction between async and concurrent_jobs is explained only here.

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 opening sentence names a specific verb and resource plus the mechanism: 'Runs a Lua script as a coroutine resumed once per frame, so long work ... does not freeze the game.' It also scopes the intended workload (scans, batches, multi-second tests), which separates it from a plain blocking Lua exec.

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

Usage Guidelines4/5

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

It gives clear context for when to reach for this tool (long-running work that must not freeze the game) and names the companion tools for follow-up operations: lua_job_status to poll, lua_job_cancel to stop, reports_read for live reports, lua_exec_file for path resolution. It never states the inverse case, e.g. that short scripts should use lua_exec instead, so there is no explicit exclusion.

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