# MCP resources and change notifications

How agents follow live workstation state (new flows, held messages, scan
progress) over MCP, what the installed SDK (`mcp` 2.3.0) supports on our
stateless endpoint, and what Phantom implements.

## Summary

- Read-only **resources** mirror the main views. They are read through the
  same services as the tools and redacted by the MCP policy.
- Clients on the **2026-07-28** protocol can listen for
  `notifications/resources/updated` with `subscriptions/listen`. This works
  on the existing stateless endpoint, needs no session and survives
  backend restarts like every other request.
- Clients on the **2025 handshake protocols** (2025-06-18, 2025-11-25) are
  not offered subscriptions. They poll the resources or the tools. Polling
  stays the supported fallback for every client.

## Resources

| URI | Content | Changes on |
|---|---|---|
| `phantom://vaults` | Vault list with scope rules and statistics | `vault.*` |
| `phantom://vault/{vault_id}` | One vault, live statistics | `vault.*`, `target.*`, `finding.*`, `scan.*` |
| `phantom://vault/{vault_id}/flows/recent` | 50 newest flow summaries | `proxy.flow`, `proxy.cleared` |
| `phantom://vault/{vault_id}/findings` | Findings, most severe first | `finding.*` |
| `phantom://vault/{vault_id}/scan/{scan_id}` | Scan status, progress, last 50 log lines | `scan.updated`, `scan.log`, `scan.deleted` |
| `phantom://proxy/status` | Listener, capture vault, intercept switch, held count | `proxy.status`, `intercept.*` |
| `phantom://intercept/queue` | Intercept options and held messages | `intercept.*` |

All resources are `application/json`, use the REST schemas, and carry the
same error codes as the tools. An error comes back as a JSON-RPC error
whose message is the REST error body. Captured data is always redacted in
resources: the per-call `reveal_secrets` argument exists only on detail
tools, and only when the user allows it.

Scans are addressed per vault (`/vault/{vault_id}/scan/{scan_id}`), like
every other scan operation, so a URI never reaches a scan outside its
vault.

## Transports and what each can push

| Transport | Standing server-to-client channel | Resource notifications |
|---|---|---|
| Stateless Streamable HTTP, 2025 protocols (our mount) | None: every POST is its own exchange, a GET stream needs a session | Not possible. The SDK only serves `resources/subscribe` on stateful sessions. |
| Stateful Streamable HTTP (session id + GET SSE stream) | Yes, per session | Possible with `resources/subscribe`, but sessions live in server memory, break on restart and need sticky routing. |
| Legacy HTTP+SSE | Yes | Deprecated transport; not worth adding. |
| stdio (our bridge) | Relays whatever the HTTP endpoint sends | Same as the endpoint it relays to. |
| 2026-07-28 Streamable HTTP | The response to `subscriptions/listen` is the stream (SSE) | Yes, without sessions: the client names URIs in the listen request. |

The SDK's stateless handler answers every request as JSON
(`json_response=True`), except for `subscriptions/listen`, which always
streams as SSE. So the existing `/mcp` mount serves both protocol eras:
2025 clients get plain JSON, 2026 clients can open listen streams. A
second, stateful mount would add session state and restart failure modes
to reach the 2025-era `resources/subscribe`, which the SDK marks
deprecated (removed in 2026-07-28). We decided not to add one.

### Client support (November 2026)

- The Python SDK 2.3 client (`Client.listen`) speaks 2026-07-28 and was
  used for the tests and the live check below.
- Clients still pinned to the 2025 protocols (many desktop and IDE hosts)
  get no `resources.subscribe` capability from us. They can still list and
  read resources, or call `scan_get`, `intercept_list` and `flow_list`.
- Claude Code reaches tools and resources over HTTP. Whether a host reacts
  to resource notifications depends on the host, so agents must not rely
  on them for correctness.

## Implementation

- `app/mcp/resources/` registers the resources (`catalog.py`) through one
  wrapper (`registry.py`). It runs each read as an agent call, applies the
  MCP redaction policy and maps domain errors to the REST error body.
- `app/mcp/realtime.py` holds `EventBridge`, the MCP server's subscription
  bus (`MCPServer(subscriptions=...)`). The first open listen stream
  subscribes it to the workstation `EventBus`; the last one to close stops
  it. Each domain event is mapped to the URIs it changes (`resource_uris`),
  bursts are coalesced over 200 ms, and one `ResourceUpdated(uri)` is
  published per URI. The SDK's listen handler filters by the URIs each
  stream asked for, buffers at most 1024 events per stream and ends the
  stream if a client stops reading. The client then listens again and
  re-reads the resources it cares about.
- Notifications carry only a URI, never data. What the client reads next
  goes through the redaction policy, so the push channel cannot leak a
  captured secret.

## Verification

- `tests/test_mcp_resources.py`: listing, reading, redaction, error codes,
  the event-to-URI map, coalescing and bridge shutdown, a listen stream in
  process with the SDK client, and a 2025-era `initialize` that reports no
  subscribe capability and still reads resources.
- Live check over real HTTP (uvicorn on a temporary database, SDK client
  over Streamable HTTP): negotiated `2026-07-28`, `resources.subscribe:
  true`, `vault_create` produced `ResourceUpdated('phantom://vaults')` on
  the open listen stream, and the resource read back the new vault.

## Polling guidance

Poll the resource or tool that matches the view: `phantom://vault/{id}/scan/{scan_id}`
or `scan_get` while a scan runs, `phantom://intercept/queue` or
`intercept_list` while interception is on, `flow_list` with `before_id`
paging for history. A listen stream is only a hint to read sooner. On a
dropped stream, listen again and re-read; nothing is replayed.
