matter.work
Building Apps / Runtime & Shipping

Worker and tools

Run a background Node worker, and the stdio MCP tools your agents and pages use.

An app’s code is two small sets of files, split by where they run. Each is a JavaScript file in the deployed folder, named in matter.json. Matter runs them with its own bundled Node runtime as the signed-in member—so teammates never need Node installed, and they behave identically across macOS, Windows, and Linux.

  • The worker runs on one computer per member: background work, like schedules, webhooks and filling your tables.
  • The tools run on any computer the member uses, started when a page, a view or an agent calls one there, and stopped soon after.

So a tool on a member’s second computer can’t read files the worker keeps on their first. Data every computer needs belongs in your app’s tables, which sync everywhere.

Bundle your entry files

A deployed app folder does not include node_modules unless you ship one. Bundle each process entry file together with its dependencies (using esbuild, for example).

"worker" Long-running

Runs on one of each member’s computers and keeps running across Matter restarts. Watches tables and acts on changes without an agent.

"tools" On demand

How your pages, views and agents get data that isn’t in Matter’s tables. Run on whichever computer calls them, and stopped soon after.

Worker#

"worker": "worker.mjs" is a long-running background process. It runs on one computer for each member, and keeps running across Matter restarts until they choose Stop in the app's menu. It restarts automatically whenever a new version of the app is deployed.

Use a worker for deterministic work that should happen immediately as data changes, without spinning up an agent: sending an email someone just approved, syncing with an external API, or tidying up rows. Watch the tables it cares about with live reads. The worker on the app’s runner computer also takes its webhooks.

A Worker, Step by Step#

matter app create --worker scaffolds all of this automatically. To add a worker to an existing project:

  1. Write the worker entry (src/worker.ts)

    Import @matter.work/app-sdk just as your pages do. A worker runs until Matter stops it, and its active live reads keep the Node event loop alive:

    TypeScript
    import matter from '@matter.work/app-sdk'
    
    const context = await matter.context()
    console.log(`Worker started for ${context.user.name}`)
    
    matter
      .table('actions')
      .watchIds({ index: 'by_sender_status', eq: { sender: '$viewer', status: 'approved' } }, (state) => {
        if (state.status === 'ready') for (const id of state.value.ids) void send(id)
      })
  2. Bundle it into your build output

    Bundle the worker after your pages build so it runs without node_modules:

    JSON
    "scripts": {
      "build": "tsc --noEmit && vite build && npm run build:worker",
      "build:worker": "esbuild src/worker.ts --bundle --platform=node --format=esm --target=node22 --outfile=dist/worker.mjs"
    }
  3. Declare it in matter.json

    Name the built file relative to deploy.dir:

    JSON
    "worker": "worker.mjs"

One Computer per Member#

A member’s worker runs on the computer they added the app on (for your own app, the first computer you open it on), in the background, starting with Matter. Its matter.onAdd runs there once. On their other computers the app’s pages work as ever, and its header says where it runs, with Run here in its menu to move it. The computer that takes the app’s webhooks runs its worker as well. Since Matter 0.5.31; before, every computer that opened the app ran it.

Every Member Runs It#

Every member who has added the app runs its worker on one of their computers—and in development, your dev worker runs alongside your live one. That means two workers can see the same row change at the same time.

Make worker actions safe to run concurrently: claim work with a conditional update (if)—which only one worker can win—before performing an external side effect. Work that should happen on a schedule once for the whole app belongs in an app-scoped routine instead.

Tools#

Your tools are declared in matter.json by name and handled in one file: a default export of a function for each, named as declared. Matter loads the file in a process on the computer a call comes from (an agent’s turn, a page, a view, a widget, the brief, a search), calls the handler as the member, and stops the process once it’s been unused for a couple of minutes.

src/tools.ts
import { defineTools } from '@matter.work/app-sdk'
import matter from '@matter.work/app-sdk'

