Skip to main content
Glama
README.md
# TroveMCP

An MCP server that answers questions over a **local SQLite database** that a
sync keeps in step with a Notion workspace, instead of calling the Notion API for
every question. It is read-only unless given a write profile.

The server makes no network calls and needs no key. The sync (`trove-sync`) is
the only part that talks to Notion, and the only part that writes the database.

## Why

The official Notion MCP returns at most 25 items per request. That is fine for
"find me this page" and useless for anything that needs the whole picture — a
morning briefing, a digest, "which imprints sign authors but have nothing in the
works". Those questions are about *all* of the corpus at once, and a paged API
answers them with a slice while looking like it answered them completely.

TroveMCP reads the synced database, computes the answer over the entire corpus,
and returns **the answer** rather than the pages. A few thousand pages are a
couple of megabytes; the arithmetic is instant, and it works while the API is
unreachable.

```
> which imprints sign authors but have nothing in the works?

Lantern Books    6 authors, 0 manuscripts in the works
Quayside Press   6 authors, 0 manuscripts in the works

(computed over 40 authors and 103 live manuscripts of 320, synced 4 minutes ago)
```

That is the demo below, asked in its own words.

## Requirements

- Python 3.11 or newer
- [uv](https://docs.astral.sh/uv/)
- for your own data: a Notion workspace where you may add an integration

## Install

Either clone the repository:

```bash
git clone https://github.com/breakneck-git/trovemcp.git
cd trovemcp
uv sync
```

or let a client start it straight from git, with nothing installed by hand:

```bash
uvx --from git+https://github.com/breakneck-git/trovemcp trove
```

Three commands come with it:

| command | does |
|---|---|
| `trove` | the MCP server over stdio; opens the database read-only, makes no network calls |
| `trove-sync` | the sync: reads Notion into the database, runs until SIGTERM or SIGINT |
| `trove-export-check` | checks an export folder against `[export.contract]` |

## Try it without any data of your own

The repository can generate a synthetic sync database — a small publishing
house that does not exist — written through the sync's own functions, so it has
the shape a real one has. From the clone:

```bash
uv run python examples/make_demo_db.py        # writes examples/demo.db
```

Add it to Claude Code (run in the clone, so `$PWD` is its root):

```bash
claude mcp add trove-demo \
  -e TROVE_CONFIG="$PWD/examples/demo.toml" \
  -e TROVE_HOST=demo \
  -e TROVE_DB="$PWD/examples/demo.db" \
  -- uv run --directory "$PWD" trove
```

or, in any client that takes a JSON config (Claude Desktop's
`claude_desktop_config.json`, a project's `.mcp.json`):

```json
{
  "mcpServers": {
    "trove-demo": {
      "command": "uv",
      "args": ["run", "--directory", "/path/to/trovemcp", "trove"],
      "env": {
        "TROVE_CONFIG": "/path/to/trovemcp/examples/demo.toml",
        "TROVE_HOST": "demo",
        "TROVE_DB": "/path/to/trovemcp/examples/demo.db"
      }
    }
  }
}
```

`TROVE_DB` is there because `db_path` in `demo.toml` is relative and the client
decides where the server starts. The database is stamped when it is generated:
left for a few hours it is stale, and the answers will say so.

## Pointing it at your own workspace

1. **Make an integration.** In Notion, *Settings → Connections → Develop or
   manage integrations → New integration*, internal. To only read, it needs
   *Read content*. Copy its secret.
2. **Share your databases with it.** On each database (or a page above them):
   *⋯ → Connections → your integration*. The sync reads every data source the
   integration can see.
3. **Write `trove.toml`.** The smallest one that works:

   ```toml
   [hosts.laptop]
   role = "primary"                 # this machine writes the database from Notion
   db_path = "/absolute/path/to/trove.db"
   incremental_interval_s = 30      # seconds between incremental passes
   integration = "my-integration"   # a label for your integration, kept in the database
   ```

   [`trove.example.toml`](trove.example.toml) shows every key with comments;
   copy it to `trove.toml` (git ignores that name) and replace its example
   `[[sources]]`, `[dossier]` and `[briefing]` entries with your own, or delete
   them.
4. **Run the sync**, with the secret in the environment (the variable is
   `TROVE_NOTION_TOKEN` unless the host's `token_env` names another):

   ```bash
   export TROVE_NOTION_TOKEN=your_token_here
   TROVE_CONFIG=/path/to/trove.toml TROVE_HOST=laptop uv run trove-sync
   ```

   It keeps running and keeps the database in step; run it under your service
   manager (launchd, systemd) to keep it going.
5. **Add the server** as in the demo, with your `TROVE_CONFIG` and
   `TROVE_HOST` and without `TROVE_DB`. The server needs no token.

### The config

One TOML file describes the workspace, and **that file is the only thing that
changes between workspaces**. It holds:

- `[hosts.<name>]` — a machine that holds a copy of the database: its `role`
  (`primary` writes it from Notion, `replica` follows), its `db_path`,
  `integration` (a label) and `token_env`, the **name** of the environment
  variable that holds the integration token. The token itself is never written
  in the file.
- nothing about which databases to read: the sync reads every data source
  shared with its integration. Each one found is a collection named after its
  title (`Task list` → `task_list`), kept under that name if the database is
  renamed later. `[sync] exclude = ["<data source id>", …]` leaves some out.
  Pages outside databases (under another page, or in the workspace) are the
  collection `pages`, with the title alone: each tick reads the ones edited
  since the last, and once a day a sweep notices the ones that went. They are
  read only; a `[[sources]]` entry named `pages` takes `export` alone.
- `[[sources]]` — optional overrides of the data sources they name:
  `data_source_id`, the collection's `name` (`a-z`, `0-9` and `_` only: it
  names a file of the export), and the roles of their fields,
  addressed by property id so that a rename in Notion changes nothing here.
  The export folder (`export_dir`) gets a file for every collection synced,
  `pages` too, and that includes the ones found later: a database shared with
  the integration tomorrow is in the folder after the next full pass, whoever
  reads the folder. `export = false` on an override keeps that one out. Beside
  the files it keeps `trove-export.manifest`, the names it published last: a
  file it published before and no longer does is removed, anything else in the
  folder is left alone, and a `<collection>.json` it did not write is never
  written over: that collection stays out of the set, and `export.last_error`
  names the file. A folder with no manifest yet (an earlier version wrote it)
  is taken as the export's where a `<collection>.json` is a plain file that
  begins `{"export_generated_at": "`, as every export file does; only that
  head is read. The manifest begins with the line `# trove export manifest v1`:
  a file of that name without it, or a file at one of the export's hidden
  `.<name>.tmp` names that is not the export's, is left as it is, and nothing is
  published until it is moved away (`export.last_error` names it).
- `[sources.writeback]` — optional, off without it, and the one write that
  goes past the profiles of `[write]`: switch it on only if you want it.
  `completed_field`, the
  property id of a date. After each whole full pass the `primary` host fills it
  on every page its `status_field` closes (`closed_status`) and whose date is
  empty, with the page's last edit time. It is the one write the sync makes
  itself, so its integration needs Update content in Notion. Every page is read
  first and written only if it is still closed and still without the date; at
  most `[sync] writeback_max_per_pass` pages (50) a pass, the rest the next.
  Each pass leaves a `sync_runs` row of kind `writeback`.
- `[export.contract]` — optional: what the programs reading the export folder
  take from it. By collection, the keys of the file (`wrapper`) and of each of
  its `items` (`item`), each with the types its value may have (`str`, `bool`,
  `number`, `list`, `dict`, `null`). A set that breaks it is not published, and
  `trove-export-check --config trove.toml <export folder>` checks a folder
  against it. With none, every set is published.

  ```toml
  [export.contract.manuscripts.item]
  id = ["str"]
  Stage = ["str", "null"]
  ```
- `[sync] timezone` — the fixed offset (`±HH:MM`, default `"+00:00"`, UTC) in
  which `today` and the day words, date buckets (`day`, `hour`, `weekday`, …)
  and the window of a `coverage = "window"` source are counted. A zone name
  such as `Europe/Athens` is refused: there is no summer time, so set the
  offset you want the days cut at.
- `[briefing]` and `[dossier]` — what the standing briefing is made of and how
  long a dossier's lists get. `project_dossier` needs `[dossier]
  projects_collection`, the collection that holds projects; there is no
  default, and without it the tool refuses and names the key.

```toml
[[sources]]
name = "manuscripts"
data_source_id = "<the data source's id>"
archived_fields = []                 # [] says "this source has no archive"
status_field = "<property id>"
closed_status = ["Printed", "Withdrawn"]

[sources.relations]
"<property id>" = "authors"          # a relation into another source

[sources.date_fields]
"<property id>" = "start"            # the date lives inside an object
```

[`examples/demo.toml`](examples/demo.toml) is the same shape over the demo.

The config is found via `TROVE_CONFIG`, then `./trove.toml`, then
`~/.config/trove/trove.toml`. `TROVE_HOST` picks the host; `TROVE_DB` replaces
its `db_path`.

### Heartbeat

`trove-sync` can tell a monitor that it is alive. Set `TROVE_HEARTBEAT_URL` in
the sync's environment (not in `trove.toml`: the URL carries the monitor's
token, and is never logged). After a tick the sync sends one GET (5 s
timeout): the first after a start and a change between up and down at once,
the same state again at most once per `[sync] heartbeat_interval_s` (60 s by
default; set it to the monitor's own interval or less). `?status=up` when no
collection failed and the last export passed the
contract gate (`export.last_error` empty), else `?status=down&msg=<the error,
up to 200 characters>`. This is the push format of Uptime Kuma, for example
`https://kuma.example/api/push/your_token_here`; any monitor that takes it
works. A status, msg or ping in the URL (the monitor's UI hands them out) is not needed: the
sync sets its own. The whole GET has 5 s, not each wait. Unset or empty: nothing
is sent. A heartbeat that fails (an error, a 4xx / 5xx,
no answer in 5 s) is a warning in the log, without the URL, and goes again on
the next tick; it changes nothing else.

## Tools

Which tools a client sees depends on the config: by default only the read
tools; the rest appear only when you allow them.

| tool | answers | offered |
|---|---|---|
| `describe` | what collections exist, their fields and types, what is truncated, what could not be read; per collection the Notion `data_source_id`, `schema_hash` and `properties` (type, the `options` of a select, multi-select or status, a relation's `target`) for writing | always |
| `query` | items from one collection, filtered, sorted, sliced | always |
| `aggregate` | the whole collection grouped and measured — counts, sums, extremes | always |
| `briefing` | the standing sections your config declares | always |
| `project_dossier` | everything pointing at one project | always |
| `status` | how far behind the database is and what the last sync passes did | always |
| `writes` | the journal of writes made through Trove, refusals and dry runs included | always |
| `read_body` | the body of one page as markdown, read live from Notion | with `[write]` |
| `comments` | the unresolved comments on one page, read live | with `[write]` |
| `update_page` | changes properties or the icon of a page | profile: `write` |
| `create_page` | creates a page in a collection, with properties and a body | profile: `write` |
| `append_body`, `replace_body` | adds to or replaces a page's body from markdown | profile: `write` |
| `add_comment` | comments on a page or answers a thread | profile: `write` |
| `trash_page`, `restore_page` | moves a page to Notion's trash and back | profile: `delete` |
| `revert` | undoes one write from the journal | any profile |
| `update_schema`, `update_schema_destructive` | changes a collection's properties; the second may lose values | profile: `admin` |
| `update_database`, `create_database`, `trash_database`, `restore_database` | a database's title, description, icon and cover; a new one; to the trash and back | profile: `admin` |
| `list_views`, `create_view`, `update_view`, `delete_view` | the views of a collection | profile: `admin` |

Every answer carries an envelope: when the sync last succeeded, how old that
makes the data, and a warning once it has fallen behind.

## Writing, by profile

By default Trove only reads: a server started without `TROVE_PROFILE` offers
no write tool at all, and the sync writes nothing to Notion unless a source has
a `[sources.writeback]` table (see the config above), which you add yourself
and which no profile governs. Writing by profile takes three things you add
yourself:

```toml
# trove.toml
[write]
socket   = "/path/to/write.sock"      # the sync daemon takes writes here; at most 103 bytes
profiles = "/path/to/profiles.toml"
```

```toml
# profiles.toml
[profiles.example-editor]
write = ["example_db"]                # collections whose pages it may change
delete = ["example_db"]               # ... and move to the trash and back
```

and `TROVE_PROFILE=example-editor` (with `TROVE_CALLER` to name who asks) in
the `env` of the `trove` server. The integration then needs the Notion
capabilities its writes use: *Update content*, *Insert content*, and for
comments *Read comments* and *Insert comments*. The server holds no token: it passes a write
to `trove-sync`, which checks the profile, the page's version and the value
against the schema, writes to Notion, puts the fresh page into the database at
once and journals the call. An edit of the profile file takes effect without a
restart, and the server tells the client its tools changed. A refusal starts
with its code: `forbidden`, `version_conflict`, `invalid_value`,
`unknown_option`, `sync_down`, `config_invalid`, …; `outcome_unknown` says
Notion's answer was lost and the write may have landed.

**Who may write is who may open the socket.** The daemon makes the socket
`0660`: its owner and group can connect, and so can anyone the socket's
directory lets through to it. The profile is named by the client
(`TROVE_PROFILE` in the server's environment) and the daemon does not check
who is on the other end of the connection: any process that can connect may
name any profile in the profiles file. So the security boundary is the
permissions of the socket and of its directory — give the group, and the
directory, only to the accounts that may write with every profile there.

Every write tool takes `dry_run`: it checks everything and returns the request
it would send, without sending it. The check is the daemon's, so a dry run needs
`trove-sync` running too; without it the answer is `sync_down`. The socket path
must fit a unix socket — at most 103 bytes — or the config is refused.

Nothing deletes a page for good: `trash_page` moves it to Notion's trash,
which keeps it 30 days. `revert` takes a `write_id` from `writes` and does the
reverse — the values before an update, formatting included, if the page has
not changed since; a restore for a trash, a trash for a restore.

## Four things it refuses to fake

**A truncated relation is not a count.** Notion caps a relation property in a
page response, and an author with 90 manuscripts can arrive with 25 of them.
The sync reads such a property whole; what it could not read whole is marked,
and TroveMCP flags those fields in every answer that reads them, not only in
`describe`. Dossiers are built from the reverse direction — the items that point
back, which carry their pointer whole.

**A window is not a corpus.** Where a collection holds only a rolling window,
answers say so and carry the window boundary instead of passing a week off as a
lifetime.

**A missing collection is not an empty one.** If the sync has not registered a
collection, or its schema cannot be read, the honest answer is "this source is
unavailable", not `0`. The second one is worse than an error, because every
other number in the response stays correct.

**A stale database is not fresh data.** A sync can lag or fail. Every answer
states by how much and warns past a threshold you set. It never refuses to
answer: working while the API is down is half the point.

## What it does not do

- **No page bodies in the database.** It holds page properties, not block
  content. With a `[write]` table, `read_body` reads a body live from Notion
  through the sync daemon; without one, results include each page's `id` and
  `url`, so a Notion client can fetch the body when you actually need it.
- **No write without a profile, or a writeback you switched on.** The server
  opens the database read-only and never writes Notion itself; only
  `trove-sync` writes: by the profile file, and the completion dates of
  `[sources.writeback]` where you configured one.
- **No network from the server.** If `trove` ever needs a token, something has
  gone wrong with it.

## Development

```bash
uv sync                                  # project + pytest, into .venv
uv run python tools/full_test_run.py     # the full run, behind a local lock: one at a time
```

The wrapper runs `uv run pytest -q` and exits with pytest's exit code. If another
full run holds the lock it prints who holds it and waits;
extra arguments go straight to pytest. For a quick run of a single file while
working, `uv run pytest tests/test_query.py` is fine.

Tests build their own synthetic databases (`tests/db_fixtures.py`) and never
touch a real workspace.

## Licence

MIT.