Skip to main content
Glama
saralegui-solutions

MCP Self-Learning Server

README.md
# MCP Self-Learning Server

A sophisticated Model Context Protocol (MCP) server that autonomously learns from interactions, optimizes performance, and continuously improves its knowledge base through pattern recognition and machine learning techniques.

## 🌟 Features

### 🧠 Autonomous Learning Engine
- **Pattern Recognition**: Automatically identifies and learns from interaction patterns
- **Feature Extraction**: Analyzes tool sequences, context, performance metrics, and semantic embeddings
- **Confidence Scoring**: Evaluates pattern reliability based on frequency, recency, and consistency
- **Memory Consolidation**: Manages short-term and long-term pattern storage

### šŸ”„ Knowledge Synchronization
- **Auto-sync**: Every 60 seconds between MCP servers
- **Knowledge Export/Import**: JSON and Markdown formats
- **Pattern Merging**: With deduplication
- **Cross-server Learning**: Through shared knowledge directory

### šŸ“Š Self-Improvement Capabilities
- **Performance Optimization**: Identifies redundancies and bottlenecks
- **Predictive Suggestions**: Anticipates next actions based on learned patterns
- **Error Pattern Analysis**: Learns from failures to improve success rates
- **Adaptive Recommendations**: Generates context-aware optimizations

### šŸ’¾ Data Persistence
- **Automatic Data Saving**: Every 5 minutes with backup rotation
- **Learning Data Recovery**: Loads previous sessions on startup
- **Export Knowledge**: Multiple formats (JSON, Markdown)
- **Backup System**: Automatic backup creation before saves

### šŸ“ Advanced Logging
- **Multi-level Logging**: Debug, Info, Warn, Error with colors and emojis
- **File & Console Output**: Simultaneous logging to both
- **Log Rotation**: Prevents disk space issues
- **Performance Monitoring**: Tool execution times and memory usage

## šŸš€ Quick Start

### Prerequisites
- Node.js 18+ 
- npm or yarn

### Installation

1. **Clone/Download the Project**
   ```bash
   cd ~/saralegui-solutions-llc/shared/MCPSelfLearningServer
   ```

2. **Install Dependencies**
   ```bash
   npm install
   ```

3. **Configure Claude Desktop**
   
   Add to `~/.config/Claude/claude_desktop_config.json`:
   ```json
   {
     "mcpServers": {
       "self-learning": {
         "command": "node",
         "args": ["/home/ben/saralegui-solutions-llc/shared/MCPSelfLearningServer/mcp-self-learning-server.js"],
         "env": {
           "NODE_ENV": "production",
           "LEARNING_MODE": "autonomous"
         }
       }
     }
   }
   ```

4. **Start the Server**
   ```bash
   npm start
   ```

## šŸ“‹ Available Commands

### Development & Testing
```bash
npm run dev          # Start in development mode
npm run debug        # Start with debug logging
npm test             # Run all tests
npm run test:unit    # Run unit tests only
npm run test:integration  # Run integration tests only
```

### Monitoring & Health
```bash
npm run health       # Run comprehensive health check
npm run monitor      # Real-time monitoring
npm run monitor:details  # Detailed monitoring with change tracking
```

### Manual Operations
```bash
# Health check
node tools/health-check.js

# Real-time monitoring
node tools/monitor.js [--interval 5] [--details]

# Start server directly
node mcp-self-learning-server.js
```

## šŸ› ļø Available MCP Tools

### Core Learning Tools

#### `analyze_pattern`
Analyze and learn from interaction patterns
```json
{
  "interaction": {
    "type": "tool_usage",
    "input": "user input",
    "output": "tool output", 
    "context": {},
    "performance": { "duration": 100 },
    "success": true
  }
}
```

#### `get_insights`
Get current learning analytics and insights
```json
{}
```

#### `trigger_learning`
Manually trigger a learning cycle
```json
{}
```

### Knowledge Management

#### `export_knowledge`
Export learned knowledge to file
```json
{
  "format": "json|markdown"  // Optional, defaults to json
}
```

#### `import_knowledge`
Import knowledge from external source
```json
{
  "source": "file_path_or_url",
  "format": "json"  // Optional
}
```

### Performance & Optimization

#### `optimize_tool`
Get optimization suggestions for specific tools
```json
{
  "tool_name": "example_tool"  // Optional
}
```

#### `predict_next_action`
Get predictive suggestions based on current context
```json
{
  "context": {
    "current_tool": "analyze_pattern",
    "user_intent": "optimization"
  }
}
```

#### `get_performance_metrics`
Get detailed performance analytics
```json
{
  "tool_name": "specific_tool"  // Optional, for tool-specific metrics
}
```

## šŸ“Š Monitoring & Analytics

### Health Check Results
The health check tool verifies:
- āœ… Server startup functionality
- āœ… Data persistence system
- āœ… Logging system
- āœ… Performance metrics (startup time)

### Real-time Monitoring
The monitor displays:
- Learning engine status (patterns, knowledge, cycles)
- Log file metrics and activity
- System resource usage
- Change indicators showing growth over time

### Performance Expectations

| Metric | Target | Excellent |
|--------|--------|-----------|
| Startup Time | <5s | <1s |
| Memory Usage | <100MB | <50MB |
| Response Time | <500ms | <100ms |
| Learning Accuracy | >70% | >90% |

## šŸ—‚ļø Directory Structure

```
MCPSelfLearningServer/
ā”œā”€ā”€ mcp-self-learning-server.js    # Main server file
ā”œā”€ā”€ package.json                   # Dependencies and scripts
ā”œā”€ā”€ README.md                      # This file
ā”œā”€ā”€ data/                         # Persistent learning data
│   ā”œā”€ā”€ learning-engine.json      # Main learning data
│   └── learning-engine.backup.json # Backup
ā”œā”€ā”€ logs/                         # Server logs
│   └── mcp-server.log           # Main log file
ā”œā”€ā”€ lib/                         # Shared libraries
│   └── logger.js               # Enhanced logging system
ā”œā”€ā”€ test/                       # Test suites
│   ā”œā”€ā”€ unit/                  # Unit tests
│   └── integration/          # Integration tests
└── tools/                    # Development tools
    ā”œā”€ā”€ health-check.js      # Health check tool
    └── monitor.js          # Real-time monitoring
```

## šŸ”§ Configuration

### Environment Variables

| Variable | Default | Description |
|----------|---------|-------------|
| `NODE_ENV` | `production` | Environment mode |
| `LOG_LEVEL` | `info` | Logging level (debug/info/warn/error) |
| `LOG_CONSOLE` | `true` | Enable console logging |
| `LOG_FILE` | `true` | Enable file logging |
| `LEARNING_MODE` | `autonomous` | Learning behavior mode |

### Learning Engine Settings

- **Max Memory Size**: 1000 patterns in memory
- **Auto-save Interval**: 5 minutes
- **Pattern Confidence Threshold**: 0.5
- **Learning Trigger**: Every 100 interactions or 50 tool uses

## 🚨 Troubleshooting

### Common Issues

1. **Server Won't Start**
   - Check Node.js version (18+ required)
   - Verify all dependencies installed: `npm install`
   - Check file permissions

2. **Data Not Persisting**
   - Verify `data/` directory permissions
   - Check disk space
   - Review logs for errors: `tail -f logs/mcp-server.log`

3. **High Memory Usage**
   - Run health check: `npm run health`
   - Check pattern count: `npm run monitor`
   - Consider reducing max memory size

4. **Slow Performance**
   - Enable performance logging: `npm run debug`
   - Check system resources
   - Review learning cycle frequency

### Log Analysis

```bash
# View recent logs
tail -f logs/mcp-server.log

# Search for errors
grep "ERROR" logs/mcp-server.log

# Count log levels
grep -c "INFO\|WARN\|ERROR" logs/mcp-server.log
```

## šŸ“ˆ Expected Learning Outcomes

### Immediate (0-100 interactions)
- Basic pattern recognition active
- Initial knowledge base building
- Tool usage tracking enabled

### Short-term (100-1000 interactions)
- Pattern confidence scores stabilizing
- First optimization recommendations
- Predictive accuracy ~50%

### Long-term (1000+ interactions)
- Predictive accuracy >70%
- Response time improvements ~30%
- Comprehensive knowledge graph
- Cross-server knowledge sharing
- Self-documenting insights

## šŸ¤ Integration with Claude

Once configured, the server provides these tools in Claude:
- Pattern analysis for learning from conversations
- Performance insights for optimization
- Predictive suggestions for improved responses
- Knowledge export for documentation
- Real-time learning from every interaction

## šŸ“ License

ISC License

## šŸ†˜ Support

For issues or questions:
1. Run health check: `npm run health`
2. Check logs: `tail -f logs/mcp-server.log` 
3. Review this documentation
4. Check server status: `npm run monitor`

---

**Built with ā¤ļø for autonomous learning and continuous improvement**

TDQS

C2.9/5.0

Scored across 8 tools

Disambiguation4/5

Most tools have distinct purposes, such as analyze_pattern for pattern analysis, export_knowledge for exporting, and get_insights for insights. However, get_insights and get_performance_metrics could be slightly overlapping, as insights might include performance data, but their descriptions help differentiate them.

Naming Consistency5/5

All tool names follow a consistent verb_noun pattern with clear, descriptive verbs like analyze, export, get, import, optimize, predict, and trigger. The naming is uniform and predictable throughout the set.

Tool Count5/5

With 8 tools, the count is well-scoped for a self-learning server, covering key operations like analysis, knowledge management, insights, and optimization. Each tool appears to serve a specific function without redundancy.

Completeness4/5

The tool set covers core self-learning functions, including analysis, knowledge import/export, insights, metrics, optimization, prediction, and manual triggering. A minor gap might be the lack of a tool for resetting or clearing knowledge, but overall, it supports the server's purpose effectively.