linkwarden-mcp
# linkwarden-mcp
[](https://github.com/dohwi/linkwarden-mcp/actions/workflows/ci.yml)
[](LICENSE)
[](https://www.python.org/)
A lightweight, security-hardened **Model Context Protocol (MCP)** server for self-hosted [Linkwarden](https://linkwarden.app/), supporting both **Streamable HTTP** (for Docker / reverse-proxy / multi-client deployments) and **stdio** transports.
## Features
- **Focused 6-tool surface**: search/list bookmarks, read metadata and preserved readability text, create bookmarks, update metadata/collection/tags, and list collections or tags — no destructive delete tools exposed.
- **Dual transport**: runs over `stdio` for local clients or stateless **Streamable HTTP** (`--http`, `/mcp` + `/health`) for containerized self-hosting.
- **Handles Linkwarden v2.17+ API quirks cleanly**:
- Uses `/api/v1/search` for both full-text search and cursor-based browsing (avoiding deprecated `/api/v1/links`).
- Fixes Meilisearch relevance-offset vs. Prisma `id desc` page overlap with opaque cursors (`q:<upstream>:<skip>`) and automatically advances past empty filtered pages.
- Enforces exact client-side **AND** intersection for `tag_id` + `pinned_only` (working around upstream `OR` filtering).
- Resolves `collection.ownerId` on `update_link`, preserves `icon`/`color`/nullable `url` (for uploaded PDFs/images), and omits `pinnedBy` to avoid triggering Linkwarden's shared-collection pin-only no-op branch.
- Paginates `/api/v1/tags` across all `nextCursor` pages (no 50-tag truncation).
- Falls back to Linkwarden's indexed `textContent` when `/api/v1/archives/{id}?format=3` returns `404`, while surfacing non-`404` upstream errors as explicit warnings.
- **Hardened for self-hosting**:
- Constant-time Bearer token auth (`hmac.compare_digest`, minimum 24-character token) on `/mcp`.
- Fail-closed DNS rebinding protection (`MCP_ALLOWED_HOSTS` required on non-loopback binds).
- 5 MiB upstream response size cap for low-memory containers (runs comfortably under 256 MiB RAM, non-root, read-only rootfs, `cap_drop: [ALL]`).
- Strict `http(s)` URL scheme, field length, and tag count validation.
- Untrusted web-data framing in MCP instructions and preserved content responses.
## Tools
| Tool | Description |
|---|---|
| `search_links` | Search bookmarks by query or list recent bookmarks with `collection_id`, `tag_id`, `pinned_only`, `cursor`, and `limit` (1–50). |
| `get_link` | Fetch bookmark details and optional preserved readability text (`include_content=true`, with `max_chars` and `offset` chunking). |
| `create_link` | Save a new `http(s)` URL with optional `name`, `description`, `collection_id`, and `tags`. |
| `update_link` | Update an existing bookmark's `name`, `description`, `collection_id`, and/or full `tags` list without wiping archives. |
| `list_collections` | List all collections (`id`, `name`, `description`, `parent_id`, `link_count`). |
| `list_tags` | List all tags across all pages (`id`, `name`, `link_count`). |
## Quick Start
First, create a Linkwarden Access Token in **Linkwarden → Settings → Access Tokens**.
### 1. Local `stdio` mode
```bash
git clone https://github.com/dohwi/linkwarden-mcp.git
cd linkwarden-mcp
pip install .
LINKWARDEN_URL=http://localhost:3000 \
LINKWARDEN_API_TOKEN=your-linkwarden-token \
linkwarden-mcp
```
### 2. Streamable HTTP mode (CLI)
```bash
cp .env.example .env
# Edit .env with your LINKWARDEN_URL, LINKWARDEN_API_TOKEN, MCP_AUTH_TOKEN (>=24 chars), and MCP_ALLOWED_HOSTS
linkwarden-mcp --http --host 127.0.0.1 --port 8000
```
Health check (no auth): `GET http://127.0.0.1:8000/health` → `{"status":"ok"}`
MCP endpoint (Bearer auth): `POST http://127.0.0.1:8000/mcp`
### 3. Docker Compose (recommended for self-hosting)
```bash
cp .env.example .env
cp compose.example.yaml compose.yaml
# Edit .env and compose.yaml for your Linkwarden container/URL and reverse proxy host
docker compose up -d --build
```
If exposing behind Nginx / Caddy / Traefik, disable response buffering for `/mcp` and include the external hostname in `MCP_ALLOWED_HOSTS`.
## MCP Client Configuration
### Streamable HTTP (Claude Code, Cursor, generic MCP clients)
```json
{
"mcpServers": {
"linkwarden": {
"type": "http",
"url": "https://mcp.example.com/mcp",
"headers": {
"Authorization": "Bearer YOUR_MCP_AUTH_TOKEN"
}
}
}
}
```
### Stdio
```json
{
"mcpServers": {
"linkwarden": {
"command": "linkwarden-mcp",
"env": {
"LINKWARDEN_URL": "https://bookmarks.example.com",
"LINKWARDEN_API_TOKEN": "YOUR_LINKWARDEN_ACCESS_TOKEN"
}
}
}
}
```
## Environment Variables
| Variable | Required | Default | Description |
|---|---|---|---|
| `LINKWARDEN_API_TOKEN` | Yes | — | Linkwarden Access Token (`Bearer` auth to Linkwarden API). |
| `LINKWARDEN_URL` | No | `http://localhost:3000` | Linkwarden API base URL. |
| `LINKWARDEN_PUBLIC_URL` | No | `LINKWARDEN_URL` | Public Linkwarden URL used to construct `linkwarden_url` links. |
| `LINKWARDEN_TIMEOUT_SEC` | No | `20` | Upstream HTTP timeout in seconds. |
| `MCP_AUTH_TOKEN` | HTTP mode | — | Bearer token required by clients on `/mcp` (minimum 24 chars). |
| `MCP_ALLOWED_HOSTS` | Non-loopback HTTP | loopback only | Comma-separated Host header allowlist (e.g. `localhost:*,mcp.example.com`). |
| `MCP_HOST` | No | `127.0.0.1` | Default bind host when `--host` is omitted. |
| `MCP_PORT` | No | `8000` | Default listen port when `--port` is omitted (also honors `PORT`). |
## Development & Tests
```bash
pip install -e ".[dev]"
pytest -v
```
## License
[MIT](LICENSE)
TDQS
Scored across 6 tools
Each tool has a clearly distinct role: search/list links, get one link, create a link, update a link, and list tags or collections. There is no meaningful overlap between search_links and get_link because one supports discovery/listing and the other retrieves detailed metadata for a specific bookmark.
All tool names follow the same snake_case verb_noun convention: search_links, list_tags, get_link, create_link, update_link, list_collections. The pattern is predictable and readable across the entire set.
Six tools are well-scoped for a focused Linkwarden bookmark integration. Each tool covers a distinct operation, and the set is neither thin nor bloated.
The bookmark surface supports search/list, get, create, and update, but lacks deletion and any mutation operations for tags or collections. These are notable gaps for full lifecycle coverage, though basic bookmark creation and retrieval workflows remain usable.