# Design context — MCP detection, generate vs link, and the degrade path

How a connected design tool turns into the actual feature design the UI step materializes. The design
tool is reached through its **MCP** (a harness MCP server), the same shape as Impeccable's
slash-commands — detect it, use it when present, degrade cleanly when absent. This is the design-side
analogue of `yad-connect-repos`'s code-context.

## Provider detection

`connect`/`refresh` records `provider` (the concrete MCP) and `source` (the MCP id, or `unavailable`).
Detection is best-effort against the user's own authenticated MCP session (for OpenPencil, its CLI on
PATH first):

| `tool` | MCP / provider | Capability |
|--------|----------------|------------|
| `figma` | a Figma Dev Mode MCP | **read/link** — reference a file, read frames back into `ui-design.md` |
| `figma` | html.to.design MCP (`import-html`) | **generate** — render HTML/CSS screens into a Figma file |
| `pencil` | the `pencil` MCP (`batch_design`) | **generate** — author `.pen` web/mobile screens directly |
| `openpencil` | its CLI (`openpencil eval … --write`), or its MCP while the desktop app is open | **generate** — create frames in the epic's design file in the repo, then save it |
| other | the adapter's named MCP | per that adapter |

**Honest capability note:** not every design-tool MCP can *write*. A read-only Figma Dev Mode MCP
supports **link + read-back** only; *generate* needs a write-capable provider (html.to.design for Figma,
`pencil` or `openpencil`). `yad-ui` picks the direction the connected provider actually supports and records which one
it used (`direction: generated | linked`). It never claims to have generated screens a read-only MCP
cannot produce.

## Generate (push screens into the tool)

When the connected provider is write-capable, the `ux-designer` lens produces the epic's screens in the
tool, covering the screens/states `ui-design.md` enumerates and the user flows the architecture defines:

- **Figma via html.to.design** — the lens drafts each screen as HTML/CSS (reusing `DESIGN.md` tokens),
  then imports it into the Figma file via the MCP's `import-html`, one frame per screen.
- **pencil** — the lens calls `batch_design` to author the screens as `.pen` frames directly (mobile
  and/or web per the epic).
- **openpencil** — the lens writes the screens into the epic's design file, `epics/EP-<slug>/design/<name>.fig`,
  one frame per screen. `eval` cannot create a file: the first time, make it with
  `openpencil import <seed.html> -o epics/EP-<slug>/design/<name>.fig` (a small HTML page, such as the first
  screen), then add each frame with `openpencil eval <file> --stdin --write` (Figma
  Plugin API code, run on the file with no app open). With the desktop app open on the file, the
  `open-pencil` MCP may be used instead. **Give each frame yad's own ID:** OpenPencil numbers nodes in the
  order it loads them, so its own node IDs change when the file is reopened after a frame is deleted or
  added — and an old ID can then name another frame. Store a plain, unique ID on each frame with
  `frame.setSharedPluginData('yad', 'nodeId', '<id>')` (for example `checkout-mobile`), and record that as
  the screen's `nodeId`. Every read finds the frame by it:
  `figma.root.findAll((n) => n.getSharedPluginData('yad', 'nodeId') === '<id>')`. `yad fold` commits the
  `design/` folder with the UI review, and the review covers it: a change to the design file after
  approval needs a fresh review.

Reuse what already exists: load the connected code repos' code-maps (`yad-ui` Step 2b) and any
Impeccable `DESIGN.md` tokens so generated screens match built components, not invented ones.

## Link (reference a human-made design)

When a designer has already built the screens (or the provider is read-only), point `yad-ui` at the
existing file and **read the frames back** so `ui-design.md` reflects the real design: list each frame as
a screen, capture its node id + URL, and map components/tokens into `DESIGN.md`.

## Write back the linkage (done by `yad-ui`, per epic)

Either direction ends by writing `epics/EP-<slug>/.sdlc/design-links.json` — the machine-readable
screen→frame map — and a `## Design (<tool>)` section in `ui-design.md` linking each screen to its frame
URL. The design itself lives in the tool; the Product keeps the *links* and the Markdown spec beside the
other epic artifacts.

