Skip to main content
Glama
apolosan

Design Patterns MCP Server

by apolosan
blackboard.json10.5 kB
{ "id": "blackboard", "name": "Blackboard", "category": "Behavioral", "description": "Artificial intelligence pattern for combining disparate sources of data", "when_to_use": "Complex problem solving\nMultiple knowledge sources\nIncremental solution building\nAI/ML systems", "benefits": "Modular knowledge\nIncremental reasoning\nFlexible architecture\nKnowledge reuse", "drawbacks": "Control complexity\nDebugging difficulty\nPerformance overhead\nSynchronization issues", "use_cases": "Expert systems\nSignal processing\nPattern recognition\nDecision support", "complexity": "High", "tags": ["behavioral", "blackboard", "ai", "knowledge-based", "expert-system"], "examples": { "typescript": { "language": "typescript", "code": "// Blackboard Pattern\n// AI pattern for combining disparate sources of data to solve complex problems\n\n// Blackboard - central data structure\nclass Blackboard {\n private data: Map<string, any> = new Map();\n private listeners: Map<string, Set<(value: any) => void>> = new Map();\n\n // Write data to blackboard\n write(key: string, value: any): void {\n this.data.set(key, value);\n console.log(`Blackboard: ${key} = ${JSON.stringify(value)}`);\n\n // Notify listeners\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.forEach(listener => listener(value));\n }\n }\n\n // Read data from blackboard\n read<T>(key: string): T | undefined {\n return this.data.get(key);\n }\n\n // Check if data exists\n exists(key: string): boolean {\n return this.data.has(key);\n }\n\n // Subscribe to changes\n subscribe(key: string, listener: (value: any) => void): void {\n if (!this.listeners.has(key)) {\n this.listeners.set(key, new Set());\n }\n this.listeners.get(key)!.add(listener);\n }\n\n // Unsubscribe from changes\n unsubscribe(key: string, listener: (value: any) => void): void {\n const keyListeners = this.listeners.get(key);\n if (keyListeners) {\n keyListeners.delete(listener);\n }\n }\n\n // Get all current data\n getAllData(): Record<string, any> {\n const result: Record<string, any> = {};\n for (const [key, value] of this.data) {\n result[key] = value;\n }\n return result;\n }\n}\n\n// Knowledge Source - contributes to problem solving\nabstract class KnowledgeSource {\n protected name: string;\n protected blackboard: Blackboard;\n protected conditions: string[];\n\n constructor(name: string, blackboard: Blackboard, conditions: string[]) {\n this.name = name;\n this.blackboard = blackboard;\n this.conditions = conditions;\n }\n\n // Check if this knowledge source can contribute\n abstract canContribute(): boolean;\n\n // Execute the knowledge source's logic\n abstract execute(): void;\n\n // Process the blackboard data\n protected process(): void {\n if (this.canContribute()) {\n console.log(`${this.name}: Executing knowledge source`);\n this.execute();\n }\n }\n}\n\n// Controller - manages the problem-solving process\nclass BlackboardController {\n private blackboard: Blackboard;\n private knowledgeSources: KnowledgeSource[] = [];\n private isRunning: boolean = false;\n\n constructor(blackboard: Blackboard) {\n this.blackboard = blackboard;\n }\n\n // Add knowledge source\n addKnowledgeSource(ks: KnowledgeSource): void {\n this.knowledgeSources.push(ks);\n }\n\n // Run the problem-solving process\n async run(): Promise<void> {\n this.isRunning = true;\n console.log('Starting blackboard system...');\n\n let iteration = 0;\n const maxIterations = 100;\n\n while (this.isRunning && iteration < maxIterations) {\n console.log(`\\n--- Iteration ${iteration + 1} ---`);\n\n // Execute all applicable knowledge sources\n for (const ks of this.knowledgeSources) {\n ks.process();\n }\n\n // Check if problem is solved\n if (this.isProblemSolved()) {\n console.log('Problem solved!');\n this.isRunning = false;\n break;\n }\n\n iteration++;\n\n // Small delay between iterations\n await new Promise(resolve => setTimeout(resolve, 100));\n }\n\n if (iteration >= maxIterations) {\n console.log('Maximum iterations reached');\n }\n }\n\n // Check if the problem is solved\n private isProblemSolved(): boolean {\n // Check if we have a final solution\n return this.blackboard.exists('solution');\n }\n\n // Stop the process\n stop(): void {\n this.isRunning = false;\n }\n}\n\n// Example: Medical Diagnosis System\n\n// Medical knowledge sources\nclass SymptomAnalyzer extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('symptoms') && !this.blackboard.exists('possible_conditions');\n }\n\n execute(): void {\n const symptoms = this.blackboard.read<string[]>('symptoms')!;\n let conditions: string[] = [];\n\n if (symptoms.includes('fever') && symptoms.includes('cough')) {\n conditions.push('flu', 'pneumonia');\n }\n if (symptoms.includes('headache') && symptoms.includes('nausea')) {\n conditions.push('migraine', 'concussion');\n }\n if (symptoms.includes('chest_pain')) {\n conditions.push('heart_attack', 'angina');\n }\n\n this.blackboard.write('possible_conditions', conditions);\n }\n}\n\nclass TestResultAnalyzer extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('test_results') && this.blackboard.exists('possible_conditions');\n }\n\n execute(): void {\n const testResults = this.blackboard.read<any>('test_results')!;\n const conditions = this.blackboard.read<string[]>('possible_conditions')!;\n\n // Filter conditions based on test results\n const confirmedConditions = conditions.filter(condition => {\n switch (condition) {\n case 'flu':\n return testResults.flu_test === 'positive';\n case 'pneumonia':\n return testResults.chest_xray === 'abnormal';\n case 'migraine':\n return testResults.neurological_exam === 'normal';\n case 'concussion':\n return testResults.ct_scan === 'negative';\n default:\n return false;\n }\n });\n\n this.blackboard.write('confirmed_conditions', confirmedConditions);\n }\n}\n\nclass TreatmentRecommender extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('confirmed_conditions') && !this.blackboard.exists('treatment_plan');\n }\n\n execute(): void {\n const conditions = this.blackboard.read<string[]>('confirmed_conditions')!;\n const treatments: any = {};\n\n conditions.forEach(condition => {\n switch (condition) {\n case 'flu':\n treatments[condition] = {\n medications: ['antiviral', 'pain_reliever'],\n rest: '7-10 days',\n follow_up: 'if symptoms worsen'\n };\n break;\n case 'migraine':\n treatments[condition] = {\n medications: ['triptan', 'anti_nausea'],\n rest: 'dark quiet room',\n triggers: 'avoid caffeine, stress'\n };\n break;\n case 'heart_attack':\n treatments[condition] = {\n emergency: true,\n procedure: 'immediate medical attention required',\n medications: ['aspirin', 'nitroglycerin']\n };\n break;\n }\n });\n\n this.blackboard.write('treatment_plan', treatments);\n this.blackboard.write('solution', 'diagnosis_complete');\n }\n}\n\n// Usage: Medical diagnosis example\nconst blackboard = new Blackboard();\nconst controller = new BlackboardController(blackboard);\n\n// Add knowledge sources\ncontroller.addKnowledgeSource(new SymptomAnalyzer('Symptom Analyzer', blackboard, ['symptoms']));\ncontroller.addKnowledgeSource(new TestResultAnalyzer('Test Analyzer', blackboard, ['test_results', 'possible_conditions']));\ncontroller.addKnowledgeSource(new TreatmentRecommender('Treatment Recommender', blackboard, ['confirmed_conditions']));\n\n// Add initial data\nblackboard.write('symptoms', ['fever', 'cough', 'fatigue']);\nblackboard.write('test_results', {\n flu_test: 'positive',\n chest_xray: 'normal',\n neurological_exam: 'normal'\n});\n\n// Run the diagnosis\ncontroller.run().then(() => {\n console.log('\\nFinal diagnosis:');\n console.log(blackboard.getAllData());\n});\n\n// Advanced example: Signal processing system\nclass SignalProcessor extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('raw_signal') && !this.blackboard.exists('filtered_signal');\n }\n\n execute(): void {\n const signal = this.blackboard.read<number[]>('raw_signal')!;\n // Apply low-pass filter\n const filtered = signal.map((value, index) => {\n if (index === 0) return value;\n return 0.3 * value + 0.7 * signal[index - 1];\n });\n this.blackboard.write('filtered_signal', filtered);\n }\n}\n\nclass FeatureExtractor extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('filtered_signal') && !this.blackboard.exists('features');\n }\n\n execute(): void {\n const signal = this.blackboard.read<number[]>('filtered_signal')!;\n const features = {\n mean: signal.reduce((a, b) => a + b, 0) / signal.length,\n variance: signal.reduce((acc, val) => acc + Math.pow(val - features.mean, 2), 0) / signal.length,\n peaks: signal.filter((val, i) => i > 0 && i < signal.length - 1 && val > signal[i-1] && val > signal[i+1]).length\n };\n this.blackboard.write('features', features);\n }\n}\n\nclass PatternClassifier extends KnowledgeSource {\n canContribute(): boolean {\n return this.blackboard.exists('features') && !this.blackboard.exists('classification');\n }\n\n execute(): void {\n const features = this.blackboard.read<any>('features')!;\n let classification = 'normal';\n\n // Simple classification logic\n if (features.peaks > 10 && features.variance > 100) {\n classification = 'abnormal';\n }\n\n this.blackboard.write('classification', classification);\n this.blackboard.write('solution', 'signal_processed');\n }\n}\n\n// Benefits of Blackboard Pattern:\n// 1. Modular knowledge sources\n// 2. Incremental problem solving\n// 3. Flexible and extensible architecture\n// 4. Natural fit for AI/ML systems\n// 5. Knowledge reuse across different problems" } } }

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/apolosan/design_patterns_mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server