In these docs, a hook means a handler you register in extension code with The matcher
on(...). The hooks.json feature that runs commands on events is called command hooks and is outside the scope of these docs. MCode calls your hook when the matching event happens. This page covers how to register hooks, the static rules registration must follow, when each event fires and what e contains, and what happens when a hook fails or times out.Extensions have three kinds of events:ui.render: MCode calls it when it needs to draw a site, and you return the elements to show. See Interface.- Observe events: a session starts, a turn starts or ends, a tool call starts or ends, context usage changes, or the user closes a panel. Observe events notify your hook after the event happens, and a slow or failing hook never slows the conversation down.
command.run: the user ran a slash command (/command) your plugin registered. See command.run.
above-prompt. A panel is a tab in the conversation’s side panel on the right, next to tabs such as Files and Browser. Its site name in code is pane. The backdrop is the backdrop behind the conversation in the main window, a layer under all the panels. It replaces the wallpaper while a plugin theme is active. Its site name in code is backdrop.The type files are the source of truth for fields. If
.minimax-plugin/types/ in your development folder disagrees with this page, trust the type files.Register hooks
The extension modulehooks/register.tsx exports a register function. MCode calls it once when it loads the module and passes in on. Call on() in the function body to register all your hooks.hooks/register.tsx
on has two forms:Handler parameters:
ctx is the Extension API for this Activation. All hooks in the same Activation share one ctx. e is the event payload and is read-only.Only hooks receive ctx. Callbacks such as <Button onPress>, <Input onSubmit>, ctx.clock, and ctx.store.onDidChange receive only their own values (for example onPress={() => …}, onSubmit={(value) => …}). When you need ctx there, use the ctx of the enclosing hook. Declaring your own ctx parameter in a callback doesn’t raise an error, but that parameter holds nothing, and the code fails at run time.matcher
A matcher narrows what a hook receives. Only the events below have matchers. All fields are optional (exceptsite for ui.render and command for command.run). Names must match exactly, except command, which is compared without regard to case:session.start, session.measure, turn.start, and turn.complete have no matcher.Static rules
Preview andtest first scan your source statically. That’s how MCode learns which hooks you register and which ctx methods you call. So there are a few hard rules on how you write code. Breaking one reports the diagnostic below, and the extension doesn’t load. Type checking can’t catch these problems, so watch for them yourself.Loading also runs two run-time checks. If
register throws, MCode reports EXTENSION_REGISTER_FAILED. If the on() calls that actually run don’t match the static scan, it reports EXTENSION_ENTRY_MISMATCH.Here are some common mistakes:Observe events
Observe events call your hook after the event happens. Each firing is a new Invocation that still uses the samectx from this Activation. In the hook you can write ctx.state and ctx.store, call MCP, show a toast, ask for a redraw, fill text into the message box, call a model, or start a background subagent.A few rules apply to all of them:- They don’t slow the conversation down. If a hook is slow or fails, the conversation goes on as usual.
- They can see conversation content. Events include your prompt and the input and output of tool calls, as is, without truncation or redaction. Thinking never appears. The Agent’s reply appears only as the
answerfield in theturn.completeof a subagent your plugin started. - Nothing is read if nobody listens. MCode reads an event’s content only when an activated plugin in the session registered that event.
- Only the Agent’s own tool calls count. Your extension’s own
ctx.mcp.calldoesn’t producetool.call/tool.result, so calling MCP from an observe event can’t cause a loop. - Subagents fire them too.
turn.start,turn.complete,tool.call, andtool.resultcarryagentId. The main Agent has no such field. For a subagent, or a task it started, it’s the child session id. To watch only the main Agent, checke.agentId === undefinedfirst.
session.start
Fires when the plugin becomes active in a session, once per Activation. All of these create a new Activation: previewing or installing partway through a session, reloading after a change, and a Runtime restart. A reloaded version also gets its ownsession.start.session.start has no sessionId, turnId, or agentId, and it runs before any other event in the Activation. Use it to prepare initial data, or to open a panel that should be there from the start, with a fixed id and no focus. The same session can receive it more than once, so write initialization that is safe to run repeatedly.session.measure
After each main Agent turn, MCode measures context usage. It sends this event only when the result differs from the last one it sent. Subagent and task turns aren’t measured.The first
session.measure after the plugin loads always lists 'context'. Measurements don’t pile up: while one is still being delivered, the results of later turns are merged into the next one. After a hook fails, the same change isn’t sent again.turn.start and turn.complete
Fire when a turn starts and ends. Both events havesessionId (this session’s id) and turnId. Subagent turns also have agentId.turn.start:turn.complete:answer, answerTruncated, and reason go only to the plugin that started this subagent (including its reloaded versions). Other plugins get the same event without these three fields. This event can arrive up to 10 seconds after the subagent ends. See Background subagents for usage.prompt is missing only when MCode can’t read it, for example when the turn has already ended by the time the event arrives. The event itself still comes.tool.call and tool.result
tool.call fires when the Agent calls a tool, and tool.result fires when the call ends. Both built-in tools and MCP tools fire them. Use the matcher’s tool or server to receive only some tools. Both events have sessionId and turnId. Subagent calls also have agentId.tool.call:tool.result:input and output are missing only when MCode can’t read them, for example when the turn has already ended by the time the call ends.ui.close
Fires when the user closes a pane tab your plugin opened. Only the plugin that opened it receives the event. Callingctx.ui.close(id) yourself doesn’t fire it.There’s no
turnId or agentId. For opening and closing panels, see Interface.command.run
Coming soonFires when the user runs a slash command your plugin registered in the message box: by picking it in the/ menu, or by typing /<command name> arguments and pressing Enter. For how to register a command and how it shows in the menu, see Add slash commands.{ command } is required. Write a command name this module registered, without the /. It’s compared without regard to case. The messages below all use this /note command as the example.There’s no
turnId or agentId. Only the plugin that registered a command receives its command.run.What to return
text is only for the user. The model never sees it, and it doesn’t enter the conversation. It isn’t a ctx.ui.toast call and doesn’t count toward the toast rate limit. A short sentence is enough.When it runs
A command is a user action, the same as a button action:- It runs right away. A command doesn’t wait for the current turn to end, or for this plugin’s observe events. So one of this plugin’s observe event hooks may be running at the same time. If the command changes something the current turn depends on, handling the consequences is up to you.
- It shares a queue with button actions. This plugin’s commands and button actions in this session run one at a time in arrival order. See Queue limits.
- It times out after 15 seconds without a result. MCode reports
COMMAND_TIMEOUT, and the user sees “/note timed out and may have run already”. A command still waiting in the queue won’t run. A hook that’s already running keeps going until it finishes (including any permission card it’s waiting on), but its result is discarded, and it holds its place in the queue until it finishes. A timed-out command may have run already, so a command that changes things should be safe to run twice, or should check its ownctx.statefirst.
When it fails
In the cases below, the command gives no result and the user sees a message. For the ones marked “diagnostic”,inspect also records the same code:ui.render
on('ui.render', { site, id? }, async (ctx, e, next) => …) is called when MCode needs to draw a site. MCode decides e: { site: 'above-prompt' } for the status band above the message box, { site: 'pane', id } for a panel, and { site: 'backdrop', props } for the backdrop. Coming soon above-prompt-list, which arranges the status band’s rows, gets { site: 'above-prompt-list', props: { rows } }. See Status band row arrangement. For the backdrop, e.props tells you which theme from which plugin is active. See Interface: backdrop. All matching plugins draw a site in layers, and every layer’s drawing must keep await next().For the render chain, when redraws happen, and what a drawing can do, see Interface: render chain.Example: count tool calls
This extension shows, on the status band, how many times the main Agent called a tool in this session and the name of the most recent tool. When a turn ends with an error, it shows a toast.hooks/state.ts
hooks/register.tsx
ctx.state belongs only to the current session. After a write, drawings that read it redraw automatically, so the hooks don’t need to call ctx.ui.invalidate().Failures and pausing
When an extension fails, only that extension is affected, never the conversation. The rules are below.Time limits
“Its own time” counts only time when your code is running. Time spent waiting on
ctx requests (waiting for an MCP server, for the user to handle a permission card, or for a model reply) doesn’t count.An error thrown by a hook reports thrown, and the message includes the error.Paused after repeated failures
If the same hook fails or times out 3 times in a row in the same session, it’s paused in that session and MCode reportsEXTENSION_DISABLED. One success in between resets the count.- Pausing affects only the current session. The same extension keeps running in other sessions.
- A paused layer is skipped on the status band or panel, and other plugins still show. Only when every layer at a location is paused does MCode show “Paused · this session” and “Failed or timed out 3 times in a row; paused in this session”, with a “Retry” button.
- Clicking “Retry” clears this plugin’s paused state in this session. A reloaded version with fixed code also starts counting from zero.
- A drawing cut off by its deadline while waiting on a
ctxrequest doesn’t count toward pausing. Only a drawing still running its own code when the deadline hits counts.
Runtime crashes
Extension code runs in a separate runtime environment that MCode sets up for it. An infinite loop or ever-growing memory crashes and restarts that environment, MCode reportsEXTENSION_WORKER_CRASHED, and every Invocation in progress fails. If MCode can tell which version of which extension caused it, it pauses that version and shows “The plugin hung, crashed or failed to load; paused in this session”, again with “Retry”. A load failure shows the same thing.Queue limits
In each session, each plugin has two standing queues, and each runs its hooks one at a time in arrival order: one for user actions, shared by button actions and slash commands, and one for observe events. While an earlier hook is still running, later ones in the same queue wait behind it. The most common case is a permission card raised byctx.mcp.call that the user hasn’t handled yet, so the hook keeps waiting.Time a command spends waiting in the queue counts toward its 15-second timeout. A timeout reports
COMMAND_TIMEOUT. See command.run.For what each diagnostic code means and how to fix it, see Diagnostic codes.Next steps
Extension API
Save state, call MCP, fill the message box, call models, and add slash commands from hooks.
Interface: sites and elements
The status band, panels, the render chain, and elements.
Test and troubleshoot
Write tests and use inspect to view diagnostics.
Diagnostic codes
What each diagnostic code means and how to fix it.