# MCP-Consult Massive Overhaul Plan
## π Analysis Summary
### Critical Issues Found
**handlers.ts Analysis:**
- β **Cyclomatic Complexity: 126** (callToolHandler) - Max recommended: 10
- β **Cognitive Complexity: 140** - Makes code hard to understand
- β **Nesting Depth: 5 levels** - Should be max 3
- β οΈ **Nested Loops: O(nΒ²) complexity** at line 594
- β οΈ **String Concatenation in Loops** - Memory inefficient
- β **tryCallMemoryServer: Complexity 30** - Needs breakdown
### Current State
β
16 tests passing (Vitest)
β
5 MCP tools working
β
Good documentation
β
GitHub Actions CI
β οΈ No linting configured
β οΈ No formatting standards
β οΈ No coverage tracking
β οΈ Monolithic architecture
## π― Refactoring Goals
1. **Reduce Complexity**: Break down 126-complexity function to <10 per function
2. **Improve Performance**: Eliminate O(nΒ²) operations
3. **Add Quality Tools**: ESLint, Prettier, coverage thresholds
4. **Modularize**: Separate concerns into focused modules
5. **Enhance Testing**: Achieve 80%+ coverage
6. **Document**: Add comprehensive guides like mcp-optimist
## π Phase 1: Foundation & Tooling (Day 1)
### 1.1 Add Development Tools
- [ ] Install ESLint + TypeScript rules
- [ ] Install Prettier for code formatting
- [ ] Configure Jest or maintain Vitest with coverage
- [ ] Add coverage thresholds (60%+ branches, 80%+ lines)
- [ ] Create `.prettierrc` and `.eslintrc.json`
### 1.2 Set Up TDD Infrastructure
- [ ] Ensure all existing tests pass
- [ ] Add test coverage reporting
- [ ] Document test structure
- [ ] Create test utilities/helpers
### 1.3 Documentation Improvements
- [ ] Create CONTRIBUTING.md
- [ ] Add ARCHITECTURE.md
- [ ] Create detailed API documentation
- [ ] Add examples for each tool
### Deliverables
- β
All tools configured
- β
Tests passing with coverage
- β
Documentation structure
## π Phase 2: Handler Refactoring (Days 2-3)
### 2.1 Break Down callToolHandler (Complexity: 126 β <10)
**Current Structure:**
```typescript
async function callToolHandler(name: string, args: any) {
// 500+ lines of switch/case logic
// All 5 tools in one function
// Deep nesting and complexity
}
```
**Target Architecture:**
```typescript
// Handler Registry Pattern
interface ToolHandler {
validate(args: any): boolean;
execute(args: any): Promise<any>;
}
class ConsultHandler implements ToolHandler {}
class ListModelsHandler implements ToolHandler {}
class CompareHandler implements ToolHandler {}
class RememberHandler implements ToolHandler {}
class SequentialChainHandler implements ToolHandler {}
// Main router (complexity: ~5)
async function callToolHandler(name: string, args: any) {
const handler = handlerRegistry.get(name);
if (!handler) throw new Error(`Unknown tool: ${name}`);
if (!handler.validate(args)) {
throw new Error('Invalid arguments');
}
return handler.execute(args);
}
```
### 2.2 Extract Individual Handlers
Each handler should be in its own file with:
- Input validation
- Business logic
- Error handling
- ~50-100 lines max
- Complexity < 10
**File Structure:**
```
src/
handlers/
base/
ToolHandler.ts (interface)
BaseHandler.ts (shared logic)
ConsultHandler.ts
ListModelsHandler.ts
CompareModelsHandler.ts
RememberHandler.ts
SequentialChainHandler.ts
registry/
HandlerRegistry.ts
```
### 2.3 Refactor tryCallMemoryServer (Complexity: 30 β <10)
Break into:
1. `ConfigLoader.ts` - Load memory config
2. `MemoryClientFactory.ts` - Create client
3. `MemoryStorageAdapter.ts` - Abstract storage
4. `LocalFileStorage.ts` - File fallback
5. `McpMemoryClient.ts` - MCP memory client
### Deliverables
- β
All handlers < 10 complexity
- β
Tests updated and passing
- β
No functionality lost
- β
Improved maintainability
## π Phase 3: Performance Optimizations (Day 4)
### 3.1 Fix Nested Loop (Line 594)
**Issue:** O(nΒ²) complexity in conversation context building
**Before:**
```typescript
while (condition) {
for (const item of items) {
// O(nΒ²) operation
}
}
```
**After:**
```typescript
// Use Map for O(1) lookups
const itemMap = new Map(items.map((i) => [i.id, i]));
while (condition) {
const item = itemMap.get(id); // O(1)
}
```
### 3.2 Fix String Concatenation (Lines 618, 649)
**Issue:** String concatenation in loops creates new strings each iteration
**Before:**
```typescript
let context = '';
for (const msg of messages) {
context += msg.content; // Creates new string each time
}
```
**After:**
```typescript
const parts: string[] = [];
for (const msg of messages) {
parts.push(msg.content);
}
const context = parts.join(''); // Single allocation
```
### 3.3 Add Performance Testing
- [ ] Add benchmarks for critical paths
- [ ] Test with large datasets (1000+ messages)
- [ ] Profile memory usage
- [ ] Document performance characteristics
### Deliverables
- β
All O(nΒ²) operations eliminated
- β
String operations optimized
- β
Performance tests added
- β
Benchmarks documented
## π Phase 4: Testing & Coverage (Day 5)
### 4.1 Increase Test Coverage
- [ ] Add tests for each new handler class
- [ ] Test error paths and edge cases
- [ ] Test performance optimizations
- [ ] Add integration tests
**Target Coverage:**
- Statements: 85%+
- Branches: 70%+
- Functions: 90%+
- Lines: 85%+
### 4.2 Add Test Utilities
- [ ] Mock factory for Ollama responses
- [ ] Test fixtures for common scenarios
- [ ] Helper functions for assertions
- [ ] Performance testing utilities
### Deliverables
- β
Coverage thresholds met
- β
All edge cases tested
- β
Test utilities documented
## π Phase 5: CI/CD Enhancement (Day 6)
### 5.1 Improve GitHub Actions Workflow
- [ ] Add lint job (runs first)
- [ ] Add test job with coverage
- [ ] Add build job
- [ ] Add release automation (tags)
- [ ] Cache dependencies for speed
### 5.2 Release Automation
- [ ] Auto-generate release notes
- [ ] Publish to npm on tags
- [ ] Create GitHub releases
- [ ] Attach build artifacts
### 5.3 Add Quality Gates
- [ ] Require tests to pass
- [ ] Enforce coverage thresholds
- [ ] Require lint to pass
- [ ] Block PRs that fail checks
### Deliverables
- β
Full CI/CD pipeline
- β
Automated releases
- β
Quality gates enforced
## π Phase 6: Documentation & Polish (Day 7)
### 6.1 Comprehensive Documentation
- [ ] ARCHITECTURE.md - System design
- [ ] CONTRIBUTING.md - How to contribute
- [ ] API_REFERENCE.md - All tools documented
- [ ] EXAMPLES.md - Code examples
- [ ] PERFORMANCE.md - Benchmarks and tips
- [ ] RELEASE.md - Release process
### 6.2 Code Documentation
- [ ] Add JSDoc comments to all public APIs
- [ ] Document complex algorithms
- [ ] Add inline comments for tricky code
- [ ] Generate API docs (TypeDoc)
### 6.3 Polish
- [ ] Clean up all TODOs
- [ ] Remove unused code
- [ ] Consistent naming conventions
- [ ] Update package.json metadata
### Deliverables
- β
Professional documentation
- β
API docs generated
- β
Polished codebase
## π― Success Metrics
### Before Refactoring
- Complexity: 126 (critical)
- Nesting: 5 levels
- Performance: O(nΒ²) operations
- Coverage: Unknown
- Lint: None
- Format: Inconsistent
### After Refactoring
- Complexity: <10 per function β
- Nesting: β€3 levels β
- Performance: O(n) or better β
- Coverage: 80%+ β
- Lint: 0 errors β
- Format: Consistent (Prettier) β
## π Implementation Strategy
### TDD Approach
1. **RED**: Write failing test for new handler
2. **GREEN**: Implement minimal code to pass
3. **REFACTOR**: Improve code quality
4. **REPEAT**: For each handler
### Migration Path
1. Keep old `callToolHandler` working
2. Build new handlers alongside
3. Add feature flags to test new code
4. Migrate one tool at a time
5. Remove old code when all tests pass
6. Deploy incrementally
### Risk Mitigation
- β
Never break existing tests
- β
Keep all functionality working
- β
Deploy to staging first
- β
Monitor for errors
- β
Have rollback plan
## π Timeline
| Phase | Duration | Focus |
| ----- | -------- | -------------------- |
| 1 | 1 day | Foundation & Tooling |
| 2 | 2 days | Handler Refactoring |
| 3 | 1 day | Performance |
| 4 | 1 day | Testing |
| 5 | 1 day | CI/CD |
| 6 | 1 day | Documentation |
**Total: 7 days** (1 week sprint)
## π Expected Outcomes
1. **Maintainability**: Code is easy to understand and modify
2. **Performance**: 10-100x faster for large inputs
3. **Quality**: High test coverage, no lint errors
4. **Reliability**: Comprehensive error handling
5. **Documentation**: Professional-grade docs
6. **Automation**: Full CI/CD pipeline
7. **Scalability**: Easy to add new tools
## π Notes
- This plan follows the successful patterns used in mcp-optimist
- Each phase is independent and can be reviewed
- Tests must pass after each phase
- Documentation is updated continuously
- Performance is measured and tracked
---
**Status**: Ready to begin Phase 1
**Last Updated**: 2024-11-17
**Estimated Completion**: 7 days