semantic-search
Local semantic recall MCP server for citation-integrity workflows. Enables semantic search over a text corpus, returning file paths and page numbers for verification, with 100% local processing using ONNX embeddings and LanceDB.
README
alexandria-semantic-recall
Local semantic recall for alexandria — the runnable implementation of the reference recipe in its optional-integrations §2.
If you want general-purpose semantic search over an Obsidian vault, use Smart Connections — it is mature, zero-setup, and built for exactly that. This project exists for a narrower job: serving a citation-integrity workflow, where semantic search is allowed to find passages but never to quote them. Every fragment it returns carries a file path and page numbers so a downstream agent (or you) can walk back to the source PDF and verify. Recall-only, by design and by contract.
- 100% local: ONNX embeddings via fastembed (no PyTorch), vectors in LanceDB on disk. No API calls, no cost, works offline.
- Cross-lingual: the default multilingual model lets a query in one language recall passages in another — the one thing keyword grep can never do.
- Modest hardware is enough: the upstream production instance indexes over 450,000 chunks from more than 500 source texts on a 16 GB laptop, CPU-only; incremental updates run in about 90 seconds.
- MCP, not just CLI: read-only subagents often have no shell. As an MCP server this attaches
semantic_searchdirectly to their toolchain.
The contract
This implements the interface alexandria documents for its Searcher role:
search(query, k) -> [{file, page_start, page_end, score, text}, ...]
- Fragments are pointers:
fileis relative to your corpus root, pages map back to the source PDF. - Recall-only: fragments are never citation sources. Quotes, page numbers, and emphasis are verified against the PDF.
- Degrades gracefully: agents treat semantic recall as a bonus; grep remains the backstop (a stale index means semantic silence never proves absence).
Quickstart (5 minutes, synthetic example included)
Requires uv and Python ≥ 3.11.
git clone https://github.com/kengo006/alexandria-semantic-recall
cd alexandria-semantic-recall
# 1) build an index over the bundled synthetic corpus (first run downloads the model, ~0.22 GB)
uv run python build_index.py "examples/corpus"
# 2) query it from the CLI — note the paraphrase match: no keyword overlap required
uv run python search_core.py "can spoken testimony be trusted as evidence" -k 3
# 3) start the MCP server (stdio)
uv run python server.py
Your own corpus
The input is a text layer: a folder tree of .txt files, one per source PDF, with page boundaries preserved in either of two formats:
- born-digital extraction (e.g.
pdftotext -layout): form-feed\fpage breaks - scanned/OCR sources: explicit markers
===== page N =====
How you produce the text layer is up to you — alexandria's optional-integrations §1 describes the conventions. Then:
set CORPUS_ROOT=C:\path\to\your\text-layer # or export on unix; or pass the path as an argument
uv run python build_index.py # numpy index (index/)
uv run python migrate_to_lance.py # -> LanceDB (lance_db/), the serving backend
uv run python build_lance_index.py # ANN index for fast queries
Or all three steps as one guarded unit:
uv run python rebuild.py
rebuild.py exists because of a real incident: the server prefers lance_db/, so refreshing only the numpy index silently serves stale data. The three steps are bound together and verified at the end.
Keeping it fresh
After adding, changing, moving, or deleting corpus files:
uv run python incremental_update.py --dry-run # see the delta first
uv run python incremental_update.py # embed only what changed (~90s vs hours for a full rebuild)
Not real-time — run it after ingestion as a maintenance habit. The citation layer is the second safety net: quotes are verified against PDFs regardless of index state.
Wiring it into agents
Register the server in your MCP config (.mcp.json or your client's equivalent):
{
"mcpServers": {
"semantic-search": {
"command": "uv",
"args": ["run", "--project", "/path/to/alexandria-semantic-recall",
"python", "/path/to/alexandria-semantic-recall/server.py"]
}
}
}
Then give your search agent one standing instruction — this is the discipline that makes the tool safe:
Use
semantic_searchto discover candidate passages, then go back to the source PDF (via the returned file + pages) and read the verbatim text there. Fragments locate; only the source speaks.
alexandria's Searcher role ships with this wiring already described (search path D).
Design notes
- Chunks are small (~500 chars, sentence-aligned) because the default model's max_seq is ~128 tokens, and because the consumer wants precise pointers, not summaries. Page numbers are computed from character offsets against the page marks.
- Default model
paraphrase-multilingual-MiniLM-L12-v2(0.22 GB, 384-dim, CPU): genuinely cross-lingual. In an upstream head-to-head against a larger 8192-token model, recall@5 tied — the small model stays because it is ~1.8× faster, half the vector size, and safe on 16 GB RAM. Swap via--modelif your needs differ; longer-context models need smaller embedding batches. - Embedding batch defaults to 32: measured on a 16 GB machine, larger batches with long-sequence models can spike RAM to the ceiling. Tune upward only with monitoring.
- fastembed is version-pinned: a silent pooling change in a future version would make new query vectors inconsistent with an existing index. Unpin deliberately, and rebuild when you do. (On load you may see fastembed's own warning that this model "now uses mean pooling" — it is informational; the pin keeps query-side and index-side pooling consistent, which is what matters.)
- Dual backend: LanceDB serves (text + vectors on disk, top-k reads, resident RAM roughly constant — migrating saved ~640 MB at 410k chunks); numpy remains a fallback (delete
lance_db/to fall back). Thesearch(query,k)API is identical on both. - Incremental ids start at 500,000,000 so they never collide with full-build ids; the query side never reads ids.
Status
Extracted from a production system (2026) where it serves a five-role academic writing workflow — see alexandria for the architecture it plugs into. Issues and adaptations welcome. MIT.
推荐服务器
Baidu Map
百度地图核心API现已全面兼容MCP协议,是国内首家兼容MCP协议的地图服务商。
Playwright MCP Server
一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。
Magic Component Platform (MCP)
一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。
Audiense Insights MCP Server
通过模型上下文协议启用与 Audiense Insights 账户的交互,从而促进营销洞察和受众数据的提取和分析,包括人口统计信息、行为和影响者互动。
VeyraX
一个单一的 MCP 工具,连接你所有喜爱的工具:Gmail、日历以及其他 40 多个工具。
graphlit-mcp-server
模型上下文协议 (MCP) 服务器实现了 MCP 客户端与 Graphlit 服务之间的集成。 除了网络爬取之外,还可以将任何内容(从 Slack 到 Gmail 再到播客订阅源)导入到 Graphlit 项目中,然后从 MCP 客户端检索相关内容。
Kagi MCP Server
一个 MCP 服务器,集成了 Kagi 搜索功能和 Claude AI,使 Claude 能够在回答需要最新信息的问题时执行实时网络搜索。
e2b-mcp-server
使用 MCP 通过 e2b 运行代码。
Neon MCP Server
用于与 Neon 管理 API 和数据库交互的 MCP 服务器
Exa MCP Server
模型上下文协议(MCP)服务器允许像 Claude 这样的 AI 助手使用 Exa AI 搜索 API 进行网络搜索。这种设置允许 AI 模型以安全和受控的方式获取实时的网络信息。