AFFiNE MCP Server
Enables interaction with AFFiNE workspaces through GraphQL API to manage documents, search content, handle comments, and access version history. Supports comprehensive workspace operations including document publishing, comment management, and user authentication via session cookies or personal access tokens.
README
Overview
- Purpose: MCP server exposing AFFiNE GraphQL tools to manage workspaces, docs, search, comments, and history.
- Transport: WebSocket (default, port 7821) or stdio.
- Auth: Cookie-first (recommended). Optional personal access token (PAT) via GraphQL.
- Tools: workspaces, docs (list/get), search, recent, publish/revoke, comments (list/create/update/delete/resolve), histories (list/recover), current user, access tokens, and advanced apply-updates.
Features
- Notion-like coverage: Tools mirror common Notion MCP server flows where possible in AFFiNE.
- GraphQL-first: Uses
AFFINE_BASE_URL+/graphqlfor queries/mutations. - Session auth: Provide cookies via
AFFINE_COOKIEorAFFINE_HEADERS_JSON(AFFiNE typically uses session cookies). - Dockerized: Run with
docker composeand connect over WebSocket.
Requirements
- Node.js 20+ (if running locally) or Docker.
- A reachable AFFiNE server URL (self-hosted). If your instance requires login, supply
AFFINE_COOKIEfrom your browser session.
Quick Start (Docker Compose)
- Copy
.env.exampleto.envand set values:AFFINE_BASE_URL: Base URL of your AFFiNE server, e.g.http://affine.local:3010.AFFINE_COOKIE: Session cookie string (e.g.,affine_session=<...>; affine_user_id=<...>).AFFINE_HEADERS_JSON: Optional JSON of extra headers to send.AFFINE_GRAPHQL_PATH: Usually/graphql.AFFINE_WORKSPACE_ID: Default workspace id (optional). Tools also accept explicitworkspaceId.MCP_WS_PORT: Host port to expose (default 7821).
- Build and run:
docker compose up --build -d
- The MCP server listens on
ws://localhost:${MCP_WS_PORT}(defaultws://localhost:7821).
Tool Summary
affine_list_workspaces: No input. Returns workspaces.affine_get_workspace:id.affine_list_docs:workspaceId?,first?,offset?,after?.affine_get_doc:docId,workspaceId?.affine_search_docs:keyword,limit?,workspaceId?.affine_recent_docs:first?,offset?,after?,workspaceId?.affine_publish_doc:docId,mode? (Page|Edgeless),workspaceId?.affine_revoke_doc:docId,workspaceId?.affine_list_comments:docId,first?,offset?,after?,workspaceId?.affine_create_comment:docId,content,docTitle?,docMode?,mentions?,workspaceId?.affine_update_comment:id,content.affine_delete_comment:id.affine_resolve_comment:id,resolved.affine_list_histories:guid,take?,before?,workspaceId?.affine_recover_doc:guid,timestamp,workspaceId?.affine_current_user: No input.affine_list_access_tokens: List personal access tokens (metadata only).affine_generate_access_token: Generate a PAT and return the token string.affine_revoke_access_token: Revoke a PAT by id.affine_apply_doc_updates(advanced):docId,op,updates,workspaceId?.
Notes:
- If
workspaceIdis omitted where optional, the server usesAFFINE_WORKSPACE_IDwhen set. - Many AFFiNE mutations rely on user session cookies; supply
AFFINE_COOKIEfor authenticated operations. - For raw doc content (binary, markdown, or CRDT diffs), AFFiNE exposes internal
/rpcendpoints requiring an internal access token; this server does not call those by default.
GraphQL Schema Alignment
- Queries/mutations align with
packages/backend/server/src/schema.gqlin AFFiNE. - Key operations used:
workspaces,workspace(id),docs,doc,searchDocs,recentlyUpdatedDocs,publishDoc,revokePublicDoc,comments,createComment,updateComment,deleteComment,resolveComment,histories,recoverDoc,currentUser,accessTokens,generateUserAccessToken,revokeUserAccessToken,applyDocUpdates.
Authentication
- Cookie (recommended)
- In your browser after logging into AFFiNE, copy cookies
affine_sessionandaffine_user_id. - Put them into
.envas:AFFINE_COOKIE=affine_session=...; affine_user_id=....
- In your browser after logging into AFFiNE, copy cookies
- Personal Access Token (optional)
- Generate via GraphQL while authenticated with cookies:
- Mutation:
generateUserAccessToken(input: { name, expiresAt? }). - Example curl:
curl -X POST "$AFFINE_BASE_URL/graphql" \ -H "Content-Type: application/json" \ -H "Cookie: affine_session=...; affine_user_id=..." \ -d '{"query":"mutation($input:GenerateAccessTokenInput!){ generateUserAccessToken(input:$input){ id name token expiresAt } }","variables":{"input":{"name":"mcp","expiresAt":null}}}'
- Mutation:
- If you prefer Bearer auth, set
AFFINE_API_TOKENwith the returnedtoken(cookie auth remains fully supported).
- Generate via GraphQL while authenticated with cookies:
- Email/Password (self-hosted)
- You can configure
AFFINE_EMAILandAFFINE_PASSWORDin.env. On start, the server attempts to sign in againstPOST /api/auth/sign-inand stores the returned session cookies in-memory. - Or call the tool
affine_sign_inwith{ email, password }to sign in at runtime. After success, subsequent tools use the session.
Local Dev (Node)
- Install deps:
npm ci - Run in watch mode (stdio):
npm run dev - Build:
npm run build - Start (stdio):
npm start - Switch transport: set
MCP_TRANSPORT=stdiofor stdio mode, orwsfor WebSocket (MCP_WS_PORTcontrols port).
Connecting from MCP Clients
- WebSocket (recommended with Docker):
- Example client config:
- Name:
affine - URL:
ws://localhost:7821 - No command is needed; the container hosts the server.
- Name:
- Example client config:
- Stdio (runs as a child process):
- Command:
node - Args:
["dist/index.js"] - Env:
AFFINE_BASE_URL,AFFINE_COOKIE(orAFFINE_API_TOKEN),AFFINE_WORKSPACE_IDas needed.MCP_TRANSPORT=stdio
- Command:
Codex CLI example (WebSocket):
- Add an MCP server entry pointing to
ws://localhost:7821.
Codex CLI example (stdio):
- Add an MCP server entry with
command: "node",args: ["dist/index.js"]and the env variables above.
Security
- Treat
AFFINE_API_TOKENas sensitive. Use.envor secrets management; do not commit tokens. - Prefer cookie-based auth: Use
AFFINE_COOKIEfrom a logged-in browser session if your AFFiNE instance is private. - When exposing WebSocket ports, consider network access controls if AFFiNE data is private.
Limitations
- AFFiNE’s schema may vary; this server targets
packages/backend/serverGraphQL. - Document creation and low-level content edits are CRDT-driven and not fully exposed via public GraphQL. Use
affine_apply_doc_updatesonly if you know how to generate Yjs updates.
Codex CLI Integration
- WebSocket
- Ensure the server is running (Docker or Node with
MCP_TRANSPORT=ws). - Add an MCP server entry in your Codex CLI config (example JSON):
{ "mcpServers": { "affine": { "type": "websocket", "url": "ws://localhost:7821" } } }
- Auth stays in the server’s environment (e.g., container env). No extra client setup needed.
- Ensure the server is running (Docker or Node with
- Stdio
- Build:
npm run build - Configure Codex CLI to launch the process with env:
{ "mcpServers": { "affine": { "type": "stdio", "command": "node", "args": ["dist/index.js"], "cwd": "/absolute/path/to/affine-mcp-server", "env": { "AFFINE_BASE_URL": "http://localhost:3000", "AFFINE_COOKIE": "affine_session=...; affine_user_id=...", "MCP_TRANSPORT": "stdio" } } } }
- Build:
Claude CLI Integration
- WebSocket
- Run the server and expose
ws://localhost:7821. - Create or edit your MCP servers config (e.g.,
~/.config/claude/mcp.jsonon Linux,~/Library/Application Support/Claude/mcp.jsonon macOS):{ "mcpServers": { "affine": { "transport": { "type": "websocket", "url": "ws://localhost:7821" } } } }
- Run the server and expose
- Stdio
- Let Claude launch the process with env:
{ "mcpServers": { "affine": { "transport": { "type": "stdio", "command": "node", "args": ["dist/index.js"], "cwd": "/absolute/path/to/affine-mcp-server" }, "env": { "AFFINE_BASE_URL": "http://localhost:3000", "AFFINE_COOKIE": "affine_session=...; affine_user_id=...", "MCP_TRANSPORT": "stdio" } } } }
- Let Claude launch the process with env:
Note: Config schema/paths can vary between client versions. Refer to each client’s MCP docs for the latest details.
License
- MIT License. See the LICENSE file in this directory.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。