The Knowledge Tree MCP server has been successfully refactored from a monolithic 2857-line index.ts file into a clean, modular architecture following SOLID principles, DRY, and KISS.
src/
├── index.ts # Clean entry point (80 lines)
├── server/ # Server components
│ ├── KnowledgeTreeServer.ts # Main server class
│ ├── MCPHandlers.ts # MCP protocol handlers
│ ├── ServerContext.ts # Shared context implementation
│ └── types.ts # Server-specific types
├── tools/ # MCP tool implementations (12 tools)
│ ├── help.ts # Help system
│ ├── search.ts # Search functionality
│ ├── add.ts # Create entries
│ ├── update.ts # Update entries
│ ├── delete.ts # Delete entries
│ ├── link.ts # Link management
│ ├── validate.ts # Validation
│ ├── export.ts # Export functionality
│ ├── indexKnowledge.ts # Knowledge indexing
│ ├── stats.ts # Statistics
│ ├── recent.ts # Recent changes
│ └── analytics.ts # Usage analytics
├── types/ # Type definitions
│ ├── KnowledgeEntry.ts # Core knowledge types
│ ├── UsageTypes.ts # Analytics types
│ └── ServerTypes.ts # Server/tool types
├── constants/ # Application constants
│ ├── priorities.ts # Priority levels and utilities
│ ├── relationships.ts # Relationship types
│ └── defaults.ts # Default configurations
├── utils/ # Utility functions
│ ├── fileSystem.ts # File operations
│ ├── logging.ts # Usage logging
│ ├── validation.ts # Validation logic
│ └── export/ # Export format handlers
└── web/ # Optional web interface
├── server.ts # Web server setup
├── handlers.ts # WebSocket handlers
└── types.ts # Web-specific types
- Each file has a single, clear responsibility
- Tools are independent and pluggable
- Server logic is separated from business logic
- All types are centralized and properly exported
- Strong typing throughout the codebase
- Interfaces define clear contracts
- Easy to find and modify specific functionality
- New tools can be added without touching existing code
- Clear dependency flow
- Each module can be tested in isolation
- Dependencies are injected through interfaces
- Mock implementations are straightforward
- Common logic is extracted to utils
- Constants are centralized
- Export formats are pluggable
- S: Single Responsibility - Each module has one job
- O: Open/Closed - New tools don't require core changes
- L: Liskov Substitution - Interfaces can be swapped
- I: Interface Segregation - Minimal, focused interfaces
- D: Dependency Inversion - Depends on abstractions
- Validation logic centralized in utils
- File operations abstracted
- Constants defined once
- Straightforward file naming
- Clear module boundaries
- Simple import/export structure
- Code Organization: From 1 file to 40+ focused modules
- Reduced Complexity: Largest file now ~400 lines (vs 2857)
- Better IDE Support: Faster navigation and autocomplete
- Easier Debugging: Issues isolated to specific modules
- Team Collaboration: Multiple developers can work without conflicts
The external API remains unchanged:
# Same CLI interface
knowledge-tree-mcp --docs ./my-docs --port 3000
# Same MCP tools available
# Same resource URIs
# Same web interfaceThe modular structure makes it easy to:
- Add new tools by creating a single file in
tools/ - Support new export formats in
utils/export/ - Add new validation rules in
utils/validation.ts - Extend analytics in
tools/analytics.ts - Create plugins or extensions
The refactoring maintains 100% backward compatibility while providing a solid foundation for future development.