Skip to main content
Glama
Personal Knowledge MCP Server

Personal Knowledge MCP Server

README.md
# Personal Knowledge MCP Server

A persistent knowledge base for AI coding agents, exposed as an [MCP](https://modelcontextprotocol.io/) server. Agents store technical decisions, debugging insights, patterns, and facts — the server builds a knowledge graph automatically and answers natural language queries with cited, synthesized responses.

No installation needed — just add the MCP config below and your client handles the rest.

## Features

- **Hybrid search** — BM25 full-text search + vector similarity (via Ollama embeddings), fused with Reciprocal Rank Fusion
- **Knowledge graph** — Automatically built from entries (deterministic edges for tags/projects + LLM-extracted entities like tools, concepts, people)
- **Browse in the web UI** — the Home page of the hosted web UI lists new, most-connected, recently read, stale and expired entries and browses by project; every row opens the entry drawer The Home page's Add to KB panel adds entries too: drop or pick files, drop or type URLs, and the queue ingests them one at a time.
- **Agentic queries** — ReAct agent loop that plans, executes, evaluates, and retries — resolves the right entries even when single-shot search ranks them poorly
- **Graph-aware queries** — 5 traversal strategies (auto, decision_trace, timeline, related, connection) with LLM query planning
- **Synthesized answers** — `kb_summarize` retrieves relevant entries and uses Claude Haiku to produce cited prose answers
- **File ingestion** — Bulk-import existing notes, code, and docs from disk with LLM-powered extraction
- **Multi-user attribution** — Server-side identity injection (`KB_CONTRIBUTOR`, `KB_TEAM`), per-entry attribution visible in all output, contributor/team search filters, audit trail for mutations
- **Resolutions** — the corrected belief is recorded through `kb_store`'s `resolution` parameter (stored as `hints.resolution`; `lesson_learned` creates require it or `"none"`), with an optional Bash command cue; the service validates it, stamps it with provenance and delivers it to future sessions; `packages/kb-service/scripts/seed_resolutions.py` seeds them from existing procedure entries (dry run by default)
- **Surprise capture** — `KB_SURPRISE_CAPTURE` (`off|shadow|on`, default `off`) turns per-turn hook digests into detected corrections: `shadow` detects and stores candidates only, `on` also distills them and writes autonomous `lesson_learned` entries; `POST /api/kb/surprise/drain` runs one pass, `KB_SURPRISE_DISTILL_MODEL` picks the distiller model (on Bedrock the model variables take Bedrock model ids), and an observed-once resolution reaches the soft gate only after it recurs in a second session. Digests from other harnesses are stored under Claude Code's tool names (talos native names are mapped at ingest). Digests from harnesses that verify the agent's work (talos) can carry harness_correction items, which are detected deterministically as shape 4.
- **Graceful degradation** — Every optional component (Ollama, Anthropic, vector search) fails gracefully; core storage and FTS always work

## Prerequisites

- **Python 3.13+** and **[uv](https://docs.astral.sh/uv/)** (Python package manager)
- **[Ollama](https://ollama.com/)** — optional, for local vector embeddings
- **LLM provider** (pick one, optional but recommended):
  - **Anthropic API key** — simplest setup
  - **AWS Bedrock bearer token** — use Claude through your AWS account
  - **Ollama** — fully local, no API keys needed

### What works without each dependency

| Component | Without Ollama | Without LLM provider |
|---|---|---|
| Store entries | Works | Works |
| Full-text search (FTS5) | Works | Works |
| Vector similarity search | Disabled | Works (needs Ollama) |
| Graph building (deterministic) | Works | Works |
| Graph enrichment (LLM entities) | Disabled (or use Ollama LLM) | Disabled |
| Query planning (`kb_ask` auto) | Disabled (or use Ollama LLM) | Disabled |
| Answer synthesis (`kb_summarize`) | Disabled (or use Ollama LLM) | Disabled |
| File ingestion (`kb_ingest`) | Disabled (or use Ollama LLM) | Disabled |

At minimum, you get a fully functional knowledge store with full-text search and a deterministic knowledge graph. Add Ollama for vector search; add any LLM provider for the smart features.

## Quick Start

### One-liner setup

Installs all prerequisites (uv, Python 3.13, Ollama, embedding model), prompts for an optional Anthropic API key, and launches the knowledge explorer:

```bash
curl -fsSL https://raw.githubusercontent.com/jason-weddington/personal-kb-mcp/main/setup.sh | bash
```

### How local mode works (read this first)

The MCP server is a **thin HTTP client** of the `kb-service` web service.
In **local mode** the MCP server auto-spawns `kb-service` as a detached
singleton daemon on `127.0.0.1:8765` the first time a session opens, and
talks to it over loopback. Two things are required for that flow to work:

The `kb-service` console script ships with `personal-kb` (the `[local]` extra is kept for compatibility), so a plain `uvx` install has everything it needs.

**No configuration is needed for a local KB.** When `PERSONAL_KB_URL` is unset (or empty) the MCP server uses `http://127.0.0.1:8765`, sends the fixed non-secret API key `local-no-auth` (the spawned daemon runs with `KB_AUTH_MODE=none` and ignores it), and spawns the daemon on first use. The personal-kb-hook applies the same defaults but never spawns the daemon: if nothing is listening it stays silent. Set `PERSONAL_KB_URL` only to point at a hosted KB, and then `PERSONAL_KB_API_KEY` is required (a remote URL without a key is a startup error, never the sentinel). An explicit loopback URL on another port spawns the daemon on that port. A loopback URL with a real `PERSONAL_KB_API_KEY` counts as a hosted KB (a kb-service running on the same machine, or one reached through an SSH tunnel), so the MCP server connects to it and never spawns, replaces or stops it.

### With Anthropic (simplest)

Add this to your MCP client config — Claude Code (`~/.claude/mcp.json`), Claude Desktop (`claude_desktop_config.json`), etc.:

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[local]", "personal-kb"],
      "env": {
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}
```

That's it. `uvx` installs `personal-kb` AND the bundled `kb-service`, the
lifespan auto-spawns the local daemon on first use, and the MCP server
connects to it over loopback.

### Fully local (Ollama, no API keys)

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[local]", "personal-kb"],
      "env": {
        "KB_EXTRACTION_PROVIDER": "ollama",
        "KB_QUERY_PROVIDER": "ollama"
      }
    }
  }
}
```

Pull the models first:

```bash
ollama pull qwen3-embedding:0.6b   # for vector search
ollama pull qwen3:4b               # for LLM features (graph enrichment, query planning, synthesis)
```

### AWS Bedrock

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[local,aws]", "personal-kb"],
      "env": {
        "KB_EXTRACTION_PROVIDER": "bedrock",
        "KB_QUERY_PROVIDER": "bedrock",
        "AWS_BEARER_TOKEN_BEDROCK": "your-bearer-token",
        "KB_BEDROCK_REGION": "us-east-1"
      }
    }
  }
}
```

Uses the cross-region inference profiles `us.anthropic.claude-haiku-5-5` for extraction and query (thinking disabled; override with `KB_BEDROCK_MODEL`) and `us.anthropic.claude-sonnet-5-5` for synthesis (effort low; override with `KB_BEDROCK_SYNTHESIS_MODEL`). These are the same models and settings as the Anthropic provider. For a non-Claude Bedrock model, set `KB_ANTHROPIC_THINKING=default` and `KB_SYNTHESIS_EFFORT=default` so neither setting is sent. Surprise capture's detector, distiller and critic follow `KB_QUERY_PROVIDER` onto Bedrock (`us.anthropic.claude-sonnet-5-5`) unless `KB_SURPRISE_PROVIDER` is set, so a Bedrock-only KB needs no `ANTHROPIC_API_KEY`, but its kb-service interpreter needs the Bedrock SDK (kb-core's `aws` extra, 0.4.0 or later).

> **Legacy SigV4 auth** also works — set `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY` instead of `AWS_BEARER_TOKEN_BEDROCK`, or set `KB_AWS_PROFILE` to a named profile. With none of these, boto3's default credential chain is used (SSO or `aws login` sessions, instance and container roles), and temporary credentials are refreshed as they expire. Bearer token auth is preferred.

### Switching embedding provider

To embed with Amazon Titan Text Embeddings V2 through Bedrock instead of Ollama, using the same AWS account as the Bedrock LLM provider:

1. Install with the `[local,aws]` extra, using the same `uvx --from "git+https://github.com/jason-weddington/personal-kb-mcp.git[local,aws]"` form shown under AWS Bedrock.
2. Set the variables where kb-service actually reads them. For local (stdio/uvx) mode, add `KB_EMBEDDING_PROVIDER=bedrock` plus `AWS_BEARER_TOKEN_BEDROCK` (or `KB_AWS_PROFILE`) and `KB_BEDROCK_REGION` to the personal-kb MCP server's `"env"` block, because the auto-spawned daemon inherits that environment, not your shell's. For a self-run or hosted kb-service, set them in that service's environment or unit.
3. Restart. In local mode, run `kill "$(cat ~/.local/share/personal_kb/kb-daemon.pid)"` (or `$PERSONAL_KB_DAEMON_STATE_DIR/kb-daemon.pid`), and the next session respawns the daemon with the new env; a daemon is not replaced on an env change alone. For hosted mode, restart the service.
4. Run reembed with the same DB location and env: `KB_EMBEDDING_PROVIDER=bedrock KB_BEDROCK_REGION=... AWS_BEARER_TOKEN_BEDROCK=... uvx --from "git+https://github.com/jason-weddington/personal-kb-mcp.git[local,aws]" kb-service reembed` for local mode, or `kb-service reembed` in the service's environment for hosted mode. Before trusting the run, check that the summary reads `provider=bedrock, model=amazon.titan-embed-text-v2:0`. Add `--missing` to embed only entries without a vector.
5. Caveats: the vector leg mixes old and new vectors until reembed finishes, and kb-service logs a model-mismatch WARNING at startup until then. `KB_EMBEDDING_DIM` must stay at the table's dimension: Titan V2 accepts 256, 512 or 1024, and the default 1024 needs no change. Changing the dimension requires recreating the vector table, and reembed refuses to run in that case. The IAM principal needs `bedrock:InvokeModel` on the Titan model. The similarity thresholds are calibrated on qwen3-embedding.

### Ollama setup (if using embeddings or local LLM)

```bash
# Install Ollama: https://ollama.com/download

