mcp-local-knowledge
README.md
# mcp-local-knowledge
A local [MCP](https://modelcontextprotocol.io) server (stdio) that indexes your
Markdown and TXT notes under **one authorized folder** and answers searches with the
file and the line numbers of the match. Python, SQLite FTS5, the official `mcp` SDK.
Nothing leaves your machine.
## Problem
An AI assistant that can read your notes usually gets either the whole folder or
nothing. This server sits in between: the assistant gets search and line-range
reading over one folder you choose, results point at `file:line` so you can verify
them, and anything that resolves outside that folder (`..`, absolute paths, symlinks)
is refused.
## Install
Requires Python 3.10+. Dependencies are locked in `uv.lock`.
```sh
git clone <this repo> && cd mcp-local-knowledge
uv sync --frozen --extra dev # with uv
# or: python3 -m venv .venv && .venv/bin/pip install -e ".[dev]"
```
## Run
Try it from a terminal first (bundled sample data in `examples/sample_docs`):
```sh
mcp-local-knowledge --root examples/sample_docs --db ./knowledge.sqlite index
mcp-local-knowledge --root examples/sample_docs --db ./knowledge.sqlite search "restore drill" --limit 2
```
Real output (index, first run):
```json
{
"added": ["guides/backups.txt", "guides/incident-response.md", "onboarding.md"],
"updated": [],
"removed": [],
"unchanged": 0,
"refused": [],
"files": 3,
"chunks": 13
}
```
(The CLI prints the lists one item per line; shortened here.) Real output of the search:
```json
[
{
"file": "guides/backups.txt",
"start_line": 6,
"end_line": 8,
"matched_lines": [
{ "line": 6, "text": "Restore drill" },
{ "line": 7, "text": "Run a restore drill once per quarter." }
],
"score": 4.671
}
]
```
Serve over stdio for an MCP client:
```sh
mcp-local-knowledge --root /path/to/your/notes serve
```
`--root` can also come from `MCP_LOCAL_KNOWLEDGE_ROOT`, and `--db` from
`MCP_LOCAL_KNOWLEDGE_DB`. Without `--db` the index lives in
`$XDG_CACHE_HOME/mcp-local-knowledge/` (default `~/.cache/...`), one file per root.
See `.env.example`.
Typical client entry (JSON, same shape most MCP hosts use; not tested with a real host, see below):
```json
{
"mcpServers": {
"local-knowledge": {
"command": "mcp-local-knowledge",
"args": ["--root", "/path/to/your/notes", "serve"]
}
}
}
```
### Tools
| Tool | What it does |
| --- | --- |
| `search_knowledge(query, limit=5)` | Full-text search. Returns `file`, `start_line`, `end_line`, `matched_lines` (line number and text), `score`. |
| `reindex()` | Rescans the folder. Adds new files, updates changed ones, drops deleted ones. Reports files it refused. |
| `read_lines(path, start_line=1, end_line=40)` | Reads a line range of a file inside the folder. At most 200 lines per call. |
| `index_status()` | Number of indexed files and chunks. |
The server also reindexes once at startup. There is no file watcher: call `reindex`
after files change.
### Demo: search, edit a file, reindex, try to escape
```sh
python examples/demo.py # temporary copy of the sample docs, nothing is modified
python examples/stdio_client.py # spawns the real server and calls the tools over stdio
```
Real output of `examples/demo.py`, abridged to the parts that matter:
```text
== 3. search 'kubernetes' (no hits yet)
[]
== 4. reindex after editing onboarding.md
{ "added": [], "updated": ["onboarding.md"], "removed": [], "unchanged": 2, "refused": [] }
== 5. search 'kubernetes' again
[ { "file": "onboarding.md", "start_line": 16, "end_line": 16,
"matched_lines": [ { "line": 16, "text": "We now deploy to kubernetes clusters." } ], "score": 2.6845 } ]
== 6. reindex with an escaping symlink
{ ..., "unchanged": 3, "refused": [ { "path": "evil-link.md", "reason": "symlink escapes the authorized folder" } ] }
read '../outside-secret.md' -> refused: path resolves outside the authorized folder
read 'evil-link.md' -> refused: path resolves outside the authorized folder
read '<absolute path outside root>' -> refused: path resolves outside the authorized folder
```
Real output of `examples/stdio_client.py` for a path outside the folder:
```text
read_lines {"path": "../../README.md"}
{ "error": "refused: path resolves outside the authorized folder" }
```
## How it works
- Files with `.md`, `.markdown` or `.txt` are split into chunks of consecutive
non-blank lines (at most 20 lines each). Each chunk keeps its start and end line.
- Chunks go into SQLite with an FTS5 index (BM25 ranking, case and accent
insensitive). A query first requires all terms (AND), and falls back to any term (OR)
when nothing matches. Query text is quoted term by term, so FTS syntax in a query
cannot break the search.
- Reindex compares size and mtime, then a SHA-256 of the content, so a `touch` does
not rewrite the index.
- Confinement (`paths.py`): every path is fully resolved (symlinks followed) and must
land inside the resolved root, be a regular file, and have an allowed extension. The
indexer does not follow directory symlinks, and skips file symlinks whose target is
outside the root, broken links, hidden files and folders, `node_modules`,
`__pycache__`, `venv`, `env`, and files over 1 MB. Skipped symlinks are listed
under `refused` in the reindex report.
## Tests and checks
```sh
uv run ruff check .
uv run mypy
uv run pytest -q
```
33 tests, no network, keys or accounts. They cover: line numbers in results, nested
paths, accent folding, CRLF and empty files, FTS syntax in queries, OR fallback,
reindex after edit/add/delete, `touch` without content change, `..` and absolute
paths, null bytes, wrong extensions, directories, symlinks to files and directories
outside the root (refused), symlinks inside the root (allowed), a symlink swapped in
after indexing, the CLI, and the real server spawned over stdio and driven by the MCP
client.
Checked on this machine (macOS, Python 3.12.13, `mcp` 2.3.0 from `uv.lock`): ruff,
mypy (strict) and all 33 tests pass from a clean install.
## What was and was not verified
- Verified: everything above, with the official `mcp` client talking to the real
server over stdio.
- Integrations tested only with mocks: none. The project has no external service
integrations. Everything runs locally against SQLite and the filesystem.
- PENDING, not run: the CI workflow (`.github/workflows/ci.yml`) has not run on
GitHub; Python 3.10 and 3.13 from its matrix were not run locally; no real MCP
host (Claude Desktop, an IDE) was connected, so the client config above is the
standard shape, not a tested one; Windows was not tested.
## Limitations
- Keyword search only. No semantic search, no synonyms, no stemming.
- Only `.md`, `.markdown` and `.txt`. No PDF.
- Changes are picked up at startup and on `reindex`, not live. An edit that keeps the
same size and the same mtime (nanosecond) is not detected.
- A symlink to a directory is never followed, even when it points inside the root.
File symlinks pointing inside the root are indexed.
- `read_lines` checks the path and then reads it; a file swapped for a symlink in that
gap is not guarded against. Run it on a folder you control.
- Files are decoded as UTF-8 with replacement characters for invalid bytes.
- Each root gets its own SQLite file; two server processes on the same index file are
not coordinated beyond SQLite's own locking.
## Roadmap
- PDF text extraction (page numbers instead of line numbers).
- Optional semantic search (local embeddings), kept off by default.
- File watching so `reindex` is automatic.
## License
MIT, see `LICENSE`.
This server cannot be deployed
Maintenance
ActivityMaintained
ResponsivenessNo recent community issues