# The `@aisquare/ui` agent kit

Makes the library the single source of truth for UI **operationally**, not just by
convention: an MCP server your agents query, and a skill that holds them to the workflow.

```
mcp/
├── server.mjs            stdio MCP server — zero dependencies
├── cli.mjs               `aisquare-ui-agent` — wires the kit into a consumer repo
├── registry.json         GENERATED component index (see below)
└── lib/
    ├── rules.mjs         the 46 governance rules — one source for the server, the
    │                     linter, and skills/aisquare-ui/RULES.md
    ├── search.mjs        intent-aware ranking over the index
    ├── lint.mjs          the review_code rule engine
    ├── scaffold.mjs      the new-component generator
    ├── index-load.mjs    index location + library/consumer mode detection
    └── tools.mjs         tool definitions and handlers

skills/aisquare-ui/       the skill (SKILL.md + RULES.md + ADDING.md + CONSUMING.md)
.claude-plugin/           plugin + marketplace manifests
.mcp.json                 server registration (plugin root and project root both)
```

## Install

**Across every repo (recommended)** — as a Claude Code plugin:

```bash
claude plugin marketplace add AISquare-Studio/aisquare-ui
claude plugin install aisquare-ui@aisquare
```

**Into one consumer repo** — the package already contains the server and the index:

```bash
pnpm add @aisquare/ui
npx aisquare-ui-agent init      # registers the MCP server, installs the skill
npx aisquare-ui-agent check
```

**In this repo** — `.mcp.json` is picked up automatically; approve it on first run.

## Tools

| Tool | Use |
|---|---|
| `search_components` | Find by intent. The mandatory first step for any UI work. |
| `check_exists` | Batch coverage check when planning a screen. |
| `get_component` | Import line, props, cva variants, JSDoc, story/test coverage. |
| `list_components` | Inventory and categories, for orientation. |
| `get_tokens` | Semantic tokens, foundation ramps, spacing, radius, motion, elevation, Onyx classes. |
| `get_rules` | The governance rules, by topic, with rationale. |
| `review_code` | Lint a snippet or file against the rules. |
| `plan_new_component` | Overlap check + the full add-to-library plan. |
| `scaffold_component` | Generate a conforming component, story, barrel export, test entry, and demo. |
| `doctor` | Diagnose unstyled / colorless components in a consumer app. |
| `verify` | Run the library's own gates. |

Also exposed as resources (`aisquare-ui://rules`, `://inventory`, `://tokens`) and prompts
(`build-ui`, `add-component`, `audit-ui`).

Smoke test without a client: `node mcp/server.mjs --selftest`.

## The index

`registry.json` is generated by `scripts/build-agent-index.mjs`, which reads `src/` with the
TypeScript compiler API and extracts every export's layer, import path, JSDoc, props, and cva
variants, plus the categories and `since` versions from the playground registry and a snapshot
of the design tokens.

It is generated rather than read live because the server has to run inside a **consumer** repo,
where only `dist/` and this JSON are installed — there is no `src/` and no TypeScript to parse
it. Parsing 280 files per request would also make every call slow.

**So it must be regenerated when components change:**

```bash
pnpm agent:index      # rebuilds registry.json + skills/aisquare-ui/RULES.md
```

`pnpm build` runs it, so a release always ships a current index. If you add a component without
building, the index is stale and agents will not find it.

## Two modes

The server detects which repo it is in:

- **library** — `src/` is present. All tools work, including `scaffold_component` and `verify`.
- **consumer** — running from `node_modules/@aisquare/ui`. Read-only: scaffolding reports that
  the component belongs in a library PR rather than writing files it cannot land.

## Zero dependencies, on purpose

MCP's stdio transport is newline-delimited JSON-RPC, so the SDK would buy nothing and make the
package heavier for every consumer. The linter is regex-based for the same reason — it has to
run out of a consumer's `node_modules` with nothing installed. That costs precision, so every
finding carries its line and rule id, and anything resting on a heuristic is marked and
downgraded to `should`.

## Editing the rules

`mcp/lib/rules.mjs` is the only place. `skills/aisquare-ui/RULES.md` is generated from it
(`pnpm agent:docs`) so the prose an agent reads and the checks the linter runs cannot disagree.
A rule with a `detect` key is statically checked by `review_code`; the rest are judgement calls.