## Degrade path (no MCP / no tool)

If `design.json` is absent, `tool: "none"`, or `source: "unavailable"`, `yad-ui` runs **markdown-only**:
it authors `ui-design.md` / `DESIGN.md` exactly as before and records `design: none` in the frontmatter
with a one-line note (mirroring the `impeccable: not-installed` degrade). No error — the design tool is
purely additive.

## Reachable before Build (#358)

A design tool read through its running desktop app — Pencil, Figma's `figma-desktop` server, or
OpenPencil's MCP server — cannot be read while the app is closed or the design file is not open; a
hosted server needs a sign-in; and a CLI that works on the file (OpenPencil's) needs the file and the
program. So before a UI task, `yad-spec` and
`yad-implement` check it first, and stop early when it fails. yad itself cannot call an MCP server (it
belongs to the agent's session), so `yad story frames <story> --json` returns a `preflight` block that says
how to check and what to tell the person; the text for each tool is the toolbox entry's `reach` field.

| `tool` | What the agent checks | When it fails |
|--------|-----------------------|---------------|
| `pencil` | `get_app_state` (or `get_editor_state`): the active canvas editor is the design file, compared by file name (the app shows the full path). A frame inside a container is not in the top-level list, so it is read by its ID before it counts as missing | open the Pencil desktop app with the file loaded, then continue |
| `figma`, provider `figma-desktop` | `get_metadata` on one recorded frame | open the Figma desktop app with the file loaded and its Dev Mode MCP server on |
| `figma`, provider `figma` (hosted) | `get_metadata` on one recorded frame | sign in to Figma in your agent; no app is needed |
| `figma`, provider `figma-mcp` (older value), any other name, or none | `get_metadata` on one recorded frame | either of the two above — yad cannot tell which server it is |
| `figma`, provider `html-to-design` | nothing: it writes screens and cannot read a frame | not checked; build from the frame links |
| `openpencil`, provider `openpencil-cli` | the design file is in the Product checkout, `openpencil --version` runs, and `openpencil eval` on the file finds the frame whose shared plugin data `yad`/`nodeId` is the recorded ID. No app, no sign-in | pull the Product so the file is there with the frame in it, and make sure `openpencil` runs |
| `openpencil`, provider `openpencil-mcp` | the MCP's `eval` tool finds the frame by its stored `yad`/`nodeId` | open the OpenPencil desktop app with the file loaded (inside `OPENPENCIL_MCP_ROOT`) |
| `openpencil`, no provider or another name | the CLI check above, and the MCP check when that does not find the frame | either of the two above |

Also not checked (build from the frame links): no `design.json`, tool `none`, source `unavailable`; no
`design-links.json`, one that cannot be read, or one recorded for another tool (re-run the UI step); and a
story whose recorded frame IDs are all missing from `design-links.json` — the bad IDs are the problem then.

When the check fails because the tool is not on this machine at all (no such MCP server in the agent's
session), the skill first offers `preflight.connect` — `yad toolbox connect <tool> --json`, under the
connect-tool rule — and checks again once it is found (E165). A closed app, the wrong file or a sign-in
is never offered: those are the person's. Nor is Figma's `figma-desktop` provider: it is the desktop app's
own server, and Figma's connect step sets up the hosted one, so the person opens the app instead
(OpenPencil's MCP server is offered: its connect step adds it). An
older `design.json` that names no provider (or `figma-mcp`) is still offered the hosted one: the question
names what it installs, so a person who uses the desktop app says no. A server just added is seen only
after the agent restarts (check `/mcp`); until then the skill says so and stops.

A failed check stops the step. The person may choose to go on from the frame links only; that choice is
recorded (in `spec.md`'s Clarifications, or the commit's Decisions paragraph).

## Staleness / refresh

A re-generated or designer-edited file is like a moved code repo: `yad-ui` **flags** a divergence and
lets a human decide (re-run the step, or `yad-connect-design` with `action: refresh` and the same `tool`). It never silently
overwrites a designer's frames — refreshing the design is a human decision, the same discipline as
`code_context.refresh: human`.
