Virtual Infinite Context for Agents and LLMs
An MCP server that maintains a continuous rolling context window, always surfacing the most relevant memories for the task at hand — while respecting your token budget.
This repository includes an MVP MCP stdio server suitable for integration with the Cline extension for VS Code. It exposes a minimal, SQLite-only toolset over stdio with zero external services required by default.
- Version: 0.1.1
- Transport: MCP stdio
- Default storage: SQLite at ./data/memory.db (configurable; example config uses ./data/novel_memory.db)
- Optional backends: Qdrant / Neo4j (disabled by default in this MVP)
- Tools implemented:
- memory_upsert
- search_memory
- context_pack
- health_ping
Notes:
- Tool names use underscores for compatibility with the Python server decorator. Dotted aliases (e.g., context.pack) can be added later.
- All tools return a standard JSON envelope as text content: { "ok": boolean, "data": {...}, "meta": { "duration_ms": number, "backend": "sqlite", "version": "0.1.1" } }
Requires Python 3.11+
This creates ./data/memory.db if it doesn’t exist (or the path specified by config/env).
This starts the MCP server over stdio.
Add an MCP server named cont3xt that launches the stdio server:
Example configuration shape (adapt for your Cline settings UI/JSON):
Notes:
- Ensure virtual-context-mcp is in PATH (pip install -e . creates the console script).
- The --config argument is optional; environment variables can override values (see Configuration).
All tools return a JSON envelope as text content.
Envelope:
Upsert memory for a session from either combined content or a user/assistant pair.
Arguments:
- session_id: string (required)
- content: string (optional; direct content)
- user_input: string (optional; used if content not provided)
- assistant_response: string (optional; used if content not provided)
Response (example):
Simple substring search with a recency-biased score over recent SQLite chunks.
Arguments:
- session_id: string (required)
- query: string (required)
- max_results: number (optional, default 10)
Response (example):
Packs a token-budgeted context using recent conversation chunks. Reserves a small buffer and includes the current input if provided.
Arguments:
- session_id: string (required)
- current_input: string (optional, default "")
- budget_tokens: number (optional, defaults to CONTEXT_MAX_TOKENS)
Response (example):
Notes:
- If nothing fits within the budget (after a fixed buffer of 500 tokens), the pack includes: "reason": "BUDGET_TOO_SMALL".
- Truncation policy: respect budget strictly; sections are assembled most-recent-first, then presented chronologically for readability.
Returns basic status and configuration fingerprint.
Arguments: none
Response (example):
- In Cline, run a task like “Improve function X in file Y; add a unit test and make tests pass.”
- Cline should:
- Call search_memory(session_id, query="project goals")
- Call context_pack(session_id, current_input="<file diff request>", budget_tokens=12000)
- Generate plan → apply small diff → run tests
- Call memory_upsert with (user_input, assistant_response) to persist the session outcome
- Acceptance:
- From a clean clone, pip install -e ., launch Cline, and complete a guided code edit PR in one go using the above flow.
Defaults are provided in code, optionally loaded from a YAML file (e.g., configs/novel_writing.yaml) and overridden by environment variables.
Environment variable overrides (examples):
-
CONTEXT_MAX_TOKENS (default 12000)
-
CONTEXT_PRESSURE_THRESHOLD (default 0.8)
-
CONTEXT_RELIEF_PERCENTAGE (default 0.4)
-
CONTEXT_CHUNK_SIZE (default 3200)
-
CONTEXT_TOKEN_MODEL (default cl100k_base)
-
DATABASE_SQLITE_PATH (default ./data/memory.db)
-
DATABASE_QDRANT_URL
-
DATABASE_QDRANT_COLLECTION
-
DATABASE_NEO4J_URL
-
DATABASE_NEO4J_USER
-
DATABASE_NEO4J_PASSWORD
For the MVP, only SQLite is used. Qdrant / Neo4j are intentionally not required; future versions can enable them via env/config flags.
Truncation policy:
- A fixed buffer of 500 tokens is reserved.
- Packing order: include current_input (if provided) and then pack recent conversation chunks until the budget is met.
- If no sections can be included, the pack includes "reason": "BUDGET_TOO_SMALL".
Entry points:
- Console script: virtual-context-mcp → virtual_context_mcp.main:main
- MCP server class: virtual_context_mcp.server:VirtualContextMCPServer
Local logs: standard output (set PYTHONUNBUFFERED=1 for real-time logs). SQLite file: ./data/memory.db by default.
Run unit/integration tests as needed (note that legacy story-focused modules may not reflect the MVP surface area).
- MVP runs in a safe default mode: only persists to local SQLite.
- No filesystem ingestion or external network writes by default.
- Optional Qdrant/Neo4j integration behind env flags with graceful fallback.
- Aliased/dotted tool names for compatibility (e.g., context.pack).
- Richer retrieval (hybrid, deduplication), entity/graph memory, and pressure relief policies.
- DevContainer / Docker image for reproducible environments.
MIT © elevend0g
.png)