export default defineTools({
  show_deal: async ({ id }, { threadId }) => {
    const deal = await matter.table('deals').get(id)
    if (!deal) throw new Error(`No deal ${id}.`)
    return { name: deal.title, stage: deal.values.stage }
  },
})
  • A handler gets the arguments, already checked against the tool’s input, and { threadId }: the thread an agent called it from, or null.
  • It returns the tool’s result: text as text, anything else as JSON, with an object also as the result’s structured content (what your output schema describes, and what pages and views read). Throwing gives the caller an error with your message.
  • defineTools types each handler from your declarations once you’ve run matter app types.
  • Bundle the file with what it imports, as the starter’s Vite config does (dist/tools.mjs), and name it in toolsFile if it isn’t tools.mjs.
  • One process serves every caller on that computer, so keep state in your tables or MATTER_DATA_DIR, not in memory. What a handler logs goes to matter app logs tools.
  • Write tools that validate and change state safely (with conditional updates, say, so two calls can’t claim the same item). Agents get the result whole, with a line saying it’s your app’s data.
  • If the member hasn't trusted your app's publisher on their computer, its tools don’t run there.

Moving Tools Off an MCP Server#

Apps used to name an MCP server in "tools": "tools.mjs". Matter runs every tool call itself now, so that server is only overhead. Apps that still have one keep working for now, and matter app deploy warns about it. To move one:

  1. Declare each tool in matter.json: its description, its inputSchema as input, and its outputSchema as output. A tool’s view and "anywhere": true go there too.
  2. Turn each registerTool into a handler in a default export, returning the structured content instead of an MCP result. Drop the MCP server, its transport and its schema library.
  3. Read the thread from the handler’s context (threadId) rather than MATTER_THREAD_ID: one process now serves every thread.
  4. Set minMatterVersion to 0.5.31, run matter app types, and deploy.

Moving Off a Run Server#

Apps used to be able to declare "run": "server.mjs", a local HTTP server Matter forwarded their pages to. It’s going away: an app is pages, a worker and tools. Apps that declare run keep working for now, and matter app deploy warns about it. To move one:

  1. Serve your pages as files. Build them into deploy.dir, as the starter does (matter app create), and drop run from matter.json.
  2. Turn each server route into a tool. What a route read or did on the member’s computer (files on the computer, the member’s connections) becomes a tool, declared with an output schema.
  3. Call the tools from your pages with matter.tools.call instead of fetch, typed by matter app types. Tools run as the member, so there are no requests to verify: drop matter.verifyRequest.
  4. Move background work to the worker. Anything your server did on a timer or as data changed belongs in the worker.

The Environment#

Your worker and tools receive these environment variables:

Variable What it is
MATTER_API_URL, MATTER_APP_TOKEN Endpoint and credentials for the local data API (used automatically by the SDK).
MATTER_SDK_URL File URL to the local SDK module (works across macOS, Windows, and Linux).
MATTER_SDK Filesystem path to the same SDK module.
MATTER_APP_ID, MATTER_MEMBER_ID, MATTER_WORKSPACE_ID IDs for the app, signed-in member, and workspace.
MATTER_DEPLOYMENT_ID Current deployment ID.
MATTER_DATA_DIR Persistent local folder for this app on this computer, kept across versions.
MATTER_APP_PROCESS worker or tools.
MATTER_THREAD_ID The thread an older app’s MCP server serves. Declared tools get it in each call’s context instead.

For the worker, the working directory is the deployed app folder. Treat that directory as read-only and write any local state to MATTER_DATA_DIR.

To run a worker or script manually in your terminal against live workspace data:

Terminal
eval "$(matter app env)"
node dist/worker.mjs

Logs#

Each process's stdout and stderr are written to per-app log files on the member's computer:

Terminal
matter app logs                 # tail of all logs for this app
matter app logs worker -f       # follow the worker log live
matter app logs tools -f        # what your tools' handlers log

Available log names are worker and tools. Pass --preview to view logs for a preview deployment. Logs rotate automatically at 10 MB.

When a Process Stops#

If a worker exits unexpectedly, Matter restarts it with exponential backoff up to one minute. After five crashes within ten minutes, Matter stops retrying until a new version is deployed or the member chooses Stop in the app menu and reopens the app. Stopping a process also terminates any child processes it spawned.