# Architecture

This page describes how Phantom is built: the processes that run, the
layers of the backend, how changes reach every client in real time, how
agents connect, and how the interface is organised. The ideas behind it
(vaults, scope, research objects, evidence, provenance, redaction and
agent permissions) are explained in [Concepts](./concepts.md).

## Overview

Phantom is one backend and one web interface, both running on your
machine.

```
Browser (Phantom interface)          Browser under test
        |  REST + WebSocket                  |  HTTP proxy, 127.0.0.1:8080
        v                                    v
+-------------------------------------------------------------+
| Backend (FastAPI)                                           |
|   REST API /api/v1      MCP /mcp       Agent Bridge /bridge |
|        \                   |                 /              |
|         +------------- services -----------+                |
|                            |                                |
|     repositories, models   |   runtime: proxy, scans,       |
|     (SQLAlchemy, Alembic)  |   replay, Intruder, Bridge     |
|                            |                                |
|     core: scope, redaction, signatures, proxy, TLS, events  |
+-------------------------------------------------------------+
        |
   PostgreSQL (Docker) or SQLite (local development)
```

- **Backend.** A FastAPI application serves the REST API under
  `/api/v1`, the realtime WebSocket at `/api/v1/ws`, the MCP endpoint at
  `/mcp` and the Agent Bridge under `/api/v1/bridge`. It also embeds the
  intercepting proxy, the scan runner and the other long-running work.
- **Interface.** A Vue 3 single-page application talks to the backend
  only through the REST client and the realtime channel. The backend is
  the source of truth; the browser keeps interface preferences only.
- **Database.** SQLAlchemy 2 with async sessions. The schema is managed
  by Alembic migrations, applied before the API starts. Docker runs
  PostgreSQL; local development uses SQLite unless `DATABASE_URL` says
  otherwise.

Everything binds to the loopback interface by default. The Docker setup
publishes only the interface and the proxy, and only on `127.0.0.1`.

## Backend layers

| Layer | Folder | Responsibility |
|---|---|---|
| API | `backend/app/api/` | Thin routers: validate input with Pydantic schemas, call one service, shape the answer. REST and the WebSocket boundary. |
| Schemas | `backend/app/schemas/` | Request and response models shared by REST and MCP. |
| Services | `backend/app/services/` | Use cases: transactions, scope and permission checks, domain events. Every interface calls the same services. |
| Repositories | `backend/app/repositories/` | Queries, filters and pagination. |
| Models | `backend/app/models/` | SQLAlchemy models. See [Data models](./reference/models/index.md). |
| Runtime | `backend/app/runtime/` | Long-running work owned by the process: the embedded proxy and its flow writer, intercept, replay, the scan runner, Intruder, retention, the Bridge sweeps and workspace context. |
| Engines | `backend/app/engines/` | Pluggable scan engines registered by name: a crawler and the signature engine. |
| Core | `backend/app/core/` | Pure building blocks with no database access: scope policy, redaction, the signature parser and matcher, the proxy core, upstream TLS policy, limits per module, the event bus. |
| MCP | `backend/app/mcp/` | MCP tools and resources over the same services, permission enforcement, result redaction, the capability manifest. |

Two rules keep the layers honest. `core/` knows nothing about the
database, vaults or the API, so the proxy, scope and redaction logic can
be tested on their own. And no interface talks to repositories directly:
the interface, REST and MCP all go through the services, so a rule
enforced once is enforced everywhere.

### Parity between interfaces

`backend/app/mcp/capabilities.py` is a manifest of every capability and
the REST routes, MCP tools and interface calls behind it. A generator
checks it against the live route table, the MCP registry and the
interface source, and renders the [parity matrix](./roadmap/parity-matrix.md).
A test fails when an entry point is missing from the manifest or the
matrix is stale, so a new feature cannot quietly exist in one interface
only.

## Proxy

The intercepting proxy is an aiohttp server inside the backend. It
decrypts HTTPS with per-host certificates signed by a local CA, relays
HTTP/1.x, CONNECT tunnels and WebSocket upgrades, and hands each finished
exchange to the runtime, which stores it in the active vault in batches,
flags it against the vault's scope and announces it on the event bus.
Intercept holds messages for a person to edit, forward or drop. Upstream
certificates are verified by default, for the proxy, replay and scans
alike. See [Proxy](./guides/proxy/index.md).

## Realtime

Domain events (a captured flow, a scan state change, a new finding, a
saved note, a held intercept message) are published on an in-process
event bus. Each subscriber has a bounded queue, so a slow consumer loses
its oldest events instead of blocking the proxy or the scan runner.

The interface subscribes over one WebSocket per window, scoped to the
active vault, and resynchronises after a reconnect. MCP clients that
support resource subscriptions receive change notifications for the
resources they read ([MCP resources and events](./agents/mcp-realtime.md)).
The Agent Bridge keeps a durable, ordered event log of its own and
streams it over server-sent events with a polling fallback.

## Agents: MCP and the Agent Bridge

The MCP server lives inside the backend, so agents use the same live
workstation as the person at the keyboard: the running proxy, the held
intercept queue, the history, Repeater, scans, findings, notes and
reports. Every call carries a personal access token. The token decides
which vaults the agent may touch and which actions it may take; results
pass the user's redaction policy; every call is audited. See
[MCP server](./agents/mcp.md), [Agent authentication](./agents/authentication.md)
and [Agent permissions](./agents/permissions.md).

The [Agent Bridge](./agents/bridge.md) adds what plain MCP does not
reliably give: persistent conversations, messages with delivery state and
streaming, tasks with leases and retries, approvals decided by the user,
and the workspace context (what the user is looking at, filtered per
agent). Its backend and protocol are implemented; the Agent page in the
interface and direct model provider integrations are in design.

## Interface

| Folder | Holds |
|---|---|
| `frontend/src/api/` | Typed REST client, realtime channel, adapters from API shapes |
| `frontend/src/stores/` | Pinia stores, filled from the backend and kept current by events |
| `frontend/src/components/` | Atoms, molecules and organisms, plus module folders (proxy, notes, reports, findings and others) and the `ui` primitives |
| `frontend/src/composables/` | Panels, table preferences, keyboard handling, flow actions |
| `frontend/src/utils/` | HTTP syntax, diffing, secret masking and other pure helpers, with unit tests |
| `frontend/src/views/` | One page per area: vaults, targets, proxy, findings, scans, signatures, payloads, notes, reports, settings |

The interface masks secrets in every view and copy action with the same
rules the backend applies to exports and agent results; both sides share
one set of test vectors.

## Stack

FastAPI, SQLAlchemy 2 (async), Alembic, PostgreSQL or SQLite, aiohttp
with MITM TLS for the proxy, httpx, the MCP Python SDK; Vue 3, Pinia,
TypeScript, Vite, Tailwind CSS v4, TanStack Table and Virtual, Vitest.

## Where to read next

- [Concepts](./concepts.md) for the model behind the architecture.
- [Status](./roadmap/status.md) for what is available, limited or planned.
- [Roadmap](./roadmap/index.md) for the milestones ahead.
