Skip to main content
Glama
README.md
# Overleaf, self-hosted, with an MCP server

Overleaf Community Edition in Docker, TeX Live that compiles real documents, and an MCP server so Claude (or any MCP client) can list, read, edit, compile and manage projects. Duplicate this repo, run one command, done. Every task is `bun run <task>`.

Needs Docker (Compose v2) and [Bun](https://bun.sh). Nothing else. Overleaf's image is amd64-only: on Apple Silicon or other ARM machines it runs under emulation (Docker Desktop and Colima handle this automatically), which works but compiles more slowly. Host it on an x86 server for real use.

## Host it for a group

On a Linux server with Docker, DNS for your domain pointing at it, and ports 80/443 open:

```bash
bun run setup --domain overleaf.example.com
```

That starts Overleaf, Mongo and Redis, Caddy with an automatic Let's Encrypt certificate, and the MCP server at `https://overleaf.example.com/mcp`. It creates the admin account and prints the MCP connection details.

Everyone connects with the URL alone and signs in with **their own Overleaf account** (OAuth), so the MCP acts as them and sees only their projects:

- **Claude (claude.ai, Desktop, mobile):** Settings → Connectors → Add custom connector, URL `https://overleaf.example.com/mcp`, then Connect and sign in with your Overleaf email and password.
- **Claude Code:** `claude mcp add --transport http overleaf https://overleaf.example.com/mcp`, then `/mcp` → Authenticate.

`bun run mcp:config` prints these again (`--json` for scripts), plus the admin token form for automation.

Add people with `bun run user:add someone@example.com`; it prints a link where they set their password (no mail server needed). To have Overleaf email invites and password links instead, see [Email](#email-optional).

**Using an AI agent to set this up?** Open Claude Code (or another agent) in the repo and say "set this up". [AGENTS.md](AGENTS.md) has its instructions.

## Run it just for yourself

```bash
bun run setup
```

Overleaf at http://127.0.0.1:8600. Open Claude Code in this folder and approve the `overleaf` MCP server from `.mcp.json` (it runs over stdio with `bun`).

## Tasks

| Task | Command |
| --- | --- |
| First install (local, or `--domain d` to host; `--email`, `--port` optional) | `bun run setup` |
| Start / apply `.env` changes | `bun run up` |
| Stop | `bun run down` |
| Containers and MCP connection details | `bun run status` |
| Logs (all, or one service: `bun run logs sharelatex`) | `bun run logs` |
| Add a user, get their set-password link | `bun run user:add a@b.com` |
| Send invite / password emails over SMTP (optional) | `bun run email:setup --host h --from "Overleaf <o@x>"` |
| Send a test email | `bun run email:test you@example.com` |
| MCP config to hand out (`--json` for scripts) / new admin token | `bun run mcp:config` / `bun run mcp:token` |
| Backup to `backups/<stamp>/` | `bun run backup` |
| Automatic backups every 6h, keep 14, optional offsite copy ([Backups](#backups)) | `bun run backup:schedule [--every 6h] [--keep 14] [--remote name:path]` |
| One scheduled-style backup now / list local and remote backups | `bun run backup:now` / `bun run backup:list` |
| Restore (a path, a stamp from `backup:list`, or `latest`) | `bun run restore latest --yes` |
| Wipe all projects and users | `bun run reset --yes` |
| Install a TeX package now | `bun run texlive <pkg>` |
| Tests (unit) / full hosted install check | `bun run test` / `bun run test:e2e` |

## Email (optional)

Without a mail server everything still works: `user:add` prints the set-password link and project invites show up in the recipient's project list. To have Overleaf send activation, password-reset and project-invite emails:

```bash
bun run email:setup --host smtp.example.com --port 587 --user <smtp user> --from "Overleaf <overleaf@example.com>"
bun run email:test you@example.com
```

It prompts for the password without echoing it (or pipe it in with `--password-stdin`), writes the `OVERLEAF_EMAIL_*` variables to `.env`, and recreates the `sharelatex` container. `email:test` sends from inside the container with Overleaf's own mailer and settings. After that, `user:add` tells you the activation email went out and still prints the link as a fallback. Other flags: `--secure` (implicit TLS; automatic on port 465), `--no-verify-tls`, `--ignore-tls`, `--reply-to`, `--name`, `--logger`, `--test <addr>`. `--require-confirmation` makes new accounts confirm their address (off by default). `bun run email:setup --off` removes it all.

Common providers (any SMTP relay works; the `--from` domain must be verified with the provider):

| Provider | `--host` | `--port` | `--user` | Password |
| --- | --- | --- | --- | --- |
| Gmail / Google Workspace | `smtp.gmail.com` | 587 | your address | an [app password](https://myaccount.google.com/apppasswords) (needs 2-step verification) |
| AWS SES | `email-smtp.<region>.amazonaws.com` | 587 | SES SMTP username | SES SMTP password (not your AWS secret key) |
| Resend | `smtp.resend.com` | 465 | `resend` | an API key |
| Postmark | `smtp.postmarkapp.com` | 587 | server API token | the same server API token |

## Backups

`bun run backup:schedule` turns on the `backup` container (compose profile `backup`, added to `COMPOSE_PROFILES` next to `hosted`). It runs inside Docker, so there is no host cron or systemd: it backs up once on start and then every `BACKUP_INTERVAL` (`30m`, `6h`, `1d`). Each backup is `backups/scheduled/<stamp>/` with `mongo.archive.gz` (mongodump over the compose network), `sharelatex-data.tgz` (the `overleaf-data` volume, mounted read-only) and `env` (a copy of `.env`). It keeps the newest `BACKUP_KEEP` locally and on the remote. `--every`, `--keep` and `--remote` write `BACKUP_INTERVAL`, `BACKUP_KEEP` and `BACKUP_RCLONE_REMOTE` to `.env`; `--remote none` clears it, `--off` stops the schedule (backups stay). Every backup task takes `--json` for machine-readable output (see AGENTS.md). `BACKUP_DIR` moves the local folder. Logs: `bun run logs backup`.

**Offsite.** Set `--remote <rclone remote>:<path>` and every backup is copied there with [rclone](https://rclone.org/overview/) (S3, B2, Google Drive, SFTP, ...). Configure the remote either with a file, `BACKUP_RCLONE_CONFIG=/path/to/rclone.conf` in `.env` (make it with `rclone config`), or with rclone's env vars in `.env`, for example:

```sh
# S3 (or any S3-compatible store)         remote: s3:my-bucket/overleaf
RCLONE_CONFIG_S3_TYPE=s3
RCLONE_CONFIG_S3_PROVIDER=AWS
RCLONE_CONFIG_S3_ACCESS_KEY_ID=...
RCLONE_CONFIG_S3_SECRET_ACCESS_KEY=...
RCLONE_CONFIG_S3_REGION=us-east-1
# Backblaze B2                            remote: b2:my-bucket/overleaf
RCLONE_CONFIG_B2_TYPE=b2
RCLONE_CONFIG_B2_ACCOUNT=...
RCLONE_CONFIG_B2_KEY=...
```

Google Drive needs an OAuth token, so use the file: run `rclone config` on any machine with a browser, create a `drive` remote named `gdrive` (scope `drive.file`), and set `BACKUP_RCLONE_CONFIG` to that `rclone.conf` and `--remote gdrive:overleaf`.

**Secrets never leave the machine.** The `env` file (admin password, `MCP_TOKEN`, invite secret) is excluded from every upload; only the Mongo dump and the data archive go offsite. A restore from the remote therefore keeps your current `.env`, which is all it needs: users and projects live in Mongo. The uploaded data itself (projects, password hashes) is not encrypted by default; to encrypt it, point `--remote` at an rclone [crypt](https://rclone.org/crypt/) remote wrapping your bucket (`RCLONE_CONFIG_SECRET_TYPE=crypt`, `RCLONE_CONFIG_SECRET_REMOTE=s3:my-bucket/overleaf`, `RCLONE_CONFIG_SECRET_PASSWORD=<rclone obscure output>`, then `--remote secret:`), and keep the crypt password somewhere other than this server.

**Restore.** `bun run restore <stamp> --yes` finds the stamp in `backups/` or `backups/scheduled/`; a stamp that only exists on the remote is downloaded first. `latest` picks the newest local one.

## How fast is setup

The Overleaf + TeX Live image is prebuilt by CI (`.github/workflows/image.yml`) and pulled, so setup time is your download speed; restarts take seconds. If the pull fails, setup builds the image locally, which takes about 25 minutes (TeX Live installs ~3,600 small packages). If you duplicate the repo under another owner, run the Image workflow once and set `OVERLEAF_IMAGE_NAME=ghcr.io/<you>/<repo>` in `.env`.

## What is in `.env`

Written by `bun run setup`, gitignored, owner-only. `OVERLEAF_INVITE_TOKEN_SECRET`, `OVERLEAF_PORT`, `OVERLEAF_SITE_URL`, `OVERLEAF_DOMAIN` + `COMPOSE_PROFILES=hosted` (hosted only; `backup` is appended by `backup:schedule`), `MCP_TOKEN`, and the admin `OVERLEAF_EMAIL` / `OVERLEAF_PASSWORD` the MCP server signs in with. Optional: the `OVERLEAF_EMAIL_*` SMTP settings and `EMAIL_CONFIRMATION_DISABLED` (written by `bun run email:setup`), `OVERLEAF_IMAGE_NAME`, `OVERLEAF_LOCAL_TAG`, `TEXLIVE_PACKAGES`, `TEXLIVE_SCHEME`, `OVERLEAF_IMAGE` (the last three make `up` build locally), and the `BACKUP_*` / `RCLONE_CONFIG_*` settings from [Backups](#backups).

## The MCP server

Tools: status, list/select/create/rename/delete/download projects, invite collaborators, list/read/grep/write/edit/delete files, read images, history, diff, restore, compile and compile log, reference check, word count. Details in [mcp-client/README.md](mcp-client/README.md).

Locally it runs over stdio, signs in as the admin with the password from `.env` and renews its own session.

Hosted it runs as the `mcp` container (`mcp-client/src/http.ts`, `hosted.ts`) behind Caddy and is an OAuth 2.1 authorization server per the MCP spec (`mcp-client/src/oauth/`): discovery at `/.well-known/oauth-protected-resource/mcp` and `/.well-known/oauth-authorization-server`, dynamic client registration, authorization code + PKCE, refresh tokens, all endpoints under `/mcp/oauth/` so they don't collide with Overleaf's `/login` and `/register`. The sign-in page checks the email and password by logging in to Overleaf and keeps only that Overleaf session (cookie jar) in the `mcp-session` volume, never the password. Each MCP session gets its own context and project selection. Tokens survive restarts; if Overleaf signs the user out, their tokens are dropped and the client asks them to sign in again. The shared `MCP_TOKEN` (`Authorization: Bearer`, or `/mcp/<token>`) still works and acts as the admin, for automation and CI.

## TeX Live

The upstream image ships TeX Live's minimal scheme: roughly 150 packages, enough for a blank `article` and little else. `docker/Dockerfile` installs these collections on top: `latexrecommended`, `latexextra`, `fontsrecommended`, `bibtexextra` (biblatex and biber), `mathscience` (siunitx, physics), `pictures` (TikZ, pgfplots), `publishers` (IEEEtran, acmart, elsarticle, revtex) and `langenglish`. That covers most papers, theses and Beamer slides.

- **Need one more package now:** `bun run texlive algorithm2e minted`. This installs into the running container and is lost on the next rebuild.
- **Keep it permanently:** add the package to `TEXLIVE_PACKAGES` in `.env` (space separated, this replaces the default list, so start from the list in `docker/Dockerfile`) and run `bun run up`. Any `TEXLIVE_*` or `OVERLEAF_IMAGE` setting makes `bun run up` build locally (about 25 minutes) instead of pulling, and the package survives restarts, pulls and reboots.
- **Everything:** set `TEXLIVE_SCHEME=scheme-full` in `.env` and rerun `bun run up`. Expect several GB and a long build.

A compile that fails with `File 'something.sty' not found` is this, not a bug in the document.

## Security

Community Edition runs LaTeX compiles without a sandbox. Anyone who can compile a project has full access inside the `sharelatex` container, including the data volume and the network. Locally every port binds to 127.0.0.1. Hosted mode puts Caddy (HTTPS) in front, but Overleaf accounts are still the only gate, so host it for people you trust and read upstream's note on [Sandboxed Compiles](https://docs.overleaf.com/on-premises/configuration/overleaf-toolkit/server-pro-only-configuration/sandboxed-compiles) before you consider it.

`.env` holds the admin password and `MCP_TOKEN`; anyone with the token acts as the admin through the MCP server, so give people the OAuth URL instead and keep the token for automation. Rotate it with `bun run mcp:token` (OAuth users are unaffected). The `mcp-session` volume holds each OAuth user's live Overleaf session and hashed tokens. `.session/session.json` holds a live session cookie. Both are gitignored and written with owner-only permissions.

## Layout

- `package.json` / `scripts/cli.ts`: every `bun run` task. `scripts/email.ts`: `email:setup` / `email:test`. `scripts/e2e.ts`, `scripts/e2e-email.ts`: the hosted install and SMTP checks CI runs (`docker/mailpit.ci.yml` is CI's mail catcher).
- `docker-compose.yml`: Overleaf (6.3.0 + TeX Live), Mongo replica set, Redis; plus `mcp` and `caddy` under the `hosted` profile and `backup` under the `backup` profile. Data lives in named volumes.
- `scripts/backup.ts` + `docker/backup/`: backup, restore and the scheduled backup container (mongo tools + rclone).
- `docker/Dockerfile`: TeX Live collections on the upstream image. `docker/Caddyfile`: HTTPS, `/mcp*` and `/.well-known/oauth-*` routing to the MCP server.
- `mcp-client/`: the MCP server (fork of [overleaf-claude-mcp](https://github.com/MarvelCollin/overleaf-claude-mcp)), run from TypeScript source by bun.
- `server/`: optional submodule of overleaf/overleaf for reading server code; not used at runtime.

## License

Root files are MIT (see [LICENSE](LICENSE)). `mcp-client/` is MIT, forked from MarvelCollin/overleaf-claude-mcp. `server/` is Overleaf's AGPL-3.0 source. The official Overleaf image has its own terms.