> ## Documentation Index
> Fetch the complete documentation index at: https://docs.prisme.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Build an app that calls your workspace

> Workspace builder: create an isolated app in Builder that reads the signed-in user, calls a webhook, exchanges events with an automation, and deploys to its own page.

Use this procedure to build a Builder app in the **isolated** format: the app embeds its own libraries and reaches the platform only through the `app` object it receives. When it succeeds, your app is online at `/apps/<workspace slug>` and `/p/<workspace slug>`, and its buttons trigger your workspace automations.

```mermaid theme={null}
flowchart LR
  APP["Your app (React)"] -->|"app.webhooks.call"| WH["Webhook automation"]
  APP -->|"app.events.emit"| EV["Event automation"]
  EV -->|"emits a response event"| APP
  APP -->|"app.callServerTool"| MCP["mcp automation (tools)"]
  APP -.->|"app.user"| USER["Signed-in user"]
```

The page hosting the app sends every call with the user's session. Isolated means the app embeds its own libraries and styles, not that its code is sandboxed: in its page it runs in the platform's origin, with the visitor's session, like any other app of the platform.

## Before you begin

* **Who:** a workspace builder with edit access to the workspace in Builder.
* **Where:** Builder, **Pages** section of the workspace.
* **Requires:** a workspace without a page. The starter adds four automations (`_auth`, `on-app-greeting-requested`, `v1/status`, `mcp`) and keeps any automation that already has one of these slugs.
* **Impact:** the deployed app is visible to signed-in users of the platform, or to anyone if you make it **Public**.

## Create the app

**1. Start from the isolated starter**

In Builder, open the workspace and select **+** next to **Pages**. The preview shows the starter home page with the **Isolated** badge, and the four automations appear in the sidebar.

**2. Run the examples in the preview**

The home page, **Learn how your app works**, offers one tile per mechanism. Select a tile to try it: **Your backend** shows `{"status":"ok", ...}`, **Real time** shows `Hello World from the automation!`, **Shared tools** shows `Hello <name>!`. **See all the steps** lists every mechanism with the file and the code it uses.

**3. Edit the code**

Open `src/pages/Home.tsx` and change the title. The preview updates within a second. The app entry is `src/App.tsx`; its default export receives `{ app }`.

**4. Deploy**

Select **Deploy** in the header, then **Deploy** in the dialog. An isolated app has no **App format** choice: it always deploys as isolated. The dialog confirms `Deployed v<version>`.

## Call the workspace from your code

Each call below matches a starter example. Replace the slugs and event names with your own automations.

```tsx theme={null}
// Webhook automation `v1/status` (POST /workspaces/slug:<workspace>/webhooks/v1/status)
const data = await app.webhooks.call('v1/status', { method: 'POST', body: {} })

// Event automation: listen for the response, then trigger it
const stop = app.events.on('app.greeting.completed', (event) => console.log(event.payload))
await app.events.emit('app.greeting.requested', { name: 'World' })

// Tool `hello` of the `mcp` automation
const result = await app.callServerTool('hello', { name: 'World' })

// Signed-in user (absent for an anonymous visitor of a public app)
const user = app.user
```

> [!WARNING]
>
> A webhook is public: anyone with its URL can call it. The starter's `v1/status` and `mcp` call the private `_auth` automation first, which refuses anonymous callers with `401` and gives the caller as `auth.user_id` and `auth.auth_type` (`user`, `apikey`, `workspace`, `anonymous`). Start each webhook you add the same way. The app calls with the signed-in user's session, and an agent given the `mcp` server calls it as the user who talks to it: the platform carries that user's identity to the servers it hosts, no API key needed. An organization API key (`x-prismeai-api-key` header) is only for a caller without a user, such as a scheduled automation. `_auth` only tells who calls: every tool and webhook must also check that this caller may read or change the data it touches, and refuse otherwise. The app's sharing decides who opens its page and sees its views; it does not protect a tool, which can be called from the chat, a view or directly. For an endpoint a **Public** app calls without sign-in, pass `allow_public: true` to `_auth`.

