Cortex-MCP
Provides LangGraph agents with cross-agent persistent memory and context compilation, enabling bi-temporal fact tracking and stale-context suppression across sessions.
This server does not currently have a deployable release on Glama. Follow the repository's setup instructions to install and configure it for your MCP client, including any required credentials and local dependencies.
Cortex-MCP (cortex-mcp)
Local-First Bi-Temporal Knowledge Graph, Cross-Agent Persistent Memory & Token-Budgeted Context Compiler MCP Server for AI Coding Agents.
Why cortex-mcp Exists
Modern AI coding agents (Claude Code, Cursor, Windsurf, Antigravity, LangGraph) suffer from two compounding failure modes across multi-session engineering workflows:
Cross-Session & Cross-Agent Context Amnesia: Architectural decisions (ADRs), subtle bug root causes, and module invariants discovered in one session vanish when a developer switches sessions or tools.
Stale Context Bloat: Static
.cursorrules,CLAUDE.md, or append-only vector stores accumulate contradictory facts as systems evolve (e.g., an old note sayingauth_serviceusesJWT_HS256alongside a newer note saying it migrated toPASETO_v4). Stuffing both into the prompt wastes tokens and causes hallucinated regressions.
cortex-mcp solves both problems deterministically with zero external API keys and zero cloud dependencies:
Bi-Temporal Knowledge Graph (
BiTemporalMemoryGraph): TracksEntities,TemporalFacts, andEpisodeswith automatic conflict detection and temporal supersession (valid_from,valid_until,superseded_by). When an architectural fact changes, the old fact is retired into historical lineage rather than polluting active context.Hybrid BM25 + 2-Hop Graph Spreading Activation (
ContextCompiler): Ranks active facts, ADRs, bug root causes, and file invariants using Okapi BM25 lexical scoring, exact code-identifier boosting, and K-hop graph activation, then packs the highest-utility items into a strictmax_tokensbudget with100%stale-fact suppression.Standards-Compliant JSON-RPC 2.0 MCP Server (
CortexMCPServer): Exposes tools, resources, and prompts overstdioor in-memory transport for immediate integration with any MCP client.
Related MCP server: Hebbrix MCP Server
System Architecture
+-----------------------------------------------------------------------------------+
| AI Coding Agents (Claude Code / Cursor / Windsurf / AGY) |
+-----------------------------------------+-----------------------------------------+
|
JSON-RPC 2.0 over stdio / in-memory
(initialize, tools/call, resources/read, prompts/get)
|
v
+-----------------------------------------------------------------------------------+
| CortexMCPServer (server.py) |
| Tools: |
| - memory_record_episode - memory_query_entity |
| - memory_upsert_fact - memory_time_travel |
| - memory_compile_context - memory_verify_integrity |
+-------------------+-------------------------------------------+-------------------+
| |
v v
+---------------------------------------+ +---------------------------------------+
| BiTemporalMemoryGraph (graph.py) | | ContextCompiler (compiler.py) |
| | | |
| * Entities (service, module, ...) | | * Code-Aware Tokenizer (snake/Camel) |
| * TemporalFacts (valid_from/until, | | * Okapi BM25 Lexical Scoring |
| singular conflict supersession) | | * 2-Hop Graph Spreading Activation |
| * Episodes (ADR, bug_root_cause, | | * Stale-Fact Filter (valid_until=None)|
| invariant, migration, incident) | | * Greedy Utility-Density Knapsack |
| * Point-in-Time Snapshot & K-Hop BFS | | Packing under strict `max_tokens` |
| * SHA-256 Hashes + Graph Merkle Root | | * Provenance Citations (@sha256:...) |
+-------------------+-------------------+ +---------------------------------------+
|
v
+---------------------------------------+
| Atomic JSON Snapshot Persistence |
| (~/.cortex-mcp/memory.json) |
+---------------------------------------+Quickstart
1. Installation
git clone https://github.com/codesphere-u/cortex-mcp.git
cd cortex-mcp
pip install -e ".[dev]"2. Run the Multi-Session Engineering Demo
python -m cortex_mcp.cli demoSample output demonstrating automatic supersession of JWT_HS256 by PASETO_v4_public, point-in-time historical inspection, and token-budgeted context compilation:
==============================================================================
CORTEX-MCP: BI-TEMPORAL KNOWLEDGE GRAPH & CONTEXT COMPILER DEMO
==============================================================================
Entities tracked : 3
Active temporal facts : 6
Superseded stale facts: 2 (automatically retired)
Engineering episodes : 4
SHA-256 Merkle Digest : e893bf67424f995283813e265a227224...
------------------------------------------------------------------------------
[Time-Travel Inspection @ 2026-02-01T00:00:00Z (Pre-PASETO Migration)]
* auth_service --(uses_token_format)--> JWT_HS256
* auth_service --(signing_primitive)--> HMAC_SHA256_SHARED_SECRET
------------------------------------------------------------------------------
[Compiled Active Context | Budget: 307/320 tokens (95.9%) | Suppressed Stale Facts: 2]
## Cortex-MCP Compiled Context (query: `auth_service token format signing_primitive and payment_ledger idempotency invariant`)
- [FACT:fact_auth_key_v2] `auth_service` --(signing_primitive)--> `Ed25519_Asymmetric_Keypair` (valid_from=2026-04-20T16:45:00+00:00, src=ep_adr_019)
- [FACT:fact_ledger_auth_dep] `payment_ledger` --(verifies_caller_via)--> `auth_service` (valid_from=2026-03-01T11:15:00+00:00, src=ep_inv_007)
- [INVARIANT:ep_inv_007] **INV-007: Payment Ledger Idempotency & Zero-Sum Double Entry** | files: src/payments/ledger.py
Every mutation in payment_ledger MUST supply a deterministic idempotency_key and balance debit/credit postings to zero in a single SERIALIZABLE transaction.
- [FACT:fact_auth_token_v2] `auth_service` --(uses_token_format)--> `PASETO_v4_public` (valid_from=2026-04-20T16:45:00+00:00, src=ep_adr_019)
- [FACT:fact_ledger_isolation] `payment_ledger` --(requires_isolation_level)--> `SERIALIZABLE_WITH_IDEMPOTENCY_KEY` (valid_from=2026-03-01T11:15:00+00:00, src=ep_inv_007)
- [FACT:fact_order_depends_ledger] `order_repository` --(depends_on)--> `payment_ledger` (valid_from=2026-02-14T14:30:00+00:00, src=ep_bug_042)
==============================================================================Connect to Claude Code / Cursor / Windsurf / Antigravity (mcp_config.json)
Add cortex-mcp to your agent's mcp_config.json (or .cursor/mcp.json / claude_desktop_config.json):
{
"mcpServers": {
"cortex-mcp": {
"command": "python3",
"args": [
"-m",
"cortex_mcp.cli",
"serve-stdio",
"--storage",
".cortex/memory_graph.json"
]
}
}
}Exposed MCP Tools (tools/list)
Tool Name | Purpose |
| Record an immutable engineering episode ( |
| Record a directed |
| Compile a provenance-cited, token-budgeted context bundle ( |
| Inspect an entity's active facts, superseded historical facts, related episodes, and K-hop ego-graph neighborhood. |
| Query the entire knowledge graph as of any historical ISO-8601 UTC timestamp ( |
| Verify canonical SHA-256 hashes across all facts and episodes and compute the graph Merkle root digest. |
Exposed MCP Resources & Prompts
Resources (
resources/list,resources/read):cortex://graph/summary: Entity, active fact, superseded fact, and episode counts + Merkle digest.cortex://graph/integrity: Full cryptographic verification report.cortex://episodes/recent: The 20 most recent engineering episodes.
Prompts (
prompts/list,prompts/get):architecture-briefing: Compiles a verified architectural briefing for a targetcomponent.bug-investigation-context: Compiles active invariants and historical root causes for a bugsymptom.
Reproducible Benchmarks
Run the built-in benchmark suite (500 temporal facts, 150 bi-temporal supersessions, 100 episodes, 60 entities):
python -m cortex_mcp.cli benchmarkOperation | Samples | P50 Latency (ms) | P95 Latency (ms) |
| 100 | 0.003 | 0.005 |
| 500 | 0.003 | 0.004 |
| 60 | 0.123 | 0.185 |
| 50 | 4.879 | 5.229 |
| 1 | 1.412 | 1.412 |
Active Facts:
350| Superseded Facts:150Stale-Fact Suppression Rate:
100.0%(0leaked stale facts across 50 compilation queries)Token-Budget Compliance Rate:
100.0%(0budget overruns)Cryptographic Integrity Verified:
True
Architectural Decision Records (ADRs)
ADR-01: Bi-Temporal Supersession over Destructive Overwrite or Append-Only RAG
Context: Append-only vector stores return both old and new architectural facts when queried, confusing LLM agents. Destructive overwrites destroy auditability ("why did we stop using
JWT_HS256?").Decision: Every
TemporalFactcarriesvalid_from,valid_until, andsuperseded_by. Inserting a newsingular=Truefact for(subject_id, predicate)atomically closes the prior active fact's validity window (valid_until = new_fact.valid_from) and linkssuperseded_by = new_fact.id.Consequence: Active context compilation achieves
100%stale-fact suppression whilememory_time_travelandmemory_query_entityretain complete historical lineage.
ADR-02: Hybrid BM25 + 2-Hop Spreading Activation over Embedding-Only Retrieval
Context: Dense embeddings frequently miss exact code identifiers (
OrderRepository,SERIALIZABLE_WITH_IDEMPOTENCY_KEY) and fail to retrieve 2-hop architectural dependencies not lexically mentioned in the prompt.Decision: Combine a code-aware tokenizer (
snake_case,CamelCase, file paths) with Okapi BM25 lexical scoring, exact identifier boosting, and 2-hop graph spreading activation over active edges, followed by greedy utility-density packing undermax_tokens.Consequence: Deterministic, sub-5ms context compilation with zero external embedding API latency or cost.
Running the Test Suite
pytest -vAll 67 tests validate bi-temporal supersession chains, point-in-time queries, K-hop graph traversal, token-budget compliance, cryptographic tamper detection, JSON-RPC 2.0 MCP protocol compliance, and CLI subcommands.
License
MIT License — Copyright (c) 2026 Akandwanaho Ivan Alvin.
This server cannot be deployed
Maintenance
Related MCP Connectors
Persistent memory for AI agents, for code, research and everyday life. Stale facts retire.
Memory for AI agents that knows what is still true. Typed bi-temporal facts, EU-hosted.
Persistent memory and knowledge graphs for AI agents. Hybrid search, context checkpoints, and more.
Persistent engineering memory designed to follow developers across coding agents.
Related MCP Servers
- FlicenseNot gradedqualityDmaintenanceEnables AI assistants to build and query temporally-aware knowledge graphs from conversations and data, maintaining persistent memory of entities, relationships, and facts across interactions.-

Hebbrix MCP Serverofficial
AlicenseAqualityBmaintenanceProvides long-term memory and a temporal knowledge graph for AI agents, enabling persistent memory and reasoning across sessions.3350 PyPI2MIT- AlicenseAqualityBmaintenanceEnables AI agents to maintain a persistent knowledge graph of a project, providing dependency context, impact analysis, side-effect discovery, and session recording for more informed coding decisions.5MIT
- AlicenseNot gradedqualityCmaintenanceEnables AI agents to store and retrieve typed facts with bi-temporal validity, supersede outdated information, and query what was true as of any past date.MIT