Skip to content
  1. Docs
  2. Apps
  3. Developers

Agent background mode

How an installed app serves functions to AI assistants through MCP Center without opening a window, and how to adopt it.

enzim apps can offer typed functions to AI assistants through MCP Center, the per-user capability broker. An assistant never talks to an app directly: it discovers and calls functions through the broker, which handles enrollment, consent and activity history.

There are two ways an app can take part:

  • Desktop connector. The app registers its functions while its window is open. Apps built with the optional center feature do this; it connects only when a credential has been enrolled for the app's ID.
  • Agent background mode. The installed app can be started on demand, without a window, to answer a call. It runs as one ordinary process of the same app, using the same state and persistence as its window.

Which apps run in the background

AppNative background modeWhat it offers
Calendaryeslist_events, create_event, go_to_date
Mailyeslist_mailboxes, list_messages, select_mailbox, prepare_send, review_status
Filesyescurrent_folder, open_folder, go_up, go_home

Background launch also depends on the package installing the manifest and service. Several other apps offer functions from open windows. The reasons are about what a function means without a window:

  • Remote: sessions exist only in a window.
  • Reader: its functions act on what a window has open.
  • Music: headless playback would turn it into a playback daemon.

A window-only connector advertises its functions while its application is running.

How activation works

  1. The package installs a manifest at /usr/share/enzui/agent-apps/APP_ID.json and a user unit enzui-agent-APP_ID.service. The manifest is generated from the app's live function registry.
  2. The broker lists the app from that catalog without starting it.
  3. On an authorized call, the broker starts the user unit, which runs the app with --agent-background.
  4. The windowless process registers with the broker, serves the call, and exits after it has been idle with no window, no active call and no app-owned work in progress.

If a window of the app is already open, the broker uses it and starts no unit. A desktop launch while a background worker is running is forwarded to that worker, which then opens its window, so GUI launches and background activation always converge on one instance.

The generated unit for Mail shows the shape. It reads the app credential and broker socket from fixed locations and declares only the sandbox the background functions need:

text
[Service]
Type=exec
ExecStart=/usr/bin/mail-app --agent-background
Environment=ENZUI_APP_CREDENTIAL=%S/enzui/apps/io.github.enzui.Mail.credential.json
Environment=ENZUI_CENTER_SOCKET=%t/enzui/center.sock
Restart=no
UMask=0077
NoNewPrivileges=yes
PrivateTmp=yes
ProtectSystem=strict
ProtectHome=read-only
ReadWritePaths=-%h/.local/share/mail-app
RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6

Review before external effects

Functions with effects outside the app, such as sending mail, never complete on their own. Mail's prepare_send prepares a draft and presents the real compose window; it does not send. A person performs the final send. Such functions declare the ui.user_review capability, and the broker rejects external-side-effect functions without it by default. Granting an assistant access to a function is separate from approving what it prepared.

Adopting background mode

Use the framework’s enzui-agent-app crate. Calendar's src/background.rs is a minimal reference. An adoption needs:

  1. A feature and a process handle. The center feature enables the agent crates, and the app state holds an AgentProcess<Message>.

  2. Launch classification in main. LaunchMode::from_args distinguishes three launches: --export-agent-manifest prints the catalog, --agent-background runs the windowless worker, and anything else is a desktop launch that first claims the single instance (and exits if it was forwarded).

    rust
    match background::launch_mode() {
        Ok(LaunchMode::ExportManifest) => return background::export_manifest(),
        Ok(LaunchMode::Background) => {
            return match background::run_background() {
                Ok(Some(app)) => run_gui(Some(app)),
                Ok(None) => std::process::ExitCode::SUCCESS,
                Err(error) => {
                    eprintln!("calendar-app: background mode: {error}");
                    std::process::ExitCode::FAILURE
                }
            };
        }
        Ok(LaunchMode::Gui) => {}
        Err(error) => {
            eprintln!("calendar-app: {error}");
            return std::process::ExitCode::from(2);
        }
    }

    run_background returns the state to present when a window was requested, or nothing when the worker finished.

  3. A windowless boot. Work that needs a window (opening it, platform queries such as theme and fonts, anything chained after them) is deferred until a window exists.

  4. A BackgroundApp adapter. It wires the app's reducer, the center and agent subscriptions, what the app itself counts as busy (a folder load, a pending save, a startup read), and call deadlines.

  5. Calls wait for state. A call that arrives before the state it reads exists waits for it, bounded by its deadline, instead of failing or answering with stale data.

  6. Generated packaging. packaging/agent/ (or the package build) holds the catalog and unit generated from the live registry, with a test that detects drift. A background-app declaration states the unit, the executable, a one-line purpose and the activation sources the app actually has.

Functions are declared once, as typed specs in the app's function registry, and the GUI and background worker share them. A trimmed Calendar spec:

rust
let go_to = FunctionSpec::new(
    "go_to_date",
    "Go to a date",
    "Moves the calendar view to this date.",
    object(json!({"date": date_schema()}), &["date"]),
    object(json!({"date": {"type": "string"}}), &["date"]),
    FunctionRisk::Mutating,
    once_or_session,
);

Each function declares a risk (SensitiveRead, Mutating or an external side effect) and the consent scopes the broker may offer for it.

Qualification

A background-capable app is qualified end to end against the real broker and the user's systemd manager, in private state. The scenario checks that offline discovery starts nothing, that a call awaiting consent starts nothing, that one cold start serves the scenario from one windowless process, that the worker exits when idle, and that a drifted catalog is refused. Optional passes cover uninstall and reinstall while a worker runs, a desktop launch forwarded to the worker, and a broker that uses an already open window.