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

A remote [MCP](https://modelcontextprotocol.io) server for [DSpace](https://dspace.org)
7 and later. It runs next to your repository at `https://<your-dspace>/mcp`, and lets
AI assistants (Claude, Claude Code, and other MCP clients) search, read, edit and
deposit in DSpace **as the person who is logged in, with that person's DSpace
permissions** — nothing more.

## Why

Repository staff and depositors already have DSpace accounts with the right
permissions. With this server they connect their AI assistant once (OAuth, using
their normal DSpace login) and can then say things like:

- *"Find everything by J. Smith from 2019–2021 that has no abstract."*
- *"Normalise the author name 'Smith, J.' to 'Smith, John' in these 40 items."*
- Drop a PDF into the chat: *"Add this to the repository, in the Theses collection."*
  The assistant reads the document, fills in the submission form fields, shows you
  the result and submits only after you approve.

Every change is shown as a preview first, and every write is recorded in an audit log.
Nothing can be deleted through this server.

<!-- Screenshots: add images to docs/img/ and link them here.
![Consent page](docs/img/consent.png)
![Claude adding a PDF to the repository](docs/img/submission.png)
-->

## How it works

- **The server is its own OAuth 2.1 authorization server** as the MCP specification
  requires: authorization server and protected resource metadata (RFC 8414 / RFC 9728),
  dynamic client registration, PKCE, refresh token rotation, revocation. MCP clients
  discover all of it from the URL alone.
- **Users log in with their DSpace account.** If they are already logged in to the
  DSpace UI on the same host, the consent page recognises them; otherwise they use an
  e-mail/password form that is checked by DSpace (rate limited). They then choose
  *read only* (default) or *read and write*.
- **Every DSpace REST call uses that user's own DSpace token**, kept alive by the
  server (renewed before it expires) and stored encrypted. DSpace decides what is
  visible and what may be changed.
- Stateless Streamable HTTP transport; state (clients, grants, hashed tokens, audit
  log) lives in one SQLite file.

## Install next to an existing DSpace (about 5 minutes)

You need: DSpace 7+ with its REST API, Docker, and a reverse proxy (nginx, Caddy,
Apache…) that serves DSpace over HTTPS.

1. **Run the container.** Copy [`docker-compose.example.yml`](docker-compose.example.yml)
   and set at least the four required variables:

   ```sh
   curl -O https://raw.githubusercontent.com/iplweb/dspace-mcp/main/docker-compose.example.yml
   mv docker-compose.example.yml docker-compose.yml
   echo "MCP_SECRET_KEY=$(openssl rand -base64 32)" > .env
   $EDITOR docker-compose.yml      # DSPACE_PUBLIC_URL, DSPACE_REST_URL, MCP_PUBLIC_URL
   docker compose up -d
   ```

   Or with plain Docker:

   ```sh
   docker run -d --name dspace-mcp --restart unless-stopped \
     -p 127.0.0.1:8000:8000 -v dspace-mcp-data:/data \
     -e DSPACE_PUBLIC_URL=https://repository.example.org \
     -e DSPACE_REST_URL=https://repository.example.org/server \
     -e MCP_PUBLIC_URL=https://repository.example.org/mcp \
     -e MCP_SECRET_KEY="$(openssl rand -base64 32)" \
     ghcr.io/iplweb/dspace-mcp:0.2.0
   ```

2. **Route `/mcp` in your reverse proxy** — `/mcp`, `/mcp/*`,
   `/.well-known/oauth-protected-resource/mcp` and
   `/.well-known/oauth-authorization-server/mcp`, unbuffered, path unchanged.
   Ready-made snippets for Caddy and nginx: [docs/reverse-proxy.md](docs/reverse-proxy.md).

3. **Check it:**

   ```sh
   curl -s https://repository.example.org/mcp/healthz
   curl -s https://repository.example.org/.well-known/oauth-protected-resource/mcp
   ```

> **Same host for cookie login.** Recognising a user who is already logged in to the
> DSpace UI works only when `/mcp` is served on the same host name as the DSpace UI
> (browsers send the UI's `dsAuthInfo` cookie only there). On another host, users log
> in with the e-mail/password form. Users who log in to DSpace only through
> Shibboleth, ORCID or another external method need the same-host setup.

No DSpace configuration changes are needed. The REST API must be reachable from the
container, and DSpace's CORS settings do not matter (the browser never calls DSpace
from the MCP pages).

## Connect an AI client

The URL to give any MCP client is `https://<your-dspace>/mcp`.

- **Claude.ai / Claude Desktop:** *Settings → Connectors → Add custom connector*, paste
  the URL. Claude opens the consent page; log in, pick *read only* or *read and write*.
- **Claude Code:**

  ```sh
  claude mcp add --transport http dspace https://repository.example.org/mcp
  ```

  then run `/mcp` in Claude Code and choose *Authenticate*.
- **Other clients** that support remote MCP servers with OAuth (Streamable HTTP)
  work the same way.

Files: Claude Code can pass small files directly (`file_base64`, up to 10 MB). In
Claude.ai the model cannot hand over the raw bytes of an attachment, so the assistant
gives you a one-time upload link instead, where you drop the file in the browser.

## Tools

| Tool | Access | What it does |
|------|--------|--------------|
| `whoami` | read | The DSpace user the connection acts as |
| `search` | read | Discovery search with filters, scope, sorting, facets |
| `get_item` | read | Metadata (all languages), owning collection, files |
| `browse` | read | Communities and collections |
| `get_full_text` | read | Text extracted by DSpace (TEXT bundle), in parts |
| `list_submission_collections` | read | Collections the user may submit to |
| `describe_submission_form` | read | A collection's submission form: fields, required, hints, allowed values |
| `get_history` | read | Writes made through this server (admins: everyone's; others: their own) |
| `preview_metadata_edit` | write | Add / replace / remove values in up to 100 items; returns a diff and a `confirm_token` |
| `apply_metadata_edit` | write | Applies a previewed edit; items changed meanwhile are skipped as conflicts |
| `create_upload_link` | write | One-time browser upload page for a file (30 min) |
| `prepare_submission` | write | Creates or updates a draft (workspace item) with metadata and file; returns DSpace's validation errors and a `confirm_token` |
| `submit_submission` | write | Accepts the deposit license on the user's behalf and submits (workflow or archive) |

Write tools need the *read and write* choice on the consent page (scope
`dspace.write`). There is no tool to delete items or files; removing a single
metadata value is an edit. Provenance, accession dates, `dc.identifier.uri`,
`relation.*` and relationship-derived values cannot be edited. Edits add a
`dc.description.provenance` note ("Metadata edited via MCP by … using … on …")
unless `MCP_PROVENANCE=false`.

## Security model

Please read this before enabling write access for your users.

- **The real boundary is DSpace.** The server holds no privileges of its own; every
  call uses the user's DSpace token, so a user (or their assistant) can do exactly what
  that user can do in the DSpace UI — no more, but also no less. An administrator's
  assistant can edit anything an administrator can.
- **Scopes.** Read-only connections cannot call write tools. The user picks the level
  per client on the consent page; read only is the default.
- **The client's tool approval.** Write tools carry MCP annotations
  (`readOnlyHint`, `destructiveHint`), so clients such as Claude ask the user before
  calling them, unless the user has turned that off.
- **Preview → confirm_token → apply is a usability guardrail, not a security
  boundary.** The token is returned to the model, which could in principle call the
  apply tool without showing you the preview. It makes sure that what is written is
  exactly what was previewed (and bound to the same connection, single use, 15 min),
  and it makes accidental writes unlikely. It does not protect against a model that
  is manipulated (e.g. by prompt injection in a document it reads) into writing; the
  client's tool approval and the scope do.
- **Prompt injection.** Content read from the repository or from fetched files is
  untrusted input for the model. Grant write access to people who review what their
  assistant proposes.
- **Tokens.** OAuth codes and tokens are stored as SHA-256 hashes. The DSpace tokens
  the server must use are stored encrypted (Fernet) with `MCP_SECRET_KEY`. If that is
  unset, a key file is generated in the data volume: this protects a leaked copy of the
  database file, but not someone who can read the whole volume. Grants last at most
  90 days and end when the user logs out of DSpace.
- **Login form.** Failed attempts are rate limited per client address and per e-mail
  with doubling lockouts (configurable). Locked-out attempts never reach DSpace.
- **OAuth endpoints.** Dynamic client registration is open, as MCP clients expect.
  `/register`, `/authorize` and `/token` are rate limited per client address (HTTP 429
  with `Retry-After`), the number of registered clients is capped, and unused or
  long-idle registrations are removed periodically. Hosted clients such as Claude.ai
  call `/register` and `/token` from shared addresses, so keep those limits generous.
- **File URLs** (`file_url`) are fetched only from public addresses: every resolved
  address is checked, the connection is pinned to the checked address, redirects are
  re-checked; size limits apply.
- **Pages** (consent, login, upload) use per-browser CSRF tokens, an Origin check,
  a strict Content-Security-Policy and `frame-ancestors 'none'`.

Report vulnerabilities privately: see [SECURITY.md](SECURITY.md).

## History and audit

DSpace keeps no history of metadata edits, so the server records every write in its
SQLite database (table `audit`): time, DSpace user, OAuth client, tool, item, full
metadata before and after, the diff, the JSON Patch sent, and the result
(`ok` / `conflict` / `error`). Assistants can query it with `get_history`; for
reports export it:

```sh
docker compose exec mcp dspace-mcp-history --format csv --since 2026-01-01 > history.csv
docker compose exec mcp dspace-mcp-history --item <uuid>          # JSON lines
```

## Configuration

| Variable | Default | Meaning |
|----------|---------|---------|
| `DSPACE_PUBLIC_URL` | **required** | Public URL of the DSpace UI (login link, links in results) |
| `DSPACE_REST_URL` | **required** | DSpace REST API as reachable from the container, e.g. `http://dspace:8080/server` |
| `MCP_PUBLIC_URL` | `${DSPACE_PUBLIC_URL}/mcp` | Public URL of the MCP endpoint; also the OAuth issuer. Must have a path |
| `MCP_SECRET_KEY` | key file in `/data` | Encryption key for stored DSpace tokens, also the source of the CSRF key (`openssl rand -base64 32`, ≥ 16 chars) |
| `MCP_SECRET_KEY_PREVIOUS` | – | Comma-separated old keys; values are re-encrypted with the current key at startup |
| `MCP_FORWARDED_ALLOW_IPS` | `*` | Proxies whose `X-Forwarded-For`/`-Proto` are trusted (addresses or networks) |
| `MCP_DEFAULT_LANGUAGE` | `en` | Page language (`en` or `pl`) when the browser prefers neither |
| `MCP_ACCESS_TOKEN_TTL` | `3600` | OAuth access token lifetime (s) |
| `MCP_REFRESH_TOKEN_TTL` | `2592000` | Refresh token lifetime (s, 30 days) |
| `MCP_GRANT_MAX_AGE` | `7776000` | Maximum age of a connection, whatever the refreshes (s, 90 days) |
| `MCP_CONFIRM_TTL` | `900` | Validity of a preview's `confirm_token` (s) |
| `MCP_UPLOAD_LINK_TTL` | `1800` | Validity of a one-time upload link (s) |
| `MCP_MAX_BASE64_MB` | `10` | Limit for `file_base64` (also sets the request body limit) |
| `MCP_MAX_FILE_MB` | `100` | Limit for `file_url` downloads and browser uploads |
| `MCP_PROVENANCE` | `true` | Add a `dc.description.provenance` note to edited items |
| `MCP_LOGIN_MAX_FAILURES_PER_IP` | `20` | Failed logins per client address within the window (0 = off) |
| `MCP_LOGIN_MAX_FAILURES_PER_EMAIL` | `5` | Failed logins per e-mail within the window (0 = off) |
| `MCP_LOGIN_WINDOW` | `900` | Window for counting failures (s) |
| `MCP_LOGIN_LOCKOUT` | `300` | First lockout (s); doubles with each further lockout |
| `MCP_LOGIN_LOCKOUT_MAX` | `86400` | Longest lockout (s) |
| `MCP_RATE_REGISTER` | `60/3600` | Client registrations per client address (`N/SECONDS`; `0` = off) |
| `MCP_RATE_AUTHORIZE` | `60/600` | Authorization requests per client address |
| `MCP_RATE_TOKEN` | `600/600` | Token requests (code exchange, refresh) per client address |
| `MCP_MAX_CLIENTS` | `10000` | Cap on registered OAuth clients (`0` = no cap); over it, registration gets HTTP 503 |
| `MCP_CLIENT_UNUSED_TTL` | `86400` | Registered clients never used for a login are removed after this (s) |
| `MCP_CLIENT_IDLE_DAYS` | `30` | Clients without an active connection are removed after this many idle days |
| `MCP_DB_PATH` | `/data/mcp.sqlite3` | SQLite database (uploads and the key file go next to it) |
| `MCP_HOST` / `MCP_PORT` | `0.0.0.0` / `8000` | Listen address |
| `MCP_FETCH_ALLOW_PRIVATE` | `false` | Allow `file_url` to private addresses. **Tests only** (disables the SSRF guard) |

DSpace tokens expire after 30 minutes by default (`jwt.login.token.expiration`); the
server renews them in the background, also for idle connections. If the server is
down longer than that, affected users simply authorize again.

Back up the `/data` volume (it holds the audit log) and keep `MCP_SECRET_KEY`
separately. Losing the key only ends active connections: users log in again.

## Compatibility

Tested with **DSpace 10.1**. Written against the DSpace 7+ REST API (Server API
contract) and expected to work with DSpace 7.x, 8.x and 9.x; reports welcome.
Submission uses the standard workspace item / workflow endpoints and the
configured submission forms; entity relationships are not created.

Clients: any MCP client with Streamable HTTP and OAuth 2.1 (dynamic client
registration) support; tested with Claude.ai and Claude Code.

## Related projects

For a single-user, local assistant using your own DSpace credentials (stdio, no
server to install) see [mpasternak/dspace-mcp](https://github.com/mpasternak/dspace-mcp)
(read-only) and [mpasternak/dspace-mcp-write](https://github.com/mpasternak/dspace-mcp-write).
This project is the multi-user server installed alongside the repository.

## Development

```sh
uv sync
uv run pytest
uvx ruff check .
```

End-to-end checks against a running server and a DSpace with the demo data:

```sh
DSPACE_PUBLIC_URL=http://localhost:4000 DSPACE_REST_URL=http://localhost:8080/server \
  MCP_PUBLIC_URL=http://localhost:8765/mcp MCP_PORT=8765 MCP_DB_PATH=./data/mcp.sqlite3 \
  uv run dspace-mcp &
uv run python scripts/e2e.py --mcp http://localhost:8765/mcp \
  --dspace http://localhost:8080/server --email admin@example.org --password admin --mode form
uv run python scripts/e2e_write.py --mcp http://localhost:8765/mcp \
  --dspace http://localhost:8080/server --email admin@example.org --password admin --mode form
```

`e2e_write.py` creates and submits test items and edits metadata: run it only against
a test instance. See [CONTRIBUTING.md](CONTRIBUTING.md).

## License

BSD 3-Clause, © 2026 IPLweb Michał Pasternak. See [LICENSE](LICENSE).