Skip to main content
Glama
Code Mode for Zotero

Code Mode for Zotero

by ltalignani
README.md
# Code Mode for Zotero

Read your Zotero library from Claude, ChatGPT or any MCP client, by letting the model write short Python scripts instead of calling dozens of tools. The scripts run in a sandbox on your computer, read the local `zotero.sqlite`, and only what they print goes back to the model. Passages can be searched by meaning, in French or English, and every hit comes with its page, so answers can be cited as `citekey, p. 12`.

> Not affiliated with Zotero or the Corporation for Digital Scholarship. Zotero is their project; this is an independent tool that reads the files Zotero keeps on your disk.

Status: alpha (0.1). macOS and Linux. Windows is not supported yet, because scripts cannot be isolated there.

## Why code mode

A classic Zotero MCP server exposes one tool per operation, which puts thousands of tokens of tool definitions in every conversation and sends every intermediate result through the model. This server has two tools:

| Tool | What it does |
|---|---|
| `search_api(query="")` | Lists the `zotero.*` functions a script can call, with their documentation. |
| `execute(code, timeout_seconds=30)` | Runs a script and returns what it printed. |

The two definitions take about 230 tokens. A search over 200 items, a filter and a sort happen inside the script, and only the five lines the script prints reach the model.

## What a script can call

All read-only. Records are small flat dicts (key, title, creators, year, DOI, tags, `citationKey`, library).

| Function | Returns |
|---|---|
| `zotero.search(query, ...)` | Items matching a query, at most 200 |
| `zotero.get_item(ref, detail)` | One item, by Zotero key, citation key or Better BibTeX key |
| `zotero.fulltext(ref, first, last)` | The text of an item's PDF, one entry per page |
| `zotero.find(ref, query)` | Paragraphs of one document that contain every word, with page numbers |
| `zotero.passages(query, limit, keys)` | The most relevant passages across the library, with page numbers |
| `zotero.rag_index(...)`, `zotero.rag_status()` | Build and inspect the passage index |

Every library in the database is searched (personal and groups) unless `ZOTERO_MCP_LIBRARY` says otherwise (`0` for the personal library, or a group id).

## Install

Requires Python 3.10 or later and Zotero installed on the same computer (it does not need to be running).

```bash
uv tool install 'code-mode-for-zotero[embeddings]'    # or: pipx install ..., pip install ...
code-mode-zotero --check-sandbox                       # what scripts can and cannot do here
```

Claude Desktop, in `claude_desktop_config.json`:

```json
{
  "mcpServers": {
    "zotero": {
      "command": "code-mode-zotero",
      "env": {
        "ZOTERO_RAG_EMBED_URL": "http://localhost:1234/v1",
        "ZOTERO_RAG_EMBED_MODEL": "text-embedding-bge-m3"
      }
    }
  }
}
```

The `env` block is optional; without it, passage search works on keywords only. Claude Desktop does not read your shell profile, so variables go in this block.

## Passage search

```bash
code-mode-zotero --check-embeddings   # one call to your embeddings server, with a sanity check
code-mode-zotero --index              # build or refresh the index; resumable
code-mode-zotero --index --force      # rebuild everything, e.g. after changing the model
```

Passages are cut from each PDF without crossing a page, cleaned of reference lists, tables and line numbers, and stored in one SQLite file. Keyword search uses French and English word stems, so "pyréthrinoïde" finds "pyréthrinoïdes". When an embeddings endpoint is configured (LM Studio, Ollama, vLLM or any OpenAI-compatible `/v1/embeddings`), passages are also ranked by meaning and the two rankings are merged. If the endpoint is unreachable, search falls back to keywords and says so in each result.

## What it runs, reads, writes and sends

This section is complete; if the code does something not listed here, it is a bug.

- **Runs**: the MCP server, and for each `execute` call a child Python process with the script. On macOS the child runs under `sandbox-exec` with no network, no file writes, no new processes and no access to your home folder; on Linux it runs in an empty network namespace (files and processes are not blocked there, so scripts only run with `ZOTERO_MCP_SANDBOX=relaxed`). CPU time, output size and run time are limited everywhere, memory on Linux.
- **Reads**: `zotero.sqlite`, read-only (it may be copied to a temporary folder so it can be read while Zotero is writing), `better-bibtex.sqlite` if present, and attachment files in Zotero's storage folder.
- **Writes**: the passage index (`~/.local/share/code-mode-for-zotero/rag.sqlite`, or `ZOTERO_RAG_DB`) and a cache of extracted text (`~/.cache/code-mode-for-zotero/fulltext/`). It never writes to your Zotero library.
- **Sends**: passage text and search queries to the embeddings endpoint you configure, and nowhere else. With no endpoint configured, the server makes no network call. There is no telemetry.

See [PRIVACY.md](PRIVACY.md) and [SECURITY.md](SECURITY.md).

## Settings

| Variable | Default | Meaning |
|---|---|---|
| `ZOTERO_DB_PATH` | found automatically | Path to `zotero.sqlite` |
| `ZOTERO_BBT_DB` | next to `zotero.sqlite` | Path to `better-bibtex.sqlite` |
| `ZOTERO_MCP_LIBRARY` | `all` | `all`, `0` (personal) or a group id |
| `ZOTERO_MCP_SANDBOX` | `strict` | `relaxed` runs scripts without full isolation |
| `ZOTERO_RAG_DB` | `~/.local/share/code-mode-for-zotero/rag.sqlite` | Passage index |
| `ZOTERO_RAG_EMBED_URL` | none | OpenAI-compatible endpoint, e.g. `http://localhost:1234/v1` |
| `ZOTERO_RAG_EMBED_MODEL` | `nomic-embed-text` | Embedding model name |
| `ZOTERO_RAG_EMBED_KEY` | none | Bearer token, if the endpoint needs one |

## Limits

- Read-only: no adding, tagging or annotating items.
- A timeout stops a script but not a database call already running.
- Scanned PDFs without a text layer are reported (`needsOcr`) and not indexed.
- An index built with one embedding model cannot be queried with another.

## Authors and origin

Loïc Talignani and Thierry Valéro. The data-access layer comes from [Zotero MCP](https://github.com/54yyyu/zotero-mcp) (MIT); see [NOTICE](NOTICE). MIT License.

TDQS

A4/5.0

Scored across 2 tools

Disambiguation5/5

The two tools occupy clearly separate roles: search_api is pure introspection/documentation of the execute() surface, while execute runs code. There is no plausible way to confuse them, and the description of execute explicitly points at search_api for detail.

Naming Consistency4/5

Both names are lowercase snake_case, but search_api follows a verb_noun pattern while execute is a bare verb. Minor deviation rather than a conflict, and both remain readable and predictable.

Tool Count4/5

A code-mode server deliberately needs only an introspection tool and an execution tool, so the count is well-matched to the design rather than thin. A third convenience tool (e.g. a schema/help shortcut) is not needed but the 2-tool surface sits at the low end.

Completeness3/5

For read-only research workflows the surface is workable: search, get_item, fulltext, find, and passages cover discovery and retrieval. However, there are no write operations (adding items, notes, tags, collections) and no explicit collection/tag listing or export, so Zotero library management is only half-covered.

Maintenance

ActivityMaintained
ResponsivenessNo recent community issues