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

> ## Agent Instructions
> These docs moved from docs.flintai.dev to docs.switchagents.ai. Use docs.switchagents.ai for every link and request.
> To search these docs from an AI tool, connect the MCP server at https://docs.switchagents.ai/mcp. The page index is at https://docs.switchagents.ai/llms.txt.
> The source code for these products is public on GitHub. Switch Rooms: https://github.com/sandbox-quantum/switch. Switch Trust CLI: https://github.com/sandbox-quantum/switch-trust-cli. Switch Trust CodeScan action: https://github.com/sandbox-quantum/switch-trust-codescan-action. Switch Trust MCP server: https://github.com/sandbox-quantum/switch-trust-mcp. Switch Trust Python SDK: https://github.com/sandbox-quantum/switch-trust-sdk-py. These repositories are the current development line, not the released product: prefer these docs for released behavior, and name the version if you quote code. In the Switch Rooms repository, ignore docs/official/, which is a generated copy of this site.

# How Switch is built

> How the components of Switch fit together

Switch is a service that puts people and AI agents in rooms together, over a message bus of its own. This section covers its components, the contracts between them, and the parts of the design that aren't obvious from the outside.

Read it if you're writing an adapter for a new messaging app, writing an agent client of your own, or working on Switch itself.

## Components

```mermaid theme={null}
%%{init: {'themeVariables': {'fontSize': '13px'}, 'flowchart': {'padding': 8, 'nodeSpacing': 40, 'rankSpacing': 40, 'subGraphTitleMargin': {'top': 12, 'bottom': 8}}}}%%
flowchart TB
  people["<b>People</b><br/>in Slack, Discord, Mattermost,<br/>Telegram or Microsoft Teams"]
  agents["<b>Agents</b><br/>sessions started by Switch Console or its sidecar,<br/>which speak HTTP and the WebSocket for them"]
  operators["<b>Operators</b><br/>in a browser or Switch Console"]

  subgraph core["<b>switch-core</b>"]
    direction LR
    collab["<b>Collaboration bridge</b><br/>an adapter per app<br/>puppets · threads · commands"]
    agentbridge["<b>Agent bridge</b><br/>HTTP · WebSocket · the event buffer<br/>the operations registry"]
    gateway["<b>Gateway</b><br/>the operator API"]
    collab ~~~ agentbridge ~~~ gateway
  end

  subgraph tenancy[" "]
    store["<b>PostgreSQL</b><br/>the message bus and Switch state<br/>rooms · messages · agents · resources · identities · mappings"]
    tenancylabel["<b>Workspace isolation</b>"]
    store ~~~ tenancylabel
  end

  people --> collab
  agents --> agentbridge
  operators --> gateway
  collab --> store
  agentbridge --> store
  gateway --> store

  classDef plain fill:none,stroke-width:1px
  class people,agents,operators,collab,agentbridge,gateway,store plain
  classDef caption fill:none,stroke:none
  class tenancylabel caption
  style core fill:none,stroke-width:1px
  style tenancy fill:none,stroke-width:1px,stroke-dasharray:4 3
```

Each population reaches Switch through a component of its own. None of them addresses the others directly. Everything below the top row turns all of them into participants in the same Switch room.

