zotero-cloudflare-mcp
README.md
# zotero-cloudflare-mcp
Self-hosted remote MCP server for your Zotero library, running on your own Cloudflare account. Claude can search your library and read your PDFs from the claude.ai website, the Claude phone apps, Claude Desktop and Claude Code, **without Zotero desktop running**.
> Not affiliated with, endorsed by or sponsored by Zotero or the Corporation for Digital Scholarship. "Zotero" is used only to describe what this software connects to.
## What it does
- **Searches and reads your library** through the [Zotero Web API](https://www.zotero.org/support/dev/web_api/v3/start): items, collections, tags, notes, recent additions.
- **Reads PDF text** page by page and in chunks, so long papers don't flood the conversation.
- **Indexes PDFs Zotero hasn't indexed.** Zotero's API only has full text for PDFs that Zotero desktop has already processed. This server downloads the PDF itself, extracts the text and **saves it back to Zotero's full-text index**, so it becomes searchable everywhere, including in Zotero itself. An hourly background job does this for new PDFs, so papers you add from your phone become searchable without asking.
- **Sign-in with GitHub**, limited to the GitHub usernames you list. Works as a claude.ai custom connector.
- **Read-only by default.** Optional write tools (add items, add notes, add/remove tags and collections) are off unless you turn them on. There are **no delete tools**.
- **Optional WebDAV file storage on Cloudflare R2** ([packages/webdav](packages/webdav)) as a cheaper alternative to Zotero Storage. The MCP server can read PDFs from either.
### Tools
| Tool | What it returns |
| --- | --- |
| `search_items` | Matching items (title, creators, year, venue, DOI, tags, short abstract), with paging. `qmode: everything` also searches indexed full text. |
| `get_item` | Full metadata of one item plus its attachments and notes. |
| `list_collections` | All collections with their full path and item counts. |
| `get_collection_items` | Items in one collection. |
| `list_tags` | Tags with item counts. |
| `get_recent_items` | Most recently added or modified items. |
| `get_library_stats` | Counts of items, attachments, notes, collections, tags, indexed PDFs. |
| `get_item_fulltext` | Text of an item's PDF, by page range (`"3-7"`) and in chunks (`offset`, `maxChars`). |
| `save_item` \* | Creates an item from metadata. |
| `create_note` \* | Adds a note to an item, or a standalone note. |
| `update_item` \* | Adds/removes tags, adds/removes collections, edits fields. Never replaces the whole tag or collection list; refuses if the item changed in the meantime. |
| `attach_pdf_from_url` \* | Downloads a PDF and attaches it to an existing item, after automatic safety and match checks (see [Attaching PDFs](#attaching-pdfs-from-a-link)). |
\* Only when `ENABLE_WRITES` is `"true"`.
### Limits
- **Citation keys are not searchable.** Zotero's quick search doesn't index them (e.g. Better BibTeX keys). Search by title, author or year instead.
- **Scanned PDFs without a text layer** are detected and reported, but not OCR'd.
- **PDFs over 30 MB** are skipped (change with `MAX_PDF_MB`; Workers have 128 MB of memory).
- **Linked files** (files that live only on one computer, not synced to Zotero Storage or WebDAV) can't be read.
- **Personal library only.** Group libraries are not supported in this version.
- The background job indexes 5 PDFs per hour by default (`INDEX_PER_RUN`, up to 20) and looks at the 2,000 most recent attachments. A large backlog of older, unindexed PDFs takes a while. Any PDF is indexed immediately when Claude reads it with `get_item_fulltext`.
### Costs
| | Needed? | Cost |
| --- | --- | --- |
| Cloudflare **Workers Paid** plan | Yes, for PDF text extraction and background indexing | US$5/month, includes far more requests and CPU time than one person uses |
| Workers KV (2 namespaces) | Yes | Within the included allowance for personal use |
| R2 (WebDAV file storage) | Optional | First 10 GB free, then about US$0.015 per GB-month |
| Workers AI (fallback text extractor) | Optional, off by default | Pay per use |
| Zotero Storage | Only if you use it for files | Free up to 300 MB, paid plans beyond |
| GitHub account and OAuth App | Yes, for sign-in | Free |
On the Workers **Free** plan (10 ms CPU per request) the metadata tools should work, but reading or indexing PDFs will fail, because extracting a typical paper takes 100–700 ms of CPU. Check Cloudflare's [Workers pricing](https://developers.cloudflare.com/workers/platform/pricing/) for current numbers.
## How PDF text works
When Claude asks for an item's text:
1. If this server extracted the PDF before, the stored text (with page boundaries) is used.
2. Otherwise, if Zotero already has indexed full text, that is used.
3. Otherwise the PDF is downloaded, from the R2 WebDAV bucket if configured (Zotero stores each file there as `zotero/KEY.zip`), otherwise from Zotero Storage.
4. Text is extracted page by page with [unpdf](https://github.com/unjs/unpdf) (PDF.js) inside the Worker. In tests on real papers this took 0.1–0.25 s per paper in the Workers runtime and keeps page boundaries, which page ranges need. Workers AI's `toMarkdown()` can be enabled as a fallback for PDFs unpdf can't open; it returns text without page boundaries and was not tested against real papers for this release.
5. The text is **written back** to Zotero (`PUT …/fulltext`) with page breaks, so Zotero search (`qmode: everything`) and all your Zotero apps can find it. If your API key can't write, the text is kept in the server's KV cache instead. Text that Zotero desktop indexed is never overwritten.
## Attaching PDFs from a link
`attach_pdf_from_url` (write tools only) attaches a PDF to an item that already exists. It runs these checks automatically and **refuses with the specific reason** if any fail. Nothing is created in Zotero until every check has passed.
1. **The link:** `https://` only; no local or private network addresses; 30-second download limit; the size cap (`MAX_PDF_MB`).
2. **It's really a PDF:** checked from the file's contents, not the website's label. Login pages, paywalls and landing pages are recognised and refused ("the link returned a web page, not a PDF").
3. **No risky features.** Refused if the PDF contains JavaScript, a *Launch* action (can start programs), embedded files, Flash or other rich media, an XFA form, a form that submits data, a data-import action, or links that open files or programs on your computer. The file is checked twice: once by scanning its internal structure (including compressed sections, and undoing tricks that disguise these names), and once by the PDF library. Normal papers don't contain any of these. *This is a filter for known-dangerous features, not an antivirus.* It cannot detect attacks on bugs in PDF reader software.
4. **It matches the reference:** the item's DOI appears on the PDF's first two pages, or at least 80% of the title's significant words **and** the first author's surname do. Scanned PDFs without text can't be checked and are refused. A preprint whose title changed may be refused too.
5. **Not a duplicate:** if the exact same file is already attached, nothing new is added.
There is deliberately no "attach anyway" switch: if Claude could set it, a malicious web page could talk Claude into setting it. For the rare refused file you trust, attach it yourself in Zotero.
The file goes to **one** place: your WebDAV bucket if `ZOTERO_FILES` is configured, otherwise Zotero Storage, which should match the file-sync setting in your Zotero apps. Its text is indexed straight away. If the upload fails partway, the server removes the empty attachment it just created, the only case where it deletes anything, so you aren't left with a broken attachment.
## Before you start
You need:
- A **Zotero account** with your library synced to zotero.org.
- A **Cloudflare account** (free to create) on the **Workers Paid** plan ($5/month). Upgrade under *Workers & Pages → Plans* in the [Cloudflare dashboard](https://dash.cloudflare.com/).
- A **GitHub account**: it holds your copy of this code, and you sign in with it.
Everything below happens in a web browser, so a phone or tablet works too. You don't need to install anything. Cloudflare builds the server straight from GitHub and redeploys it whenever the repository changes.
## Deploy, step by step
### 1. Get your own copy of the repository
On this repository's GitHub page, click **Fork → Create fork**. (If you are the repository owner, skip this.)
### 2. Create a Zotero API key
1. Go to [zotero.org/settings/keys](https://www.zotero.org/settings/keys) and sign in.
2. Note the number after **"Your userID for use in API calls is"**. That is your library ID (not secret).
3. Click **Create new private key**. Name it e.g. `Cloudflare MCP`. Under **Personal Library**, tick:
- **Allow library access** (needed to read items and download PDFs)
- **Allow notes access** (needed to read notes)
- **Allow write access** (needed to save extracted PDF text back to Zotero, and for the optional write tools). If you leave this off, the server still works and keeps extracted text in its own cache, but Zotero search won't find it.
4. Leave *Default Group Permissions* at **None**. Click **Save Key** and keep the key somewhere private (a password manager); Zotero shows it only once.
### 3. Create the Worker from GitHub
1. In the [Cloudflare dashboard](https://dash.cloudflare.com/), go to **Workers & Pages → Create application** and choose to import a repository from **GitHub**. The first time, Cloudflare asks to connect to GitHub; allow it access to your copy of this repository.
2. Pick the repository, then fill in the project settings:
| Setting | Value |
| --- | --- |
| Project / Worker name | `zotero-mcp` (must match `name` in `packages/mcp-server/wrangler.jsonc`) |
| Branch | `main` |
| Root directory | `packages/mcp-server` |
| Build command | `cd ../.. && npm ci` |
| Deploy command | `npx wrangler deploy` (the default) |
3. Click **Save and Deploy** and wait for the build to finish (a few minutes). Cloudflare creates the two storage namespaces it needs automatically.
4. Note your server's address, shown on the Worker's page: `https://zotero-mcp.<your-subdomain>.workers.dev`. Opening it should show a short "Zotero MCP server" page.
From now on, every change pushed to `main` is built and deployed automatically.
### 4. Create a GitHub OAuth App (for sign-in)
1. Go to [github.com/settings/developers](https://github.com/settings/developers) → **OAuth Apps** → **New OAuth App**.
2. Fill in:
- **Application name:** `Zotero MCP` (anything)
- **Homepage URL:** `https://zotero-mcp.<your-subdomain>.workers.dev`
- **Authorization callback URL:** `https://zotero-mcp.<your-subdomain>.workers.dev/callback`
3. Click **Register application**. Copy the **Client ID**. Click **Generate a new client secret** and copy it.
The app only asks GitHub for your public profile (to learn your username). It gets no access to your repositories.
### 5. Enter your settings and secrets
In Cloudflare: your Worker → **Settings → Variables and Secrets → Add**. Add each of these, then click **Deploy**:
| Name | Type | Value |
| --- | --- | --- |
| `ZOTERO_LIBRARY_ID` | Text | your userID from step 2 |
| `ALLOWED_GITHUB_USERS` | Text | your GitHub username (several: comma-separated) |
| `ZOTERO_API_KEY` | **Secret** | the Zotero key from step 2 |
| `GITHUB_CLIENT_ID` | **Secret** | from step 4 |
| `GITHUB_CLIENT_SECRET` | **Secret** | from step 4 |
Secrets are stored encrypted and can't be viewed again. These values live only in Cloudflare, never in the repository, and later deploys from GitHub keep them (the config sets `keep_vars`).
### 6. Connect Claude
- **claude.ai (web) and the phone apps:** In claude.ai, open *Settings → Connectors → Add custom connector*. Enter a name and the URL `https://zotero-mcp.<your-subdomain>.workers.dev/mcp`, then click **Connect**. A page asks whether to allow Claude; click **Allow**, then sign in with GitHub. Connectors you add on the web also appear in the Claude phone apps.
- **Claude Desktop:** same steps under *Settings → Connectors*.
- **Claude Code:** `claude mcp add --transport http zotero https://zotero-mcp.<your-subdomain>.workers.dev/mcp`, then run `/mcp` inside Claude Code to sign in.
Try: *"Search my Zotero library for papers on attention mechanisms and summarise the methods section of the most recent one."*
### Prefer the command line?
From a clone: `npm install`, then in `packages/mcp-server` run `npx wrangler login` and `npx wrangler deploy`. Set the step 5 values with `npx wrangler secret put NAME` (secrets) or in the dashboard (text values).
## Optional settings
Add these as **Text** variables under your Worker's **Settings → Variables and Secrets**, then click **Deploy**:
| Setting | Default | Meaning |
| --- | --- | --- |
| `ENABLE_WRITES` | `false` | `true` adds `save_item`, `create_note`, `update_item` and `attach_pdf_from_url`. Needs *Allow write access* on the Zotero key. |
| `ENABLE_CRON_INDEXING` | `true` | Hourly indexing of new PDFs. |
| `INDEX_PER_RUN` | `5` | PDFs indexed per hourly run (max 20). |
| `MAX_PDF_MB` | `30` | Largest PDF the server will download. |
| `MCP_RESOURCE` | your address + `/mcp` | Only needed with a custom domain that serves several hostnames. |
Removing a name from `ALLOWED_GITHUB_USERS` locks that account out on its next request.
These two are changed in the code instead. Edit `packages/mcp-server/wrangler.jsonc` on GitHub (open the file, click the pencil icon, then **Commit changes**), and Cloudflare redeploys automatically:
- **PDFs in WebDAV on R2:** set up [packages/webdav](packages/webdav/README.md), then uncomment the `r2_buckets` line (binding `ZOTERO_FILES`). The server checks R2 first and falls back to Zotero Storage, so mixed setups work.
- **Workers AI fallback extractor:** uncomment the `"ai"` line.
## Security
- **Only you can sign in.** Sign-in goes through GitHub and only usernames in `ALLOWED_GITHUB_USERS` get through; the list is checked again on every request. Every app that connects must first be approved on a consent page showing where access will be sent.
- **No tokens in URLs.** Claude gets short-lived OAuth tokens (1 hour, refreshed automatically) bound to your server's address.
- **Secrets** live only in Cloudflare's encrypted secret store (added as **Secret** in the dashboard, or with `wrangler secret put`). `.dev.vars` (local testing) is git-ignored. Never commit keys.
- The server **never logs** API keys, tokens or file contents.
- **Read-only unless you opt in**, and no delete tools at all. (The only delete the server ever does is removing an attachment it created moments earlier when that attachment's upload fails.)
- Your Zotero API key can do whatever its permissions allow. Use a dedicated key for this server, and revoke it at zotero.org/settings/keys if you stop using it.
## Development
```powershell
npm install
npm test # unit tests (Zotero API mocked)
npm run lint
npm run typecheck
```
Local server: copy `packages\mcp-server\.dev.vars.example` to `.dev.vars`, fill in values (use a GitHub OAuth App whose callback is `http://localhost:8787/callback`), then:
```powershell
cd packages\mcp-server
npx wrangler dev
npx @modelcontextprotocol/inspector # connect to http://localhost:8787/mcp
```
Design notes:
- MCP over Streamable HTTP via `createMcpHandler` (Cloudflare `agents`) with MCP SDK v2 (`@modelcontextprotocol/server`). Stateless: no Durable Objects.
- OAuth 2.1 via [`@cloudflare/workers-oauth-provider`](https://github.com/cloudflare/workers-oauth-provider), with its consent-page and upstream sign-in helpers (per-client consent before the GitHub redirect, browser-bound state, PKCE).
- Repository layout: `packages/mcp-server` (the MCP server), `packages/webdav` (optional WebDAV server).
## Troubleshooting
| Symptom | Fix |
| --- | --- |
| The build fails | Open the build log (Worker → *Deployments*). Check the step 3 settings: name `zotero-mcp`, root directory `packages/mcp-server`, build command `cd ../.. && npm ci`. |
| Tools say "Server setup problem: ZOTERO_API_KEY is not set" | Add the step 5 variables and secrets, then click **Deploy**. |
| "Not allowed" after GitHub sign-in | Your GitHub username isn't in `ALLOWED_GITHUB_USERS` (not case-sensitive). |
| "GitHub sign-in failed" | Check the GitHub secrets and that the OAuth App's callback URL ends in `/callback`. |
| Tools say "Forbidden: the Zotero API key lacks permission" | Recreate the key with the permissions in step 2 and update the `ZOTERO_API_KEY` secret. |
| "File not found in Zotero Storage" | The file hasn't synced from Zotero desktop yet, it's a linked file, or it lives in WebDAV and `ZOTERO_FILES` isn't configured. |
| PDF tools time out or fail on the Free plan | Upgrade to Workers Paid. |
Live logs: your Worker → **Observability** (or **Logs**) in the Cloudflare dashboard, or `npx wrangler tail` from a clone.
## License
Apache-2.0. See [LICENSE](LICENSE). `packages/webdav` includes code from [abersheeran/r2-webdav](https://github.com/abersheeran/r2-webdav) (Apache-2.0); see its [NOTICE](packages/webdav/NOTICE).
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues