How context-hub/generator works
Project type and purpose
• This is an open-source, PHP-based CLI tool (“ctx”) that also embeds a small HTTP/MCP server.
• Purpose: automatically gather and assemble context (code, diffs, docs, web pages, MCP items) into markdown files or serve them via an MCP API to LLMs.
Top-level architectural components
A. CLI Front-end
• app.php, Console/*Command.php
• Entrypoint for commands: ctx init, ctx (generate), ctx server, ctx self-update, etc.
B. Core Kernel & Bootloaders
• src/Application/Kernel.php, Application/Bootloader/*
• Microkernel pattern: each Bootloader registers services into the service container (config loader, HTTP client, source fetchers, modifier registry, compiler, logger).
C. Configuration subsystem
• src/Config (loader, parser, reader, importer, registry)
• Loads and validates context.yaml using JsonSchema, merges nested imports, applies exclusions, variables.
D. Source Fetchers & Registry
• src/Source and src/Source/Registry
• A registry of factories for file, GitHub, GitLab, GitDiff, URL, MCP sources.
E. Modifiers & Content Builders
• src/Modifier/*, src/Lib/Content/*
• Apply transformations (strip bodies, sanitize, extract PHP signatures), assemble content blocks, render to Markdown.
F. Document Compiler
• src/Document/Compiler/DocumentCompiler.php
• Coordinates sources + modifiers + content builders to produce CompiledDocument instances.
G. Logging
• src/Application/Logger/*
• Pluggable console or file loggers with levels and formatters.
H. MCP Server (optional)
• src/McpServer/ServerRunner.php, Routing/*, Action/*
• Bootloader for server; PSR-7 adapter; attribute-based routing to Action handlers for prompts, tools, resources, projects.
I. External integrations
• src/Lib/GithubClient, src/Lib/GitlabClient, src/Lib/HttpClient, src/Lib/ComposerClient
• Strategy and client classes to reach external APIs or local composer.
Relationships and data flow
• CLI invocation → Kernel loads Bootloaders → Service container ready → ConsoleDispatcher routes to a Command →
Config subsystem loads context.yaml → Source registry fetches content from FS, Git, Web, MCP → Modifiers applied → ContentBuilder/Renders → Markdown file output or stream to stdout.
• Server mode: CLI “ctx server” → Kernel loads MCPServerBootloader → ServerRunner starts HTTP server → Incoming request → PsrRequestAdapter → RouteRegistrar matches attribute → ActionCaller invokes Action → uses Source/Prompt/Tool registries → produces JSON response → Psr7ResponseStrategy writes HTTP response.
Architectural patterns and principles
• Microkernel with Bootloader plug-in pattern for modularity.
• Dependency Injection & Service Container to decouple modules.
• Composite & Strategy patterns for config loaders, parsers, renderers, modifiers, update strategies.
• Attribute-driven routing in the MCP server.
• Clear separation of concerns: Configuration, Sourcing, Modification, Compilation, Rendering, Delivery (CLI vs. HTTP).
Key technologies and frameworks
• PHP 8+, Composer for autoloading and dependencies.
• JSON Schema validation (json-schema.json) and YAML/JSON readers.
• PSR-18 HTTP client, PSR-7 adapters.
• Custom console dispatcher (similar to Symfony Console).
• PHPUnit, Psalm, Rector for testing and static analysis.