mcp-excalidraw-server

repository·main·Indexed 24 days ago

https://github.com/yctimlin/mcp_excalidraw

An Excalidraw toolkit for AI coding agents providing a persistent, interactive canvas. It includes an MCP server with 26 tools, a CLI, and a REST API, allowing agents to programmatically create, inspect, and refine diagrams as versionable code artifacts. Supports integration with Claude Desktop, Claude Code, and Cursor, and offers features like scene summaries, screenshots, and exports to Obsidian Excalidraw format.

Tokens
17K
Snippets
31
Records
102
Agent score
80%

What's inside mcp-excalidraw-server

  1. What is the Excalidraw MCP Server?

    main

    The mcp-excalidraw-server provides AI agents with a persistent, live Excalidraw canvas. Unlike one-shot diagram generators, this project allows agents to programmatically create, inspect, refine, and save diagrams as .excalidraw files within a repository.

    It operates via three primary interfaces:

    1. Agent Skill + CLI: Recommended for coding agents (Claude Code, Cursor, etc.). Uses npx -y mcp-excalidraw-server <command> for zero-config, composable JSON input/output.
    2. MCP Server: Provides 26 tools over stdio for Model Context Protocol clients (Claude Desktop, Cursor, etc.).
    3. REST API: Plain HTTP interface for LangChain or custom frameworks.

    Core Architecture:

    • Canvas Server: Runs the Excalidraw web UI, REST API, and WebSocket real-time sync (default http://127.0.0.1:3000).
    • Drivers: The CLI, MCP server, or HTTP clients drive the same central canvas.
    • Auto-start: Canvas-driving commands (CLI/MCP) automatically spawn the canvas server if it isn't already running. You can opt out by setting EXCALIDRAW_NO_AUTOSTART=1.
  2. Technical Implementation Details for Elements

    main

    When constructing element JSON for the API or CLI, note these specific requirements:

    • Labels: Use "label": {"text": "..."} for shape labels. (Note: This differs from the simplified text field used in some CLI contexts).
    • Arrows: Bind arrows using "start": {"id": "..."} and "end": {"id": "..."}.
    • Curved Arrows: For smooth curves, use "roundness": {"type": 2} with 3 or more points. For right-angle routing, use "elbowed": true.
    • Fonts: fontFamily must be a string (e.g., "1" or "helvetica"). Do not pass a number.
    • Points: The points field accepts both [[x,y]] (tuples) and [{x,y}] (objects).
  3. How Excalidraw MCP differs from the official Excalidraw MCP

    main

    Unlike the official Excalidraw MCP which acts as a chat widget for streaming diagrams into a conversation, this project is designed as a workbench for coding agents.

    Key differences include:

    • Persistent Local Canvas: Provides a persistent workspace with element-level CRUD (Create, Read, Update, Delete) capabilities.
    • Advanced Tooling: Includes layout tools, snapshots, and .excalidraw file I/O.
    • Agent Visibility: Agents can use describe to get a structured text summary of the scene and screenshot to receive a rendered PNG, allowing them to visually inspect and fix diagrams.
    • Multiple Interfaces: Can be driven via CLI, MCP, or a REST API.
  4. Configure Excalidraw MCP for Claude Desktop

    main

    To use the Excalidraw MCP server with Claude Desktop, add the configuration to your claude_desktop_config.json file.

    Locations:

    • macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
    • Windows: %APPDATA%\Claude\claude_desktop_config.json
    • Linux: ~/.config/Claude/claude_desktop_config.json

    Recommended Configuration (npx):

    {
      "mcpServers": {
        "excalidraw": {
          "command": "npx",
          "args": ["-y", "mcp-excalidraw-server"]
        }
      }
    }
    {
      "mcpServers": {
        "excalidraw": {
          "command": "npx",
          "args": ["-y", "mcp-excalidraw-server"]
        }
      }
    }
  5. Configure Excalidraw MCP for Claude Code

    main

    You can add the Excalidraw MCP server to Claude Code using the claude mcp add command.

    Recommended (npx):

    claude mcp add excalidraw --scope user -- npx -y mcp-excalidraw-server

    Local (node):

    claude mcp add excalidraw --scope user \
      -e EXPRESS_SERVER_URL=http://127.0.0.1:3000 \
      -e ENABLE_CANVAS_SYNC=true \
      -- node /absolute/path/to/mcp_excalidraw/dist/index.js

    Docker:

    claude mcp add excalidraw --scope user \
      -- docker run -i --rm \
      -e EXPRESS_SERVER_URL=http://host.docker.internal:3000 \
      -e ENABLE_CANVAS_SYNC=true \
      ghcr.io/yctimlin/mcp_excalidraw:latest

    Management Commands:

    • claude mcp list: List configured servers
    • claude mcp remove excalidraw: Remove a server
    claude mcp add excalidraw --scope user -- npx -y mcp-excalidraw-server
  6. Iterative diagram refinement workflow

    main

    For high-quality diagrams, use a feedback loop combining describe and screenshot (or their MCP equivalents describe_scene and get_canvas_screenshot).

    1. describe: Provides structured text (element IDs, types, positions, labels, connections). Use this to find IDs and understand bounding boxes before making updates.
    2. screenshot: Provides a PNG/SVG of the rendered canvas. Use this for visual verification of truncation, overlap, and arrow routing.

    Refinement Loop Example:

    1. Add elements.
    2. Run screenshot $\rightarrow$ identify visual issues (e.g., "text truncated").
    3. Use update <id> --set '{"width": 220}' to fix.
    4. Run screenshot again to verify.

    Refining existing diagrams:

    • Identify elements by id or label text (avoid using coordinates as they change).
    • Use update <id> --set '{...}' to modify, delete <id> to remove, or apply for patches.
    • Note: Bound arrows re-route automatically when you move or resize their endpoints; you do not need to recreate them.
  7. Install the Excalidraw Agent Skill

    main

    To give a coding agent the ability to use the Excalidraw toolkit, use the install-skill command. This copies a portable skill directory into the agent's designated skill root, teaching the agent the workflow for layout planning, screenshot verification, and file I/O.

    Installation Commands:

    • Modern coding agent: npx -y mcp-excalidraw-server install-skill --dir <skills-root>
    • Claude Code shortcut: npx -y mcp-excalidraw-server install-skill (installs to ~/.claude/skills)
    • Codex shortcut: npx -y mcp-excalidraw-server install-skill --target codex (installs to ~/.codex/skills)

    Re-running install-skill performs an in-place upgrade by replacing the target directory.

    npx -y mcp-excalidraw-server install-skill --dir <skills-root>
  8. Configure Excalidraw MCP for Cursor

    main

    Add the Excalidraw MCP server to Cursor by editing your .cursor/mcp.json (project-level) or ~/.cursor/mcp.json (global) file.

    Recommended (npx):

    {
      "mcpServers": {
        "excalidraw": {
          "command": "npx",
          "args": ["-y", "mcp-excalidraw-server"]
        }
      }
    }
    {
      "mcpServers": {
        "excalidraw": {
          "command": "npx",
          "args": ["-y", "mcp-excalidraw-server"]
        }
      }
    }
  9. Quick Start with the Excalidraw CLI

    main

    You can use the Excalidraw CLI without cloning or manual configuration. The following workflow demonstrates starting the canvas, adding elements via JSON, inspecting the work, and exporting the result.

    Prerequisites: Node.js ≥ 18.

    Workflow Example:

    # 1. Start the canvas and open it in a browser
    npx -y mcp-excalidraw-server start
    open http://127.0.0.1:3000
    
    # 2. Add elements using a JSON payload via stdin
    echo '[{"id":"api","type":"rectangle","x":100,"y":100,"width":160,"height":80,"text":"API Server"}]' | npx -y mcp-excalidraw-server add
    
    # 3. Inspect the canvas (structured text) and take a screenshot
    npx -y mcp-excalidraw-server describe
    npx -y mcp-excalidraw-server screenshot --out diagram.png
    
    # 4. Export the diagram as a file for your repo
    mkdir -p docs
    npx -y mcp-excalidraw-server export --out docs/architecture.excalidraw
    # start the canvas (drawing commands auto-start it too) and open it
    npx -y mcp-excalidraw-server start
    open http://127.0.0.1:3000   # browser tab enables screenshots & mermaid
    
    # draw something
    echo '[\n  {"id":"api","type":"rectangle","x":100,"y":100,"width":160,"height":80,"text":"API Server","backgroundColor":"#a5d8ff"},\n  {"id":"db","type":"rectangle","x":400,"y":100,"width":160,"height":80,"text":"Database","backgroundColor":"#99e9f2"},\n  {"type":"arrow","x":0,"y":0,"startElementId":"api","endElementId":"db","text":"SQL"}\n]' | npx -y mcp-excalidraw-server add
    
    # let your agent see its work
    npx -y mcp-excalidraw-server describe
    npx -y mcp-excalidraw-server screenshot --out diagram.png
    
    # diagrams as repo artifacts
    mkdir -p docs
    npx -y mcp-excalidraw-server export --out docs/architecture.excalidraw
  10. Use Excalidraw with Obsidian vaults

    main

    If you are working within an Obsidian vault (any directory containing .obsidian/), use the .excalidraw.md extension to ensure compatibility with the Excalidraw Obsidian plugin.

    Using .excalidraw.md allows for block references, vault-wide search, and proper Obsidian Sync support. The CLI handles the conversion to the plugin's native format automatically.

    Commands for Obsidian:

    • Export to Obsidian format: export --out "$VAULT/path/to/file.excalidraw.md" (or use --format obsidian).
    • Import from Obsidian format: import "$VAULT/path/to/file.excalidraw.md" --replace (supports both plain and compressed Drawing blocks).
    npx -y mcp-excalidraw-server export --out "$VAULT/diagrams/system-map.excalidraw.md"
    npx -y mcp-excalidraw-server import "$VAULT/diagrams/system-map.excalidraw.md" --replace
  11. Install and Run the Excalidraw CLI

    main

    You can run the Excalidraw CLI using npx or by installing it globally. The CLI commands interact with a running canvas server.

    Key Configuration:

    • Canvas URL: Controlled by the EXPRESS_SERVER_URL environment variable (defaults to http://127.0.0.1:3000) or via the --url <canvasUrl> flag.
    • Auto-start: Any canvas-touching CLI command will automatically attempt to start the server if it is not running. To disable this behavior, set EXCALIDRAW_NO_AUTOSTART=1.
    • Exit Codes:
      • 0: Success
      • 1: Error
      • 2: Usage error
      • 3: Canvas unreachable
      • 4: Browser tab required (for certain visual commands)
    npx -y mcp-excalidraw-server <command>
    # Or after global installation:
    excalidraw-canvas <command>
  12. Quick Start: Running from Source

    main

    To run the Excalidraw components from source (requires Node >= 18):

    1. Install dependencies:
      npm ci
    2. Build the project:
      npm run build
    3. Run the Canvas Server (Terminal 1):
      PORT=3000 npm run canvas
    4. Run the MCP Server (Terminal 2):
      node dist/index.js
    5. Use the CLI (Optional):
      node dist/bin.js status
    npm ci
    npm run build
    PORT=3000 npm run canvas
    node dist/index.js