Architecture
Technical architecture documentation for 5ire, an Electron-based AI assistant with MCP integration and local knowledge base capabilities.
High-Level Architecture
5ire is built on Electron's dual-process architecture, separating system-level operations from the user interface.
Process Model
Main Process (Node.js)
- Manages application lifecycle and native system APIs
- Handles file I/O, SQLite database operations, and document processing
- Runs MCP servers and manages their lifecycle
- Executes the embedding model for the knowledge base
- Processes IPC requests from the renderer
Renderer Process (Chromium + React)
- Runs the React/TypeScript UI application
- Manages application state with Zustand stores
- Communicates with the main process via IPC bridge
- Handles user interactions and real-time streaming responses
Preload Scripts
- Secure bridge between main and renderer processes
- Exposes controlled IPC channels to the renderer
- Prevents direct Node.js access from the renderer for security
Application Layers
┌─────────────────────────────────────┐
│ Presentation Layer (React) │
│ Components, Pages, State Stores │
└─────────────────────────────────────┘
↕ IPC
┌─────────────────────────────────────┐
│ Service Layer (Main) │
│ Chat Services, MCP Client, RAG │
└─────────────────────────────────────┘
↕
┌─────────────────────────────────────┐
│ Data Layer │
│ SQLite, File System, Vector Store │
└─────────────────────────────────────┘
↕
┌─────────────────────────────────────┐
│ Integration Layer │
│ AI Provider APIs, MCP Servers │
└─────────────────────────────────────┘
Technology Stack
Frontend
- React 18 with TypeScript for type safety
- Fluent UI component library for consistent design
- SCSS for styling with CSS modules
- Zustand for lightweight state management
- i18next for internationalization
Backend (Main Process)
- Electron for cross-platform desktop capabilities
- Node.js runtime environment
- SQLite (better-sqlite3) for local data persistence
Key Dependencies
@modelcontextprotocol/sdk- MCP protocol implementationmammoth,xlsx,pdf-parse- Document parsing librariesmarkdown-it- Markdown rendering with custom pluginselectron-store- Persistent configuration storageelectron-updater- Auto-update functionality
Directory Structure
src/
├── main/ # Electron main process
│ ├── main.ts # Application entry point
│ ├── mcp.ts # MCP server management
│ ├── knowledge.ts # Knowledge base operations
│ ├── embedder.ts # Embedding model interface
│ ├── sqlite.ts # Database operations
│ └── docloader.ts # Document loading utilities
├── renderer/ # React application
│ ├── App.tsx # Root component
│ ├── pages/ # Page components
│ ├── components/ # Reusable UI components
│ └── stores/ # Zustand state stores
├── intellichat/ # AI chat implementation
│ ├── services/ # Provider-specific chat services
│ └── readers/ # Stream response readers
├── providers/ # AI provider configurations
├── types/ # TypeScript type definitions
├── utils/ # Shared utilities
└── hooks/ # React custom hooks
Core Components
Chat System
The chat system uses an abstract service pattern to support multiple AI providers through a unified interface.
IChatService Interface
interface IChatService {
context: IChatContext;
provider: IServiceProvider;
apiSettings: { base: string; key: string; model: string };
chat({ message, onMessage, onComplete, onError }): void;
abort(): void;
isReady(): boolean;
}
Each provider (OpenAI, Anthropic, Google, etc.) implements this interface with provider-specific logic for:
- Request formatting
- Streaming response handling
- Error management
- Token counting
Chat Readers Stream readers parse provider-specific response formats (SSE, JSON streams) and normalize them into a consistent message format for the UI.
Context Management
- System prompts define AI behavior
- Conversation history maintains context
- Knowledge base integration injects relevant documents
- MCP tools provide additional capabilities
MCP Integration
The Model Context Protocol (MCP) enables standardized tool integration.
MCP Client (src/main/mcp.ts)
- Manages connections to multiple MCP servers
- Supports both local (stdio) and remote (HTTP/SSE) servers
- Handles server lifecycle (activation, deactivation, reconnection)
- Implements timeout and retry logic for reliability
Transport Types
StdioClientTransport- Local servers via stdin/stdoutSSEClientTransport- Remote servers via Server-Sent EventsStreamableHTTPClientTransport- Remote servers via HTTP streaming
Tool Discovery and Execution
- Client connects to MCP server on activation
- Server capabilities are queried (tools, prompts, resources)
- Tools are listed and made available to AI models
- AI requests tool execution during conversation
- Tool calls are validated against approval policy
- Results are returned to AI for continued processing
Configuration
MCP servers are configured in mcp.json with:
- Command and arguments for local servers
- URL and headers for remote servers
- Environment variables and proxy settings
- Activation state and capabilities
Knowledge Base (RAG)
The knowledge base implements Retrieval-Augmented Generation using local embeddings.
Document Processing Pipeline
- User selects documents (docx, xlsx, pptx, pdf, txt, csv)
- Documents are parsed into text chunks
- Chunks are embedded using local bge-m3 model
- Embeddings are stored in SQLite with metadata
- Documents are organized into collections
Embedding Service (src/main/embedder.ts)
- Runs local bge-m3 model via node subprocess
- Supports multilingual text (90+ languages)
- Generates 1024-dimensional embeddings
- Processes documents in batches for efficiency
Retrieval Process
- User message is embedded using same model
- Vector similarity search finds relevant chunks
- Top-k chunks are retrieved with metadata
- Context is injected into chat prompt
- AI generates response with retrieved knowledge
Storage
- Document metadata in SQLite
- Embeddings stored as binary blobs
- Collections organize related documents
- Chunk references maintain document structure
Provider System
The provider system abstracts differences between AI providers.
Provider Configuration Each provider defines:
- API endpoints and authentication
- Supported models and capabilities
- Request/response formats
- Token limits and pricing
Capability Detection
- Vision support (image inputs)
- Tool calling (function execution)
- Streaming responses
- JSON mode
- System prompts
Model Management
- Users configure API keys per provider
- Models are selected per conversation
- Settings include temperature, max tokens, etc.
- Usage is tracked per provider and model
Data Flow
Chat Message Flow
User Input (Editor)
↓
IPC: chat-message
↓
Chat Service (Main Process)
↓
Prepare Request:
- Load conversation context
- Inject knowledge if enabled
- Add available MCP tools
↓
HTTP Request to AI Provider
↓
Stream Response via Reader
↓
IPC: stream-data events
↓
UI Update (Real-time)
↓
Save to SQLite
↓
Update Usage Statistics
Knowledge Base Query Flow
User Message
↓
Check if knowledge is enabled
↓
Embed message (Node subprocess)
↓
Vector similarity search (SQLite)
↓
Retrieve top-k chunks
↓
Format as context
↓
Inject into system prompt
↓
Send to AI provider
↓
AI generates response with context
MCP Tool Execution Flow
AI requests tool execution
↓
Parse tool call from response
↓
Check approval policy
- Auto: execute immediately
- Manual: request user approval
↓
IPC: mcp-call-tool
↓
MCP Client routes to server
↓
Server executes tool
↓
Result returned to client
↓
Result sent back to AI
↓
AI continues with tool result
Key Subsystems
IPC Communication
Extensive IPC handlers in src/main/main.ts enable renderer-main communication:
request- Generic HTTP requests with proxy supportmcp-*- MCP server management and tool executionknowledge-*- Document import and searchencrypt/decrypt- Secure credential storageget-store/set-store- Persistent settings
Authentication & Sync
Optional Supabase integration provides:
- User account management
- Settings backup across devices
- OAuth authentication flow
- Offline-first design (works without an account)
Analytics
Usage tracking system monitors:
- API calls per provider and model
- Token consumption and costs
- Feature usage patterns
- Optional telemetry via Axiom
Settings Management
Electron-store persists user preferences:
- Appearance (theme, language)
- Provider API keys and models
- MCP server configurations
- Knowledge base settings
- Chat preferences
Architecture Decisions
Why Electron?
- Cross-platform desktop application (Windows, macOS, Linux)
- Access to native APIs (file system, system info)
- Ability to run local models for embeddings
- Rich UI with web technologies
- Offline-capable with local data storage
Why Local Embeddings?
- Privacy: Documents never leave the device
- Offline: No internet required for the knowledge base
- Cost: No API fees for embeddings
- Multilingual: bge-m3 supports 90+ languages
- Control: Full control over the embedding process
Why MCP?
- Standardization: Open protocol for tool integration
- Extensibility: Add tools without modifying core code
- Community: Growing ecosystem of MCP servers
- Security: Controlled execution with approval policies
- Flexibility: Support both local and remote tools
Why Multiple Providers?
- Choice: Users select preferred AI provider
- Fallback: Switch providers if one is unavailable
- Cost: Optimize spending across providers
- Capabilities: Access different model strengths (vision, reasoning, speed)
Why SQLite?
- Embedded: No separate database server required
- Fast: Efficient for local queries
- Reliable: ACID transactions
- Portable: Single file database
- Vector Support: Store embeddings as blobs
Security Considerations
- Preload scripts limit renderer access to Node.js APIs
- API keys encrypted at rest using system keychain
- External URLs validated before opening
- MCP tool execution requires approval policies
- Certificate validation for HTTPS requests
- Proxy support for corporate environments
Performance Optimizations
- Streaming responses for real-time feedback
- Batch embedding generation for documents
- SQLite indexes on frequently queried fields
- Lazy loading of UI components
- Debounced search and input handlers
- Connection pooling for MCP servers