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

# Build an agent into your product

> Put a Formation agent inside your own web app, dashboard or mobile app, with streaming answers, your app's context and tools that act as your users.

Formation agents do not have to live only in Slack or Teams. The same agent can
sit inside your own product: a chat panel in your web dashboard, an assistant
screen in your mobile app, a support widget on your site. The agent keeps its
identity, memory and tools, and your product decides who the user is and what
they are looking at.

This page walks through the whole pattern, using one worked example throughout.
Each step links to the reference page that owns the contract. For every way an
agent can connect to your systems, including REST APIs, account linking and
self-hosted models, see [Connect agents to your systems](https://docs.formation.texturehq.com/connect-your-systems.md).

## The worked example

An operations company runs a web dashboard and a companion iOS app for its
customers. It wants one assistant in both:

* A signed-in user opens the assistant from any screen and asks questions in
  plain language.
* The answer streams in as it is written, with live activity ("Looking up
  devices", "Reading the report") while the agent works.
* The agent knows which organization and workspace the user is in and what
  screen they asked from.
* The agent can look things up and take actions in the company's own system,
  limited to what that user is allowed to see.
* Conversations survive closing the app: the user can come back to the list
  of their past conversations and pick one up.
* The company's engineers can see exactly what the agent did on a given turn.

The same agent also has a Slack connection for the company's own staff. Nothing
below changes that.

## Architecture

```mermaid theme={null}
flowchart LR
    W[Web dashboard] --> G
    I[iOS app] --> G
    subgraph Your backend
      G[Agent gateway]
      MCP[MCP server with your tools]
    end
    G -- signed messages, snapshots, SSE --> F[Formation]
    F -- tool calls --> MCP
    MCP --> D[(Your data)]
```

The key design rule: **your clients never talk to Formation directly.** A small
server-side gateway in your backend holds the connection credentials and speaks
to Formation for both the web app and the mobile app. The browser and the iOS
app only talk to your gateway, with your own session or token.

The gateway:

1. Authenticates the user with your existing auth.
2. Decides the user's `actor_id` and current scopes from your own records.
3. Signs requests to Formation with the connection's secret.
4. Relays snapshots and the event stream back to the client.

The secret never ships in a bundle or binary, and a client can never claim to be
a different user, because the client never chooses the `actor_id`.

## Step 1: Create a webhook connection

On the agent's **Connections** page, add a **Webhooks** connection (the generic
connector). Formation shows an install id, a signing secret and the events URL
once. Store them as server-side secrets for your gateway.

You can leave the delivery URL empty. An application that reads replies through
snapshots and streaming, as in this example, does not need Formation to POST
replies back. Set a delivery URL only if you also want push delivery.

Turn on **Stream answers** for the connection so answers arrive incrementally.

Reference: [Webhooks](https://docs.formation.texturehq.com/connectors/generic.md), [Application streaming](https://docs.formation.texturehq.com/connectors/application-streaming.md#opt-in-per-connection).

## Step 2: Send messages as the signed-in user

When the user sends a message, the client calls your gateway. The gateway posts a
signed message to `POST /generic/events`:

```json theme={null}
{
  "message_id": "msg_2f1c...",
  "conversation_key": "ws-1842:conv-77",
  "actor_id": "ws-1842:user-301",
  "actor_display_name": "Dana Ruiz",
  "body": "Which sites had the most alerts this week?",
  "topology": "direct",
  "session_scopes": [
    { "kind": "organization", "id": "org-55", "name": "Northfield Energy" },
    { "kind": "workspace", "id": "ws-1842", "name": "Operations" }
  ]
}
```

Choices that matter:

* **`actor_id` comes from your auth, never from the client.** If one person can
  use several workspaces, give them a distinct actor id per workspace, as above,
  and check membership before each request. That keeps what the agent learned
  in one workspace out of another.
* **`message_id` is stable.** Generate it once on the client or gateway and
  reuse it on retries. Formation de-duplicates on it.
* **`session_scopes` tells the agent where the user is.** Up to four scopes,
  such as organization, workspace, project or site. They are context the
  agent sees for this message, not permissions.
* **`topology: "direct"`** makes this a private conversation between the user
  and the agent, which is what lets the user read it back through the stream.

A `200` means the message is stored durably and the run will happen, even if
Formation restarts. The reply arrives through the stream.

Reference: [Sending a message](https://docs.formation.texturehq.com/connectors/generic.md#sending-a-message-to-the-agent).

## Step 3: Stream the answer

To show the conversation, the gateway first asks for a snapshot, then subscribes
from that point:

1. `POST /generic/conversations/snapshot` returns the full conversation state:
   messages, runs, activity, pending inputs, and a `sequence` number.
2. `POST /generic/conversations/stream` with `after_sequence` set to that
   number returns a server-sent event stream: `message.upsert`, `text.delta`,
   activity and input events, each with the next sequence.

The gateway relays the stream to the browser as server-sent events and to the
iOS app over its own streaming request. If the connection drops, reconnect with
the last sequence you applied. If Formation reports a gap or asks for a fresh
snapshot, reload the snapshot and subscribe again. The agent keeps working in
Temporal while no one is connected, so closing the app never stops a run.

Short answers can arrive as one complete message instead of a stream of deltas,
and incremental text depends on the model provider supporting streaming. Your
client should handle both.

Disable response buffering in any proxy between Formation and the client, or the
stream will arrive in one lump at the end.

Reference: [Snapshot](https://docs.formation.texturehq.com/connectors/application-streaming.md#snapshot),
[Subscription and replay](https://docs.formation.texturehq.com/connectors/application-streaming.md#subscription-and-replay),
[Activity](https://docs.formation.texturehq.com/connectors/application-streaming.md#activity-and-explicit-follow-ups).

## Step 4: Show past conversations

The gateway can list the user's own private conversations with
`POST /generic/conversations/list`, and search them with
`POST /generic/conversations/search`. Each entry returns a `conversationKey`
to open with the snapshot and stream calls, and whether it still has unfinished
work. A user only ever sees conversations recorded under their own `actor_id`
on that connection.

Reference: [Conversation history](https://docs.formation.texturehq.com/connectors/application-streaming.md#conversation-history).

## Step 5: Give the agent your app's tools

The webhook connection carries conversation only. To let the agent act inside
your system, expose your API as a remote MCP server and connect it on the
agent's **Tools** page, with OAuth or a dedicated bearer token for the agent.
Formation discovers the tools, honors their read-only and destructive
annotations, and gates, budgets and audits every call.

In the example, the MCP server offers tools such as "search sites", "get device
history" and "open a work order". Tools can also supply short public activity
labels, which is where the "Looking up devices" lines in the app come from.

Reference: [Remote MCP](https://docs.formation.texturehq.com/runtime/remote-mcp.md), [Pair it with tools](https://docs.formation.texturehq.com/connectors/generic.md#pair-it-with-tools-from-the-same-system).

### Acting as the user

An agent's MCP connection authenticates the agent, not your user. If each user
should only see their own data through the agent, the MCP server has to restrict
each call to what the asking user can access.

Formation can attach execution context to read-only tool calls that originate
from a private webhook conversation: which agent, connection, actor,
conversation and run the call belongs to. Your MCP server uses that context to
look up a short-lived grant your own system issued for that user, and refuses
the call without one. Formation never forwards a user's credential, and the
context alone is not authorization. Tools opt in by declaring the extension; read
[Delegated MCP queries](https://docs.formation.texturehq.com/runtime/delegated-mcp.md) for the contract your server must
implement.

## Step 6: Let the user answer the agent

Sometimes the agent needs a decision, such as whether to keep going on a long
task. These arrive in the snapshot and stream as **inputs** with a title and a
pending status. Render them as a prompt in your UI, and send the user's answer
back through `POST /generic/events` as a signed message with an `input_response`
naming the input and whether it was approved. Only one answer wins, retries with
the same `message_id` are safe, and the receipt appears in the stream once it is
recorded.

The agent can also collect things that do not belong in chat:

* **Secrets** through a one-time [vault link](https://docs.formation.texturehq.com/runtime/agent-vault.md).
* **Drawings** through a [sketch](https://docs.formation.texturehq.com/runtime/sketches.md) canvas link.

Both links point at Formation's `PUBLIC_URL`, so the user must be able to open
it. See [Configuration](https://docs.formation.texturehq.com/operations/configuration.md#features-that-need-a-public-url).

Reference: [Application input decisions](https://docs.formation.texturehq.com/connectors/application-inputs.md).

## Step 7: Inspect what happened

For your engineers, two read paths are available:

* **Run diagnostics from your servers.** A server-side tool holding an operator
  API token can read the recorded evidence for a run: the model input, the tools
  offered and called, and timings. See
  [Application diagnostics](https://docs.formation.texturehq.com/runtime/application-diagnostics.md).
* **An inspector in your own app.** Create a read-only diagnostic key for the
  agent on its **Diagnostic keys** page and paste it into your inspector. The key
  reads diagnostics directly from Formation and cannot send messages, run tools
  or change configuration. See [Inspector access](https://docs.formation.texturehq.com/runtime/inspector-access.md).

The chat connection's signing secret never grants diagnostics, and a diagnostic
key never grants chat. Never put an operator API token in a browser or mobile app.

## Checklist

* A webhook connection with **Stream answers** on, credentials stored only in
  your backend.
* A gateway that derives `actor_id` and `session_scopes` from your own auth and
  signs every request.
* Snapshot, then stream from its sequence; reload the snapshot on a gap.
* A remote MCP server for your tools, connected to the agent.
* An HTTPS `PUBLIC_URL` that your servers and your users can reach.
* A test: send a message from the app, close it mid-answer, reopen it, and check
  the full answer and the conversation list.


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