affine-markdown-mcp
# affine-markdown-mcp
MCP server for **self-hosted [AFFiNE](https://affine.pro)**. Lets an AI agent (Claude Code, Claude Desktop, Cursor, any MCP client) work with your AFFiNE workspace like a teammate:
- see the **folder tree** like the sidebar
- **read docs as Markdown** and **write Markdown** back (append or replace)
- run **Kanban boards** fully: create boards, edit lanes, fields and views, add and move cards, put them in order, give them a Markdown description
- create docs and folders, move docs, trash and restore
It talks to AFFiNE the same way the web app does (Yjs over the sync socket), so changes show up live for everyone who has the doc open.

*Left: Claude Code gets one sentence of task and uses the MCP. Right: the AFFiNE board and the new doc update live. Sped up. [MP4 version](https://raw.githubusercontent.com/Tankonyako/affine-markdown-mcp/main/docs/demo/affine-mcp-demo.mp4).*

*Everything on this page was written by the MCP: the Markdown (heading, bold, italic, todos, table, code) and the card moved to Complete with a new Author value.*
<br>
## Tools
| Tool | What it does |
|---|---|
| `list_workspaces` | workspaces the bot can open |
| `tree` | folders and docs with ids, like the sidebar |
| `search` | docs whose title or text has the words, with snippets |
| `read_doc` | a doc as Markdown |
| `write_doc` | Markdown into a doc, `append` or `replace` |
| `create_doc` | new doc from Markdown, optionally in a folder |
| `trash_doc` | move to trash or restore, nothing is deleted for good |
| `create_folder` | sidebar folder, at the top or inside another |
| `move_doc` | put a doc into a folder |
| `list_boards` | Kanban boards in a doc or the whole workspace, with lanes |
| `read_board` | cards lane by lane in board order, with fields and views |
| `get_card` | one card with its fields and its description as Markdown |
| `create_board` | new board in a doc: lanes (with colors), fields, optional Table view |
| `edit_board` | rename; add, rename, delete fields; add, rename, recolor, reorder, delete lanes; add a Kanban or Table view |
| `delete_board` | remove a board and its cards |
| `add_card` | new card with fields `{"Status": "In Progress", "Author": "Yurii"}`, a Markdown description and a place in the lane |
| `update_card` | change a card title, fields, description or place |
| `move_card` | move a card to another lane, optionally to the top, bottom, before or after a card |
| `delete_card` | remove a card |
Docs, folders, boards and cards can be named by **id or by title**, so the agent can just say `move_card {"doc": "Sprint", "card": "Fix login", "lane": "Done"}`. A new select value (status, author) is added to the board automatically.
Set `AFFINE_READ_ONLY=true` to register only the reading tools.
<br>
## Requirements
- AFFiNE server **0.20 to 0.27.4**, self-hosted (`GET /info` must say so). 0.27.5 changed the sync join protocol, the server refuses to start against it with a clear message instead of breaking silently
- Node.js **20.19+** for the npm package (22.18+ to run the sources from a clone)
- an AFFiNE account for the agent, a separate bot user is best (see below)
<br>
## Install
Nothing to install by hand, MCP clients start it with `npx`:
```bash
npx -y affine-markdown-mcp
```
It reads its settings from environment variables:
| Variable | Needed | Meaning |
|---|---|---|
| `AFFINE_BASE_URL` | yes | your AFFiNE, `https://affine.example.com` (plain http only for localhost) |
| `AFFINE_EMAIL` | yes | account the agent signs in as |
| `AFFINE_PASSWORD` | yes | its password |
| `AFFINE_WORKSPACE_ID` | no | default workspace, the id from `/workspace/<id>/...`. Without it tools take a workspace name |
| `AFFINE_READ_ONLY` | no | `true` = only the reading tools |
**Bot user (recommended).** Edits then show up as the bot, and your own password stays out of config. In the AFFiNE admin panel (`/admin`, Accounts) create a user such as `claude@example.com` with a long random password, then invite it to your workspace from Settings > Members and accept the invite once as that user. A collaborator is enough for everything the tools do.
<br>
## Use it from Claude Code
```bash
claude mcp add affine \
--env AFFINE_BASE_URL=https://affine.example.com \
--env AFFINE_EMAIL=claude@example.com \
--env AFFINE_PASSWORD=... \
--env AFFINE_WORKSPACE_ID=<workspace id> \
-- npx -y affine-markdown-mcp
```
or in a project `.mcp.json` (values come from your environment, nothing secret in the file). Claude Desktop, Cursor and other clients take the same `command` / `args` / `env` block
```json
{
"mcpServers": {
"affine": {
"command": "npx",
"args": ["-y", "affine-markdown-mcp"],
"env": {
"AFFINE_BASE_URL": "${AFFINE_BASE_URL}",
"AFFINE_EMAIL": "${AFFINE_EMAIL}",
"AFFINE_PASSWORD": "${AFFINE_PASSWORD}",
"AFFINE_WORKSPACE_ID": "${AFFINE_WORKSPACE_ID}"
}
}
}
}
```
A line in the project's `CLAUDE.md` helps the agent use it, for example *"Tasks are cards on the board in the AFFiNE doc Sprint, docs are in the AFFiNE folder Engineering. Use the affine tools, start with tree."*
<br>
## What is supported
✅ works, ⚠️ works with a limit, ❌ not supported
**Doc content** (read gives Markdown, write takes Markdown)
| AFFiNE | Markdown | Read | Write | Note |
|---|---|:-:|:-:|---|
| Headings 1 to 6 | `#` to `######` | ✅ | ✅ | |
| Paragraphs | text | ✅ | ✅ | |
| Bold, italic, strike, inline code, links | `**b**` `*i*` `~~s~~` `` `c` `` `[t](url)` | ✅ | ✅ | |
| Underline, text color, highlight | | ⚠️ | ❌ | read as plain text, gone after a `replace` |
| Bulleted and numbered lists, nested | `- item`, `1. item` | ✅ | ✅ | |
| Todo lists | `- [ ] todo`, `- [x] done` | ✅ | ✅ | |
| Quotes | `> quote` | ✅ | ✅ | |
| Code blocks with language | fenced code | ✅ | ✅ | |
| Dividers | `---` | ✅ | ✅ | |
| Tables | GFM table | ✅ | ✅ | AFFiNE table block |
| Links to other docs (`@` mention) | `[Title](affine://<docId>)` | ✅ | ✅ | |
| Kanban board (database block) | `<!-- board:... -->` section | ⚠️ | ⚠️ | read as a table, kept as it is on `replace`, made and changed with the board tools |
| Images, attachments, embeds, bookmarks, callouts, LaTeX and other blocks | `<!-- affine:block <id> <type> -->` | ⚠️ | ⚠️ | read as a marker, kept in place on `replace` while the marker stays, new ones cannot be added |
| Edgeless (canvas) content | | ❌ | ❌ | only the document view |
| Doc properties (Info), comments | | ❌ | ❌ | |
**Boards**
| Feature | | Note |
|---|:-:|---|
| Find boards in a doc or the whole workspace, with lanes | ✅ | `list_boards` |
| Read cards lane by lane in the order the board shows them | ✅ | `read_board`, lane order and card order as in the editor |
| Create a board with lanes, lane colors and fields | ✅ | `create_board`, added at the end of a doc |
| Rename a board, delete a board | ✅ | `edit_board`, `delete_board` |
| Add, rename, delete fields | ✅ | the field that holds the lanes cannot be deleted |
| Add, rename, recolor, reorder, delete lanes | ✅ | a lane with cards is deleted only with `moveTo` |
| Kanban and Table views | ✅ | add a Table view or a second Kanban grouped by another select |
| Add, rename, move, delete cards | ✅ | by card title or id |
| Card order inside a lane | ✅ | top, bottom, before or after a card |
| Card description (what opens when you click a card) | ✅ | Markdown, kept in a doc linked from the card, the way the AFFiNE editor does it |
| Fields: select, multi-select, text, number, checkbox, date, link, progress | ✅ | new select values (a new status, a new author) are added to the board |
| Other field types (member, created time, attachments, ...) | ⚠️ | read as stored, not created |
| Filters, sorting, hidden fields of a view | ❌ | set them in the editor, they are kept |
| Calendar and other view types | ❌ | |
**Workspace**
| Feature | | Note |
|---|:-:|---|
| Folder tree like the sidebar | ✅ | `tree` |
| Search docs by title and text | ⚠️ | reads docs one by one, max 300 |
| Create, rename and move docs | ✅ | |
| Create folders | ✅ | at the top or inside another |
| Rename or delete folders | ❌ | |
| Trash and restore docs | ✅ | |
| Delete a doc for good | ❌ | on purpose, empty the trash in AFFiNE |
| Tags, collections | ❌ | |
| Several workspaces | ✅ | by id or name, `AFFINE_WORKSPACE_ID` is the default |
| Read only mode | ✅ | `AFFINE_READ_ONLY=true` |
| Edits next to people working in the same doc | ✅ | changes merge through Yjs, nothing is overwritten whole |
**Server**
| Setup | | Note |
|---|:-:|---|
| Self-hosted AFFiNE 0.20 to 0.27.4 | ✅ | |
| Self-hosted AFFiNE 0.27.5 and newer | ❌ | new sync join protocol, the server stops with a clear message |
| AFFiNE Cloud (app.affine.pro) | ❌ | self-hosted only |
| stdio (Claude Code, Claude Desktop, Cursor, ...) | ✅ | |
| HTTP transport | ❌ | planned |
| Sign in with email and password | ✅ | a bot user is best |
| Accounts with OAuth or magic link only | ❌ | give the bot user a password |
When the agent writes a doc back with `replace` and leaves a marker or the board section in place, that block stays exactly as it was (at the new position). Taking a marker out removes the block, so `replace` is marked destructive for the client.
<br>
## Development
Node 22.18+ runs the TypeScript sources directly (the npm package is plain JS and runs on Node 20.19+).
```bash
npm ci
cp .env.example .env # bot login, see above
npm start # the server on stdio, for an MCP client or the MCP Inspector
npm test # unit tests, no network (node:test)
npm run test:integration # real server against AFFINE_TEST_WORKSPACE_ID (a throwaway workspace), writes and trashes docs there
npm run typecheck
npm run lint
npm pack # builds dist/ and the .tgz that npm would publish
```
Release: automatic. Every push to `main` that changes the package (`src/`, `package.json`, `README.md`, ...) runs the `Publish` workflow: checks, raises the patch version, adds a `CHANGELOG.md` entry from the commit subjects, commits and tags `v<version>`, publishes to npm through trusted publishing (no token) and makes a GitHub release. Put `#minor` or `#major` in the commit message for a bigger step, `[skip release]` to skip it, or run the workflow by hand and pick the part. The server reads its version from `package.json`, nothing else to bump.
Code layout: `src/types` (plain types), `src/domain` (Markdown and board rendering, no I/O), `src/infrastructure/affine` (HTTP session, sync socket, Yjs page/board/folder adapters), `src/application` (use cases, id or title resolution), `src/api/mcp-server.ts` (tools), `src/main.ts` (wiring). Style: `.editorconfig` and `eslint.config.js` (4 spaces, single quotes, Allman braces). Only erasable TypeScript, so Node runs the sources directly.
<br>
## Credits
The sync protocol details (join, load, push update, folder and table storage) were learned from [DAWNCR0W/affine-mcp-server](https://github.com/DAWNCR0W/affine-mcp-server) (MIT). This is a separate, smaller implementation focused on Markdown and Kanban.
License: MIT
TDQS
Scored across 19 tools
Most tools target clearly distinct resources and actions (docs vs boards vs cards vs folders), and the read/list tools are separated by scope (list_boards vs read_board vs get_card vs read_doc). A few names are generic or could be confused, such as 'tree' and 'search', and 'read_doc' vs 'get_card' when cards contain Markdown, but descriptions generally resolve the ambiguity.
The majority follow verb_noun (create_board, add_card, delete_card, move_doc), but conventions are mixed: bare names like 'tree' and 'search', and inconsistent verbs for similar actions (read vs get, create vs add, write vs update, delete vs trash). It remains readable, but the set does not maintain a single predictable pattern.
19 tools is slightly above the ideal 3–15 range, but the domain covers workspaces, docs, boards, cards, and folders, so each tool largely earns its place. The surface is broad but not bloated, with no obvious duplicate tools.
Core lifecycle operations are well covered for docs (create/read/write/trash/move), boards (create/read/edit/delete/list), and cards (add/get/update/move/delete). Gaps remain around folder lifecycle (no delete/rename folder), doc title updates, and view deletion, but these are minor workaroundable omissions for the main workflow.