Passer au contenu principal
Y

local-kb-mcp (dsh-ai-kb)

ye-light/local-kb-mcp/packages/dsh-ai-kb

Local-first document knowledge base for DeepSeek Harness: search, overview, note deposit, sync and remove tools backed by a bundled Python/SQLite engine.

Installer

dsh plugin --profile web add github:ye-light/local-kb-mcp

Ce plugin se trouve dans le sous-répertoire packages/dsh-ai-kb du dépôt.

README

🗂️ local-kb-mcp

A local-first document knowledge base for AI agents

Python CLI · MCP Server · DeepSeek Harness Host Plugin

CI Python 3.8+ SQLite 3.24+ MCP stdio License: MIT

English · 简体中文


Index Markdown, text, JSON, source code, and logs in SQLite, then retrieve only the relevant chunks with keyword, semantic, or hybrid search. It is designed for local project documentation and notes that are too large to fit into an agent's context.

The core CLI and MCP server use only the Python standard library. Your documents and database stay on your machine.

Open-source tool, private data. This repository contains no knowledge-base data. You choose what to import and where the database is stored.

Why local-kb-mcp?

  • Local-first: no hosted database, API key, or external retrieval service is required.
  • Agent-ready: use the CLI directly, connect any MCP-compatible host, or install the self-contained DeepSeek Harness plugin.
  • Retrieval that fits technical notes: adaptive hybrid ranking, exact-identifier protection, project filters, and an optional local BGE embedding model.
  • Maintainable data: incremental imports, partition overviews, note ingestion and removal, verified backups, and built-in health checks.

Features

  • Project partitions: assign every document to a project and filter searches by partition.
  • Structure-aware chunking: split by Markdown headings and paragraphs, with bounded chunk sizes and configurable overlap.
  • Incremental imports: skip unchanged files using content SHA1; retry interrupted batches without re-importing everything.
  • Four retrieval modes: hybrid (default), keyword, semantic, and semantic with a minimum score.
  • Exact identifier protection: function names, node names, and other single ASCII identifiers automatically use literal keyword search.
  • Optional local embeddings: use a BGE Chinese ONNX model; missing models or dependencies fall back to keyword search.
  • Partition overview: inspect file/chunk counts, common subdirectories, and topic terms before searching.
  • Knowledge inbox: write reusable notes under inbox/<project>/, ingest them with sync, or create them directly through deposit.
  • Verifiable backups: gzip compression, rotation, optional mirrored copies, SHA256 checks, and restore path-traversal protection.
  • Health diagnostics: doctor checks Python, SQLite, database integrity, backups, locks, and host configuration.

Requirements

ComponentRequirement
Core CLI / MCP serverPython 3.8+ and SQLite 3.24+; no third-party Python packages required
DSH Host pluginPython 3.10+; python or py -3 available, or configure pythonPath
Semantic search (optional)onnxruntime, tokenizers, numpy, and a BGE Chinese ONNX model

Quick start

# 1) Initialize the database (stored beside kb.py by default)
python kb.py init

# 2) Import documents into project partitions
python kb.py add notes ./my-notes/
python kb.py add papers ./pdfs-text/

# 3) Search
python kb.py search "keyword another-term" -k 5
python kb.py search "keyword" --project notes
python kb.py search "keyword" --full

# 4) Optional: build local embeddings
python kb.py embed
python kb.py search "a paraphrased query" --semantic

# 5) Inspect, back up, and restore
python kb.py overview
python kb.py doctor
python kb.py backup
python kb.py backups
python kb.py restore kb-20260101-120000.db.gz

# 6) Ingest or remove reusable notes
python kb.py sync
python kb.py rm "inbox/notes/example.md"

Retrieval modes

ModeCommandBest for
Hybrid (default)search "query"Everyday retrieval across natural language and technical terms
Keyword--keywordExact terms, identifiers, or running without an embedding model
Semantic--semanticParaphrases and concept-level similarity
Semantic + threshold--semantic --min-score 0.45Reducing weak matches when the corpus may not contain an answer

Hybrid search fuses keyword and semantic candidates with reciprocal-rank scoring. Semantic weight is fixed at 1.0; keyword weight adapts to the query:

  • Rare ASCII terms (document frequency < 1000): 1.0
  • Frequent ASCII terms (document frequency ≥ 1000): 0.2
  • Chinese-only queries: 0.3
  • Chunks containing the complete query string receive a keyword-score boost.
  • A single ASCII identifier automatically bypasses hybrid ranking and uses literal keyword search.

