Skip to main content
Glama
README.md
# anki-mcp

An [MCP](https://modelcontextprotocol.io) server that lets AI assistants such as Claude
add, search, update and delete notes in your **Anki** collection, and syncs every change
with **AnkiWeb** (or your own sync server).

A typical use: after a language class you send Claude a photo of your notes from your
phone. Claude extracts the vocabulary and calls this server, and the new cards show up in
Anki on all your devices.

- Works with any note type, deck and fields
- Uses the official [`anki`](https://pypi.org/project/anki/) Python library on its own copy of your collection
- Sync-safe: a normal sync runs before and after every change, and the server **never** does a full upload
- Streamable HTTP transport, protected by a static bearer token
- One `docker compose` service. Setup is one command.

## How syncing works

The server keeps its own copy of your collection in `data/`. Every tool call does this:

```
lock → open collection → normal sync → change → normal sync → close → unlock
```

- If the sync server asks for a **full sync** (for example because the collection was
  changed in a way that needs one), the call is aborted **before** anything is changed. You
  get a clear error. The server never overwrites AnkiWeb.
- If the sync **after** a change fails, the error says that the change was only saved
  locally. All further calls stop at the first sync until you resolve the conflict.
- The only full sync the server ever does is the one-time **download** during setup, and
  only into an empty local copy.
- A lock prevents parallel access to the collection, including from CLI commands.
- Before a change, the server makes an Anki backup in `data/backups/` (at most every
  30 minutes, rotated like in Anki desktop). If the backup fails, nothing is changed.

## Quick start

You need Docker with Compose, a server reachable over HTTPS (see
[Reverse proxy](#reverse-proxy)), and an AnkiWeb account. Before you start, sync from the
Anki desktop app once so AnkiWeb is up to date.

```bash
git clone https://github.com/antonleh04/anki-mcp.git && cd anki-mcp
mkdir -p data && cp .env.example .env      # create both first, or Docker creates root-owned dirs
# edit .env: set MCP_PUBLIC_HOST (e.g. anki.example.com)

docker compose build
docker compose run --rm anki-mcp setup     # generates the token, asks for your AnkiWeb login, downloads the collection
docker compose up -d
```

`setup` prints the bearer token at the end. It is also in `.env` as `MCP_BEARER_TOKEN`.
Your AnkiWeb password is only used once to get a sync token (`ANKI_HKEY`) and is never stored.

If your host user's UID/GID is not 1000, set `UID` and `GID` in `.env` before building.

### Connect Claude

In claude.ai go to **Settings → Connectors → Add custom connector**:

| | |
|---|---|
| URL | `https://anki.example.com/mcp` |
| Authentication | *No sign-in* |
| Request headers | `Authorization` = `Bearer <MCP_BEARER_TOKEN>` (include the word `Bearer `) |

If your account does not show *Request headers* (it is a beta feature), you can set
`MCP_ALLOW_TOKEN_IN_PATH=true` and use the URL `https://anki.example.com/mcp/<MCP_BEARER_TOKEN>`
instead. Only do this if you have to: the token is then part of the URL, and URLs end up in
proxy logs (Caddy logs them on errors, for example).

In Claude's connector settings, set `update_note` and `delete_notes` to **require approval**
(see [Security](#security)).

Connectors you add on claude.ai are also available in the Claude mobile apps. Any other MCP
client that supports Streamable HTTP and custom headers works too.

## Note types

The server works with your existing note types. Claude can call `list_note_types` to see the
fields. Duplicates are detected on the **first field** of a note type, like Anki does.

To create a simple new note type: field 1 is the question, the other fields show on the
back. `--reverse` adds a second card that asks field 2.

```bash
docker compose run --rm anki-mcp create-note-type "Spanish Vocab" --fields "Spanish,English,Notes,Example" --reverse
```

Adding a new note type normally syncs fine. If the sync server asks for a full sync anyway,
the command stops and tells you how to recover. In that case create the note type in Anki
desktop instead.

To restrict the tools to certain note types, set `ANKI_ALLOWED_NOTE_TYPES` in `.env`.

## Tools

| Tool | Description |
|---|---|
| `list_note_types` | Note types with their fields and card templates, plus all decks |
| `add_notes` | Add one or more notes to a deck (created if missing) with tags. Skips duplicates and reports them. |
| `search_notes` | Search with [Anki's search syntax](https://docs.ankiweb.net/searching.html), newest first |
| `update_note` | Change some fields and/or replace the tags of a note |
| `delete_notes` | Delete notes (the assistant is told to ask for confirmation first) |

Field values are plain text. HTML is escaped on the way in and stripped on the way out.
Every result includes the sync status.

## Skills: tailoring it to your use case

The server is generic on purpose. Rules for a specific use case, such as which deck and note
type to use, how to format entries, or which tag to set, belong in a
*skill* (or custom instructions) for your assistant. [`examples/skills/spanisch-vokabeln`](examples/skills/spanisch-vokabeln/SKILL.md)
is an example (in German) for turning photos of Spanish class notes into flashcards.

To use it in Claude, zip the skill folder and upload it under **Settings → Capabilities → Skills**.

## Reverse proxy

The container only listens on `127.0.0.1:8000`. Put a TLS-terminating reverse proxy in
front of it, for example [Caddy](https://caddyserver.com):

```caddyfile
anki.example.com {
    reverse_proxy 127.0.0.1:8000
}
```

If your proxy runs in Docker on a shared network, create a `docker-compose.override.yml`
(it is gitignored) instead of publishing the port:

```yaml
services:
  anki-mcp:
    ports: !reset []
    networks: [web]
networks:
  web:
    external: true
```

Then point the proxy at `anki-mcp:8000`.

## Configuration

All settings are environment variables in `.env`:

| Variable | Description |
|---|---|
| `MCP_BEARER_TOKEN` | Required. At least 24 characters. Generated by `setup`. |
| `ANKI_HKEY` | Sync token. Set by `setup` / `login`. |
| `MCP_PUBLIC_HOST` | Public hostname. Enables the Host header check (DNS rebinding protection). |
| `ANKI_ALLOWED_NOTE_TYPES` | Optional comma-separated allow-list of note types |
| `ANKI_SYNC_ENDPOINT` | Optional self-hosted sync server URL |
| `MCP_ALLOW_TOKEN_IN_PATH` | Also accept the token as `/mcp/<token>` (default `false`) |
| `UID` / `GID` | Build args for file ownership (default 1000) |

After changing `.env`, run `docker compose up -d --force-recreate`.

## CLI

```
docker compose run --rm anki-mcp <command>

  setup                 first-time setup (token, login, download); safe to re-run
  login                 log in again, e.g. after changing your password
  download [--redownload]
                        download the collection; --redownload backs up the local copy to data/backups/
  create-note-type NAME --fields A,B,... [--reverse]
  serve                 run the server (default)
```

### Recovering from a sync conflict

1. Open Anki desktop, sync, and resolve the conflict there.
2. Then replace the server's copy:

```bash
docker compose stop anki-mcp
docker compose run --rm anki-mcp download --redownload
docker compose up -d
```

## Security

Know what you are connecting before you run this:

- **The sync token (`ANKI_HKEY`) is a powerful credential.** Whoever has it can read your
  whole AnkiWeb collection and overwrite it. This server never uploads, but someone who
  steals the token could. It stays valid until you **change your AnkiWeb password**, so do
  that if you think the token or the server was compromised. Keep `.env` private (`chmod 600`).
- **The bearer token protects everything.** `setup` generates a random 256-bit token. Use the
  `Authorization` header, not the token-in-path fallback, and serve the endpoint over HTTPS only.
- **Prompt injection.** Your assistant reads untrusted content: photos, documents, web pages,
  and the notes themselves. Text in that content could trick it into changing or deleting
  notes, and those changes sync to all your devices. To limit the damage:
  - Set `update_note` and `delete_notes` to *require approval* in your MCP client.
  - Use `ANKI_ALLOWED_NOTE_TYPES` so the tools only see the note types they need. Without
    it, the assistant can read your whole collection.
  - Backups in `data/backups/` (and Anki desktop's own backups) let you restore. A
    `.colpkg` backup can be imported in Anki desktop via *File → Import*.
- **Data on the server.** `data/` holds a full copy of your collection. Treat the server
  like any device that has your Anki data on it.

## Development

```bash
docker compose --profile test run --rm --build test
```

The tests run against temporary local collections in a container without network access. A
guard fails any test that tries to reach a real sync server. Without Docker, use
`pip install ".[dev]" && pytest`.

## Limitations

- Media files are not synced, and images cannot be added.
- Field values are plain text, without formatting.
- One collection (one AnkiWeb account) per server instance.

## License

[MIT](LICENSE)