latex-workspace-mcp
by Alexchen93
README.md
# LaTeX SafeFix — LaTeX Workspace MCP
Safe, precise LaTeX editing, compilation, and PDF verification through Model Context Protocol tools for local LaTeX projects. The server lets an AI assistant inspect a configured workspace, make narrow `.tex` edits, compile with a fixed `latexmk` command, and verify that the generated PDF and image assets are usable.
## Features
- Scan LaTeX projects and infer the main file, engine, bibliography mode, included files, figure references, citations, and labels.
- Read `.tex` files, create or overwrite `.tex` files only with explicit overwrite approval, and replace exactly one unique text occurrence. Preview an edit, confirm its application, and roll it back if needed.
- Compile a selected main file with `pdflatex` or `xelatex` through `latexmk` into the document-local `build/` directory.
- Analyze build logs for errors, warnings, undefined references, and undefined citations.
- Check citation keys, duplicate labels, undefined labels, and figure path existence.
- Validate image assets before a build with `validate_latex_assets`, including reachable `\input`/`\include` files, first-page PDF metadata, dimensions for PNG/JPEG/PDF assets, and similar-path suggestions.
- Verify generated PDFs with `verify_latex_pdf`, including MIME type, page count, page sizes, and embedded image metadata from `pdfimages`. Render a page to PNG with `render_latex_pdf_page` for visual inspection.
- Run the full asset-check, build, and PDF-verification flow with `build_and_verify_latex`.
- Use either stdio MCP for local editors or Streamable HTTP MCP for ChatGPT-style clients.
## Security Model
- Every path is resolved under the configured `--root` directory. Paths outside the root are rejected.
- Write tools only accept project-relative `.tex` files. There is no delete, rename, arbitrary shell, or generic file-write API.
- `write_tex_file` refuses to overwrite existing files unless `overwrite: true` is supplied.
- `replace_tex_text` requires one exact unique match. Empty, missing, or multiple matches are rejected.
- Compilation uses a fixed `latexmk` command with `shell: false`, disabled shell escape, bounded output, and build artifacts under `build/`. By default it passes `-norc` so `latexmk` does not load configuration files. Set `LATEX_MCP_ALLOW_LATEXMKRC=1` only for trusted projects to restore customized builds: latexmk configuration can execute commands.
- Workspace paths containing symbolic links are rejected. Writes use temporary files and atomic replacement; previewed edits also reject changes since the preview.
- The server is not an OS sandbox. Compile only trusted TeX or use a separate OS-level sandbox: TeX and its toolchain run with the server user's permissions. Portable filesystem checks also cannot prevent a hostile local process from racing directory changes.
- The HTTP server requires an API key from `LATEX_MCP_API_KEY_FILE` and accepts either `Authorization: Bearer ...` or `X-API-Key`.
- The HTTP server binds to `127.0.0.1` by default and allows only `localhost,127.0.0.1` host headers unless `LATEX_MCP_ALLOWED_HOSTS` is set.
## Requirements and installation
The MCP server and its external programs must run in the same environment, with
`node`, `latexmk`, `pdflatex`, `file`, `pdfinfo`, `pdfimages`, and `pdftoppm` on its `PATH`.
Node.js 20 or newer is required. Install `xelatex` as well if using the XeLaTeX
engine. Your document may require additional TeX packages and fonts.
### Linux (Debian or Ubuntu)
```bash
sudo apt update
sudo apt install git nodejs npm latexmk texlive-latex-recommended texlive-xetex file poppler-utils
```
Check `node --version`: if the distribution package is older than Node 20, install
an up-to-date Node release from [nodejs.org](https://nodejs.org/) first. Other
Linux distributions can install equivalent TeX Live, `file`, and Poppler packages
with their package manager.
### macOS
Install Node.js 20 or newer, a TeX distribution containing `latexmk` and the
chosen engine (for example [MacTeX](https://www.tug.org/mactex/)), and Poppler.
For example, with [Homebrew](https://brew.sh/) and MacTeX installed:
```bash
brew install node poppler
```
After installing MacTeX, open a new terminal and check that `/Library/TeX/texbin`
is on `PATH`. macOS includes the `file` utility.
### Windows
Use [WSL 2](https://learn.microsoft.com/windows/wsl/install) with an Ubuntu
installation. Run the Linux commands above inside the WSL terminal, and keep the
server, Node, TeX, Poppler, and project root in that same WSL environment. An
MCP client running in Windows must be able to launch `wsl.exe`; configure it to
invoke the Linux Node executable and Linux paths within WSL. Native Windows
installation requires compatible versions of all command-line dependencies on
`PATH` and is not currently documented or tested.
### Install the server
In the environment that will run the server:
```bash
git clone https://github.com/Alexchen93/latex-workspace-mcp.git
cd latex-workspace-mcp
npm ci
node --version
latexmk -v
pdflatex --version
file -v
pdfinfo -v
pdfimages -v
pdftoppm -v
```
Use an existing absolute project root when starting either server:
```bash
node server.mjs --root /path/to/latex/project
```
The stdio process communicates with the MCP client over standard input/output;
start it through the client configuration below. The project root should be the
smallest directory containing the LaTeX files and assets the server needs.
## Build limits
Set `LATEX_MCP_TIMEOUT_MS` to the maximum compilation time in milliseconds
(default `120000`) and `LATEX_MCP_MAX_OUTPUT_BYTES` to the maximum captured
compilation output in bytes (default `12000`). Set these environment variables
in the MCP client's server configuration or in the service environment before
starting either transport.
By default, latexmk configuration files are disabled with `-norc`. For a trusted
project that requires its latexmk configuration, set
`LATEX_MCP_ALLOW_LATEXMKRC=1` in the server environment. Configuration files can
execute commands, so enabling this changes the security assumptions of a build.
## Stdio MCP
Use stdio when the MCP client launches local commands directly.
```json
{
"mcpServers": {
"latex-workspace": {
"command": "node",
"args": ["/path/to/latex-workspace-mcp/server.mjs", "--root", "/path/to/latex/project"]
}
}
}
```
For a Windows client invoking a WSL installation, an example configuration is:
```json
{
"mcpServers": {
"latex-workspace": {
"command": "wsl.exe",
"args": ["--", "node", "/home/YOUR_USER/latex-workspace-mcp/server.mjs", "--root", "/home/YOUR_USER/latex-project"]
}
}
}
```
Replace both Linux paths and ensure `node` is on the WSL launch environment's
`PATH`; use its absolute path if necessary.
## HTTP MCP
Create an API key file with permissions limited to the service user:
```bash
install -d -m 700 ~/.config/latex-workspace-mcp
openssl rand -base64 48 > ~/.config/latex-workspace-mcp/api-key
chmod 600 ~/.config/latex-workspace-mcp/api-key
```
Start the loopback HTTP server:
```bash
LATEX_MCP_API_KEY_FILE="$HOME/.config/latex-workspace-mcp/api-key" \
LATEX_MCP_HTTP_HOST=127.0.0.1 \
LATEX_MCP_HTTP_PORT=8787 \
LATEX_MCP_ALLOWED_HOSTS=localhost,127.0.0.1 \
node http-server.mjs --root /path/to/latex/project
```
The MCP endpoint is `http://127.0.0.1:8787/mcp`. Unauthenticated calls should return `401`.
If you put a trusted reverse proxy in front of the server, keep the origin private and set `LATEX_MCP_ALLOWED_HOSTS` to the exact host headers your proxy sends.
## systemd
Copy `examples/latex-mcp-http.service.example` and replace `YOUR_PROJECT_ROOT` with the absolute LaTeX workspace root. Keep `LATEX_MCP_HTTP_HOST=127.0.0.1` when the proxy runs on the same machine.
## ChatGPT Prompt Pattern
Use a prompt that keeps edits precise and makes the verification flow explicit:
```text
Use the LaTeX Workspace MCP for this project. Before changing figure paths, call validate_latex_assets with the project-relative main_file. For source edits, prefer replace_tex_text and only use write_tex_file with overwrite: true when I explicitly ask for a full-file replacement. After any document or image-path change, call build_and_verify_latex. If the build fails, use analyze_log and validate_latex_assets to make the smallest precise repair.
```
For projects with multiple `main.tex` files, always pass a project-relative `main_file`, such as `paper/main.tex`.
## Image And First-Page Validation Workflow
1. Put images under the configured project root and reference them with paths relative to the main file directory.
2. Call `validate_latex_assets` before compiling. Missing `\includegraphics` assets stop `build_and_verify_latex` before LaTeX runs.
3. For PNG and JPEG assets, the tool reports dimensions from the file header.
4. For PDF assets, the tool calls `pdfinfo` and reports first-page page-size metadata without extracting binary content.
5. After compilation, call `verify_latex_pdf` or `build_and_verify_latex` to confirm the expected `build/<stem>.pdf` exists, has pages, and lists embedded images.
## Preview, confirm, and roll back an edit
Call `preview_tex_edit` with `file`, `search`, and `replacement` to inspect an
exact, unique replacement as a diff. Once the change is approved, call
`apply_tex_edit` with the returned `edit_id` and `confirmed: true`. To undo it,
call `rollback_tex_edit` with the returned `rollback_id` and `confirmed: true`.
Application and rollback reject a file that changed since the associated preview
or edit, so review a fresh preview if that happens. IDs are scoped to the MCP
server session, are lost on restart, and can expire when the bounded history is
full; they are not a substitute for version control or backups. The existing
`write_tex_file` and `replace_tex_text` tools remain available for compatibility.
Call `render_latex_pdf_page` with `main_file` and a one-based `page` after a
successful build to obtain a PNG page for visual inspection. It requires
Poppler's `pdftoppm`; metadata verification by itself cannot establish that a
page looks correct.
## Tool Summary
- `get_project_info`
- `scan_project`
- `analyze_log`
- `check_citations`
- `check_labels`
- `check_figures`
- `validate_latex_assets`
- `verify_latex_pdf`
- `build_and_verify_latex`
- `list_tex_files`
- `read_tex_file`
- `write_tex_file`
- `replace_tex_text`
- `compile_latex`
- `preview_tex_edit`
- `apply_tex_edit`
- `rollback_tex_edit`
- `render_latex_pdf_page`
## Agent skill
The reusable [LaTeX SafeFix skill](skills/latex-safefix/SKILL.md) contains a
client configuration example, task prompts, and troubleshooting guidance. The
[ZIP package](dist/latex-safefix-skill.zip) includes the skill and MIT license.
Extract it into a skill directory supported by your agent, configure the MCP
server separately, and replace the example paths with your own.
## License
[MIT](LICENSE)
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues