gnosis-mcp
CLAUDE.md29 starsChanged 8 months agoMITView on GitHub
Open-source Python MCP server for searchable documentation. Zero-config SQLite default, PostgreSQL optional. Dual-mode service (v0.14.0+): In addition to the Claude Code MCP (stdio) mode, gnosis-mcp runs as a production embeddings service — Dockerised, exposing POST /v1/embed (OpenAI-compatible) backed by local ONNX inference. External services use it as a self-hosted embeddings backend without calling any cloud API. All database operations go through DocBackend (a typing.Protocol in backend.py). Two implementations: Auto-detection: DATABASE_URL set to postgresql://... → PostgreSQL. Not set → SQLite at…
- Reads credentials
The file
We split this CLAUDE.md into the rules it gives an agent
9 rules. About 0 are common, 3 are distinctive, and 6 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
6
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 34
All database operations go through DocBackend (a typing.Protocol in backend.py).
- Very specific: line 43
Default install: mcp>=1.20 + aiosqlite>=0.20.
- Very specific: line 89
SQL injection prevention: All identifiers validated via regex in GnosisMcpConfig.__post_init__()
- Very specific: line 91
Write gating: Write tools are withdrawn from tools/list when cfg.writable is false, _and_ still check it on every call.
- Distinctive: line 149
Every version commit MUST:
- Very specific: line 153
Update relevant docs (README.md, llms.txt, llms-full.txt, CLAUDE.md) when adding features
- Distinctive: line 170
No pydantic, no click, no ORM
- Distinctive: line 171
All SQL identifiers must be validated
- Very specific: line 173
Write tools must always check cfg.writable first
≈ “Common” is an estimate: at least 1 in 5 of the 12,729 CLAUDE.md files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Gnosis MCP -- MCP Documentation Server
Open-source Python MCP server for searchable documentation. Zero-config SQLite default, PostgreSQL optional.
Dual-mode service (v0.14.0+): In addition to the Claude Code MCP (stdio) mode, gnosis-mcp runs as a production embeddings service — Dockerised, exposing
POST /v1/embed(OpenAI-compatible) backed by local ONNX inference. External services use it as a self-hosted embeddings backend without calling any cloud API.
Architecture
src/gnosis_mcp/
├── backend.py # DocBackend Protocol + create_backend() factory
├── pg_backend.py # PostgreSQL backend — asyncpg pool, $N params, tsvector, pgvector, UNION ALL
├── sqlite_backend.py # SQLite backend — aiosqlite, FTS5 + sqlite-vec hybrid (RRF), ? params
├── sqlite_schema.py # SQLite DDL — tables, FTS5 virtual table, vec0 virtual table, sync triggers
├── config.py # GnosisMcpConfig frozen dataclass, backend auto-detection, GNOSIS_MCP_* env vars
├── db.py # Backend lifecycle + FastMCP lifespan context manager
├── server.py # FastMCP server: 9 tools + 3 resources + auto-embed queries
├── ingest.py # File ingestion + converters: multi-format (.md/.txt/.ipynb/.toml/.csv/.json + optional .rst/.pdf), smart chunking, hashing
├── crawl.py # Web crawler: sitemap/BFS URL discovery, robots.txt, ETag caching, trafilatura HTML→markdown, rate-limited async fetching
├── parsers/ # Non-file ingest sources
│ ├── __init__.py # Package init
│ └── git_history.py # Git log → searchable markdown: parse commits, group by file, render, ingest via existing pipeline
├── watch.py # File watcher: mtime polling, debounce, auto-re-ingest + auto-embed on changes
├── schema.py # PostgreSQL DDL — tables, indexes, HNSW, hybrid search functions
├── embed.py # Embedding providers: openai/ollama/custom/local, batch backfill
├── local_embed.py # Local ONNX embedding engine — stdlib urllib model download, CPU inference
├── clients.py # MCP client registry: per-client config render, managed-block writes, `setup`/`doctor` wiring
├── blocking.py # Offloads CPU-bound local ONNX inference (embed + rerank) off the event loop: bounded semaphore, optional timeout
└── cli.py # argparse CLI: serve, init-db, ingest, prune, ingest-git, crawl, search, embed, stats, export, diff, check, setup, doctor, cleanup, fix-link-types, eval, savings, usageBackend Protocol
All database operations go through DocBackend (a typing.Protocol in backend.py). Two implementations:
- PostgresBackend (
pg_backend.py): asyncpg,$Nparams,::vectorcasts,ts_rank,websearch_to_tsquery,<=>,information_schemaqueries,UNION ALLfor multi-table - SqliteBackend (
sqlite_backend.py): aiosqlite,?params, FTS5MATCH+bm25(), sqlite-vec for hybrid search (RRF),sqlite_masterfor existence,PRAGMA table_infofor column checks
Auto-detection: DATABASE_URL set to postgresql://... → PostgreSQL. Not set → SQLite at ~/.local/share/gnosis-mcp/docs.db. Override with GNOSIS_MCP_BACKEND=sqlite|postgres.
Dependencies
Default install: mcp>=1.20 + aiosqlite>=0.20. Optional extras: [postgres] (asyncpg), [embeddings] (onnxruntime, tokenizers, numpy, sqlite-vec), [web] (httpx, trafilatura), [rst] (docutils), [pdf] (pypdf), [formats] (docutils + pypdf). Model download uses stdlib urllib (no huggingface-hub dependency).
Tools
Read (always available)
- search_docs(query, category?, limit?, query_embedding?) -- keyword (FTS5/tsvector), hybrid (with embedding on SQLite via sqlite-vec or PG via pgvector), or custom function search. Auto-embeds query when local provider configured.
- get_doc(path, max_length?) -- reassemble document chunks by file_path + chunk_index (optional truncation)
- get_related(path, depth?, relation_type?, include_titles?) -- bidirectional link graph query with multi-hop traversal
- search_git_history(query, author?, since?, until?, file_path?, limit?) -- search indexed git commit history by message, author, date range, or file
- get_context(topic?, limit?, category?) -- usage-weighted context summary. With topic: search + access count enrichment. Without topic: most-accessed docs + stats.
- get_graph_stats(category?) -- knowledge graph topology: orphans, hubs, relation distribution, edge/node counts
Write (requires GNOSIS_MCP_WRITABLE=true)
- upsert_doc(path, content, title?, category?, audience?, tags?, embeddings?) -- insert/replace document with auto-chunking (optional pre-computed embeddings)
- delete_doc(path) -- delete document chunks + links
- update_metadata(path, title?, category?, audience?, tags?) -- update metadata on all chunks
Resources
- gnosis://docs -- list all documents (path, title, category, chunk count)
- gnosis://docs/{path} -- read document content by path
- gnosis://categories -- list categories with doc counts
REST API (optional, v0.10.0+)
Enable with --rest flag or GNOSIS_MCP_REST=true. Runs alongside MCP on the same HTTP port.
- GET /health — server status, version, doc count
- GET /api/search?q=&limit=&category= — search docs (auto-embeds with local provider)
- GET /api/docs/{path} — get document by file path
- GET /api/docs/{path}/related — get related documents
- GET /api/categories — list categories with counts
- POST /v1/embed — OpenAI-compatible embeddings (
{texts, model?}→{model, dim, vectors, usage}). v0.14.0+. Lets external services use gnosis-mcp as a self-hosted embeddings backend with their choice of ONNX model (mdbr-leaf-ir / multilingual-e5-large / BGE-M3 / etc.). - GET /api/context?topic=&limit=&category= — usage-weighted context summary
- GET /api/graph/stats?category= — knowledge graph topology
Config: GNOSIS_MCP_CORS_ORIGINS (comma-separated or *), GNOSIS_MCP_API_KEY (Bearer auth).
New file: rest.py — Starlette routes, own backend lifespan, CORS + auth middleware.
Key Design Decisions
- Backend Protocol pattern: High-level Protocol (not connection wrapper) — PG and SQLite SQL differ too much for a thin wrapper
- FastMCP lifespan pattern: Backend created once via
app_lifespan(), shared across tool calls - Streamable HTTP transport:
gnosis-mcp serve --transport streamable-httpexposes/mcpendpoint via uvicorn. Supports remote deployment. Configure with--host/--portorGNOSIS_MCP_HOST/GNOSIS_MCP_PORT - SQL injection prevention: All identifiers validated via regex in
GnosisMcpConfig.__post_init__() - Multi-table support: PostgreSQL only —
GNOSIS_MCP_CHUNKS_TABLEaccepts comma-separated tables, queries useUNION ALL - Write gating: Write tools are withdrawn from
tools/listwhencfg.writableis false, and still check it on every call. A read-only client sees six tools, not nine that can only fail - Webhook notifications: Fire-and-forget POST to
GNOSIS_MCP_WEBHOOK_URLon write operations - Custom search delegation: Set
GNOSIS_MCP_SEARCH_FUNCTIONto use your own hybrid search (PostgreSQL only) - Column overrides:
GNOSIS_MCP_COL_*are for connecting to existing tables with non-standard names - Frontmatter link extraction:
ingestparsesrelates_tofrom frontmatter (comma-separated or YAML list), inserts into links table forget_relatedqueries. Glob patterns are skipped. - Content link extraction:
ingestparses[text](path.md)markdown links and[[wikilinks]]from body content, stored asrelation_type='content_link'. Separate from frontmatterrelates_tolinks - Multi-hop graph traversal:
get_related(depth=2)uses recursive CTE (PostgreSQL) or Python BFS (SQLite), max depth 3, cycle-safe - Git history link types: Cross-file commit links use
git_co_change, source file references usegit_ref— separates noisy git links from curated documentation links - Smart recursive chunking:
ingestsplits by H2 (primary), H3/H4 (for oversized sections), then paragraphs. Never splits inside fenced code blocks or tables - Content hashing:
ingestskips unchanged files using SHA-256 hash comparison - 4-tier embedding support: (1) Local ONNX via
[embeddings]extra, (2) pre-computed embeddings via tools, (3) backfill withgnosis-mcp embed, (4) built-in hybrid search whenquery_embeddingis provided - Local ONNX embedder:
local_embed.py— HuggingFace model auto-download, ONNX Runtime CPU inference, mean pooling, L2 normalization, Matryoshka dimension truncation - sqlite-vec hybrid search: Reciprocal Rank Fusion (RRF) merges FTS5 keyword + vec0 cosine results. Better than linear blending for incompatible score scales.
- Zero embedding deps for remote providers: Remote providers use stdlib
urllib.request— no new runtime dependencies - HNSW vector index: PostgreSQL
init-dbcreates an HNSW index for fast cosine similarity search - FTS5 with porter tokenizer: SQLite uses FTS5 with porter stemming, sync triggers for INSERT/UPDATE/DELETE
- XDG-compliant paths: SQLite default at
~/.local/share/gnosis-mcp/docs.db, no platformdirs dependency - Web crawl:
crawl.pydiscovers URLs (sitemap.xml or BFS), fetches with httpx, extracts content with trafilatura, reuseschunk_by_headings()andbackend.ingest_file()from ingest pipeline - URL as file_path: Crawled pages use the full URL as
file_path— no schema changes, works with existing search/get_doc - Crawl cache: JSON sidecar at
~/.local/share/gnosis-mcp/crawl-cache.jsonfor ETag/Last-Modified conditional requests - Deferred web deps:
[web]extra (httpx + trafilatura) imported only whencrawl_url()is called — same pattern as[rst]/[pdf] - Access tracking:
search_access_logtable records which documents are accessed viasearch_docs(top 3) andget_doc.get_contextuses access frequency to surface important docs. Fire-and-forget logging, opt-out viaGNOSIS_MCP_ACCESS_LOG=false - Client wiring is a registry, not a branch:
clients.pyholds oneClientrow per MCP client (config path, config key, entry shape, vendormcp addcommand, always-loaded instruction file).setupanddoctorread that table, so supporting a new client is a data change plus a test. Three strategies in order — the vendor's own CLI, then a marker-delimited managed block or a JSON merge, then a printed snippet — which is what makes the fallback universal rather than a list of clients someone remembered. instructionsis not universal: an MCP server may return aninstructionsfield and Claude Code surfaces it, butdsh-mcp-clientregisters the tools and discards it. Where a client drops it,setupwrites the equivalent rule into that client's always-loaded instruction file. That is whyClient.rulesisNonefor exactly the clients that do forwardinstructions— a second copy would bill the user twice for one paragraph.- Usage evidence, not intent:
doctorreadssearch_access_loggrouped byclient(recorded from the session'sinitializehandshake). A config file proves someone meant to wire it up; a non-empty result proves an agent called it. Wired-but-never-called is a warning, and--strictturns it into an exit code for CI. - A miss is a row, not an absence: a
search_docsthat matches nothing is logged with an emptyfile_path. The index saying "nothing here" is the most useful thing the log can record for whoever maintains the corpus, and it cannot be confused with a served document —usageranks misses,get_top_accessed/get_contextskip them.gnosis-mcp usageis the reader: calls, misses, top/never-served docs, by tool and client.
Testing
pytest tests/ # Unit tests (862, no DB required)
gnosis-mcp check # Integration check against live DBVersioning
Semantic versioning (pre-1.0). Patch numbers have no upper limit (0.7.99 is valid).
- Patch (0.7.x → 0.7.y): Bug fixes, small features, no new required deps
- Minor (0.7.x → 0.8.0): Breaking CLI/tool API changes, or significant architectural shift
- Major (→ 1.0.0): Stable tool/resource API, 300+ tests, all planned formats working
Releases
Version lives in these files — all must match:
pyproject.toml→version = "X.Y.Z"(the publish workflow reads this one)src/gnosis_mcp/__init__.py→__version__ = "X.Y.Z"(whatserverInforeports)server.json→"version": "X.Y.Z"(2 places)marketplace.json→"version": "X.Y.Z".claude-plugin/plugin.json→"version": "X.Y.Z"docs/rest-api.md→ the example/healthpayloaduv.lock→ the project's own version (refresh withuv lock)
pkg/arch/PKGBUILD + .SRCINFO are the exception: bump-version.sh updates their
pkgver, but the sha256sums cover the published sdist, so they are refreshed in a
follow-up commit (scripts/update-arch-sums.sh) once PyPI has the artifact.
Every version commit MUST:
- Bump every file above
- Update
CHANGELOG.md - Update relevant docs (
README.md,llms.txt,llms-full.txt,CLAUDE.md) when adding features - All tests passing
Pipeline: push to main with changed pyproject.toml → publish.yml builds, publishes to PyPI + MCP Registry, then creates vX.Y.Z tag. Also triggers on manual v* tag pushes. No manual tagging needed.
Release notes: the GitHub Release body is the CHANGELOG.md section for
that version, extracted by scripts/release-notes.sh. --generate-notes only sees
merged pull requests — it published a twenty-fix release as a single embed fix and
credited github-actions[bot] as a new contributor — so it remains only as the
fallback for a version with no CHANGELOG section.
CRITICAL: PyPI renders README.md as the package page. Any change to README.md, images, or llms*.txt MUST include a patch version bump — otherwise the changes never reach PyPI. When in doubt, bump the patch version.
Remotes: push to selify + codeberg + github (open-source project).
Rules
- No pydantic, no click, no ORM
- All SQL identifiers must be validated
- Pure functions should be unit-testable without a database
- Write tools must always check
cfg.writablefirst - Backend implementations use natural SQL in their own dialect — no leaky abstraction
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

