---
title: Actions
summary: "How the host runs an extension's declared actions in its frame: the `ActionHandler` signature, `ActionContext.via`, results, errors and the 60-second limit."
audience: both
stability: stable
since: protocol 1
---

# Actions

An action is a domain operation the extension declares in its manifest (`actions`, see
[manifest.md](../manifest.md#actiondefinition)) and implements in its module's `actions`. The assistant gets each
one as the tool `<ns>_<name>`; other extensions with the `tools` permission can run it through
`host.tools.call` ([tools.md](tools.md)); widgets can run it when it is `readOnly`; scripts and services can run it
with an API key through the [SpaceMIX API](../api.md).

This page is about the runtime side: what the host calls and what it expects back.

```ts
interface ExtensionModule {
  manifest: ExtensionManifest;
  View: ComponentType;
  actions?: Record<string, ActionHandler>;
  mcp?: (host: SpacemixHost) => Promise<McpServer[]>;
}

type ActionHandler = (args: Record<string, unknown>, host: SpacemixHost, ctx: ActionContext) => Promise<unknown>;

interface ActionContext {
  via?: ActionVia;
}

interface ActionVia {
  extension: string;      // id of the calling extension
  extensionName: string;  // its name, for display
  label?: string;         // what in it is acting (an automation's name), when it said
}
```

**Permission:** none.

## How a call runs

1. The caller (the assistant, a widget, another extension through `host.tools.call`, or an API key through the
   [SpaceMIX API](../api.md), whose call is relayed from the server to one of the person's open windows) names the
   tool.
2. The host validates the arguments against the action's `inputSchema`, with the same rules and coercions as
   collection writes (see [collections.md](collections.md#validation)): undeclared fields are refused, `required`
   is enforced. Your handler receives only declared fields.
3. The host starts the extension's frame if it isn't running (the view doesn't have to be open), waits for it to
   load, and sends it a `call` message.
4. The frame runtime calls `actions[name](args, host, ctx)` and sends back what the promise resolves to.

`host` in the handler is the same object as `import { host } from "@spacemix/sdk"`.

Approval: if the action is declared `requiresApproval`, the assistant in the SpaceMIX app asks the user before
step 3, unless the extension is switched to run without asking in **Settings → Extensions → Approvals**. That
switch is server-wide (the kv key `shell/autoApproveExtensions`) and only people with `server:settings` may change
it. Calls through `host.tools.call`, widgets and the SpaceMIX API never ask (widgets only reach `readOnly`
actions). An API key with the `tools:write` scope runs a `requiresApproval` action without asking anyone: the
person who made the key decided that by its scope; a key with only `tools:read` reaches only `readOnly` actions.

## Results

Whatever the handler resolves to is the tool result. Return plain JSON-able data the model can read: an object
with the ids and fields it needs next, or a short confirmation. Results cross the bridge by structured clone, and
anything that can't be cloned is sent as its JSON; a result that can't be either fails the call with
`The result can't be sent to SpaceMIX: <reason>`.

Don't return `{ error: "…" }` to report failure: throw instead. (A result whose only field is a string `error` is
treated as a failure by `host.tools.call` and by widgets.)

## Errors

Throw (or reject) with a readable message. The message reaches the model as the tool's error, so it can correct
itself; for `host.tools.call` it becomes the rejection.

The host adds these:

| When | Message |
|---|---|
| The manifest declares the action but `actions` has no function for it | `<Name> declares "<action>" but doesn't implement it.` |
| The handler takes longer than 60 seconds | `<Name> didn't finish "<action>" within 60 seconds.` |
| The extension isn't installed | `"<id>" isn't installed.` |
| The frame was dropped while the call was running | `<Name> was reloaded.` |
| The frame failed to load | the load error |

The 60-second limit is the host's. A handler that needs longer (a large import) should start the work, return at
once with what it started, and let the user follow along in the view.

## `ctx.via`: who is behind the call

`ctx.via` is set when another extension ran the action through `host.tools.call`. The host fills it from the
calling frame, so `extension` and `extensionName` can't be forged; `label` is the caller's own `options.via`
(trimmed, left out when blank).

It is also set when an API key ran the action through the [SpaceMIX API](../api.md), either directly or through the
assistant running on the server: `{ extension: "ai.spacemix.api", extensionName: "SpaceMIX API", label: <the key's
name> }`. `ai.spacemix.api` is reserved: no extension can be installed with that id, so don't look it up among the
installed extensions; treat it as "a program the person connected with an API key".

It is absent when the assistant in the SpaceMIX app runs the action for the user, or a widget does.

An action whose effect carries an author should attribute it to `via` when set: a message posted by an
automation should say so, rather than look as if the user wrote it.

## Several windows

Every open window and browser runs its own frame, but each call goes to exactly one: the frame in the window that
made the call. Handlers must not assume module state is shared with other windows; keep shared state in a
collection or `host.storage`.

### Actions run from the server

A call through the [SpaceMIX API](../api.md) starts on the server, which can't run JavaScript in a frame. It goes to
one of the key owner's open SpaceMIX windows or browser tabs, the first to claim it, and runs in that window's frame
as that person, exactly as above. With none of their windows open the API answers `503` at once. The server waits
90 seconds for the result; an action still running then may finish after the caller got `504`.

## The `mcp` resolver

`ExtensionModule.mcp` is called the same way (in the frame, 60-second limit) when the assistant starts a turn or
`host.tools.list` runs, but only for extensions with a `net:` permission. See [mcp.md](mcp.md).

## Example

```ts
import type { ActionHandler } from "@spacemix/sdk";

interface Task {
  title: string;
  done: boolean;
}

export const actions: Record<string, ActionHandler> = {
  async complete(args, host) {
    const id = String(args.id);
    const task = await host.collection<Task>("items").update(id, { done: true });
    await host.events.emit({ type: "task_completed", payload: { id: task.id, title: task.title }, summary: `Task “${task.title}” completed`, target: task.id });
    return { id: task.id, done: true };
  },
};
```

## Stability

Stable: `ActionHandler`, `ActionContext`, `ActionVia` and the call flow above. The example assumes the manifest
declares the `items` collection, the `complete` action and the `task_completed` event.
