How brocodeadi/contextforge. works
Purpose and Entry Points
ContextForge is a local Python context-compilation engine for cybersecurity skill corpora. It converts a natural-language task (optionally ambient desktop state) into a budget-constrained JSON context package. Primary entry points are the Typer CLI in context_compiler/main.py, desktop UI in context_compiler/app.py, tray launcher run_tray_app.py, and MCP stdio server context_compiler/mcp_server.py.
Corpus Ingestion and Persistent Data
Markdown skills are parsed into structured records and stored locally in SQLite; indexing then creates lexical and dense-search artifacts. The repository includes a small sample corpus and persisted Chroma data under sample_skills/; production artifacts are expected to be generated externally from a larger corpus.
• context_compiler/ingest.py
• context_compiler/lexical_index.py
• context_compiler/semantic_index.py
• sample_skills/
Hybrid Retrieval
Query handling combines BM25 lexical search, dense embedding cosine similarity, domain metadata, MITRE/framework matches, and a history/recency signal. hybrid_retrieval.py is the orchestration boundary over SQLite metadata plus lexical and semantic indexes, returning ranked candidates with score breakdowns.
• context_compiler/hybrid_retrieval.py
• context_compiler/lexical_index.py
• context_compiler/semantic_index.py
Budgeting and Compression Pipeline
Ranked skills flow through a greedy three-tier allocator: retain full content, mark viable overflow for compression, or drop it. The compressor first attempts local Ollama model synthesis and falls back to section-aware truncation, preserving an offline path. The CLI’s compile command coordinates retrieval, allocation, compression, and JSON assembly.
• context_compiler/token_budget.py
• context_compiler/compress.py
• context_compiler/main.py
Ambient Context and Event-Driven Use
Windows desktop context can supplement a query through foreground-window and clipboard capture. A polling watcher detects clipboard changes; hotkey, popup, and tray layers invoke compilation and present results without requiring the main GUI. This boundary is platform-sensitive because collection uses Win32 capabilities.
• context_compiler/collector.py
• context_compiler/watcher.py
• context_compiler/tray_app.py
• context_compiler/hotkey_handler.py
Delivery Surfaces
The same compiler capability is exposed through CLI, a CustomTkinter desktop application, and MCP tools/resources for clients such as Claude Desktop or Cursor. MCP is a stdio JSON-RPC integration rather than an HTTP service; it supports search, detail lookup, compilation, ambient collection, catalog, and domains.
• context_compiler/main.py
• context_compiler/app.py
• context_compiler/mcp_server.py
Runtime, Infrastructure, and External Boundaries
The runtime is Python 3.10+ with local SQLite, serialized BM25 data, NumPy embedding matrices, and tiktoken-style token accounting. Dense embeddings use the local all-MiniLM-L6-v2 model; optional Ollama (documented default qwen3:8b) is the only model-serving dependency. The system is designed for no external API calls; MCP clients and Ollama are local-process/service boundaries. Evaluation measures retrieval and token-budget compliance.
• requirements.txt
• context_compiler/evaluate.py
• benchmark_results.json
Main entry points: hybrid_retrieval.py Hybrid retrieval · main.py Typer CLI · token_budget.py Three-tier allocator · ingest.py Markdown ingestion
Components 19
Delivery surfaces
- Typer CLI · Compile command coordinates the context package flow.
context_compiler/main.py - Desktop app
context_compiler/app.py - MCP stdio server · Exposes search, lookup, compilation, ambient, catalog, and domain tools.
context_compiler/mcp_server.py
Windows ambient context
Platform-sensitive desktop collection and triggers.
- Tray launcher
run_tray_app.py - Tray and popup
context_compiler/tray_app.py - Hotkey handler
context_compiler/hotkey_handler.py - Clipboard watcher
context_compiler/watcher.py - Ambient collector · Captures foreground-window and clipboard state.
context_compiler/collector.py
Compilation pipeline
- Hybrid retrieval · Combines lexical, dense, metadata, framework, and recency signals.
context_compiler/hybrid_retrieval.py - Three-tier allocator · Retains, compresses, or drops ranked skills.
context_compiler/token_budget.py - Compressor · Uses Ollama synthesis with section-aware truncation fallback.
context_compiler/compress.py
Local corpus and indexes
- Markdown ingestion · Parses skills into structured local records.
context_compiler/ingest.py - SQLite metadata · Stores structured skill records and metadata.
- BM25 index · Serialized local lexical-search artifact.
context_compiler/lexical_index.py - Dense index · Local embedding matrix and dense-search artifacts.
context_compiler/semantic_index.py - Skill corpus and artifacts · Sample skills and persisted Chroma data.
Local dependencies
- Ollama · Optional local synthesis model service.
- MCP clients · Clients such as Claude Desktop or Cursor.
Other components
- Evaluation
context_compiler/evaluate.py
Connections 24
- MCP clients → MCP stdio server: stdio JSON-RPC
- Typer CLI → Hybrid retrieval: compile query
- Typer CLI → Three-tier allocator: coordinate compile
- Typer CLI → Compressor: assemble JSON package
- Desktop app → Typer CLI: invoke compiler
- MCP stdio server → Hybrid retrieval: search and lookup
- MCP stdio server → Three-tier allocator: compile tool
- Tray launcher → Tray and popup: start
- Clipboard watcher → Ambient collector: clipboard changes
- Hotkey handler → Typer CLI: trigger compilation
- Tray and popup → Typer CLI: trigger and present
- Ambient collector → Typer CLI: ambient query context
- Hybrid retrieval → SQLite metadata: metadata and history
- Hybrid retrieval → BM25 index: BM25 scores
- Hybrid retrieval → Dense index: dense similarity
- Hybrid retrieval → Three-tier allocator: ranked candidates
- Three-tier allocator → Compressor: overflow skills
- Compressor → Ollama: optional synthesis
- Markdown ingestion → SQLite metadata: structured records
- Markdown ingestion → BM25 index: build index
- Markdown ingestion → Dense index: build embeddings
- Skill corpus and artifacts → Markdown ingestion: source skills
- Evaluation → Hybrid retrieval: measure retrieval
- Evaluation → Three-tier allocator: check token compliance