affine-markdown-mcp

v0.3.0
affine-markdown-mcp Checked 1 day ago Published 1 day ago

MCP server for self-hosted AFFiNE: read and write docs as Markdown, see the folder tree, manage Kanban boards, upload images.

MCP server for self-hosted AFFiNE. Lets an AI agent (Claude Code, Claude Desktop, Cursor, any MCP client) work with your AFFiNE workspace like a teammate: 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. 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.

Summary extracted from the README.

affine-markdown-mcp

MCP server for self-hosted AFFiNE. 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
  • upload images: pictures in Markdown, doc and card icons, doc covers, the workspace logo

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.

Claude Code moves a card, adds one and writes a doc through the MCP while AFFiNE updates live

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.

A doc and a Kanban board written through the MCP, as AFFiNE shows them

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.

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
set_doc_icon icon of a doc, or of a card on a board: an emoji or an uploaded image
set_doc_cover cover image above the title of a doc
set_workspace_logo workspace avatar in the sidebar

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.

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)

Install

Nothing to install by hand, MCP clients start it with npx:

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.

Use it from Claude Code

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

{
    "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."

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 ![caption](file path or URL) on its own line ⚠️ ✅ written: the file is uploaded and becomes an image block; read as a marker that keeps the image on replace
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 with images, kept in a doc linked from the card, the way the AFFiNE editor does it
Card icon (shown before the card title) ✅ set_doc_icon with card, emoji or image
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 ❌
Doc icons: emoji ✅ set_doc_icon, shows everywhere
Doc icons: image ⚠️ stored like AFFiNE stores them, but upstream AFFiNE 0.27 shows only emoji and icon-set icons
Doc covers ⚠️ stored in the doc list (headerImage), upstream AFFiNE 0.27 has no cover UI
Workspace logo ✅ set_workspace_logo, same as Settings > Workspace
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

Images

An image is given as a local file path, an http(s) URL or a data: URL; PNG, JPEG, GIF, WebP, AVIF, SVG, BMP and ICO, up to 20 MB. The type is checked from the file's first bytes, so nothing else is uploaded. Files go into the workspace blob storage under the same key the AFFiNE editor uses (SHA-256 of the bytes), so the same picture is stored once.

Release screenshot:

![The new board](./shots/board.png)

![Logo](<C:/My Pictures/logo.png>)

A line with only images becomes image blocks; an image inside a sentence stays a link. Put <...> around paths with spaces. All images of a write are uploaded before the doc changes, so a missing file stops the write with nothing half written.

Small image icons (up to 96 KB) are kept inside the icon as a data URL, bigger ones are uploaded and linked. A card's icon is the icon of the doc behind the card (its description); a card without one gets an empty linked doc, as the editor does.

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.

Development

Node 22.18+ runs the TypeScript sources directly (the npm package is plain JS and runs on Node 20.19+).

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.

Credits

The sync protocol details (join, load, push update, folder and table storage) were learned from DAWNCR0W/affine-mcp-server (MIT). This is a separate, smaller implementation focused on Markdown and Kanban.

License: MIT