ollama pull qwen3-embedding:0.6b   # for vector search
ollama pull qwen3:4b               # only if using Ollama as LLM provider
```

## Connecting over HTTP (recommended for hosted KBs)

kb-service serves the full MCP tool set over MCP streamable HTTP at `/mcp`, authenticated with the same bearer API keys as the REST API. Nothing runs on your machine: Claude Code talks to the service directly. The endpoint is stateless (no MCP session ids), so it works behind any load balancer.

Connect Claude Code to a hosted personal KB:

```bash
claude mcp add --transport http personal-kb https://kb.example.com/mcp --header "Authorization: Bearer YOUR_API_KEY"
```

For a team KB, use the server name `team-kb`. The team kb-service must run with `KB_INSTANCE_ROLE=team` so its tools carry the `team_kb_` prefix, and `personal-kb-hook` matches the server names `personal-kb` and `team-kb`:

```bash
claude mcp add --transport http team-kb https://team.example.com/mcp --header "Authorization: Bearer YOUR_API_KEY"
```

The equivalent `.mcp.json` / `~/.claude.json` entry:

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "http",
      "url": "https://kb.example.com/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_API_KEY"
      }
    }
  }
}
```

Local use works the same way against a kb-service you start yourself: run `KB_AUTH_MODE=none kb-service serve --port 8765`, then `claude mcp add --transport http personal-kb http://127.0.0.1:8765/mcp`. Over HTTP there is no daemon auto-start, so keep that service running yourself (for example as a user systemd unit). If an older install's stdio server already spawned a kb-service daemon on that port, stop it first and confirm `curl http://127.0.0.1:8765/api/health` reports the new version.

The stdio server (`uvx ... personal-kb`) is deprecated and will be removed after one release; a stdio server pointed at a hosted KB logs a deprecation warning and appends the `claude mcp add` command to `kb_preflight` output.

Over `/mcp`, `KB_INSTANCE_ROLE`, `KB_MANAGER`, `KB_CONTRIBUTOR` and `KB_SKIP_SAFETY` are read from the kb-service environment, not from your client config: they decide the tool prefix, whether `kb_maintain` / `kb_bulk_update` and the contributor/team listing tools are served, and secret scanning.

`kb_ingest` is not served over HTTP, because it reads files on the machine running the tool, which for `/mcp` is the service host. During the overlap, use the stdio server for file ingestion, or call `kb_ingest_url` with the file's text in `content`.

A reverse proxy in front of `/mcp` needs a read timeout of at least 300s: `kb_ask`, `kb_summarize` and `kb_ingest_url` run LLM calls inside a single request.

## Tools

### `kb_store`

Store or update a knowledge entry. Each entry has a short title, long title, full content, entry type, optional tags, and optional project reference. Updates create version records preserving full history. Graph edges and embeddings are built automatically on store. An optional `ttl` parameter (e.g. `'7d'`, `'24h'`, `'2w'`) sets an expiration date after which the entry is excluded from search results.

