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

# Show a tool's result in a view

> Render a tool's result in an MCP Apps host with the typed client and React.

An MCP Apps host can render a tool's result in a view: a small browser
module that binds Elements components to your functions with the typed
client. Declare the view in the backend as data with `uiResource(spec)` and
attach it to tools by value. The backend never imports the view's code.

```ts notes.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { mutation, query, role, t, table } from "@bijectionhq/bijection";

export const rows = table({ owner: t.principal, title: t.string, archived: t.boolean }, {
  index: [["owner", "archived"]],
});

export const mine = query({ allow: [role.signedIn] }, (ctx) =>
  ctx.list(rows.where({ owner: ctx.frame.as.id, archived: false }).first(50)),
);

export const archive = mutation({ allow: [role.signedIn], args: { id: t.key(rows) } }, async (ctx, { id }) => {
  const note = await ctx.get(rows(id));
  if (note === null || note.owner !== ctx.frame.as.id) return ctx.refuse("Absent", "No such note");
  await ctx.patch(rows(id), { archived: true });
});
```

```ts app.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { app } from "@bijectionhq/bijection";
import { mcp, tool, uiResource } from "@bijectionhq/library-mcp";
import * as notes from "./notes";

const board = uiResource({
  name: "board",
  entry: "./views/board.tsx",
  // csp: { resourceDomains: ["https://images.example.com"] },
  // permissions: { clipboardWrite: {} },
});

export const server = mcp({
  server: { name: "notes", version: "1.0.0" },
  tools: {
    mine: tool(notes.mine, { description: "List my notes", view: board, app: true }),
    archive: tool(notes.archive, { description: "Archive one of my notes", app: "only" }),
  },
});

export default app({ notes, server });
```

`name` is lowercase words joined by `-`. `entry` is a string literal,
relative to the directory that holds `app.ts`. At deploy the
CLI bundles it with its imports into one HTML document, with scripts and
styles inline, served at `ui://<server path>/<view name>.<digest>.html`. Its
URI changes when its bytes change. `csp` (`connectDomains`,
`resourceDomains`, `frameDomains`, `baseUriDomains`) and `permissions`
(`camera`, `microphone`, `geolocation`, `clipboardWrite`) default to none,
so a view reaches no network origin. `bijection.lock` records each origin
and permission you add.

## Which tools a view may call

`app` sets which tools a view may call, and views may call none by default.
`app: true` lets the model and views call the tool. `app: "only"` hides it
from the model. The library always states `_meta.ui.visibility`, because
the MCP Apps default would let views call every tool. A tool with a view
also needs `app` if its view reads it again.

`app` controls what the host offers; it grants nothing. A view's call runs
as the user, under the function's label, and every guard judges it. The
server cannot tell a view's call from the model's, so put a confirmation
step inside any destructive function.

A client without MCP Apps support gets no `_meta.ui` and no `app: "only"`
tools. Every tool still returns `content` and `structuredContent`, so the
text answer works on its own.

## Write the view

`@bijectionhq/library-mcp/view` starts the view and gives the transport for
`connect`. In React, render your component under `<Bijection client={client}>`
from `@bijectionhq/bijection/react`, so `useWatch` and `useMutation` work as
in any React app, and subscribe to `view.state()` with `useSyncExternalStore`:

```tsx views/board.tsx theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { useSyncExternalStore } from "react";
import { createRoot } from "react-dom/client";
import { api, connect } from "@bijectionhq/bijection/client";
import { Bijection, useMutation, useWatch } from "@bijectionhq/bijection/react";
import { mountView } from "@bijectionhq/library-mcp/view";
import type App from "../app";

const view = mountView();
const app = api<typeof App>();
const client = connect({ contract: "discover", transport: view.transport });

function Board() {
  const { phase } = useSyncExternalStore(view.subscribe, view.state, view.state);
  const notes = useWatch(app.notes.mine, {});
  const archive = useMutation(app.notes.archive);
  if (phase === "connecting" || notes === undefined) return <p>Loading</p>;
  if (!notes.ok) return <p>{notes.message}</p>;
  return (
    <ul>
      {notes.value.map((note) => (
        <li key={note.id}>
          {note.title} <button onClick={() => archive({ id: note.id })}>Archive</button>
        </li>
      ))}
    </ul>
  );
}

const root = createRoot(document.getElementById("root") ?? document.body);
root.render(
  <Bijection client={client}>
    <Board />
  </Bijection>,
);
```

Without React, call `client.query` and `client.mutate` directly:

```ts views/standalone.ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { api, connect } from "@bijectionhq/bijection/client";
import { mountView } from "@bijectionhq/library-mcp/view";
import type App from "../app";

const view = mountView();
const app = api<typeof App>();
const client = connect({ contract: "discover", transport: view.transport });
const notes = await client.query(app.notes.mine, {});
```

`mountView()` performs the `ui/initialize` handshake over `postMessage`.
The iframe holds no credential. Each `client.query` or `client.mutate` is
one host `tools/call`, made by the host as the user, of the tool the server
names for that function's tree path in `_meta["com.bijection/tools"]`, else
of the tool named by the path itself. `mountView({ tools })` maps other
paths to tool names. `view.state()` exposes `input`, `result` and
`hostContext` as sent by the host. `mountView` applies the host's style
variables, and `applyHostTheme` maps them to the tokens the Elements read.

## How a view stays current

The Apps protocol gives a view no subscription, so `client.watch` and
`useWatch` keep their signatures and read again:

* The first value comes from the tool result, when the query and its
  arguments are those of the tool that opened the view.
* It reads again after each call the view makes that is not a watched query.
* It reads again when the view becomes visible.
* It polls from 2 s, doubling while unchanged up to 60 s, and pauses while
  hidden. `mountView({ refresh: { min, max } })` changes the bounds in
  milliseconds, and `refresh: "manual"` turns polling off.

Each read sends `_meta["com.bijection/ifVersion"]` with the version it
holds, and the server answers "unchanged" without the body when nothing
changed.

## Results in a view

Every call answers a `Result`. A refused call answers the typed refusal the
host or the server set in `_meta["com.bijection/error"]`: its `code`, its
public `reason`, `message`, an app refusal's declared `data` and, on
`Forbidden`, `guard`. A failure in the view itself is
`Failed`, and a lost host reply is `Unknown`.

`client.mutate` and `useMutation` mint a fresh `once` for each call by
default and send it as `_meta["com.bijection/once"]`. To retry an `Unknown`,
pass its `once` with the original arguments. See
[Retries answer once](https://docs.bijection.com/mcp/native-tools.md#retries-answer-once).

`client.trial`, `optimistic` edits and framed clients (`.as`, `.under`,
`.in`, `.at`) are not available in a view. They answer `Failed` with an
`UnsupportedInView` sentence that names the alternative.

## Preview a view

Preview a view against the deployment in a local host page:

```sh theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
bijection mcp view mcp mine '{}'
```


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