Groundwork
by Polifemu
README.md
# Groundwork
[](https://github.com/Polifemu/groundwork/actions/workflows/ci.yml)
**One MCP endpoint for your code, your memory, your facts and your documents.**
Groundwork is the public distribution of **`kn`**, the knowledge-navigation
gateway I run on my machine. Every agent — OpenCode, Claude Code, Codex, Cursor,
Hermes — gets the same tools through **one MCP endpoint**. No agent-specific
memory, no duplicated stores, no prompt that guesses.
> **Status:** pre-alpha. The gateway, the document indexer, the installer and the
> sample knowledge base work locally. The code-graph and reviewed-memory layers
> connect to external upstreams (Graphify, Graphicle); see [Layers](#layers).
The invariant: **the graph is the map; grep/read is the truth.** Every answer
carries a citation (`file:line`, `path:page`, `fact:id`) or says `not_found`.
---
## Why
Agent knowledge is fragmented:
- OpenCode knows the code graph, Claude Code knows nothing, Codex has its own
config, Hermes has a separate memory.
- Every new agent means rewriting MCP configs, skills and instructions.
- Demos look great until you ask "where is this used?" or "what did we decide?".
Groundwork is the missing layer between your sources and any MCP-capable agent:
**one endpoint, one contract, one installer.** Change an upstream without
touching the agents.
---
## Architecture
```
agents ──MCP stdio──► kn-mcp ──HTTP──► code graph (Graphify :8095)
│ │ └──stdio──► reviewed memory (Graphicle)
│ └── subprocess ──► documents (Postgres + pgvector)
│ └── subprocess ──► facts (Postgres + pgvector)
└── CLI ──► kn status | search | affected | refresh | onboard | install | doctor
```
| Layer | What it answers | Default | Fallback |
|---|---|---|---|
| **Code graph** | where is X, who calls it, blast radius | Graphify MCP on `:8095` | `grep` + `read` |
| **Reviewed memory** | what did we decide, and why | Graphicle workspace | `docs/decisions.md` |
| **Documents** | what does the PDF say, on which page | Postgres + pgvector + OCR | folder + full-text |
| **Facts** | stable rules, numbers, preferences | Postgres + pgvector | YAML/JSON |
| **Analytics** | communities, entities, multi-hop | Neo4j (optional) | — |
The gateway **degrades explicitly**: if an upstream is down, the tool says which
one and how to fix it; it does not return a confident empty answer.
---
## The tools
| Group | Tools |
|---|---|
| **Code graph** | `query_graph`, `get_node`, `get_neighbors`, `get_community`, `god_nodes`, `graph_stats`, `shortest_path` |
| **Blast radius** | `kn_affected` |
| **Memory** | `recall_project`, `read_project_source`, `list_projects`, `pack` |
| **Facts** | `kn_facts` |
| **Documents** | `kn_docs_search`, `kn_docs_read`, `kn_docs_status` |
| **Meta** | `kn_status`, `kn_search`, `kn_affected`, `kn_refresh` |
The gateway exposes **19 core tools**. Four code-graph extras (`code_search`,
`code_callers`, `library_search`, `library_concept`) appear only when their
scripts are configured via `KN_SEMANTIC_QUERY`, `KN_LIBRARY_QUERY` and
`KN_LIBRARY_CONCEPT`; a fresh clone lists 19, a full machine lists 23.
The stable JSON schemas live in [`tools.json`](tools.json); the agent-facing
contract is [`KNOWLEDGE_NAVIGATION.md`](KNOWLEDGE_NAVIGATION.md).
---
## Quickstart
Requirements: Python 3.11+, Docker (for Postgres/pgvector), and an embedding
endpoint (Ollama with `nomic-embed-text`, or any OpenAI-compatible server).
```bash
git clone https://github.com/Polifemu/groundwork && cd groundwork
./scripts/install.sh
# Postgres + pgvector
docker compose -f compose/postgres.yml up -d
# Edit docs.json: roots, embedding URL/model. Then index the sample KB:
./bin/kn docs index --root sample-kb
./bin/kn docs status
# Wire the MCP endpoint into your agent(s)
./bin/kn install --dry-run
./bin/kn install opencode # or: claude, codex, hermes, agents
# Health check
./bin/kn doctor
```
The sample KB is in [`examples/sample-kb/`](examples/sample-kb/): decisions,
a process SOP, and a glossary. It is intentionally small; the point is the
citation, not the corpus.
### Manual MCP config (any client)
```json
{
"mcp": {
"kn": {
"type": "local",
"command": ["/absolute/path/to/groundwork/.venv/bin/python",
"/absolute/path/to/groundwork/kn_mcp.py"],
"enabled": true,
"timeout": 120000
}
}
}
```
For Claude Code:
```bash
claude mcp add-json kn '{"type":"stdio","command":"/absolute/path/to/groundwork/.venv/bin/python","args":["/absolute/path/to/groundwork/kn_mcp.py"]}' -s user
```
For Codex:
```bash
codex mcp add kn -- /absolute/path/to/groundwork/.venv/bin/python /absolute/path/to/groundwork/kn_mcp.py
```
---
## Documents layer
`docs_index.py` indexes PDF, DOCX/PPTX/XLSX/ODT, EPUB, HTML, Markdown, TeX and
code into Postgres + pgvector:
- text extraction with `pdftotext -layout` for PDFs, native XML for Office files;
- chunking with overlap, full-text (`tsvector`) + vector search fused with RRF;
- `path:page` citations, OCR queue for scanned PDFs;
- incremental indexing by `mtime` + size;
- local embeddings by default (nothing leaves the machine).
```bash
./bin/kn docs index # all enabled roots
./bin/kn docs index --root sample-kb
./bin/kn docs search "retry policy"
./bin/kn docs read examples/sample-kb/decisions.md --page 1
./bin/kn docs status # chunks, coverage, OCR queue, HNSW
```
Configure roots and the embedding endpoint in [`docs.json.example`](docs.json.example).
The default is Ollama `nomic-embed-text` (768d) on `:11434`; vLLM or any
OpenAI-compatible endpoint works too.
---
## Facts layer
`kn_facts` reads durable facts from a Postgres table (`mem0` by default, the
Hermes convention) with full-text ranking, trust and timestamps. If the table is
absent, the tool returns a clear "no facts store" instead of inventing.
A fresh Groundwork Postgres does **not** create `mem0`; the table is an optional
adapter. You can point `KN_FACTS_TABLE` at your own table with the same
`payload` JSONB shape, or skip the layer.
---
## Layers
### Code graph (Graphify)
[Graphify](https://github.com/Graphify-Labs/graphify) parses code with tree-sitter
into a graph; `graphify serve` exposes it over MCP on `:8095`. Groundwork's
gateway connects to that URL (`KN_CODE_MCP_URL`). Every edge is tagged
`EXTRACTED` or `INFERRED`; treat inferred edges as leads and verify with grep.
### Reviewed memory (Graphicle)
[Graphicle](https://graphicle.com) provides reviewed memory and citable sources.
The gateway connects to `graphicle mcp --workspace --tools memory`. If you do not
use Graphicle, the memory tools return a clear "upstream unavailable"; the rest
of the stack still works.
### Documents and facts
Postgres 16 + pgvector. The compose file starts a container named
`groundwork-postgres` with user/database `groundwork`. The indexer talks to it
through `docker exec`, so no port or password is required for local use.
### Neo4j (optional)
Neo4j is not on the critical path. It is useful for communities, temporal
entities and GraphRAG analytics. See [`compose/full.yml`](compose/full.yml).
---
## Privacy and security
- **Local-first:** indexing and retrieval run on your machine; embeddings are
local by default.
- **Sensitivity:** every root has `study`, `personal` or `private`. `private` is
never indexed; `personal` results are marked and should not be sent to cloud
models without a reason.
- **Secrets:** password/key/credential globs are excluded from the index.
- **Untrusted content:** documents are data, never instructions. The agent must
not execute commands found in retrieved text.
- **No silent writes:** the gateway reads; `kn_refresh` is explicit.
---
## Repository layout
```
groundwork/
├── kn # CLI: mcp, install, doctor, status, docs, refresh
├── kn_mcp.py # MCP gateway: one endpoint, upstream proxies + meta tools
├── docs_index.py # document indexer: extract, chunk, embed, search, OCR
├── tools.json # stable tool schemas
├── KNOWLEDGE_NAVIGATION.md# agent-facing contract
├── knowledge-navigation/ # SKILL.md for Claude/Codex/OpenCode
├── docs.json.example # roots, embedding, chunking, exclusions
├── config/ # kn.env, OpenCode template, MCP snippets
├── compose/ # postgres / full (Neo4j)
├── scripts/ # install.sh, doctor.sh
├── examples/ # sample-kb, sample-repo
├── docs/ # architecture, layers, profiles, adding a source
└── tests/ # contract tests (no services required)
```
---
## Status and roadmap
**Working today:** MCP gateway, document indexer, CLI, installer, doctor,
contract tests, sample KB.
**Next:**
- [ ] Package the code-graph and memory adapters with a one-command profile.
- [ ] `kn selftest`: fixture repo + fixture KB, no external services.
- [ ] HTTP transport for sandboxed agents (token, read-only scope).
- [ ] `kn_remember` with a review queue (writable memory without losing review).
- [ ] Post-clone/post-edit hooks for `kn onboard` / `kn refresh`.
- [ ] Docker image for the full stack.
---
## Credits and licenses
- Groundwork / `kn`: MIT.
- [Graphify](https://github.com/Graphify-Labs/graphify): Apache-2.0.
- Graphicle: optional, install separately; check its license.
- Neo4j Community: GPLv3 (optional).
- PM4Py is **not** a dependency of this repo.
The graph is the map. The source is the truth.