The `app` object offers the following methods. Each namespace is available when the host announces it: check `app.hasCapability('<name>')` before using it in a view that can also run elsewhere.

| Need | Methods | Limits |
| - | - | - |
| Call a webhook of the app's workspace | `app.webhooks.call(path, { method, body, query })` | `path` is a webhook slug of the workspace, at most 512 characters |
| Workspace events, or events between apps shown together | `app.events.on(type, listener, { scope })`, `app.events.emit(type, payload, { scope })` | `scope` is `workspace` (default) or `page`; payload at most 100 KB; 50 subscriptions |
| Signed-in user | `app.user` | Absent when nobody is signed in |
| Tools of the workspace MCP endpoint | `app.callServerTool(name, args)`, `app.readServerResource(uri)` | |
| Route of the app, kept in the page URL | `app.navigation.current()`, `app.navigation.navigate(path)`, `app.navigation.onChange(listener)` | A page route appears as `/p/<slug>#/<route>` |
| Files | `app.files.upload(blob, { name, public })`, `app.files.download(urlOrBlob, filename)` | Upload at most 10 MB, download at most 20 MB |
| Values kept in the browser, per app, across reloads (not shared between devices) | `app.storageGet`, `app.storageSet`, `app.storageDelete`, `app.storageKeys` | 100 KB per app, 200 keys, 20 KB per value |

## Open the app's agent

Your app can open the chat of an agent floating over its page, so the user asks questions about what the app shows without leaving it.

**1. Pick the agent**

In the **Deploy** dialog, select an agent under **Agent of the app**, then select **Deploy**. The list shows the agents you can use. Under **When the page opens**, choose how its chat shows when a user opens the page: **Closed** (a chat button, the default), **Open over the app**, or **Side by side**, a column next to the app. On a page too narrow for both, side by side opens the chat over the app.

**2. Open the chat from your code**

Call `app.agent.open()`, for example from a button. Check `app.hasCapability('ai.prisme/agent')` first: the page offers it only to a user who can use the agent.

**3. Share what the user sees**

Call `app.updateModelContext({ content: [{ type: 'text', text: 'Selected month: March' }] })`. The context goes with the user's next question.

```tsx theme={null}
{app.hasCapability('ai.prisme/agent') && (
  <button onClick={() => app.agent.open()}>Ask {app.agent.name() ?? 'the agent'}</button>
)}
```

As soon as the agent is available, its chat shows as chosen at deployment. The user can then fold it to a button at the bottom of the page, open it again, or move it between floating over the app and side by side with it. `app.agent.close()` reduces it to that button.

### Start a new conversation

`app.agent.close()` folds the chat but keeps its conversation. To start over without reloading the page, call `app.agent.open({ newConversation: true })`: the chat opens empty, on a new conversation. Without the option, `app.agent.open()` keeps the conversation in progress. The user can do the same from the **New conversation** button in the chat's header.

```tsx theme={null}
<button onClick={() => app.agent.open({ newConversation: true })}>New question</button>
```

The page announces the option in its capability: `app.hostCapabilities.experimental?.['ai.prisme/agent']?.newConversation` is `true`. A page that does not announce it opens the chat on the conversation in progress.

### React to a button in the agent's answer

