The current index.ts file is 2857 lines long and contains everything in a single file, violating SOLID principles and making maintenance difficult.
src/
├── index.ts # Main entry point (CLI args, server instantiation)
├── server/ # Server-related code
│ ├── KnowledgeTreeServer.ts # Main server class
│ └── MCPHandlers.ts # MCP protocol handlers
├── types/ # Type definitions
│ ├── index.ts # Export all types
│ ├── KnowledgeEntry.ts # Core knowledge types
│ ├── UsageTypes.ts # Usage analytics types
│ └── ServerTypes.ts # Server configuration types
├── tools/ # MCP tool implementations
│ ├── index.ts # Tool registration and exports
│ ├── help.ts # Help system
│ ├── indexKnowledge.ts # Knowledge indexing
│ ├── search.ts # Search functionality
│ ├── add.ts # Add knowledge entries
│ ├── update.ts # Update knowledge entries
│ ├── delete.ts # Delete knowledge entries
│ ├── link.ts # Link management
│ ├── validate.ts # Validation logic
│ ├── export.ts # Export functionality
│ ├── stats.ts # Statistics
│ ├── recent.ts # Recent changes
│ └── analytics.ts # Usage analytics
├── utils/ # Utility functions
│ ├── fileSystem.ts # File operations
│ ├── logging.ts # Usage logging
│ ├── validation.ts # Common validation logic
│ └── export/ # Export format handlers
│ ├── markdown.ts
│ ├── json.ts
│ └── html.ts
├── web/ # Web interface
│ ├── server.ts # Web server setup
│ ├── handlers.ts # WebSocket handlers
│ └── types.ts # Web-specific types
└── constants/ # Application constants
└── index.ts # Priority levels, relationships, etc.
- Single Responsibility: Each file has one clear purpose
- Open/Closed: Tools are extensible without modifying core server
- Liskov Substitution: Tool handlers follow consistent interfaces
- Interface Segregation: Separate interfaces for different concerns
- Dependency Inversion: Core logic depends on abstractions, not implementations
- Common validation logic extracted to
utils/validation.ts - Export formats separated into individual handlers
- Shared types defined once in
types/
- Clear, descriptive file names
- Logical grouping of related functionality
- Simple import/export structure
- Maintainability: Easier to find and modify specific functionality
- Testability: Each module can be tested independently
- Scalability: New tools can be added without touching existing code
- Readability: Smaller, focused files are easier to understand
- Reusability: Utils and types can be shared across modules
- Create the new directory structure
- Extract types and interfaces first
- Extract constants and enums
- Extract utility functions
- Extract tool implementations into separate files
- Extract web server functionality
- Refactor main server class
- Create new modular index.ts
- Test the modular implementation
- Ensure all existing functionality works
- Update documentation and examples
KnowledgeEntry.ts: Core knowledge entry structure, relationship typesUsageTypes.ts: Usage logging and analytics typesServerTypes.ts: Server configuration and context typesindex.ts: Central export point for all types
Each tool file exports a single handler function with signature:
export function toolHandler(args: any, context: ServerContext): Promise<MCPResponse>fileSystem.ts: File operations, path handling, directory scanninglogging.ts: Usage tracking, analytics data collectionvalidation.ts: Common validation rules, error checkingexport/: Format-specific export handlers
server.ts: Fastify server setup, static file servinghandlers.ts: WebSocket message handling, real-time updatestypes.ts: Web-specific interfaces and types
KnowledgeTreeServer.ts: Main server class, reduced to coordinationMCPHandlers.ts: MCP protocol request/response handling
- Priority levels:
CRITICAL,REQUIRED,COMMON,EDGE-CASE - Relationship types:
related,supersedes,conflicts_with, etc. - Default configurations and settings
- Preserve existing API: All public interfaces remain unchanged
- Incremental extraction: Extract one module at a time
- Test-driven: Run tests after each extraction
- Backward compatibility: Keep original index.ts until fully migrated
This modular structure will make the codebase much more maintainable while preserving all existing functionality.