Spaces:
Paused
MCP-COGNITIVE SYSTEM INTEGRATION ARCHITECTURE
Binding the New Intelligence to Existing Platform
Dato: 2025-11-24
FormΓ₯l: Konkret mapping mellem Master Plan og eksisterende MCP kerne
ποΈ EKSISTERENDE MCP ARKITEKTUR
Current System Components
Frontend (Widget Board)
βββ widgets/ (36 widgets)
β βββ UnifiedDataService.ts (existing)
β
Backend (MCP Core)
βββ /api/mcp/route (mcpRouter.ts)
βββ /api/mcp/autonomous (autonomousRouter.ts - HansPedder)
βββ toolHandlers.ts (CMA, SRAG, PAL, Evolution, Notes handlers)
βββ SourceRegistry.ts (DataSource management)
βββ EventBus.ts (System-wide events)
β
Services Layer
βββ memory/ (CMA - MemoryRepository)
βββ srag/ (SRAGRepository)
βββ evolution/ (EvolutionRepository)
βββ pal/ (PALRepository)
βββ project/ (ProjectMemory)
β
Intelligence Layer (Current)
βββ AutonomousAgent.ts
βββ CognitiveMemory.ts
βββ PatternMemory.ts
βββ FailureMemory.ts
Current Data Flow
Widget Request
β
UnifiedDataService (frontend)
β
POST /api/mcp/route
β
mcpRouter.ts β mcpRegistry.route(MCPMessage)
β
toolHandlers[tool](payload, context)
β
Repository (CMA/SRAG/PAL/Evolution)
β
SQLite Database
β
Response β Widget
π INTEGRATION POINTS: NEW COGNITIVE SYSTEM
Phase 1 Integration: Foundation Layer
1.1 UnifiedMemorySystem β Existing Repositories
File: apps/backend/src/mcp/cognitive/UnifiedMemorySystem.ts
import { CognitiveMemory } from '../memory/CognitiveMemory.js';
import { MemoryRepository } from '../../services/memory/memoryRepository.js';
import { SragRepository } from '../../services/srag/sragRepository.js';
import { PalRepository } from '../../services/pal/palRepository.js';
import { EvolutionRepository } from '../../services/evolution/evolutionRepository.js';
import { ProjectMemory } from '../../services/project/ProjectMemory.js';
/**
* INTEGRATION POINT 1: Unified Memory Bridge
* Connects new 4-memory architecture to existing repositories
*/
export class UnifiedMemorySystem {
// EXISTING COMPONENTS (reuse)
private cognitiveMemory: CognitiveMemory; // Pattern + Failure memory
private memoryRepo: MemoryRepository; // CMA (semantic memory)
private sragRepo: SragRepository; // SRAG (document memory)
private palRepo: PalRepository; // PAL (emotional context)
private evolutionRepo: EvolutionRepository; // Evolution (learning)
private projectMemory: ProjectMemory; // Episodic memory
// NEW COMPONENTS (Phase 1)
private workingMemory: Map<string, WorkingMemoryState>; // Current context
private proceduralMemory: ProductionRuleEngine; // Pattern β Action rules
constructor() {
// Connect to existing singletons
this.cognitiveMemory = getCognitiveMemory();
this.memoryRepo = new MemoryRepository();
this.sragRepo = new SragRepository();
this.palRepo = new PalRepository();
this.evolutionRepo = new EvolutionRepository();
this.projectMemory = new ProjectMemory();
// Initialize new components
this.workingMemory = new Map();
this.proceduralMemory = new ProductionRuleEngine(this.cognitiveMemory);
}
/**
* INTEGRATION: Holographic pattern correlation
* Uses EXISTING repositories to find cross-system patterns
*/
async findHolographicPatterns(ctx: McpContext): Promise<CrossSystemPattern[]> {
// Query ALL existing systems
const [palState, cmaEntities, sragDocs, evolutionLearning, cognitivePatterns] =
await Promise.all([
this.palRepo.getRecentEvents(ctx.userId, ctx.orgId, 50),
this.memoryRepo.searchEntities({
userId: ctx.userId,
orgId: ctx.orgId,
keywords: [],
limit: 50
}),
this.sragRepo.searchDocuments(ctx.orgId, ''), // All recent
this.evolutionRepo.getRecentGenerations(10),
this.cognitiveMemory.findSimilarQueries('holographic_scan', 10)
]);
// Cross-correlate for patterns
return this.correlateAcrossSystems({
emotional: this.analyzePALPatterns(palState),
knowledge: this.analyzeCMAGraph(cmaEntities),
documents: this.analyzeSRAGContent(sragDocs),
learning: this.analyzeEvolutionTrends(evolutionLearning),
meta: cognitivePatterns
});
}
/**
* INTEGRATION: Query routing with memory context
* Enhances EXISTING mcpRouter with memory-aware decisions
*/
async enrichMCPRequest(message: MCPMessage, ctx: McpContext): Promise<EnrichedMCPMessage> {
// 1. Load working memory for this context
const workingState = this.workingMemory.get(ctx.userId) ||
await this.initializeWorkingMemory(ctx);
// 2. Get procedural rules (learned patterns β actions)
const applicableRules = await this.proceduralMemory.findRules({
tool: message.tool,
context: workingState
});
// 3. Get semantic context from CMA
const semanticContext = await this.memoryRepo.searchEntities({
userId: ctx.userId,
orgId: ctx.orgId,
keywords: this.extractKeywords(message.payload),
limit: 10
});
// 4. Get episodic context from ProjectMemory
const episodicContext = await this.projectMemory.queryHistory({
limit: 10,
filterByUser: ctx.userId
});
// 5. Enrich message
return {
...message,
enrichedContext: {
workingMemory: workingState,
proceduraRules: applicableRules,
semanticMemory: semanticContext,
episodicMemory: episodicContext,
emotionalState: await this.getEmotionalState(ctx)
}
};
}
}
Integration Point: mcpRouter.ts modification
// BEFORE (current):
mcpRouter.post('/route', async (req, res) => {
const message: MCPMessage = req.body;
const result = await mcpRegistry.route(message);
res.json({ success: true, result });
});
// AFTER (Phase 1):
import { getUnifiedMemorySystem } from './cognitive/UnifiedMemorySystem.js';
mcpRouter.post('/route', async (req, res) => {
const message: MCPMessage = req.body;
const ctx: McpContext = {
userId: req.user?.id || 'anonymous',
orgId: req.user?.orgId || 'default',
timestamp: new Date()
};
// NEW: Enrich with cognitive context
const memorySystem = getUnifiedMemorySystem();
const enrichedMessage = await memorySystem.enrichMCPRequest(message, ctx);
// Route with enriched context
const result = await mcpRegistry.route(enrichedMessage);
// NEW: Update working memory with result
await memorySystem.updateWorkingMemory(ctx, result);
res.json({ success: true, result });
});
1.2 Autonomous Task Engine β AutonomousAgent Integration
File: apps/backend/src/mcp/cognitive/AutonomousTaskEngine.ts
import { AutonomousAgent } from '../autonomous/AutonomousAgent.js';
import { UnifiedMemorySystem } from './UnifiedMemorySystem.js';
import { EventBus } from '../EventBus.js';
/**
* INTEGRATION POINT 2: Task-Driven Loop
* Extends EXISTING AutonomousAgent with BabyAGI-style task generation
*/
export class AutonomousTaskEngine {
private agent: AutonomousAgent;
private memorySystem: UnifiedMemorySystem;
private eventBus: typeof EventBus;
private taskQueue: PriorityQueue<Task>;
constructor(
agent: AutonomousAgent,
memorySystem: UnifiedMemorySystem
) {
this.agent = agent;
this.memorySystem = memorySystem;
this.eventBus = EventBus;
this.taskQueue = new PriorityQueue();
// Listen to system events for task generation
this.setupEventListeners();
}
private setupEventListeners() {
// INTEGRATION: React to existing EventBus events
this.eventBus.on('system:error', (error) => {
this.taskQueue.enqueue(new DiagnosticTask(error), 100);
});
this.eventBus.on('widget:error', ({ widgetId, error }) => {
this.taskQueue.enqueue(new WidgetFixTask(widgetId, error), 90);
});
this.eventBus.on('mcp:slow_response', ({ tool, latency }) => {
if (latency > 5000) {
this.taskQueue.enqueue(new PerformanceOptimizationTask(tool), 70);
}
});
}
/**
* INTEGRATION: Main autonomous loop
* Uses EXISTING AutonomousAgent.executeAndLearn but adds task generation
*/
async autonomousLoop() {
while (this.isActive) {
const task = this.taskQueue.dequeue();
if (!task) {
await this.sleep(1000);
continue;
}
// Execute using EXISTING AutonomousAgent
const intent = this.taskToQueryIntent(task);
const result = await this.agent.executeAndLearn(intent);
// Generate new tasks based on result (NEW)
const newTasks = await this.generateTasksFromResult(task, result);
this.taskQueue.enqueueAll(newTasks);
// Reprioritize with emotional awareness (NEW)
await this.emotionAwarePrioritization();
// Log to episodic memory (INTEGRATION)
await this.memorySystem.logEpisode({
task,
result,
newTasksGenerated: newTasks.length
});
}
}
}
Integration Point: hansPedder.ts modification
// apps/backend/src/orchestrator/hansPedder.ts
import { AutonomousTaskEngine } from '../mcp/cognitive/AutonomousTaskEngine.js';
import { getUnifiedMemorySystem } from '../mcp/cognitive/UnifiedMemorySystem.js';
export async function startHansPedder() {
const cognitive = getCognitiveMemory();
const sourceReg = getSourceRegistry();
const memorySystem = getUnifiedMemorySystem();
// EXISTING AutonomousAgent
hansPedder = new AutonomousAgent(cognitive, sourceReg);
// NEW: Task engine wraps agent
const taskEngine = new AutonomousTaskEngine(hansPedder, memorySystem);
console.info('π Starting HansPedder with Autonomous Task Engine...');
hansPedderStatus.active = true;
hansPedderStatus startedAt = new Date();
// Start task-driven loop (NEW) instead of basic learning loop
await taskEngine.autonomousLoop();
}
1.3 Emotion-Aware Decisions β PAL Integration
File: apps/backend/src/mcp/cognitive/EmotionAwareDecisionEngine.ts
import { PalRepository } from '../../services/pal/palRepository.js';
import { AutonomousAgent } from '../autonomous/AutonomousAgent.js';
/**
* INTEGRATION POINT 3: Emotion-Aware Routing
* Connects PAL emotional state to decision-making
*/
export class EmotionAwareDecisionEngine {
private palRepo: PalRepository;
private agent: AutonomousAgent;
constructor(agent: AutonomousAgent) {
this.agent = agent;
this.palRepo = new PalRepository();
}
/**
* INTEGRATION: Enhance EXISTING AutonomousAgent.selectSource
* with emotional context
*/
async makeEmotionAwareDecision(
intent: QueryIntent,
ctx: McpContext
): Promise<DataSource> {
// 1. Get EXISTING data-driven score
const dataScore = await this.agent.scoreSource(intent);
// 2. Get emotional state from EXISTING PAL
const emotionalState = await this.getEmotionalState(ctx);
// 3. Adjust scores based on emotion (NEW)
const emotionAdjustedScores = dataScore.map(sourceScore => ({
...sourceScore,
score: this.adjustForEmotion(sourceScore, emotionalState)
}));
// 4. Select best
return emotionAdjustedScores.sort((a, b) => b.score - a.score)[0].source;
}
private async getEmotionalState(ctx: McpContext): Promise<EmotionalState> {
// INTEGRATION: Use EXISTING PAL repository
const recentEvents = this.palRepo.getRecentEvents(ctx.userId, ctx.orgId, 10);
const stressDistribution = this.palRepo.getStressLevelDistribution(
ctx.userId,
ctx.orgId,
1 // last hour
);
// Analyze stress level
const highStressEvents = stressDistribution.filter(d => d.stress_level === 'high');
const stressRatio = highStressEvents.length / Math.max(stressDistribution.length, 1);
return {
stress: stressRatio > 0.5 ? 'high' : stressRatio > 0.2 ? 'medium' : 'low',
focus: this.determineFocusLevel(recentEvents),
engagement: this.determineEngagement(recentEvents)
};
}
private adjustForEmotion(
sourceScore: { source: DataSource, score: number },
emotion: EmotionalState
): number {
let adjusted = sourceScore.score;
// Stress-aware routing (NEW capability)
if (emotion.stress === 'high') {
// Prefer fast, simple sources
if (sourceScore.source.estimatedLatency < 100) {
adjusted *= 1.5; // 50% boost for fast sources
}
if (sourceScore.source.complexity === 'low') {
adjusted *= 1.3; // 30% boost for simple sources
}
if (sourceScore.source.estimatedLatency > 1000) {
adjusted *= 0.5; // 50% penalty for slow sources
}
}
// Focus-aware routing
if (emotion.focus === 'deep') {
// Allow complex, thorough sources
if (sourceScore.source.depth === 'high') {
adjusted *= 1.4;
}
}
return adjusted;
}
}
Integration Point: AutonomousAgent.ts modification
// apps/backend/src/mcp/autonomous/AutonomousAgent.ts
import { EmotionAwareDecisionEngine } from '../cognitive/EmotionAwareDecisionEngine.js';
export class AutonomousAgent {
private emotionEngine: EmotionAwareDecisionEngine;
constructor(cognitive: CognitiveMemory, registry: SourceRegistryImpl) {
// ... existing code ...
// NEW: Initialize emotion engine
this.emotionEngine = new EmotionAwareDecisionEngine(this);
}
async executeAndLearn(intent: QueryIntent, ctx?: McpContext): Promise<any> {
// BEFORE: const source = this.selectSource(intent);
// AFTER: Use emotion-aware selection
const source = ctx
? await this.emotionEngine.makeEmotionAwareDecision(intent, ctx)
: this.selectSource(intent); // fallback to existing
// ... rest of existing code ...
}
}
Phase 2 Integration: Intelligence Layer
2.1 GraphRAG β CMA + SRAG Unification
File: apps/backend/src/mcp/cognitive/UnifiedGraphRAG.ts
import { MemoryRepository } from '../../services/memory/memoryRepository.js';
import { SragRepository } from '../../services/srag/sragRepository.js';
import { HybridSearchEngine } from './HybridSearchEngine.js';
/**
* INTEGRATION POINT 4: GraphRAG
* Unifies EXISTING CMA (knowledge graph) + SRAG (documents)
*/
export class UnifiedGraphRAG {
private cmaRepo: MemoryRepository; // EXISTING semantic graph
private sragRepo: SragRepository; // EXISTING documents
private hybridSearch: HybridSearchEngine; // NEW hybrid search
/**
* INTEGRATION: Multi-hop reasoning over CMA graph + SRAG docs
*/
async query(naturalLanguageQuery: string, ctx: McpContext): Promise<GraphRAGResult> {
// 1. Hybrid search (NEW) over EXISTING data
const searchResults = await this.hybridSearch.search(naturalLanguageQuery, {
userId: ctx.userId,
orgId: ctx.orgId
});
// 2. Multi-hop graph traversal on CMA entities
const graphEntities = searchResults.cma || [];
const expandedGraph = await this.multiHopTraversal(graphEntities);
// 3. Get related SRAG documents
const relatedDocs = searchResults.srag || [];
// 4. Synthesize unified context
return {
graphContext: expandedGraph,
documentContext: relatedDocs,
unifiedSummary: await this.synthesize(expandedGraph, relatedDocs)
};
}
private async multiHopTraversal(
startEntities: MemoryEntity[],
maxHops: number = 3
): Promise<KnowledgeSubgraph> {
// INTEGRATION: Use EXISTING CMA relations
const subgraph = new KnowledgeSubgraph();
const visited = new Set<string>();
let frontier = startEntities;
for (let hop = 0; hop < maxHops; hop++) {
const nextFrontier: MemoryEntity[] = [];
for (const entity of frontier) {
if (visited.has(entity.id)) continue;
visited.add(entity.id);
subgraph.addNode(entity);
// Get related entities via CMA relations (EXISTING)
const relations = await this.cmaRepo.getEntityRelations(entity.id);
for (const rel of relations) {
const relatedEntity = await this.cmaRepo.getEntity(rel.target_entity_id);
if (relatedEntity) {
nextFrontier.push(relatedEntity);
subgraph.addEdge(entity, relatedEntity, rel.relationship_type);
}
}
}
frontier = nextFrontier;
}
return subgraph;
}
}
Integration Point: New MCP tool handler
// apps/backend/src/mcp/toolHandlers.ts
import { getUnifiedGraphRAG } from './cognitive/UnifiedGraphRAG.js';
/**
* NEW TOOL: graph_rag (Phase 2)
* Intelligent multi-hop reasoning over unified knowledge
*/
export async function graphRAGHandler(
payload: { query: string },
ctx: McpContext
): Promise<any> {
const graphRAG = getUnifiedGraphRAG();
const result = await graphRAG.query(payload.query, ctx);
return {
graphNodes: result.graphContext.nodes.length,
documents: result.documentContext.length,
summary: result.unifiedSummary,
fullContext: result
};
}
// Register in mcpRegistry
mcpRegistry.registerTool('graph_rag', graphRAGHandler);
2.2 Frontend Integration: UnifiedDataService Enhancement
File: apps/matrix-frontend/src/services/UnifiedDataService.ts (modify EXISTING)
/**
* INTEGRATION POINT 5: Frontend Access to Cognitive System
* Enhances EXISTING UnifiedDataService
*/
export class UnifiedDataService {
// EXISTING methods remain...
/**
* NEW (Phase 1): Memory-aware query
*/
async queryWithMemory(request: {
tool: string;
payload: any;
useMemoryContext?: boolean;
}): Promise<any> {
const message: MCPMessage = {
id: uuidv4(),
tool: request.tool,
payload: request.payload,
createdAt: new Date().toISOString(),
// NEW: Request memory enrichment
options: {
enrichWithMemory: request.useMemoryContext ?? true,
includeEmotionalContext: true
}
};
const response = await fetch('/api/mcp/route', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message)
});
return response.json();
}
/**
* NEW (Phase 2): GraphRAG query
*/
async queryGraphRAG(naturalLanguageQuery: string): Promise<GraphRAGResult> {
return this.queryWithMemory({
tool: 'graph_rag',
payload: { query: naturalLanguageQuery }
});
}
/**
* NEW (Phase 3): Get autonomous insights
*/
async getAutonomousInsights(): Promise<SystemInsights> {
return this.queryWithMemory({
tool: 'autonomous_insights',
payload: {}
});
}
}
π INTEGRATION DEPENDENCY GRAPH
Phase 1: Foundation (Week 1-4)
ββββββββββββββββββββββββββββββββββ
UnifiedMemorySystem (NEW)
βββΊ CognitiveMemory (EXISTING)
βββΊ MemoryRepository/CMA (EXISTING)
βββΊ SRAGRepository (EXISTING)
βββΊ PALRepository (EXISTING)
βββΊ EvolutionRepository (EXISTING)
βββΊ ProjectMemory (EXISTING)
AutonomousTaskEngine (NEW)
βββΊ AutonomousAgent (EXISTING)
βββΊ UnifiedMemorySystem (NEW - Phase 1)
βββΊ EventBus (EXISTING)
EmotionAwareDecisionEngine (NEW)
βββΊ PALRepository (EXISTING)
βββΊ AutonomousAgent (EXISTING)
HybridSearchEngine (NEW)
βββΊ MemoryRepository (EXISTING)
βββΊ SRAGRepository (EXISTING)
βββΊ CognitiveMemory (EXISTING)
ββββββββββββββββββββββββββββββββββ
Phase 2: Intelligence (Week 5-9)
ββββββββββββββββββββββββββββββββββ
UnifiedGraphRAG (NEW)
βββΊ MemoryRepository (EXISTING)
βββΊ SRAGRepository (EXISTING)
βββΊ HybridSearchEngine (NEW - Phase 1)
AgentTeam (NEW)
βββΊ AutonomousAgent (EXISTING)
βββΊ UnifiedMemorySystem (NEW - Phase 1)
StateGraphRouter (NEW)
βββΊ mcpRouter (EXISTING)
βββΊ ProjectMemory (EXISTING)
PatternEvolutionEngine (NEW)
βββΊ AutonomousAgent (EXISTING)
βββΊ CognitiveMemory (EXISTING)
βββΊ ProjectMemory (EXISTING)
ββββββββββββββββββββββββββββββββββ
Phase 3: Meta-Cognition (Week 10-12)
ββββββββββββββββββββββββββββββββββ
SelfReflectionEngine (NEW)
βββΊ AutonomousAgent (EXISTING)
βββΊ CognitiveMemory (EXISTING)
βββΊ ProjectMemory (EXISTING)
MetaLearningEngine (NEW)
βββΊ UnifiedMemorySystem (NEW - Phase 1)
RLHFAlignmentSystem (NEW)
βββΊ AutonomousAgent (EXISTING)
βββΊ ProjectMemory (EXISTING)
π DATA FLOW: BEFORE vs AFTER
BEFORE (Current):
Widget β UnifiedDataService β POST /api/mcp/route β mcpRouter
β toolHandler β Repository β Database β Response
AFTER Phase 1:
Widget β UnifiedDataService.queryWithMemory()
β
POST /api/mcp/route (enrichment enabled)
β
UnifiedMemorySystem.enrichMCPRequest()
βββΊ Working Memory (current context)
βββΊ Procedural Memory (learned rules)
βββΊ Semantic Memory (CMA)
βββΊ Episodic Memory (ProjectMemory)
βββΊ Emotional Context (PAL)
β
EmotionAwareDecisionEngine.makeDecision()
β
EnrichedMCPMessage β toolHandler β Repository β Database
β
UnifiedMemorySystem.updateWorkingMemory()
β
Response (with memory context)
AFTER Phase 2:
Widget β UnifiedDataService.queryGraphRAG()
β
POST /api/mcp/route (tool: 'graph_rag')
β
UnifiedGraphRAG.query()
βββΊ HybridSearchEngine (keyword + semantic + graph)
βββΊ Multi-hop traversal (CMA knowledge graph)
βββΊ Document retrieval (SRAG)
β
Synthesized context β Response
AFTER Phase 3:
Background Loop: AutonomousTaskEngine
β
Generate tasks β Execute via AutonomousAgent
β
SelfReflectionEngine.reflect()
β
MetaLearningEngine.optimizeLearningStrategy()
β
RLHFAlignmentSystem.optimizePolicy()
β
Update all systems β Improved performance
β INTEGRATION CHECKLIST
Phase 1 (Week 1-4)
- Create
apps/backend/src/mcp/cognitive/directory - Implement
UnifiedMemorySystem.ts - Modify
mcpRouter.tsto use enrichment - Implement
AutonomousTaskEngine.ts - Modify
hansPedder.tsto use task engine - Implement
EmotionAwareDecisionEngine.ts - Modify
AutonomousAgent.tsfor emotion awareness - Implement
HybridSearchEngine.ts - Modify frontend
UnifiedDataService.ts - Integration tests for all components
- Update
MCPMessageinterface to support enrichment
Phase 2 (Week 5-9)
- Implement
UnifiedGraphRAG.ts - Add
graph_ragtool handler - Register GraphRAG tool in
mcpRegistry - Implement
AgentTeam.ts - Implement
StateGraphRouter.ts - Implement
PatternEvolutionEngine.ts - Integration tests for Phase 2
- Frontend GraphRAG integration
- Performance optimization
Phase 3 (Week 10-12)
- Implement
SelfReflectionEngine.ts - Implement
MetaLearningEngine.ts - Implement
RLHFAlignmentSystem.ts - Connect feedback loops
- Final integration testing
- Performance tuning
- Production deployment
π― SUCCESS METRICS PER PHASE
Phase 1: Memory enrichment active, emotion-aware routing functional
Phase 2: GraphRAG queries working, multi-hop reasoning operational
Phase 3: Self-improvement autonomy, continuous optimization
Status: Integration architecture complete and ready for implementation π