Skip to content

Actions

Actions are server-only operations. Use them when you need things mutators can't do: AI calls, external APIs, randomness, or accessing server-only data.

MutatorAction
Runs onClient + ServerServer only
Instant UI updateYes (optimistic)No (waits for server)
Use whenClient has all data neededNeeds AI, external APIs, or server-only data

Declaring Actions in the Schema

typescript
const schema = defineSchema({
  schemaVersion: 1,
  tables: { /* ... */ },
  mutators: { /* ... */ },
  actions: {
    generateWithAI: {
      description: "Generate todo text from a prompt using AI",
      input: z.object({ id: z.string(), prompt: z.string() }),
      output: z.object({ text: z.string() }),
    },
  },
});

Actions are also exposed as MCP tools, so AI models can call them directly.

Implementing Actions in the Backend Config

typescript
export const todoBackendConfig = defineBackendConfig<typeof todoSchema>({
  schema: todoSchema,
  mutators: { /* ... */ },
  actions: {
    generateWithAI: async (ctx, input) => {
      const { apiKey } = await ctx.platform.call("getPoeApiKey", {});

      let generatedText = "";
      for await (const chunk of callAI(apiKey, input.prompt)) {
        generatedText += chunk;
        await ctx.mutate("setTodo", {
          id: input.id,
          text: generatedText,
          status: "generating",
        });
      }

      await ctx.mutate("setTodo", {
        id: input.id,
        text: generatedText,
        status: "ready",
      });

      return { text: generatedText };
    },
  },
});

Calling Actions from the Client

typescript
const result = await store.action.generateWithAI({
  id: "todo-1",
  prompt: "What should I cook for dinner?",
});

Enqueuing Actions from Mutators

Mutators can trigger actions as a side effect. ctx.enqueueAction() is a no-op on the client; on the server it runs the action after the mutation commits. Call it unconditionally — no ctx.isServer guard needed.

typescript
mutators: {
  createAndGenerate: async (ctx, input) => {
    // Instant: create a placeholder todo
    await ctx.table("todos").set({
      itemKey: input.id,
      value: { id: input.id, text: "Generating...", completed: false, status: "generating" },
    });

    // Queued: server will run this after the mutation commits
    ctx.enqueueAction("generateWithAI", {
      id: input.id,
      prompt: input.prompt,
    });
  },
},

When NOT to use an Action

  • Data changes that the client can compute — use a mutator (optimistic, no round trip).
  • Secrets the client should never see — use serverOnly() tables; an action can expose a derived result.