# Hosting

A hosted Tritone is what links, iframes and Markdown images point to, and what a model in a chat calls to show a
diagram. It is two apps, and each can have a domain of its own:

| App | Address | What it is | Needs |
| --- | --- | --- | --- |
| Site | `/` | The landing page: what Tritone is, and the ways into the rest | Static files |
| Site | `/docs/` | This documentation, with every example drawn: a story in the page, Mermaid text in a frame of the embed page, in the model its block names | Static files |
| Editor | `/` and `/live.html#<source>` | The live editor: write on the left, watch on the right, share from the **Share** window. With `?view` before the `#` it opens for reading: the diagram has the window, and **Edit** brings the text back | Static files |
| Editor | `/embed.html#<source>` | The page an iframe shows: the diagram and nothing else | Static files |
| Editor | `/svg/<source>.svg` | A story as a self-playing image | A function that runs code |
| Editor | `/mcp` | The [MCP server](#the-mcp-server): a story played inside a conversation with a model | A function that runs code |

The site is built by [Astro](https://astro.build) from `apps/docs`, the editor is a React app in `apps/live`,
bundled by Bun, and the MCP server is `apps/mcp`. There is **no official public instance** yet. Until there is
one, run your own.

## On Vercel, on two domains

The repository is one Vercel project per app, both made from the same repository:

| | Site | Editor |
| --- | --- | --- |
| Root Directory | The root of the repository | The root of the repository |
| Framework Preset | Other | Other |
| Build Command | `bun run build:site` | `bun run build:editor` |
| Output Directory | `apps/docs/dist` | `apps/live/dist` |
| Environment variables | `SITE_URL`, `EDITOR_URL` | `SITE_URL` |
| Domain | Yours, such as `example.org` | Another, such as `editor.example.org` |

Vercel installs with Bun, as `bun.lock` asks, and needs nothing else. The two addresses are written into the pages
while they are built:

| Variable | It is | The site uses it for | The editor uses it for |
| --- | --- | --- | --- |
| `SITE_URL` | The address of the site, `https://example.org` | Canonical links, the sitemap, the picture of a shared link | Its brand and its Docs link, which lead to the site |
| `EDITOR_URL` | The address of the editor, `https://editor.example.org` | Every way into the editor, and every diagram the site frames | |

A build on Vercel without the address it needs stops, and says which one: a site that pointed at no editor, or an
editor whose header led nowhere, would deploy without an error and be broken. So does a build given half an
address, with no `https://`, or its own address in place of the other app's: the editor with the editor's address
as `SITE_URL` deploys, and its logo and its Docs link lead back to the editor. After a change of domain, set the
variable again and deploy both.

The site also builds with `apps/docs` as its Root Directory, with Vercel's Astro preset and no command to set.
Vercel reads the `vercel.json` of the folder a project is rooted in, so `apps/docs/vercel.json` repeats what the
site needs of the one at the root. The editor is rooted at the root of the repository: its image endpoint and the
rewrite that leads to it are there.

The image endpoint is `api/svg.js`, an Edge Function: `vercel.json` sends `/svg/<source>.svg` to it, and it gives the
answer the editor's own server gives at that address. Its code is `api/_svg.ts`, which the build commands above
first make into one file, `api/_svg.bundle.js` (`bun run build:api`): a function on Vercel cannot load the library
by its name, since the package hands out TypeScript, and Vercel refuses a deployment that tries. Vercel deploys the
function with a project whose Root Directory is the root of the repository, and only the editor's is the one its
links name. `vercel.json` also sends `/docs` to `/docs/`, and so does `apps/docs/vercel.json` for a site rooted in
its own folder: Vercel would serve the documentation at both, and at `/docs` its relative links would lead out of
it.

The MCP server is `api/mcp.js`, a function on Node.js: `vercel.json` sends `/mcp` to it. Its code is
`apps/mcp/vercel.ts`, which `bun run build:api` also makes into one file, `api/_mcp.bundle.js`, with the page it
hands a host written into it: a function there reads no file and builds nothing. It is deployed with the editor
and needs no variable of its own: the links it answers with lead to the domain the project is deployed to. A
project that sets `EDITOR_URL`, as the site's does, sends them there.

## Working on Tritone

```bash
bun install
```

```bash
bun run dev
```

The site is at <http://localhost:4321>, served by Astro, and shows an edit to a page or to a Markdown file of `docs/`
at once. An edit to the library shows in the documentation once the site is started again: Astro keeps each
Markdown file as it drew it until the file changes, so the site's `dev` and `build` throw that away first
(`--force`). The editor is at <http://localhost:3350>, served by Bun, with the embed page and the image endpoint. Each
points at the other. The MCP server is at <http://localhost:3351/mcp>, and the links it answers with lead to that
editor. Bun's development server watches the folder it is started in, so `dev` starts the editor from
the top of the repository: an edit to the design system or to the library then shows in the editor at once, where
a server started inside `apps/live` would go on serving what it first read.

## The editor in one process

For a host of your own instead of Vercel, with the address of the site:

```bash
SITE_URL=https://example.org bun run start
```

Without `SITE_URL`, the editor's brand and Docs link lead to `./` and `./docs/`, which this process does not serve.
This serves the editor, the embed page, the example page and the image endpoint on port 3350. Set `PORT` to change
it, and put it behind any reverse proxy that terminates TLS. The process keeps no state: there is no database and
nothing to back up, because every diagram lives in its own address. The site is static files: build it and put it on
any static host, as the next section says.

## The live editor

| It does | How |
| --- | --- |
| Draws while you type | A third of a second after the last key. The last diagram that worked stays while the text is wrong, and a story stays on the step you were looking at |
| Shows mistakes | The message under the text, and the line marked in it, for stories |
| Paints the story language | Statements, actors, arrows, values and comments. Mermaid text gets its comments only |
| Keeps three views beside the diagram | **Code**, **Samples** and **Export** are the views of one pane. The arrows, `Home` and `End` walk their names and show the view they land on; `Tab` leaves the names for the view |
| Loads samples | The **Samples** view: one press on a name. The text is written in the **Code** view, which is shown again, as one edit: undo brings your text back. The sankey sample also picks the `sankey` model, the one that draws its amounts |
| Picks a model | The **Model** menu, at the end of the row of the views when the text is Mermaid: a story has its own floor |
| Opens a menu its own way | A menu is drawn by the editor, not by the browser. A press or `Enter` opens it, the arrows, `Home` and `End` walk it, a typed word jumps to the option that starts with it, `Enter` chooses and `Escape` leaves. The keyboard stays on the menu the whole time |
| Fills the window | From 1000px it is laid out as a design tool is: the diagram is a canvas that has the window, the pane is one panel that floats on it, with the same room around both, and the editor is exactly the window. The page does not scroll: the text and a tall diagram scroll in their own halves. Narrower, it is one column the page scrolls, under a header that stays |
| Says what it waits for | The first Mermaid text fetches Mermaid, which is large: the line over the diagram says so until it is there. A fetch that failed is tried again with the next text |
| Lets you move around | The diagram is a [canvas](embedding.md#the-canvas): pinch or `Ctrl` and the wheel to zoom, drag or, once zoomed, scroll to move, **Fit** to see it all. A change in the text keeps your zoom, your place and the angle of a graph view |
| Moves actors and nodes | Drag an actor of a story to another floor cell, or a node of a graph view anywhere: its place is written into the text, as one edit that undo takes back |
| Gives the diagram the window | **Hide source** puts the pane away, and the diagram goes to the edges of the window; **Edit** brings it back. The address says which of the two it is, with `?view` |
| Shares | **Share** opens a window with what to hand out, each with a button that copies it. **Link** is the address for reading: whoever opens it gets the diagram on the whole window with its controls, and presses **Edit** to change it. **Embed** is the iframe of the bare diagram and **Markdown image**, for a story, the picture that plays by itself, written as a link to that address so that a press on it opens the diagram: see [embedding](embedding.md). `Escape`, the close button or a press on the page behind closes the window, and the keyboard goes back to **Share** |
| Exports | The **Export** view. **Save SVG** is the self-playing file of a story, or a graph view from its starting angle. **Save PNG** and **Copy PNG** are a still picture: a story at the step on screen, settled, in the theme of the editor, at one to four pixels a unit (**PNG scale**). **Save text** saves the source, and **Open a file…** reads one in, as one edit |
| Says a mistake where you are | Under the text while you write. With the pane put away, in the line over the diagram: a shared address that holds a broken text does not open on an empty window |

The editor's address is the diagram, in the same format the embed page reads
([the format](embedding.md#1-iframe)): the two differ by the word `embed.html`, and by `view`, which only the
editor reads. Nothing is saved anywhere else, neither on a server nor in the browser, so bookmark the address of a
diagram you want to keep.

React draws the editor out of the components of `packages/ui`: the header, the views of the pane, the menus, the
buttons, the Share window and the texts it hands out. The messages
and the layout are the editor's own (`apps/live/live.css`), and tests measure that layout in a browser
(`apps/live/live.test.ts`). The diagram is drawn by the library, in an element React does not touch. The source is a textarea with a painted copy of its text behind it, with no editor library:
React paints the copy and never sets the textarea, which keeps the writer's undo. The Tab key leaves it, as in any
form field. Mermaid is fetched only when a text is neither empty nor a story.

## The MCP server

A chat shows no picture from an address its model wrote, and a link takes the reader out of the conversation. The
MCP server puts the player in it. It is an [MCP App](https://modelcontextprotocol.io/extensions/apps/overview): a
tool a model calls, and the page a host draws in the conversation when it does. Claude and ChatGPT draw MCP Apps,
as do other hosts; one that does not still has the tool, and its answer.

| It has | Which is |
| --- | --- |
| One tool, `show_story` | It takes `text`, a story, and `step`, the step to open on, counted from 1, which can be left out. Its description carries the [story language](language.md) in a compact form, so a model writes a story with nothing else at hand |
| An answer in words | For a good story: that it is drawn, and a link that opens it in the live editor (`/live.html?view#<source>`), for a reader whose host draws no interface. For a mistake: the library's own message, with its line, as an error, so the model corrects the text and calls again |
| An interface, `ui://tritone/story.html` | One page of about 67 kB, 25 kB compressed, with the library inside it. The host draws it in a sandboxed frame and hands it the story. It mounts the player as a [canvas](embedding.md#the-canvas), in the host's light or dark theme, and tells the host how tall it is. It asks no other host for anything |

It shows stories only. Mermaid text is refused, in the library's words: Mermaid would be fetched from another
host, and the frame of an MCP App loads nothing its server did not declare.

| Transport | Start it | A client is given |
| --- | --- | --- |
| Streamable HTTP, on Vercel | It is deployed with the editor, as `api/mcp.js` | `https://editor.example.org/mcp` |
| Streamable HTTP, in one process | `bun run --filter @tritone/mcp start` | `http://localhost:3351/mcp`; `PORT` changes the port |
| stdio | The client starts it | The command `bun /path/to/tritone/apps/mcp/stdio.ts` |

`EDITOR_URL` is the address of the live editor, for the link: `bun run dev` sets it to the editor beside it, and a
server given none answers with no link. The server keeps no state and asks nobody to sign in: each request is
answered on its own, and a page of any origin may ask. A client that starts local servers usually reads an entry
of this shape:

```json
{
  "mcpServers": {
    "tritone": {
      "command": "bun",
      "args": ["/path/to/tritone/apps/mcp/stdio.ts"],
      "env": { "EDITOR_URL": "https://editor.example.org" }
    }
  }
}
```

### Trying it in a host

| Host | Steps |
| --- | --- |
| Claude | **Settings**, **Connectors**, **Add custom connector**, and the address of the server, ending in `/mcp`. Then ask for a diagram: "show me, as a Tritone story, what happens when I log in" |
| ChatGPT | At [chatgpt.com/plugins](https://chatgpt.com/plugins): the plus button, **Add custom MCP server**, the address, **Create as a plugin**. Then, in a new conversation, `@` and the plugin. [OpenAI's page](https://developers.openai.com/plugins/deploy/connect-chatgpt) has every step |
| The example host of the Apps SDK | In a copy of [`modelcontextprotocol/ext-apps`](https://github.com/modelcontextprotocol/ext-apps): `npm install`, then in `examples/basic-host`, `SERVERS='["http://localhost:3351/mcp"]' npm start`, and open <http://localhost:8080> |

Claude and ChatGPT reach a server from their own machines, so it needs a public address: the deployed one, or a
tunnel to the local one, such as `npx cloudflared tunnel --url http://localhost:3351`.

The server is made with the SDK of the protocol. The page is not: what it says to its host is a few messages,
written in `apps/mcp/view.ts`, and the tests open it in a browser against the SDK's own host.

## The look

The landing page, the documentation and the live editor are made of one design system, `packages/ui`: the tokens,
and the components a page is made of (the header, a button, a menu, a block, a card, the window of a demo), each a
stylesheet and a React component. The editor draws them in the browser, and the site's build writes their markup
into its pages. Its `README.md` lists them, with the rules a page follows. To give a hosted site your own look, change `packages/ui/tokens.css`: `--accent` is
the one colour, and the surfaces are four greys per theme. The logo in the header is the `Logo` component: the head of
a trident as a cut stone and, beside it, the name. The mark is `--brand`, the same in both themes, and its facets
are that colour a shade darker. The mark alone, with its colours written out, is `packages/ui/mark.svg`, the icon
of every page.

Text, titles and controls are set in Instrument Sans, the word a title marks in Instrument Serif italic, and the
name in the logo in Montserrat. All three are under the SIL Open Font License and are files of the repository,
90 kB together, in `packages/ui/fonts` with their licences: no page asks another host for a font. The site's pages
ask for the ones of their first screen at once, as files beside their styles, and publish the licences at
`/fonts/`. The editor's pages carry Instrument Sans and Montserrat inside their stylesheet, where the bundler puts
them: on a domain of its own, the editor has no site beside it to fetch them from. They leave the serif out, since
no page of the editor shows it. Montserrat is the whole Latin font, 38 kB, for the seven letters of the name: when
the font of the logo is settled, the name becomes outlines in the `Logo` component and the file goes. The embed page carries none. Code, small labels and diagrams use the system's monospace font.

The pages open in the dark theme, whatever the reader's system says, and the button at the end of the header
switches to the light one. The choice is kept in the browser (`localStorage`, under `theme`), for each domain. A
diagram the site frames from the editor's domain follows the page around it: the frame asks for its theme when it
opens, and is told when the reader changes it. What travels between them is the name of a theme and nothing else.
That script, `packages/ui/theme.js`, is in every page. A page of the site that shows a story has one more line of
script: a story plays only while it is in view, so the reader reaches it at its first step, and one out of sight
costs nothing (with one story of nine in view, Chrome's main thread works about 45% less). For a reader who asked
for less motion, the stories stay still. To open in the light theme, or to follow the system, change `data-theme`
on the `<html>` of a page: `light`, or no attribute at all.

Diagrams are not styled by that file. They keep their own theme, which a page changes through the
[theme properties](embedding.md#theming).

## For search engines and agents

`SITE_URL` is the public address of the site, set when the site is built. On Vercel a build that is not given one
takes the domain the project is deployed to, so the site has its address there without being told:

| With it | Without it |
| --- | --- |
| Every page has a canonical link and says its address in its structured data and in `og:url` | Pages link only by relative addresses, and say nothing about where they are |
| A link to a page is shown with a picture, `docs/assets/og.jpg` (`og:image`, 1200 by 630) | A link is shown with the title and the description only: a preview is fetched from elsewhere, and needs a whole address |
| `/sitemap.xml` lists the landing page, every page of the documentation, and the editor when it is beside the site | No sitemap: one must list whole addresses |
| `/robots.txt` allows everything and points to the sitemap | `/robots.txt` allows everything |
| `/llms.txt` lists every page by its whole address | `/llms.txt` lists them by addresses relative to itself |

What does not depend on it: every page has its own title and, under it in a search result, its own first
paragraph; the landing page describes Tritone as schema.org `SoftwareApplication` and a page of documentation
describes itself as `TechArticle`; the embed page asks not to be indexed, since it is a viewer of whatever its
address carries.

An agent does not read pages the way a search engine does, so the site also hands out its text:

| Address | It is |
| --- | --- |
| `/llms.txt` | What Tritone is, the link an agent writes to hand a diagram to its reader, and every page of the documentation with a line about it, in the format of [llmstxt.org](https://llmstxt.org). It follows the menu: a page added there is listed here |
| `/llms-full.txt` | The whole documentation as one file of Markdown |
| `/docs/<name>.md` | A page as the Markdown it is written in, beside its page: `/docs/language.md` is all an agent needs to write a story. Each page of the documentation names its own in a `<link rel="alternate" type="text/markdown">` |

The landing page is about 10 kB compressed, plus two small stylesheets, the icon and three fonts. Compression is the
host's work: Vercel does it, as most static hosts do.

## Static hosting

If you need neither Markdown images nor the MCP server, no code runs on the server:

```bash
bun run build
```

This writes the site in `apps/docs/dist` and the editor in `apps/live/dist`, and beside them the files of
the [extension for VS Code and Cursor](embedding.md#in-vs-code-and-cursor) in `apps/vscode/dist`, which no host
needs. Upload each to a static host (GitHub
Pages, Netlify, an S3 bucket, a folder behind nginx), one per domain, after building with `SITE_URL` and
`EDITOR_URL` set. To put both on one domain, build with neither, and copy the editor's files beside the site's,
all but its `index.html`: the links between them are then relative. The site's styles are at `/_astro/`, so it is
served from the top of its domain; under a path such as `https://example.org/tritone/`, set `base` in
`apps/docs/astro.config.mjs`. The editor loads Mermaid from jsDelivr, pinned to 12.3.0 with an integrity hash, and
only when a diagram is Mermaid text.

Markdown images will not work on a static host: nothing is there to draw them. Nor will the MCP server, which
answers each call.

## The image endpoint elsewhere

`handleSvg` takes a standard `Request` and returns a standard `Response`, so it mounts on any runtime that speaks
them:

```ts
import { handleSvg } from 'tritone/server'

// Bun, Deno, Cloudflare Workers
export default { fetch: handleSvg }
```

It answers `GET …/<source>.svg`: whatever comes before the last path segment is ignored, so mount it on the route
you like. The source is base64 or the text itself, percent-encoded, as in [every address](embedding.md#in-a-chat). On Vercel it is mounted by `api/svg.js`, from the code in `api/_svg.ts`.

What it guarantees:

- **Same address, same picture**, for as long as the renderer is the same. A good answer is kept for a day by
  whoever fetched it, so a new renderer reaches the pages that already showed a picture the day after.
- **A wrong diagram is still a picture**: a small SVG with the error and status 400, so the mistake is visible
  where the image is.
- **Bounded input.** Addresses over 12,000 characters get status 414.
- **No script, no outside request** in the SVG it returns, and every piece of text from the diagram is escaped.

## What a host can see

- **Live editor and iframe**: nothing. The diagram is after the `#` of the address, which browsers do not send.
  For Mermaid text the model is before the `#` (`?model=city`), and that one word is sent.
- **Image**: the diagram text, because it is in the path. Do not put secrets in a diagram you turn into an image
  address, and expect it to appear in server logs.
- **MCP server**: the story, which the tool is called with, and nothing about its reader: the page it hands a
  host asks it for nothing again. It keeps nothing, but expect stories in its logs, as for an image. The host of
  the conversation sees the story as it sees the rest of the conversation.
- **Site**: which page is read. Its script keeps the theme the reader chose in the browser's own storage, and sends
  nothing. The landing page, the first page of the documentation and its page about the models show Mermaid
  diagrams in frames of the editor's embed page: each is loaded, with Mermaid, when the reader scrolls to it. The
  editor's host sees that a frame was opened and the one word of its model, as for any iframe, not the diagram.