Benchmark

A 50-query evaluation set, balanced between natural-language and terminology queries:

ModeRecall@5MRRnDCG@10
keyword0.3040.3610.307
semantic0.5600.7230.558
hybrid (adaptive)0.6560.8760.655

Relevance is weakly labeled by whether a chunk contains specified topic terms, which favors literal keyword matching. These measurements describe this evaluation set, not a universal performance guarantee.

Install the optional dependencies:

pip install onnxruntime tokenizers numpy

Place a BGE Chinese embedding model in <AI_KB_HOME>/models/bge-small-zh-v1.5/:

model.onnx
tokenizer.json

Alternatively, point AI_KB_EMBED_MODEL to another model directory, then run:

python kb.py embed

Embeddings are bound to the model SHA256, vector dimensions, and chunking parameters. Changing any of them blocks semantic queries until the index is rebuilt; the original documents are not modified. The default chunk size of 480 characters stays within the model's truncation limit.

Without a score threshold, semantic search still returns the nearest chunks when no good answer exists. Treat its output as leads and verify important claims against the source.

Use as an MCP server

kb_mcp.py is a zero-dependency stdio MCP server with six tools:

ToolPurpose
searchReturn source paths and matching chunks; supports hybrid, keyword, and semantic modes
overviewShow partition maps, file/chunk counts, path groups, and topic terms
addImport files or directories
statsShow per-project statistics
listList indexed files
depositWrite a reusable note to inbox/<project>/ and index it immediately

On Windows, register the launcher instead of hard-coding a Python executable:

command: 'cmd'
args: ['/c', '<AI_KB_HOME>\launch.cmd']

On Linux/macOS, run python3 <AI_KB_HOME>/kb_mcp.py. The launcher checks AI_KB_PYTHON, common Python installation directories, and the system PATH.

Install the DeepSeek Harness Host plugin

The self-contained Host plugin lives in packages/dsh-ai-kb and bundles its Python backend. It does not require a separately configured MCP server.

  1. Install from npm: dsh plugin add dsh-local-kb-host — the package is published as dsh-local-kb-host. For development, add packages/dsh-ai-kb as a local plugin directory instead.
  2. Start a new DSH session. The plugin registers aikb_search, aikb_overview, aikb_stats, aikb_deposit, aikb_sync, and aikb_rm.
  3. Optional: configure dataDir and pythonPath. The default data directory is %LOCALAPPDATA%\ai-kb on Windows and ~/.ai-kb on Linux/macOS.

Do not enable the Host plugin and the legacy MCP wrapper at the same time, or their instructions will be duplicated. See the plugin guide for details.

Configuration

VariableDefaultPurpose
AI_KB_HOMEDirectory containing kb.pyDatabase, inbox, models, and backups
AI_KB_MIRROREmpty (disabled)Optional mirrored backup directory
AI_KB_KEEP4Number of rotated backups to retain
AI_KB_CHUNK480Chunk size; must not exceed the embedding truncation limit
AI_KB_OVERLAP80Overlap between adjacent chunks
AI_KB_EMBED_MODEL<AI_KB_HOME>/models/bge-small-zh-v1.5Embedding model directory
AI_KB_PYTHONAuto-detectedPython executable used by the launcher
AI_KB_PLUGIN_CFGEmptyOptional host-plugin configuration checked by doctor

On Windows, local overrides can be placed in env.cmd beside launch.cmd; keep that file out of version control.

Supported files and limits

Supported extensions: .md, .txt, .markdown, .json, .py, .yaml, .yml, .csv, .log, .html, and .htm, up to 8 MB per file. Convert PDFs, images, and Office documents to text before importing.

  • Keyword matching is literal and space-separated; split Chinese queries into meaningful terms.
  • Semantic search uses a general Chinese BGE model and may under-rank specialized terminology.
  • Search results include local absolute paths; sanitize them before sharing publicly.
  • WAL and busy_timeout allow concurrent readers/writers to queue safely, but very high write concurrency is not the target use case.

Documentation

License

MIT — see LICENSE.

Plugins associés