**Supersession.** When a new entry replaces older ones, declare it with the `supersedes` parameter of `kb_store`. It is **required** on every call: a list of `kb-XXXXX` ids, or the literal `"none"` when the entry replaces nothing (`[]` is rejected as ambiguous; on deactivate and metadata-only updates pass `"none"`). Combining `supersedes="none"` with a non-empty `hints.supersedes` is rejected; list the ids in `supersedes`. `distinct_from` (create only) lists existing entries the new one is deliberately distinct from, and `superseded_by` (with `deactivate_entry_id`) names the entry that replaces the deactivated one. `change_reason` is required on update and deactivate. `kb_store_batch` entries require `supersedes` too. The hosted service checks every newly named target before writing: it must exist, be active, not be a `mental_map`, not be the entry itself, and not already supersede the writer. A rejected target fails the whole write with a 422 that names the problem. Each superseded entry's `superseded_by` then points at its newest superseder, and the field is maintained automatically when superseders are stored, updated or deactivated. On an update, supersedes targets are only ever added, never retracted. Updates and deactivations through the hosted service require a `change_reason` that says what changed and why; when you deactivate an entry because a newer one replaces it, also pass `superseded_by`.

**Resolution.** A `lesson_learned` create requires the `resolution` parameter: an object with `wrong_belief` and `corrected_fact` (both non-blank), optional `evidence`, `cue` and `scope` (`project` or `global`), or the literal `"none"` when the lesson is not a corrected belief. The KB pushes only resolutions to future sessions (the session-start gotcha list and, for a Bash cue, the pre-command gate), so a lesson stored without one is found only by search. A `hints.resolution` key satisfies the requirement instead; passing both is an error. It is optional on update (an object replaces the stored resolution) and rejected with `deactivate_entry_id`. Add a cue only for one specific Bash command, because a two-word Bash cue can arm the gate.

**Near-duplicate guard.** On the hosted service, creating an entry whose text is at or above cosine 0.88 (`KB_NEAR_DUPLICATE_FLOOR`) to an existing active entry in the same project is rejected with a 409 that lists the candidates. Cover every listed id with one of three escapes: `update_entry_id=<id>` (it is the same fact, so update that entry), `supersedes=[<id>]` (the new entry replaces it), or `distinct_from=[<id>]` (they are genuinely different facts; older clients pass `hints={"distinct_from": [...]}`). Map entries, entries without a project, and creates made while embeddings are unavailable are not checked.

### `kb_store_batch`

Store multiple entries in a single call (max 10). More efficient than repeated `kb_store` — uses a single LLM call for graph enrichment across all entries. Each entry carries its own `resolution` under the same rule as `kb_store`.

### `kb_search`

Hybrid search combining BM25 full-text search with vector similarity (when Ollama is available). Returns compact summaries (no `knowledge_details`). Supports filtering by project, entry type, tags, contributor, and team. Results include confidence scores with staleness decay and `@contributor/team` attribution badges. Set `include_expired=True` to include entries past their TTL expiration in results (excluded by default). Entries replaced by a newer entry are hidden by default; pass `include_superseded=True` to show them, marked `[SUPERSEDED by kb-X]`.

### `kb_get`

Retrieve full details for one or more entries by ID. Use after `kb_search` to read the complete `knowledge_details` of interesting results. Calling it on a superseded entry opens the output with `SUPERSEDED by kb-X — <title>`.

### `kb_ask`

Answer questions by traversing the knowledge graph. When an LLM is available, the default `auto` strategy uses a ReAct agent loop that can plan searches, explore the graph, evaluate results, and retry with different approaches — all within a hard cap of 4 tool calls. Strong initial results skip the agent entirely (0 LLM calls). Set `KB_AGENTIC_QUERY=FALSE` to fall back to single-shot query planning.

Strategies:

- **auto** — Agentic retrieval (default). The agent has access to hybrid search, graph neighbors, graph vocabulary, decision chains, and scope-based entry listing. It picks the right combination for each question.
- **decision_trace** — Follow `supersedes` chains to trace how a decision evolved over time.
- **timeline** — Chronological entries for a given scope (project, tag, etc.).
- **related** — BFS from a starting node through graph edges.
- **connection** — Find paths between two nodes in the graph.

### `kb_summarize`

Answer a question with a synthesized natural language response. Retrieves relevant entries via the auto strategy, then uses Claude Haiku to produce a coherent answer with `[kb-XXXXX]` citations. Falls back to raw search results when the LLM is unavailable.

### `kb_ingest`

Ingest files from disk into the knowledge base (only available when `KB_MANAGER=TRUE`). Reads files, runs safety checks, and uses an LLM to summarize and extract structured knowledge entries.

```
kb_ingest(file_path="/path/to/notes", project_ref="my-project", dry_run=True)
```

**Pipeline:** deny-list check → extension filter → size limit → SHA-256 dedup → secret detection → PII redaction → LLM summarize → chunking → dedup gate → LLM extract → store entries → build graph

- Supports single files or entire directories (recursive by default)
- Files become `note:` nodes in the graph, with `extracted_from` edges linking entries to sources
- Re-ingestion detects content changes via hash and replaces old entries
- `dry_run=True` previews extraction without storing anything
- Supports `.md`, `.txt`, `.py`, `.js`, `.ts`, `.yaml`, `.json`, `.toml`, and many more text formats
- Skips binaries, images, archives, keys, `.env` files, and other sensitive formats
- Optional safety libraries: `uv sync --extra safety` installs `detect-secrets` and `scrubadub`

### `kb_ingest_url`

