# External agents over MCP The editor exposes a remote [Model Context Protocol](https://modelcontextprotocol.io) server at `/mcp` (Streamable HTTP). Any MCP client, such as Claude Code, Cursor or Codex, can read and edit the live article while people keep writing in the browser. The agent is a real collaborator, not a script that overwrites the document: - It joins the same Yjs document as the browsers, with its own client id. People see it in the avatar stack and see a " agent" highlight on the text it is working on. - Every edit is a small CRDT operation. `replace_text` rewrites only the characters that differ, so concurrent typing, cursors and comment anchors around the edit survive. - It goes through the same write-permission check as a human login. - `undo` reverts the agent's own steps only, never other people's edits. - A version is saved in the article history right before the agent's first write and when its session ends, attributed to the agent. While the session is live, anyone with edit rights can click the agent's avatar and pick "Undo its changes": this reverts everything that session wrote and leaves other people's edits in place. ## Connect You need an HF access token with access to the Space. When the Space belongs to an org, you need write access to that org. In local dev (no `SPACE_ID`), any token, or none, is accepted. Replace `https://.hf.space` with your Space URL, or `http://localhost:8080` locally. ### Claude Code ```bash claude mcp add --transport http article https://.hf.space/mcp \ --header "Authorization: Bearer $HF_TOKEN" ``` ### Cursor `.cursor/mcp.json`: ```json { "mcpServers": { "article": { "url": "https://.hf.space/mcp", "headers": { "Authorization": "Bearer ${env:HF_TOKEN}" } } } } ``` ### Codex `~/.codex/config.toml`: ```toml [mcp_servers.article] url = "https://.hf.space/mcp" bearer_token_env_var = "HF_TOKEN" ``` ### Options | Option | Where | Default | |---|---|---| | Display name | `X-Agent-Name` header | The MCP client name (`claude-code` becomes "Claude Code") | | Document | `?doc=` query | `default` | ## Tools ### Text, comments, history | Tool | What it does | |---|---| | `read_article` | Live article as Markdown. Each block is preceded by ``. `outline: true` lists headings only, and `section` reads one section. | | `replace_text` | Replace a quoted passage. Minimal diff, keeps formatting and comments. | | `insert_blocks` | Insert Markdown after or before a block, or at the start or end. | | `replace_block` | Replace a whole block. Refuses blocks with comment anchors unless `force`. | | `delete_block` | Delete a block. | | `apply_edits` | Run several of `replace_text`, `insert_blocks`, `replace_block`, `delete_block`, `insert_embed` in order as one step: all or nothing, one `undo`. | | `update_metadata` | Title, subtitle, description. | | `list_comments`, `add_comment`, `reply_to_comment`, `resolve_comment` | Comment threads anchored on quoted text. | | `focus` | Highlight what the agent is looking at. Edits move the highlight automatically. | | `save_version` | Save a named version in the article history. | | `undo` | Revert the agent's own last steps in this session (text, comments, metadata and charts). | `REV` is a short hash of the block's content. Pass it as `expected_rev` to `replace_text` (with `block_id`), `replace_block`, `delete_block` or `insert_embed` (the anchor block), and the edit is refused if someone changed that block since the agent read it. Block edits also return `structuredContent` with the id, type and new `rev` of every block they touched, so an agent can chain edits without re-reading. ### Charts (embeds) A chart is a self-contained HTML fragment rendered in an iframe: a root `
` first, a `\n" } ``` The result has `"name": "model-accuracy.html"`, `"version": 1` and `check`. If `check.status` is `FAIL`, read `check.checks[].messages` and fix with `update_embed` (`patch` or `html`, `expected_version: 1`); each update returns a new check. 3. `insert_embed` with `{ "name": "model-accuracy.html", "after_block_id": "", "expected_rev": "" }`. On `rev_conflict`, read the section again and retry with the new `rev`. The title and source given to `create_embed` become the caption (override with `title`, `description`, `source`). 4. Later: `get_embed` to read the settings and `latest_version`, `update_embed` with `{ "config": { "title": "Top-1 accuracy" }, "expected_version": 2 }`, or `restore_embed_version` to go back. ## Example: publish 1. `get_article_info`: `publishable` must be true; `last_publish` says when the article was last put online. 2. `publish` (waits up to 120 s by default). `status: "succeeded"` with `urls.html` (and `urls.pdf` when a PDF was made) is the answer to give the user. `status: "running"` means the timeout passed first: call `get_publish_status` with the `job_id` until it is `succeeded` or `failed`. 3. A `publish_failed` error carries the job with its `error`; fix the cause (for example a chart that does not render) and publish again. ## How it works ``` MCP client ──HTTP──▶ /mcp (router.ts) │ one session = one AgentPeer (agent-peer.ts) ▼ HocuspocusProvider ──ws://127.0.0.1/collab──▶ Hocuspocus ◀── browsers ``` - `backend/src/mcp/router.ts` handles the Streamable HTTP sessions, bearer auth, Origin validation and the 30-minute idle cleanup. - `backend/src/mcp/agent-peer.ts` is the headless collaborator: Yjs doc, awareness, `agentFocus` and a per-session `Y.UndoManager`. - `backend/src/mcp/y-blocks.ts` does schema-free Y.XmlFragment access: stable block ids from Yjs item ids, text search across marks, minimal-diff replace and comment anchors. - `backend/src/mcp/markdown-to-json.ts` converts Markdown to TipTap JSON for new blocks. - `backend/src/mcp/tools.ts` defines the tool definitions and the server instructions sent to the agent. - `backend/src/mcp/embed-tools.ts` implements the chart tools on the shared embed modules (`backend/src/shared/embed-contract.ts`, `embed-versions.ts`, `embed-config.ts`, `data-files.ts`, also used by the Embed Studio) and the server-side checker (`backend/src/embed-check/`). - `backend/src/publisher/publish-service.ts` is the publish flow shared by the Publish button and the `publish` tool; `backend/src/chromium-lock.ts` keeps one headless Chromium job (check or PDF) at a time. The browser runs y-tiptap with `onInvalidContent`. If an agent or a newer client ever writes a node this build doesn't know, the content is preserved in the shared document instead of being deleted. Editing then pauses with a banner until the content fits again. Tests: `backend/tests/mcp-agent.test.ts` and `mcp-embeds.test.ts` run a real server, a human WebSocket peer and the official SDK client against each other; `mcp-auth.test.ts` covers permissions with OAuth on; `tests/scenarios/mcp-embeds.test.ts` runs the whole "add a chart and publish" flow against a server process and a restart. See section 7 of [TESTS.md](./TESTS.md).