Typst MCP Server

repository·main·Indexed 41 days ago

https://github.com/johannesbrandenburger/typst-mcp

An implementation of the Model Context Protocol (MCP) that enables AI models to interact with the Typst typesetting system. It provides tools for LaTeX-to-Typst conversion via Pandoc, Typst syntax validation, documentation retrieval, and rendering Typst code into PNG images.

Tokens
3.6K
Snippets
15
Records
18
Agent score
50%

What's inside Typst MCP

  1. Understand the Typst MCP Docker architecture

    main

    The project utilizes a multi-stage build system to optimize build times:

    • Base image: Contains pre-compiled Typst documentation (built from Rust nightly). This image only needs to be rebuilt when the Typst documentation itself changes.
    • Runtime image: Contains the MCP server (built from Python slim) and inherits from the base image. This is the image used for standard operations and is rebuilt when the Python server code changes.
  2. Manually build Typst MCP images

    main

    If you prefer not to use the build script, you can build the images manually using the provided Dockerfiles. The project uses a two-stage architecture:

    1. Base Image: Built from Dockerfile.base, contains pre-compiled Typst documentation.
    2. Runtime Image: Built from Dockerfile.runtime, contains the Python MCP server and uses the base image.
    # Build base image with Typst documentation
    docker build -f Dockerfile.base -t ghcr.io/johannesbrandenburger/typst-mcp-base:latest .
    
    # Build runtime image
    docker build -f Dockerfile.runtime -t ghcr.io/johannesbrandenburger/typst-mcp:latest .
  3. Setup and Local Installation of Typst MCP Server

    main

    To set up the server from source, follow these steps:

    1. Clone the repositories:

      git clone https://github.com/johannesbrandenburger/typst-mcp.git
      git clone https://github.com/typst/typst.git
    2. Generate Typst documentation: Run the following command from within the typst repository. Note: You must adjust the paths to point to your local typst-mcp directory.

      cargo run --package typst-docs -- --assets-dir ../typst-mcp/typst-docs --out-file ../typst-mcp/typst-docs/main.json
    3. Install dependencies: Use uv to sync the environment:

      uv sync
    4. Install Typst (ensure the Typst binary is available on your system).

    Running the server

    To run the server locally for testing:

    python server.py

    Or to install it directly in Claude Desktop via MCP:

    mcp install server.py
    # Generate docs (adjust paths as needed)
    cargo run --package typst-docs -- --assets-dir ../typst-mcp/typst-docs --out-file ../typst-mcp/typst-docs/main.json
    
    # Install dependencies
    uv sync
    
    # Run server
    python server.py
  4. Build the Typst MCP images using the build script

    main

    The project provides a build.sh script to manage the multi-stage build process. Use these commands depending on your needs:

    • Build everything: Rebuilds both the base and runtime images.
    • Build runtime only: Use this when you have made changes to the Python MCP server code.
    • Build base only: Use this when the Typst documentation needs updating.
    • Run: Builds and then executes the container.
    # Build both base and runtime images
    ./build.sh
    
    # Build only runtime image (for code changes)
    ./build.sh runtime
    
    # Build only base image (when docs change)
    ./build.sh base
    
    # Run the container
    ./build.sh run
  5. Configure Typst MCP for VS Code Agent Mode

    main

    VS Code's Agent Mode supports MCP natively.

    1. Enable Agent Mode: Ensure chat.agent.enabled is set to true in your VS Code settings.
    2. Configure Workspace: Create a .vscode/mcp.json file in your workspace root.

    Using Docker

    {
      "servers": {
        "typst": {
          "command": "docker",
          "args": [
            "run", "--rm", "-i",
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ]
        }
      }
    }

    For global configuration, use the MCP: Open User Configuration command in VS Code.

  6. Configure Typst MCP for OpenCode

    main

    To integrate Typst MCP with OpenCode, create or edit an opencode.json file in your project root.

    Using Docker

    {
      "$schema": "https://opencode.ai/config.json",
      "mcp": {
        "typst": {
          "type": "local",
          "command": ["docker", "run", "--rm", "-i", 
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ],
          "enabled": true
        }
      }
    }

    Using Local Installation

    {
      "$schema": "https://opencode.ai/config.json",
      "mcp": {
        "typst": {
          "type": "local",
          "command": ["python", "/path/to/typst-mcp/server.py"],
          "enabled": true
        }
      }
    }
  7. Configure Typst MCP with Docker Compose

    main

    You can use the following docker-compose.yml configuration to manage the Typst MCP service. Note that stdin_open: true is required for the MCP server to interact correctly.

    version: '3.8'
    services:
      typst-mcp:
        image: ghcr.io/johannesbrandenburger/typst-mcp:latest
        stdin_open: true
        tty: false
        restart: unless-stopped
  8. Configure Typst MCP for Claude Desktop

    main

    To use the Typst MCP server in Claude Desktop, add it to your claude_desktop_config.json file.

    File Locations:

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

    Use the pre-built image from GitHub Container Registry:

    {
      "mcpServers": {
        "typst": {
          "command": "docker",
          "args": [
            "run", "--rm", "-i",
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ]
        }
      }
    }

    Option 2: Local Installation

    If you have installed the server locally, point to the server.py file:

    {
      "mcpServers": {
        "typst": {
          "command": "python",
          "args": ["/path/to/typst-mcp/server.py"]
        }
      }
    }
  9. Configure Typst MCP for Cursor

    main

    You can configure Typst MCP for Cursor either globally or per-project.

    Project-specific Configuration

    Create a .cursor/mcp.json file in your project root.

    Using Docker:

    {
      "mcpServers": {
        "typst": {
          "command": "docker",
          "args": [
            "run", "--rm", "-i",
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ]
        }
      }
    }

    Using Local Installation:

    {
      "mcpServers": {
        "typst": {
          "command": "python",
          "args": ["/path/to/typst-mcp/server.py"]
        }
      }
    }

    Global Configuration

    Create or edit ~/.cursor/mcp.json using the same JSON structures as above.

  10. Configure Typst MCP for Claude Code

    main

    To configure the Typst MCP server for Claude Code, edit your settings file located at ~/.claude/settings.json.

    Using Docker

    {
      "mcpServers": {
        "typst": {
          "command": "docker",
          "args": [
            "run", "--rm", "-i",
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ]
        }
      }
    }

    Using Local Installation

    {
      "mcpServers": {
        "typst": {
          "command": "python",
          "args": ["/path/to/typst-mcp/server.py"]
        }
      }
    }
  11. Configure Typst MCP for Roo Code

    main

    To use Typst MCP with Roo Code, you can use project-specific settings or global settings.

    Project-specific Configuration

    Create a .roo/mcp.json file in your project root.

    Using Docker:

    {
      "mcpServers": {
        "typst": {
          "command": "docker",
          "args": [
            "run", "--rm", "-i",
            "ghcr.io/johannesbrandenburger/typst-mcp:latest"
          ]
        }
      }
    }

    Using Local Installation:

    {
      "mcpServers": {
        "typst": {
          "command": "python",
          "args": ["/path/to/typst-mcp/server.py"]
        }
      }
    }

    Alternatively, you can edit the global settings via the Roo Code MCP settings view in your IDE.