Skip to main content
Glama
Cortex Memory Engine

Cortex Memory Engine

by Bunk-beds

Cortex Memory Engine

Cortex is a self-hosted memory and pre-flight safety server for coding agents. It exposes a FastAPI REST API alongside Model Context Protocol (MCP) endpoints so agents can search past fixes, store new notes, and check risky shell commands before running them.

Why I built it this way

Coding agents start every session from scratch. They forget environment quirks, repeat container flag bugs, and can run destructive commands if nothing sits between the model and the shell. I built Cortex to fix those specific problems without turning my homelab into a fragile stack of vector databases.

Plain Markdown on disk, disposable SQLite in front

Every memory, architecture note, and troubleshooting entry lives as a plain Markdown file with YAML frontmatter (cortex/vault.py). The SQLite database (notes_meta and notes_fts) is strictly a rebuildable cache. If the index breaks or needs a schema change, wipe_index() drops it and rebuilds from the .md files on disk. A watchfiles loop (cortex/watcher.py) watches the vault directory so edits made by hand in a text editor or Obsidian show up in search automatically. Nothing is locked inside a proprietary store.

FTS5 first, Ollama embeddings optional

Exact error strings, CLI flags, and port numbers are where pure vector search tends to miss. Cortex indexes every note in SQLite FTS5 using the porter unicode61 tokenizer in WAL mode (cortex/search.py), so keyword lookups stay fast and need zero GPU memory. When an Ollama server is available, Cortex computes dense embeddings (cortex/embeddings.py) and merges them with BM25 scores through Reciprocal Rank Fusion (RRF). If Ollama is offline or turned off (CORTEX_ENABLE_VECTORS=false), search falls back to FTS5 instead of failing.

Static regex guardrails instead of an LLM judge

Asking another language model whether a shell command is dangerous adds latency and still leaves room for prompt drift. HazardInterceptor (cortex/interceptor.py) runs static regex checks timed with perf_counter_ns, finishing in under a millisecond. It blocks destructive patterns like rm -rf /, rm -rf ~, git push --force main, piped curl | bash, and pct/qm destroy, and returns warnings for subtle traps such as git reset --hard or docker run --gpus all missing --runtime=nvidia on cgroup v2.

Superseding old notes instead of deleting them

Configs change, and stale troubleshooting notes make agents repeat outdated workarounds. Calling /api/supersede marks the older note with status: superseded and a superseded_by reference, then down-ranks it during retrieval. Newer decisions surface first while the old history stays readable on disk.

Related MCP server: hexis

Quickstart (Docker)

cp .env.example .env
docker compose up -d --build

Check that the server is up:

curl http://localhost:8766/health

Markdown notes persist under ./data/vault and the SQLite index under ./data/cache.

Local development

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

export CORTEX_VAULT_PATH=./data/vault
export CORTEX_CACHE_PATH=./data/cache/index.db
export CORTEX_ENABLE_VECTORS=false

uvicorn cortex.server:app --host 0.0.0.0 --port 8766

Running tests

pip install pytest
python -m pytest -v

API and MCP endpoints

Endpoint

Method

Purpose

/health

GET

Service status, indexed note count, and Ollama availability

/api/search

POST

Hybrid BM25 and vector search with optional type filtering

/api/read

GET

Read a Markdown note by relative path

/api/store

POST

Write a new memory or troubleshooting note to the vault

/api/supersede

POST

Mark an older note as superseded by a newer one

/api/check

POST

Run a pre-flight hazard check on a shell command

/sse / /mcp

GET/POST

Model Context Protocol endpoints for coding agents

Configuration

See .env.example for all environment variables (CORTEX_OLLAMA_URL, CORTEX_ENABLE_VECTORS, CORTEX_EMBEDDING_MODEL, CORTEX_VAULT_PATH, CORTEX_CACHE_PATH, CORTEX_PORT).

License

MIT

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

Loading tool schema changelog…

    Related MCP Connectors

    Related MCP Servers

    • A
      license
      A
      quality
      B
      maintenance
      Enables coding agents to store, search, and retrieve long-term memory as plain Markdown files with a disposable SQLite index, including note management, decision/bug tracking, and codebase symbol lookup via MCP.
      8
      MIT
    • A
      license
      Not graded
      quality
      B
      maintenance
      Enables coding agents to retrieve and verify semantic memory from markdown notes via MCP, with automatic indexing and self-checking for reliability.
      1
      Apache 2.0
    • A
      license
      Not graded
      quality
      B
      maintenance
      Gives coding agents a persistent memory by searching a local, read-only index of existing markdown and letting them write findings back as notes that the next session can retrieve. It serves ranked, context-sized results over MCP, injects standing conventions at session start, and keeps data on your machine with secret redaction and archive-only deletion.
      MIT
    • A
      license
      A
      quality
      B
      maintenance
      Enables coding agents to save, search, and recall Markdown notes as persistent memory over MCP using a local index. Exposes remember, search, and recall tools so agents can reuse prior decisions and context across sessions.
      8
      428 npm
      58
      Apache 2.0