When the agent answers with buttons (an [A2UI surface](https://docs.prisme.ai/products/agent-factory/a2ui.md)), a click reaches your app at once, in the user's browser: open a form, show a record, without a round trip through the workspace.

**1. Give the agent an interactive surface**

Use an A2UI tool with `wait_for_action: true` and a `Button` whose `action` names what to do, for example `action: open_form`. Without `wait_for_action: true`, the buttons are disabled (see [Interactive surfaces](https://docs.prisme.ai/products/agent-factory/a2ui.md#interactive-surfaces)).

**2. Listen in your app**

Subscribe to `agent.action` on the page scope. The event carries the action's `name` and the values of the surface in `data`.

```tsx theme={null}
useEffect(
  () =>
    app.events.on(
      'agent.action',
      (e) => {
        if (e.payload.name === 'open_form') openForm(e.payload.data)
      },
      { scope: 'page' }
    ),
  [app]
)
```

The click also resolves the agent's pending tool call, as in any chat: the agent continues with the action. The event stays in the user's page; other users of the app do not receive it.

## Use the provided libraries

An isolated app can import only its own files and the libraries the platform provides. The build copies them into the app, so a platform upgrade never changes a deployed app. To get a fix the platform ships in these libraries, deploy the app again: the **Deploy** dialog says when a redeployment is advised.

* `react` and `react-dom` 18.3.1
* `@prisme.ai/mcp-apps` (the `app` object and its React helpers)
* `@radix-ui/react-*`: alert-dialog, avatar, collapsible, dialog, dropdown-menu, label, popover, scroll-area, select, separator, slider, slot, switch, tabs, tooltip
* `lucide-react`, `clsx`, `tailwind-merge`, `class-variance-authority`

An import of any other package, or of a platform module (`@/lib/...`, `@/hooks/...`, `@prisme.ai/sdk`), stops the deployment with the file and line of the import.

To use another npm package, build the app locally (next section).

## Build locally

The open-source [`prismeai/starter-spa`](https://github.com/prismeai/starter-spa) repository builds the same isolated app outside Builder, in your IDE, with any npm package. It holds the same starter, plus a view the `hello` tool shows in the chat.

```bash theme={null}
git clone https://github.com/prismeai/starter-spa my-app
cd my-app
npm install
cp .env.example .env    # PRISMEAI_API_URL (with /v2), PRISMEAI_ACCESS_TOKEN, PRISMEAI_WORKSPACE_ID
npm run dev             # the app in your browser, the workspace simulated
npm run release         # build and deploy to the workspace
```

* **Build:** one self-contained bundle per view (`src/App.tsx`, then each `src/views/<Name>.tsx`), with React, your npm packages and the `@prisme.ai/mcp-apps` kit inside. The kit and its types ship in the repository (`vendor/`); `npm run sync-kit` takes the kit your platform serves under `/vendor/`. No public registry is needed beyond your npm mirror.
* **Deploy:** `npm run release` pushes the automations and the sources (the app opens in Builder with the same code), uploads the views as workspace assets with the app's sharing (`PRISMEAI_SHARING`) and sets the app in the workspace configuration: format, views, agent (`PRISMEAI_APP_AGENT`). It refuses to replace an app in the shared format.
* **Builder round trip:** `npm run check` lists the imports Builder cannot compile (packages not listed above). An app that has some still builds and deploys from the repository, but its Builder preview and **Deploy** fail.

Apps in the shared format keep the repository's `legacy-shared` branch.

## Verify the result

1. Open `/p/<workspace slug>#/dashboard` in a new tab, then select the browser's back button. The route stays in the address bar and the back button returns to the home page.
2. Optional: in the **Deploy** dialog, set access to **Public** and deploy again. In a private browser window, the home page loads without a session and shows **Sign in to open the live demo**.

## Troubleshooting

### `... is a platform module, not available in isolated apps`

**Cause:** the code imports a platform module, for example code copied from a shared app.

**Owner:** the workspace builder.

**Resolution:** replace the import with the matching `app` method. See [Migrate a shared app to isolated](https://docs.prisme.ai/products/ai-builder/migrate-to-isolated.md) for each replacement, then deploy again.

### `... is not provided for isolated apps`

**Cause:** the code imports a package that is not in the provided libraries.

**Owner:** the workspace builder.

**Resolution:** remove the import or replace it with a provided library, then deploy again.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.