# For AI agents

> Copy-pasteable steps for AI coding agents: connect to a live article over the editor's MCP server, create a new article from the template, write embeds on the Embed.mount contract, and what never to do.

Source: https://tfrere-research-article-template-site.hf.space/docs/agents

Steps for coding agents (Claude Code, Cursor, Codex, Gemini CLI...) working on research articles made with the Research Article Template and its collaborative editor. Every command below is copy-pasteable; replace the `<placeholders>`. The same page is available as [Markdown](https://tfrere-research-article-template-site.hf.space/docs/agents.md).

## Pick a path

-   **Show a human what the editor feels like** - send them to the [live demo](https://tfrere-research-article-editor-demo.static.hf.space/) (no account, nothing saved, simulated co-authors). It runs entirely in the browser with no backend, so there is no MCP server to connect to: agents cannot read or edit it.
-   **Edit a live article in an editor Space** - connect to the editor's MCP server and edit next to the human authors. You need an editor Space you can write to (see below).
-   **Create a new article from the template** - free: clone the [template repository](https://github.com/tfrere/research-article-template), write MDX, deploy to a static Space.
-   **Host an editor for a team** - duplicate the [editor Space](https://huggingface.co/spaces/tfrere/research-article-template-editor). Since July 21, 2026 this needs a Hugging Face **Team or Enterprise organization** (co-authors with an org write role, which is free) or a **PRO account** (personal copy, only the owner can edit). The public editor Space is read-only for visitors.
    
    ```bash
    hf repos duplicate tfrere/research-article-template-editor your-org/paper-editor --type space
    ```
    
    Requires huggingface\_hub 1.6+ and `hf auth login`.

## Connect over MCP

Every editor Space exposes a remote MCP server at `/mcp` (Streamable HTTP). Use the Space's app URL: the Space `your-org/paper-editor` answers at `https://your-org-paper-editor.hf.space/mcp`. Authenticate with an HF access token (`HF_TOKEN`) of a user who can write to the Space: its owner, or a member of the owning organization with a write role.

**Claude Code**

```bash
claude mcp add --transport http article https://<space>.hf.space/mcp \
  --header "Authorization: Bearer $HF_TOKEN"
```

**Cursor**, in `.cursor/mcp.json`:

```json
{
  "mcpServers": {
    "article": {
      "url": "https://<space>.hf.space/mcp",
      "headers": { "Authorization": "Bearer ${env:HF_TOKEN}" }
    }
  }
}
```

**Codex**, in `~/.codex/config.toml`:

```toml
[mcp_servers.article]
url = "https://<space>.hf.space/mcp"
bearer_token_env_var = "HF_TOKEN"
```

Optional: an `X-Agent-Name` header sets the name people see (defaults to the MCP client name), and a `?doc=<name>` query opens another document (default `default`; only `default` is published). Full reference: [docs/MCP.md](https://huggingface.co/spaces/tfrere/research-article-template-editor/blob/main/docs/MCP.md).

## Edit a live article

The agent joins the same live document as the browsers: people see its name and its edits as they happen, and its own steps can be undone without touching theirs.

1.  `get_article_info` for the title, blocks, charts and publish state.
2.  `read_article` with `outline: true`, then `section: "..."`. Each block is preceded by `<!-- block:ID type rev:REV -->`.
3.  Edit with `replace_text` (minimal diff, keeps comments and formatting), `insert_blocks`, `replace_block` or `delete_block`. Pass `expected_rev` so an edit is refused if someone changed the block; on `rev_conflict`, read again and retry.
4.  Group related edits with `apply_edits`: all or nothing, one `undo`.
5.  Discuss with `list_comments`, `add_comment`, `reply_to_comment`, `resolve_comment`.
6.  Charts: `create_embed` (validated and checked in a headless browser), then `insert_embed` to place it; `update_embed` with `expected_version` to change it.
7.  `publish` puts the web page, the Markdown twin (`/llms.txt`) and, when possible, a PDF online. Give the user `urls.html`.

## Create a new article

From the template repository (Node.js 20+, Git LFS):

```bash
git clone https://github.com/tfrere/research-article-template.git my-paper
cd my-paper
git lfs install && git lfs pull
cd app
npm install
npm run dev   # dev server on port 4321
```

1.  Read [`AGENTS.md`](https://github.com/tfrere/research-article-template/blob/main/AGENTS.md) first: project layout, conventions and the list of task skills.
2.  Before a task, read the matching skill in [`.ai/skills/`](https://github.com/tfrere/research-article-template/tree/main/.ai/skills) : `write-mdx-content`, `create-html-embed` (with `runtime.md` and `template.html`), `design-dataviz`, `article-frontmatter`, `deploy-to-hf-spaces`. Cursor also finds them in `.cursor/skills/`.
3.  Layout lives in `app/src/styles/_grid.css` (`--layout-*` tokens); check alignment with `?grid` on any URL, or `G` in `npm run dev`.
4.  The entry point is `app/src/content/article.mdx` (frontmatter and imported chapters); chapters live in `app/src/content/chapters/`, embeds in `app/src/content/embeds/`, data in `app/src/content/assets/data/`.

## Write an embed

An embed is a self-contained HTML fragment. The template and the editor (including over MCP) share the same `Embed.mount` contract: the runtime loads D3, reads `data` and `config`, provides the theme CSS variables and palettes, and re-renders on resize and theme changes.

```html
<div class="d3-my-chart"></div>
<style>
  .d3-my-chart { position: relative; color: var(--text-color); }
  .d3-my-chart svg { display: block; width: 100%; overflow: visible; }
</style>
<script>
  Embed.mount('d3-my-chart', (ctx) => {
    // Setup, runs once: data, scales, persistent DOM.
    const rows = [{ x: 'A', y: 3 }, { x: 'B', y: 5 }];
    const svg = d3.select(ctx.root).append('svg');
    ctx.describe('Bar chart of 2 categories; B is highest.');
    // Render, runs on first paint and on width, theme or palette change.
    return ({ width, isDark }) => {
      const colors = ctx.colors();
      // scales, axes, data joins
    };
  });
</script>
```

-   Exactly one root `<div>` whose class matches the file name (`d3-my-chart.html` uses `.d3-my-chart`), with no `id`; every CSS rule is prefixed with the root class.
-   A classic inline script calls `Embed.mount` at top level (not in `DOMContentLoaded`, a timeout or an async wrapper). No globals; query only inside `ctx.root`.
-   `setup(ctx)` runs once and returns a render function called with `width`, `height`, `theme`, `isDark`.
-   Colors come from CSS variables and `ctx.colors()`, read inside render; never hardcode them. Describe the chart with `ctx.describe()`.
-   Template: data files go in `app/src/content/assets/data/` (served at `/data/`), passed with the `data` prop and read with `ctx.data()`; `config` arrives as `ctx.config`. Use it in MDX with `<HtmlEmbed src="d3-my-chart.html" title="..." data="my-data.csv" />`.
-   Editor: the iframe cannot fetch, so inline the data and attach source files with `data_files`.
-   Start from `.ai/skills/create-html-embed/template.html` and read [`runtime.md`](https://github.com/tfrere/research-article-template/blob/main/.ai/skills/create-html-embed/runtime.md) for the full API. Verify every embed before you hand it over:

```bash
cd app
npm run check:embed -- d3-my-chart.html --data my-data.csv
npm run check:embed -- --all   # every embed in the repo
```

## Deploy

**Free, static Space.** Create a Space with the *Static* SDK at [huggingface.co/new-space](https://huggingface.co/new-space), then:

```bash
cd app
npm run build
hf upload <you>/<your-space> dist . --repo-type space
```

**Docker Space**, built on Hugging Face with the included `Dockerfile` (see the [deploy skill](https://github.com/tfrere/research-article-template/blob/main/.ai/skills/deploy-to-hf-spaces/SKILL.md)):

```bash
git remote add space git@hf.co:spaces/<you>/<your-space>
git push space main
```

Keep this in the Space `README.md` frontmatter so the article appears in the [gallery](https://huggingface.co/spaces/tfrere/research-article-gallery):

```yaml
tags:
  - research-article-template
```

## Never do this

-   Never remove the `research-article-template` tag from a Space `README.md`: it powers the community gallery.
-   Never rewrite a whole live article to change a passage: use `replace_text` or block tools with `expected_rev`, so people's concurrent edits and comments survive.
-   Never overwrite a chart someone is editing: `update_embed` refuses with `draft_in_progress` unless `force`; ask instead.
-   Never hardcode colors in an embed, style unprefixed `body`, `svg` or `text`, or hand-roll D3 loading, resize or theme observers.
-   Never tell users that duplicating the editor Space is free: it needs HF PRO or a Team organization.
-   Never put an access token in an article, an embed or a commit.

## Machine-readable files

-   [`/llms.txt`](https://tfrere-research-article-template-site.hf.space/llms.txt) - index of this site for LLMs (llmstxt.org format).
-   [`/llms-full.txt`](https://tfrere-research-article-template-site.hf.space/llms-full.txt) - the full documentation as one Markdown file.
-   [`/docs.md`](https://tfrere-research-article-template-site.hf.space/docs.md) and [`/docs/agents.md`](https://tfrere-research-article-template-site.hf.space/docs/agents.md) - Markdown twins of the docs pages.
-   Every published article serves its own Markdown twin at `/llms.txt`, for example the [template demo article](https://tfrere-research-article-template.hf.space/llms.txt).