Ingest a web page into the knowledge base (only available when `KB_MANAGER=TRUE`). Uses [trafilatura](https://trafilatura.readthedocs.io/) for HTML-to-text extraction, then runs the same LLM summarize/extract pipeline as `kb_ingest`. Accepts `url`, `project_ref`, `tags`, and `dry_run`.

### `kb_explore`

Returns the URL of the hosted web UI's Graph page (`<service url>/graph`); open it in a browser. The page is an interactive graph explorer with zoom-aware labels, click-to-focus on a node and its neighbours, ArrowLeft backtrack, a solo legend that shows one node type at a time, and node search. Type a question in the ask bar, or press Enter on free text in the search box, to stream it to the agent and watch its traversal light up on the graph, with the answer or the matching entries in a side panel. When sign-in is on (auth mode `jwt`), an answer continues in a side chat panel whose `[kb-XXXXX]` citations fly to their nodes.

### `kb_feedback`

Report when a KB query failed to help. Always available (not gated by `KB_MANAGER`). Three feedback types: `missing` (KB lacked needed knowledge), `unhelpful` (results existed but didn't help), `friction` (tool was awkward or slow).

### `kb_maintain`

Administrative operations (only available when `KB_MANAGER=TRUE`):

- `stats` — Database overview with counts
- `deactivate` / `reactivate` — Soft-delete and restore entries
- `rebuild_embeddings` — Re-embed entries (all or only missing)
- `rebuild_graph` — Full graph reconstruction from all active entries
- `purge_inactive` — Hard-delete entries inactive for N+ days
- `vacuum` — Optimize database (PRAGMA optimize + VACUUM)
- `entry_versions` — Show version history for an entry
- `list_feedback` — Recent agent feedback with optional type/date filters
- `summarize_feedback` — LLM-clustered summary of feedback themes
- `search_stats` — Search telemetry overview (total queries, zero-result rate, top missed queries)
- `list_contributors` — Contributor/team stats for active entries
- `list_audit` — Recent mutation events (create/update/deactivate/reactivate) with optional entry_id/date filters

### `kb_map_eligibility`

Review per-project map eligibility across the whole KB: one line per project_ref with its effective verdict (computed or human override), mappable/hand-authored/ingested counts, existing mental-map count, top title prefix and evidence flags (`too_thin`, `ingest_corpus`, `journal`), plus a header of aggregate counts. Pass `project_ref` to narrow to a single project. This is the table to read before deciding which projects deserve a mental map — the review target is a project that is eligible AND has `maps == 0`.

### `kb_map_eligibility_override`

Set or clear a human map-eligibility override for one project_ref: `eligible=True/False` with a permanent `reason` forces the verdict in either direction until `clear=True` reverts it to the computed verdict. The nightly map-maintenance loop reads this table and never writes it — this tool is the human/agent-review path only.

## Mental maps — orientation nodes for your KB

A **mental map** (entry type `mental_map`) is the directory tier of your knowledge base. It is the only entry type that does not carry retrievable knowledge itself — instead, it points to *where* knowledge lives. Think of it as a small, curated index for a subsystem: "the ingestion flow lives across these five entries; auth lives across those three." Agents read a map to orient themselves, then `kb_get` the detail entries the map points to.

### When to author a map vs. a `factual_reference`

A `factual_reference` *is* the answer — a port number, a config threshold, an API signature, the name of a function. A `mental_map` *points to* answers — an orientation directory that says "for the auth subsystem, see kb-00310, kb-00312, kb-00318."

Rule of thumb: ask whether a reader would *act on the value directly* (fact) or *follow it to a source* (map). Counts of parts ("the pipeline has three stages") are pointers in disguise and belong in a map; the actual stage names, thresholds, or filenames belong in the linked factual entries a map points to.

### What a map must contain

Every `mental_map` must have **at least one outbound pointer**. A pointer is any of:

- a `kb-XXXXX` reference inside `knowledge_details`;
- a `related_entities` hint with either a dict carrying an `id`/`target` or a bare entry id string.

A `supersedes` hint is **not** a map pointer: a map cannot supersede anything, so a map whose only pointer is a `supersedes` hint is rejected as an orphan.

Tags, project refs, and person/tool hints **do not** count — those are categorization, not orientation. `kb_store` rejects an orphan map (zero pointers) on **create**, before any row is written, with an error: *"A mental_map entry requires at least one outbound pointer … A map with zero pointers is an orphan note, not a map."* As of 2026-09-19 the hosted service runs the same guard on **update**, evaluating the effective post-update body, so an update can no longer strip a map's last pointer; the local/no-auth kb-core path is still unguarded. Don't treat "the store would have caught it" as a substitute for checking your own map's pointers — the update guard is new, and every map authored before it predates that check.

### What a map should *not* contain (fact-free discipline)

A map is for **structure and relationships**, not retrievable values. `kb_store` runs a deterministic, advisory-only lint on every map body and flags content that looks like a fact — URLs, file paths, `ENV_VAR`-style tokens, dotted code identifiers (`module.func`), quoted literals, and config-like numerals (decimals, ≥4-digit integers, numerals adjacent to `=` or `:`). It also flags bodies longer than a compositional advisory budget (a fixed base allowance plus a per-pointer allowance for each distinct `kb-` reference, so well-pointed maps get more room for their glosses) with the note that an author should *cut orientation prose, not pointer glosses*.

The lint never blocks a store — every store succeeds and every warning starts with `Map lint (advisory):`. The signal is purely informational, pointing you at content that probably belongs in a `factual_reference` the map links to instead.

### Maps don't decay on a clock

Unlike the four value-bearing entry types — which lose confidence on an entry-type-specific half-life (90 d / 1 y / 2 y / 5 y) — a `mental_map` is exempt from confidence decay. It has no retrievable value of its own to go stale, and the access-aware self-heal (`last_accessed` resets the decay clock on `kb_get`) would just make a constantly-surfaced map look "fresh" while its pointers rotted. So maps don't decay at all.

Freshness for a map is instead **pointer-validity**, checked at retrieval time: when you `kb_get` a `mental_map`, the server resolves each outbound pointer and surfaces inline any target that is `superseded_by` another entry or has been deactivated. A stale map is one whose pointers no longer resolve, not one that hasn't been read recently.

### Discovering and surfacing maps

Maps are discoverable through two paired mechanisms:

- **Pull (in-process, default):** `kb_preflight(project_ref="...")` includes a `Maps:` section listing all of the project's mental maps (they're a small curated set, so there's no cap). An agent calls preflight at session start, sees which maps exist, and pulls full detail with `kb_get` only for the ones it judges relevant to the task. No hook, no extra config — this works out of the box on every install.
- **Push (opt-in CLI hook, below):** the `personal-kb-hook` console script proactively injects the same Maps directory into Claude Code's `SessionStart` and `UserPromptSubmit` hooks — for the cold-start case where the agent doesn't yet know which subsystem to ask about.

Both halves read from the same `mental_map` entries you author with `kb_store`; the push half is described in the next section.

## Surfacing maps via `personal-kb-hook` (opt-in)

The repo also ships a small stdlib-only CLI (`personal-kb-hook`) that you can wire into Claude Code's [hook system](https://docs.claude.com/en/docs/claude-code/hooks) to proactively surface the `mental_map` entries for the project you're working in. The hook itself never talks to the MCP server or the DB — it reads a denormalized JSONL index that the MCP server writes every time a `mental_map` is created, updated, or deactivated.

On every `SessionStart` the hook also lists the executables found in `KB_TOOL_DIRS` (opt-in: unset means no inventory) with a one-line description each, so the agent sees your personal tools up front; set `KB_TOOL_INVENTORY=0` to turn it off. See the [hook README](packages/personal-kb-hook/README.md) for the format and caps.

### Install

The hook lives in its **own standalone package** at `packages/personal-kb-hook/` with **zero third-party dependencies** — installing it does NOT drag in `fastmcp`, `anthropic`, `pymupdf`, etc. Install it directly from the git subdirectory:

```bash
uv tool install --from \
  "git+https://github.com/jason-weddington/personal-kb-mcp.git#subdirectory=packages/personal-kb-hook" \
  personal-kb-hook
```

This puts a single `personal-kb-hook` console script on your `PATH`. The tool venv contains exactly one package (`personal_kb_hook`) — nothing else. The hook *process* itself imports only the Python standard library and never opens the database — it reads the on-disk JSONL index — so it stays fast on the session hot path.

> The main `personal-kb` server package is installed separately (typically via `uvx --from "git+…" personal-kb` or `uv tool install --from "git+…" personal-kb`); it provides the MCP server that writes the on-disk maps index that the hook reads.

### The `.kb_project` convention

The hook resolves which project's maps to surface by walking up from the harness-provided `cwd` to the first `.kb_project` file it finds. That file is **one line** — the KB `project_ref` for the repo:

```text
# .kb_project — committed at the repo root
my-project
```

Commit one per repo so the hook works for every clone, every machine, every user — no per-user config required. Lines starting with `#` are comments; the first non-blank, non-comment line wins.

This repo's own `.kb_project` is just `personal-kb`.

### Wiring it into Claude Code

Add the hook to `~/.claude/settings.json` (or your project-level `.claude/settings.json`). The hook accepts `--format=text` (default — bare directory string) or `--format=claude-json` (the `hookSpecificOutput` envelope Claude Code understands), and, for the Antigravity (`agy`) harness, `--format=agy-json --event <Event>` (Antigravity's reply object; see section 3 of [docs/intelligent-kb.md](docs/intelligent-kb.md) for the `~/.gemini` `hooks.json` sample and the tool-name mapping).

```jsonc
{
  "hooks": {
    "SessionStart": [
      {
        "matcher": "",
        "hooks": [
          { "type": "command", "command": "personal-kb-hook --format=claude-json" }
        ]
      }
    ],
    "UserPromptSubmit": [
      {
        "matcher": "",
        "hooks": [
          { "type": "command", "command": "personal-kb-hook --format=claude-json" }
        ]
      }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          { "type": "command", "command": "personal-kb-hook --format=claude-json", "timeout": 5 }
        ]
      }
    ],
    "PostToolUseFailure": [
      {
        "matcher": "",
        "hooks": [
          { "type": "command", "command": "KB_FAILURE_CONTEXT=1 personal-kb-hook --format=claude-json", "timeout": 5 }
        ]
      }
    ],
    "Stop": [
      {
        "matcher": "",
        "hooks": [
          { "type": "command", "command": "personal-kb-hook --format=claude-json" }
        ]
      }
    ],
    "SubagentStop": [
      {
        "matcher": "",
        "hooks": [
          { "type": "command", "command": "personal-kb-hook --format=claude-json" }
        ]
      }
    ]
  }
}
```

The `PreToolUse` entry is the prevention soft gate. It runs synchronously so it can deny a call, and it matches `Bash` only because only Bash cues are gated. It never touches the network and stays inert until the service enables the gate (see `packages/personal-kb-hook/README.md`).

The `PostToolUseFailure` entry records every failed tool call to the failure-cue index and, with `KB_FAILURE_CONTEXT=1`, hands the agent the matching corrected fact once per resolution per `rearm_hours` window (re-armed by compact, resume or clear); it must be synchronous (no `async: true`) so the context lands next to the failure instead of on a later turn.

Stop flushes the gate log, refreshes the prevention settings and, when the service enables KB_SURPRISE_CAPTURE, ships the turn digest, and it prints nothing.

SubagentStop posts one turn digest per subagent run (gated by the same KB_SURPRISE_CAPTURE switches), under the parent session's id but never touching the parent's turn counter.

If you'd rather inject the bare directory string into the model context yourself, use `--format=text`:

```jsonc
{
  "hooks": {
    "SessionStart": [
      { "matcher": "", "hooks": [{ "type": "command", "command": "personal-kb-hook" }] }
    ],
    "UserPromptSubmit": [
      { "matcher": "", "hooks": [{ "type": "command", "command": "personal-kb-hook" }] }
    ]
  }
}
```

### What gets surfaced

When the hook fires, it:

1. Reads the JSON payload Claude Code writes to its stdin.
2. Walks up from `payload.cwd` to the first `.kb_project` (or stays silent if none).
3. Looks the resolved `project_ref` up in `~/.local/share/personal_kb/maps_index.jsonl` (the file the MCP server writes on mental_map store/update/deactivate).
4. Emits a single factual line like `Maps for my-project — [kb-00310] ingestion: Ingestion flow; [kb-00312] auth: Auth + sessions`.

The output is **factual**, never imperative — no "load", "use", "read" — so it never trips prompt-injection defenses.

A per-session scratch file under `~/.cache/personal_kb/` suppresses re-injection when the same project's same map ids have already been surfaced in this session. Scope drift (cwd → different repo → different `.kb_project`) re-emits. Compaction (`source=compact`) bypasses the suppression so the context is re-seeded.

Any error path — no `.kb_project`, no maps for the project, malformed stdin, write failure — exits 0 with no stdout. The hook never raises into the harness.

## Environment Variables

| Variable | Default | Description |
|---|---|---|
| **Thin client (always set)** | | |
| `PERSONAL_KB_URL` | `http://127.0.0.1:8765` | `kb-service` URL. Unset or empty means local mode (the MCP server auto-spawns a daemon on this port). Set it only for a hosted KB. A loopback URL with a real `PERSONAL_KB_API_KEY` is a hosted KB too (co-located or SSH-tunnelled) and is never spawned or stopped. |
| `PERSONAL_KB_API_KEY` | `local-no-auth` for loopback URLs; required otherwise | Bearer token for `kb-service`. Defaults to the sentinel only when the URL is loopback (the daemon ignores it). Remote mode: a per-machine API key minted from the service's Settings → API Keys pane. |
| **Core** | | |
| `KB_DATABASE_URL` | _(unset)_ | PostgreSQL URL — passed through to `kb-service` (the MCP server itself never opens a DB) |
| `KB_DB_PATH` | `~/.local/share/personal_kb/knowledge.db` | SQLite database file path (ignored when `KB_DATABASE_URL` is set) |
| `KB_LOG_LEVEL` | `WARNING` | Logging level (`DEBUG`, `INFO`, `WARNING`, `ERROR`) |
| `KB_MANAGER` | _(unset)_ | Set to `TRUE` to enable `kb_maintain` and `kb_ingest` tools |
| `KB_INGEST_MAX_FILE_SIZE` | `10485760` | Max file size in bytes for ingestion |
| `KB_AGENTIC_QUERY` | `TRUE` | Enable ReAct agent loop for `kb_ask` auto strategy |
| `KB_AGENTIC_MAX_CALLS` | `4` | Max tool calls in the agentic query loop |
| **Multi-user** | | |
| `KB_CONTRIBUTOR` | _(unset)_ | Your name — attached to entries, versions, search events, and audit trail |
| `KB_TEAM` | _(unset)_ | Your team — attached to entries alongside contributor |
| `KB_WRITE_POLICY_DEFAULT_SURFACE` | `interactive` | kb-service: the surface for API keys with none set and for no-auth callers (interactive, headless or autonomous; an unknown value means headless). Password (JWT) logins are always interactive. |
| `KB_SKIP_SAFETY` | _(unset)_ | Set to `TRUE` to bypass secret scanning on store |
| `KB_PG_POOL_MIN` | `1` | Postgres connection pool minimum size |
| `KB_PG_POOL_MAX` | `5` | Postgres connection pool maximum size |
| **Anthropic (cloud LLM)** | | |
| `ANTHROPIC_API_KEY` | _(unset)_ | API key — required for Anthropic provider |
| `KB_ANTHROPIC_MODEL` | `claude-haiku-5-5` | Model for graph enrichment and query planning; synthesis uses `KB_SYNTHESIS_MODEL` (default `claude-sonnet-5-5`) |
| `KB_SYNTHESIS_MODEL` | `claude-sonnet-5-5` | Anthropic model for cited answers, web chat and the listener vote (the synthesis role) |
| `KB_SYNTHESIS_EFFORT` | `low` | Reasoning effort for the synthesis role: `low`, `medium`, `high`, `xhigh`, `max`, or `default` (sends no effort field); anything else warns and falls back to `low` |
| `KB_ANTHROPIC_TIMEOUT` | `30.0` | Request timeout in seconds |
| `KB_LLM_DIAGNOSTICS` | `1` | kb-service: set `0` (or `false`, `off`, `no`) to stop sending the Claude API cache-diagnostics object; kb-service still records every Anthropic call in `llm_calls` |
| **Ollama (local LLM)** | | |
| `KB_OLLAMA_URL` | `http://localhost:11434` | Ollama API base URL |
| `KB_OLLAMA_MODEL` | `qwen3:4b` | Model for generation tasks |
| `KB_OLLAMA_LLM_TIMEOUT` | `120.0` | Generation timeout in seconds |
| **Ollama embeddings** | | |
| `KB_EMBEDDING_PROVIDER` | `ollama` | Embedding provider: `ollama` or `bedrock` (Titan Text Embeddings V2) |
| `KB_EMBEDDING_MODEL` | `qwen3-embedding:0.6b` (`amazon.titan-embed-text-v2:0` when the provider is `bedrock`) | Model for vector embeddings |
| `KB_EMBEDDING_DIM` | `1024` | Embedding vector dimensions |
| `KB_OLLAMA_TIMEOUT` | `10.0` | Embedding timeout in seconds |
| `KB_OLLAMA_KEEP_ALIVE` | `30m` | Per-request Ollama `keep_alive` sent with every embed call, so only the embedding model stays warm in VRAM |
| **Bedrock (AWS-managed Claude)** | | |
| `AWS_BEARER_TOKEN_BEDROCK` | _(unset)_ | Bearer token for Bedrock auth (preferred method) |
| `KB_BEDROCK_MODEL` | `us.anthropic.claude-haiku-5-5` | Bedrock model ID for extraction and query (cross-region inference profile) |
| `KB_BEDROCK_SYNTHESIS_MODEL` | `us.anthropic.claude-sonnet-5-5` | Bedrock model ID for synthesis (cited answers, chat) |
| `KB_BEDROCK_REGION` | `us-east-1` | AWS region for Bedrock |
| `KB_BEDROCK_TIMEOUT` | `60.0` | Request timeout in seconds |
| **Aurora/RDS IAM auth** | | |
| `KB_PG_IAM_AUTH` | _(unset)_ | Set `TRUE` for RDS/Aurora IAM authentication |
| `KB_PG_REGION` | `us-east-1` | AWS region for RDS IAM token signing |
| **Provider selection** | | |
| `KB_EXTRACTION_PROVIDER` | `anthropic` | LLM for graph enrichment (`anthropic`, `bedrock`, or `ollama`) |
| `KB_QUERY_PROVIDER` | `anthropic` | LLM for query planning and synthesis (`anthropic`, `bedrock`, or `ollama`); also selects the surprise capture provider when `KB_SURPRISE_PROVIDER` is unset (anthropic or bedrock only) |

> **Legacy SigV4 auth:** Bedrock also supports `AWS_ACCESS_KEY_ID` / `AWS_SECRET_ACCESS_KEY` for traditional IAM credentials. Bearer token auth (`AWS_BEARER_TOKEN_BEDROCK`) is the preferred method.

## Provider Architecture

The server uses two independent LLM slots, each configurable to use Anthropic (direct API), Bedrock (AWS-managed Claude), or Ollama (local):

- **Extraction LLM** (`KB_EXTRACTION_PROVIDER`) — Enriches the knowledge graph by extracting entities and relationships from stored entries.
- **Query LLM** (`KB_QUERY_PROVIDER`) — Plans graph queries from natural language questions and synthesizes answers in `kb_summarize`.

Both default to `anthropic`. You can mix providers (e.g., `bedrock` for extraction, `ollama` for queries). Vector embeddings always use Ollama and are independent of the provider settings.

## Team Use

Multiple people (or agents) can share a single knowledge base. Each contributor runs their own MCP server instance pointed at the same database, with `KB_CONTRIBUTOR` identifying who they are.

### Setup

Each team member sets their identity via environment variables in their MCP config:

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[postgres]", "personal-kb"],
      "env": {
        "KB_DATABASE_URL": "postgresql://user:pass@shared-host/team_kb",
        "KB_CONTRIBUTOR": "jason",
        "KB_TEAM": "platform",
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}
```

### What you get

- **Attribution** — Every entry, version, and search event records who created it. Search results show `@contributor/team` badges so you can see who wrote what.
- **Filtering** — `kb_search` accepts `contributor` and `team` parameters to scope results to a specific person or team.
- **Audit trail** — All mutations (create, update, deactivate, reactivate) are logged in the `audit_events` table. Use `kb_maintain list_audit` to review recent changes.
- **Sensitivity classification** — Entries can be tagged `internal`, `restricted`, or `public` via the `sensitivity` parameter on `kb_store`. Badges appear in output to signal handling expectations.
- **Secret scanning** — `kb_store` and `kb_store_batch` scan content for potential secrets (API keys, passwords) before storing. Override with `KB_SKIP_SAFETY=TRUE`.
- **Contributor stats** — `kb_maintain list_contributors` shows who has contributed what.

### Write policy: interactive, headless and autonomous surfaces

The hosted kb-service (`/mcp` and the REST API) decides what a write may do from the surface it comes from: `interactive` (a person is in the loop), `headless` (an unattended agent run, such as a dispatched build) or `autonomous` (an agent acting on its own initiative).

A request's surface comes from its API key's surface when the key has one. A password (JWT) login is always interactive. Otherwise (a key with no surface, or a no-auth local install) it is `KB_WRITE_POLICY_DEFAULT_SURFACE`, which defaults to `interactive`; an unknown value means headless.

The `X-KB-Mode` header (`interactive`, `headless` or `autonomous`) can only downgrade that trust, never raise it. An unknown value counts as headless.

An interactive surface writes exactly as before. From a headless or autonomous surface, `kb_store` and `kb_store_batch` creates are not written. Each is queued as a candidate for the surprise-capture pipeline, and the tool answers `Queued as candidate N ...`. Updates, deactivations, reactivations, `bulk_update` with `dry_run=false` and non-dry-run ingests are refused with a `write policy: ...` 403. Creates that supersede an entry, set `distinct_from`, are `mental_map` entries or have no `project_ref` are refused the same way. Over `/mcp`, such a caller also sees only the read tools, `kb_store`, `kb_store_batch` and `kb_feedback`; every other tool is hidden.

An API key minted by a headless or autonomous caller inherits that surface, so an unattended agent cannot mint itself a trusted key. Key surfaces are set only by an admin, with `kb-service list-keys` and `kb-service set-key-surface --key-id <id> --surface interactive|headless|autonomous|default`.

What happens to a queued create depends on `KB_SURPRISE_CAPTURE`. With `on`, the distiller and critic review it, and it may be written. With `shadow`, it gets a dry run only and nothing is written. With `off`, it is recorded for audit only. A written entry carries the `write-policy` tag and `surface:<surface>`, has confidence at most 0.7, and expires after the lesson TTL (`KB_SURPRISE_LESSON_TTL_DAYS`, 30 days).

The stdio client sends `User-Agent: personal-kb/<version>` on every request and never sends `X-KB-Mode`: its surface comes from its API key. When `HEADLESS_BUILD_ENGINE` is set, it sends that engine string as `X-KB-Engine` and the harness name `claude-code` as `X-KB-Harness`. The write-policy log line records `harness`, `engine` and `ua` separately; an older client that still puts the engine string in `X-KB-Harness` is logged with it as the harness. For an unattended agent that connects to `/mcp` directly, add `--header "X-KB-Mode: headless"` and `--header "X-KB-Harness: <name>"` to its MCP config.

`POST /api/kb/turn` clamps a digest's `mode` to the caller's surface: a headless or autonomous caller that posts `mode: interactive` (or omits it) is recorded as `headless`, and the service logs `turn_event mode_clamped surface=<surface> from=interactive`.

Rollout order for a fail-closed instance:

1. Deploy kb-service.
2. Upgrade personal-kb on every client whose key will be non-interactive. An older client renders a queued response as `Error: 'action'` and a write-policy 403 as `admin privileges required`.
3. Verify every such client is upgraded before changing any surface: have each one make a write (a store from an interactive surface logs an `outcome=allowed` line), then check that the `write-policy` log lines for its `key_id` show `ua='personal-kb/<new version>'`. A line with an `httpx` user agent, or no line at all, means that client is not verified yet.
4. Run `kb-service list-keys`, then `kb-service set-key-surface --key-id <id> --surface interactive` for each interactive machine's key AND the machine-principal key (seed_resolutions.py updates and the nightly map maintenance run as the machine principal).
5. Only then set `KB_WRITE_POLICY_DEFAULT_SURFACE=headless`, and mark keys `headless` or `autonomous` with `kb-service set-key-surface`.
6. Set `KB_SURPRISE_CAPTURE` to `shadow` or `on`. On a SQLite KB, drain with `POST /api/kb/surprise/drain`, since the background worker needs Postgres.

### Trust model and limitations

The multi-user features provide **attribution and visibility, not access control**. Understanding the trust model is important before deploying to a team:

- **Identity is environment-based, not authenticated.** `KB_CONTRIBUTOR` is set by each MCP server instance at startup. There is no login, no tokens, no verification. Anyone with database access can set any contributor name. This is appropriate for trusted teams where members configure their own environments honestly.
- **No read restrictions.** All entries are visible to all users regardless of contributor, team, or sensitivity classification. The `sensitivity` field is a label for human judgment — it does not hide or encrypt anything. A `restricted` entry is just as readable as a `public` one.
- **Write restrictions are by surface, not by owner.** Any interactive caller can update or deactivate any entry; headless and autonomous surfaces cannot (see the write policy above). The `updated_by` field and audit trail record who did what, but nothing restricts an interactive caller to its own entries.
- **Vector search ignores contributor/team filters.** The contributor and team filters apply to full-text search only. Vector similarity search returns all entries regardless of attribution. Filtered-out entries can still appear in results via the RRF fusion step. This is a known limitation of the current search architecture.
- **Audit trail is append-only but not tamper-proof.** Audit events are stored in the same database as everything else. Anyone with database access can modify or delete them. This is sufficient for "who did what" visibility, not for compliance or forensics.
- **Last-write-wins on concurrent edits.** If two contributors update the same entry simultaneously, the last write wins. There is no locking, merge, or conflict resolution. Version history preserves both changes, but only the latest version is active.

**Bottom line:** These features work well for a small trusted team sharing a knowledge base through separate MCP server instances, each configured with their own `KB_CONTRIBUTOR`. They are not designed for untrusted multi-tenant environments where users might act adversarially.

### Personal + team (dual-instance pattern)

Most team members want both a **shared team KB** (decisions, architecture, patterns) and a **personal KB** (dotfiles, shell aliases, workflow preferences). Run two MCP server instances with different names — the agent sees both and uses the server instructions to route entries correctly.

```json
{
  "mcpServers": {
    "team-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[postgres]", "personal-kb"],
      "env": {
        "KB_DATABASE_URL": "postgresql://user:pass@shared-host/team_kb",
        "KB_CONTRIBUTOR": "jason",
        "KB_TEAM": "platform",
        "KB_INSTANCE_ROLE": "team",
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    },
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git", "personal-kb"],
      "env": {
        "KB_INSTANCE_ROLE": "personal",
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}
```

Each instance serves its own web UI, and `kb_explore` returns that instance's Graph page URL.

`KB_INSTANCE_ROLE` controls two things:

1. **Role-specific instructions** prepended to the server description:
   - **`team`** — "This is the TEAM knowledge base — shared decisions, architecture, patterns, and conventions."
   - **`personal`** — "This is your PERSONAL knowledge base — your config, dotfiles, workflow preferences, and private notes."

2. **Tool name prefixing** to avoid collisions when running two instances:
   - **`personal`** → `personal_kb_store`, `personal_kb_search`, etc.
   - **`team`** → `team_kb_store`, `team_kb_search`, etc.
   - **unset** → `kb_store`, `kb_search`, etc. (backwards-compatible default)

   This is essential for MCP clients (like Kiro) that don't namespace tools by server name — without prefixing, both instances would expose identically-named tools.

Environment variables in each `env` block are scoped to that server process — no collisions.

## Steering Your Agents

The MCP server ships with built-in instructions that teach agents *what* each tool does. But agents need additional guidance in your system prompt (e.g. `CLAUDE.md`, Cursor rules, Windsurf rules) to develop good KB habits — searching before acting, storing knowledge as they work, and not letting insights evaporate at session end.

Add something like this to your agent's system prompt:

```markdown
## Knowledge Base

A personal-kb MCP server is available. This is the primary place for
durable knowledge — not memory files or scratchpads.

### Query before you act — not after you fail
Search the KB BEFORE guessing or asking the user. Concrete triggers:

- Starting a task in an unfamiliar project → kb_search(project_ref="X")
- Debugging an error you haven't seen → kb_search the error message
- Making an architectural or library choice → kb_ask("decisions about X")
- Deployment, infrastructure, operational procedures → kb_search first

The cost of a failed kb_search is one second. The cost of not searching
is minutes of wasted fumbling.

### Capture while you still have context
Knowledge evaporates when a session ends. Store entries as you work,
not as an afterthought. If you just debugged something tricky, made a
decision, or discovered a non-obvious behavior — kb_store it immediately.

### What belongs in the KB
- Decisions and their rationale ("chose X because Y")
- Lessons learned from debugging, fixes with non-obvious root causes
- Patterns, conventions, project-specific gotchas
- Factual references: API behaviors, config values, version constraints

### What does NOT belong
- Trivial or temporary info (session state, in-progress work)
- Well-known public knowledge (stdlib docs, basic git usage)
- Duplicates — always kb_search before storing; use update_entry_id if
  an entry already exists

### Good practices
- Use tags for discoverability and project_ref for scoping
- supersedes is a required kb_store parameter: the kb ids this entry
  replaces, or "none".
- A lesson_learned needs resolution: {"wrong_belief": "...",
  "corrected_fact": "..."} (add a Bash cue only for one specific
  command), or "none" when no belief was corrected.
- Use hints to build the knowledge graph: {"tool": "sqlite"},
  {"person": "jason"}
- Use kb_store_batch when capturing multiple related entries
- When a previous entry is wrong or outdated, update or deactivate it
```

The server instructions handle tool mechanics (which tool to use when, parameter formats, entry types). The system prompt guidance above handles *behavior* — when to search, when to store, and what's worth keeping. Both layers together produce agents that actively use the KB without being told.

## Development

```bash
git clone https://github.com/jason-weddington/personal-kb-mcp.git
cd personal-kb-mcp
uv sync

uv run pytest                    # run tests
uv run ruff check src/ tests/    # lint
uv run personal-kb               # run server directly
uv run kb-service serve          # run kb-service (API and web UI)
```

For Bedrock support: `uv sync --extra aws`. For secret/PII detection in `kb_ingest`: `uv sync --extra safety`. For PostgreSQL: `uv sync --extra postgres`.

## So you started with SQLite...

SQLite is the default and it works great — most users will never need to change. But if your KB has grown large, you're running the server on a shared machine, or you just prefer Postgres, switching is straightforward.

### What changes

| | SQLite | PostgreSQL |
|---|---|---|
| **Full-text search** | FTS5 with BM25 | tsvector + GIN with ts_rank_cd |
| **Vector search** | sqlite-vec (vec0) | pgvector |
| **JSON queries** | `json_extract()` | `->>` operator |
| **Concurrency** | WAL mode (single-writer) | Full MVCC |
| **Setup** | Zero — it's a file | Postgres + pgvector extension |

Everything else — entries, graph, versions, ingested files — works identically. The same MCP tools, the same entry format, the same search results.

### Prerequisites

A running PostgreSQL 15+ instance with the [pgvector](https://github.com/pgvector/pgvector) extension:

```sql
CREATE EXTENSION IF NOT EXISTS vector;
```

And the `asyncpg` optional dependency:

```bash
# If running from a clone:
uv sync --extra postgres

# If using uvx, add the extra:
uvx --from "git+https://github.com/jason-weddington/personal-kb-mcp.git[postgres]" personal-kb
```

### Migrate your data

```bash
# Preview what will be migrated (read-only):
uv run python scripts/migrate_sqlite_to_pg.py --dry-run \
  ~/.local/share/personal_kb/knowledge.db

# Run the migration:
uv run python scripts/migrate_sqlite_to_pg.py \
  ~/.local/share/personal_kb/knowledge.db

# With attribution (stamps your name on migrated entries):
uv run python scripts/migrate_sqlite_to_pg.py \
  --contributor jason --team platform \
  ~/.local/share/personal_kb/knowledge.db
```

The target Postgres connection comes from environment variables — set `KB_DATABASE_URL` before running. For Aurora IAM auth, also set `KB_PG_IAM_AUTH=TRUE` and `KB_PG_REGION`.

The script copies all data tables, then rebuilds embeddings via Ollama automatically. In merge mode (target already has entries), source IDs are remapped to avoid collisions. Use `--skip-embeddings` to defer the re-embed step. See [docs/team_setup_aws.md](docs/team_setup_aws.md) for detailed reference.

### Switch your MCP config

Update your MCP client config to set `KB_DATABASE_URL`:

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[postgres]", "personal-kb"],
      "env": {
        "KB_DATABASE_URL": "postgresql://user:pass@localhost/my_kb",
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}
```

When `KB_DATABASE_URL` is set, the server uses PostgreSQL. When it's not set, it uses SQLite (the `KB_DB_PATH` file). You can switch back and forth — both backends are always available.

### Aurora Serverless with IAM auth

For AWS deployments where database passwords aren't acceptable, the server supports RDS/Aurora IAM authentication. Instead of a static password in the connection string, the server generates short-lived SigV4-signed tokens that are refreshed automatically on each new connection.

```json
{
  "mcpServers": {
    "personal-kb": {
      "type": "stdio",
      "command": "uvx",
      "args": ["--from", "git+https://github.com/jason-weddington/personal-kb-mcp.git[postgres,iam]", "personal-kb"],
      "env": {
        "KB_DATABASE_URL": "postgresql://myuser@aurora-cluster.cluster-xxx.us-east-1.rds.amazonaws.com:5432/my_kb",
        "KB_PG_IAM_AUTH": "TRUE",
        "KB_PG_REGION": "us-east-1",
        "ANTHROPIC_API_KEY": "sk-ant-..."
      }
    }
  }
}
```

Requirements:
- The `iam` optional dependency (`boto3`) — included via `personal-kb-mcp[iam]`
- AWS credentials available through the standard chain (environment variables, `~/.aws/credentials`, instance roles)
- The database user must have IAM authentication enabled in RDS/Aurora
- The connection URL should **not** include a password — the token factory provides it

The server uses `boto3.client('rds').generate_db_auth_token()` to sign tokens locally (no network call) and passes them to asyncpg's connection pool as a callable password, so tokens are refreshed on every new connection. SSL is enabled automatically — IAM auth requires TLS.

### If embeddings were skipped

Embeddings can't be copied between backends (sqlite-vec uses packed binary, pgvector uses native arrays), so the migration script re-embeds via Ollama. If Ollama wasn't running during migration, or you used `--skip-embeddings`, rebuild them manually:

```
kb_maintain rebuild_embeddings (force=True)
```

The KB works immediately without embeddings — you just won't get vector search results until the rebuild finishes. FTS and graph search work from the start.

### Keeping SQLite as a backup

The migration is additive — it doesn't modify your SQLite database. Your original file at `~/.local/share/personal_kb/knowledge.db` stays intact. To fall back, just remove `KB_DATABASE_URL` from your config.