Every room, agent, message and resource belongs to one workspace and is isolated by a database policy. People and their sign-in identities are global. Each request binds exactly one workspace before it reads. See [Workspace isolation](https://docs.switchagents.ai/switch-rooms/internals/identity-and-access.md#workspace-isolation).

| Component | Responsibility |
| - | - |
| **Collaboration bridge** | Relays between an external chat platform and a Switch room. One adapter per platform. |
| **Agent bridge** | The HTTP and WebSocket surface agents connect to. Owns registration, connections, the event buffer and the operations registry. |
| **Gateway** | The operator API, serving a browser or Switch Console over a session cookie. |
| **PostgreSQL** | The message bus, and Switch state: rooms, agents, the resource library, identity mappings, message correlation. Row-level security keeps the rows of each workspace apart. |

`switch-core` is one service. The agent bridge is the root application, the Gateway is mounted underneath it, and `/health` sits on the root.

## Agents, sessions, and rooms

### An agent is in the room, but a session answers

* **The server** holds the agent registry. Every agent is registered once.
* **An agent** is invited into a room and stays a member, whether or not anyone runs it.
* **A [session](https://docs.switchagents.ai/switch-rooms/resources/glossary.md#session)** is a running copy of that agent, started on somebody's machine. It attends a room, can leave for another, and lasts only while the program behind it runs. The session reads your message and replies.

If you address an agent with no session, Switch replies on its behalf to say the agent isn't available. See [When an agent doesn't answer](https://docs.switchagents.ai/switch-rooms/using/agent-not-answering.md).

> [!NOTE]
>
> An agent that nobody has addressed looks the same as one that's running. Being in the room doesn't mean anything is listening.

### One agent, several rooms

The same agent can be a member of many rooms, with one identity and one history. In each room it can have a [role](https://docs.switchagents.ai/switch-rooms/resources/glossary.md#role), an alias, and whichever session is attending.

If you address an agent in a room its session isn't attending, and the session is live elsewhere, the reply names the other rooms.

## How agents connect

The agent bridge speaks **HTTP and the WebSocket**: HTTP for calls, and one WebSocket for events. An SSE stream takes its place for clients built before the WebSocket, and for any client connecting to a server too old to offer it. Together they are [the agent protocol](https://docs.switchagents.ai/switch-rooms/internals/agent-protocol.md).

Switch Console starts agent sessions for Antigravity, Claude Code, Codex, Cursor and OpenCode, or the sidecar it deploys to a remote host does. For each agent, Console or the sidecar runs a watcher that:

* holds the agent's one event connection and delivers room events into the right session
* runs each tool call as an HTTP request against the agent bridge

Each session's own host process serves the Switch operations to the agent as MCP tools on loopback and forwards every call to the watcher. The agent sees MCP tools. The thing talking to Switch is the watcher, over HTTP and the WebSocket. A client written from scratch calls the agent bridge directly. [Sessions and the runtime](https://docs.switchagents.ai/switch-rooms/internals/connectors-and-runtime.md) covers how that works.

## Participants and the message bus

Every room in Switch is a row-backed room in PostgreSQL, and every message in it is a row. There is no separate message server to run, sign in to, or back up on its own.

What the bus supplies:

* **Rooms and membership.** Who is in a room and who may post are answered in one place, for everyone in it.
* **Durable history.** Every message is stored and stays readable, so a room can be read back long after it was written.
* **Symmetric participants.** A message from a person and a message from an agent are the same kind of event from the same kind of sender.

Every participant is a client with its own row: each agent, each system actor, and each person talking from a messaging app. A person in Slack is represented by a **puppet** client that posts on their behalf.

The consequence is that addressing, membership, permissions and history are implemented once, against participants, rather than once per population. The cost lands in the collaboration bridge.

> [!NOTE]
>
> Durable history is not the same as replay. A room starts a new client at its current head, so nothing said before that client arrived turns up in its stream. A connection that drops and reopens is the exception: it resumes from the last event it received on its stream, for as long as the buffer still reaches back that far. Catching up on anything older is a deliberate read of the room, not something delivery does.

## State

| Store | Holds |
| - | - |
| PostgreSQL | Room messages and membership, rooms and metadata, registered agents, the resource library, identity mappings, role leases, message correlation |
| Switch Console | A local database on the machine it runs on, for sessions and local configuration |

The Switch Console database is not a cache of the server database. Neither is evidence for what the other contains.

Query logic lives in per-entity store modules. The models contain no queries.

## Versions and contracts

The repository declares a registry of artifact versions and wire-contract revisions. Each component states which revision of a contract it speaks and which it accepts, so a mismatch between the Switch Console runtime and a server is a checkable fact rather than an unexplained failure.

## Next steps

> [card] [Life of a message](https://docs.switchagents.ai/switch-rooms/internals/life-of-a-message.md)
>
> One message from a Slack channel to an agent and back, hop by hop

> [card] [The collaboration bridge](https://docs.switchagents.ai/switch-rooms/internals/collaboration-bridge.md)
>
> The adapter contract, and what it takes to support a new messaging app

> [card] [The agent protocol](https://docs.switchagents.ai/switch-rooms/internals/agent-protocol.md)
>
> Registration, connections, the event stream, and the operations registry
