Skip to main content
Glama

Metabase MCP Server

by MikelA92
REFACTORING_SUMMARY.md8.85 kB
# Refactoring Summary - Metabase MCP Server ## Date: November 3, 2025 ## Overview Successfully refactored the Metabase MCP Server to address monolithic file structure and implement a comprehensive error handling framework. The refactoring maintains 100% backward compatibility while significantly improving code maintainability and structure. --- ## ✅ Completed Changes ### 1. **Error Handling Framework** ✅ Created a comprehensive error handling system with custom error classes: **New Files:** - `src/shared/errors/MetabaseError.js` - Base error class and specialized error types - `MetabaseError` - Base error class - `ApiError` - API-related errors (network, HTTP) - `ValidationError` - Input validation errors - `ConfigurationError` - Configuration/environment errors - `ToolExecutionError` - Tool execution errors - `TimeoutError` - Request timeout errors **Benefits:** - Structured error information with error codes and details - Proper error inheritance chain - JSON serialization support - Timestamp tracking - Stack trace preservation ### 2. **Modular File Structure** ✅ Broke down the monolithic 1,347-line server file into focused, maintainable modules: **New Structure:** ``` src/ ├── server/ │ ├── index.js # Entry point (~50 lines) │ ├── MetabaseMCPServer.js # Main server class (~200 lines) │ ├── handlers/ # Handler modules │ │ ├── cardHandlers.js # Card operations │ │ ├── dashboardHandlers.js # Dashboard operations │ │ ├── databaseHandlers.js # Database operations │ │ ├── collectionHandlers.js # Collection operations │ │ ├── queryHandlers.js # Query execution │ │ ├── fieldHandlers.js # Field operations │ │ ├── segmentMetricHandlers.js # Segments & metrics │ │ └── userHandlers.js # User & activity operations │ ├── utils/ │ │ └── apiClient.js # API request handling │ └── config/ │ └── toolDefinitions.js # Tool definitions ├── shared/ │ ├── errors/ │ │ └── MetabaseError.js # Error classes │ └── utils/ │ ├── validators.js # Input validation │ ├── logger.js # Logging framework │ └── urlDecoder.js # URL decoding utilities └── client/ ├── MetabaseClient.js # Refactored client (~650 lines) └── cards.js # Backward compatibility wrapper ``` ### 3. **Shared Utilities** ✅ Created reusable utilities shared between server and client: **Files Created:** - `src/shared/utils/validators.js` - Input validation for all parameter types - `src/shared/utils/logger.js` - Structured logging with log levels - `src/shared/utils/urlDecoder.js` - Dashboard URL decoding and parameter extraction **Benefits:** - DRY principle - no code duplication - Consistent validation across server and client - Centralized logging with levels (debug, info, warn, error) - Better error messages with context ### 4. **Handler Modules** ✅ Separated tool implementations into focused handler classes: | Handler Class | Responsibilities | Lines | |--------------|------------------|-------| | CardHandlers | Card CRUD, query execution | ~150 | | DashboardHandlers | Dashboard operations, URL parsing | ~80 | | DatabaseHandlers | Database & table metadata | ~120 | | CollectionHandlers | Collection browsing | ~60 | | QueryHandlers | Native SQL execution | ~40 | | FieldHandlers | Field metadata & values | ~60 | | SegmentMetricHandlers | Segments & metrics | ~50 | | UserHandlers | Users & activity | ~80 | **Benefits:** - Single Responsibility Principle - Easier to test individual handlers - Clear separation of concerns - Easier to locate and modify code ### 5. **Configuration Management** ✅ Centralized tool definitions in a separate configuration file: **File:** `src/server/config/toolDefinitions.js` - All 23 tool definitions in one place - Easier to add/modify/remove tools - Clear schema definitions - Separated from business logic ### 6. **Logging Framework** ✅ Implemented structured logging throughout: **Features:** - Log levels: debug, info, warn, error - JSON structured output - Context-aware logging (child loggers) - Timestamp tracking - Error stack trace capture - Environment-controlled log level (LOG_LEVEL env var) ### 7. **API Client Improvements** ✅ Created dedicated API client with enhanced features: **Features:** - Request timeout handling (configurable) - Proper error handling with ApiError - Structured logging - Fetch with timeout support - Consistent error messages ### 8. **Backward Compatibility** ✅ Maintained 100% backward compatibility: - Old import paths still work (e.g., `import from './src/client/cards.js'`) - All existing test scripts work without modification - Same API surface - No breaking changes --- ## 📊 Metrics ### Before Refactoring: - **Server file**: 1,347 lines (monolithic) - **Client file**: 817 lines (some duplication) - **Error handling**: Inconsistent, no custom error types - **Logging**: console.error only - **Validation**: Scattered throughout code ### After Refactoring: - **Entry point**: 50 lines - **Main server class**: ~200 lines - **8 handler modules**: 50-150 lines each (average ~90 lines) - **Error classes**: 6 custom error types with proper inheritance - **Logging**: Structured JSON logging with 4 levels - **Validation**: Centralized validators for all input types - **Shared utilities**: 3 utility modules used by both server and client ### Code Quality Improvements: - **Modularity**: ✅ Improved from 1 monolithic file to 20+ focused modules - **Maintainability**: ✅ Average file size reduced from 1,082 lines to ~100 lines - **Testability**: ✅ Each module can be tested independently - **Code Reuse**: ✅ Shared utilities eliminate duplication - **Error Handling**: ✅ Comprehensive error framework with 6 error types - **Logging**: ✅ Structured logging with context and levels --- ## 🧪 Testing **All tests passed:** - ✅ Error classes export and function correctly - ✅ Validators work for valid and invalid inputs - ✅ Logger creates child contexts properly - ✅ URL decoder handles invalid URLs gracefully - ✅ MetabaseClient can be instantiated - ✅ MetabaseMCPServer can be instantiated - ✅ All 23 tool definitions are valid - ✅ All 8 handler classes are properly exported - ✅ No syntax errors in any file - ✅ 100% success rate on structure tests --- ## 📁 Files Backed Up The following original files were backed up before refactoring: - `src/server/index.old.js` - Original monolithic server (1,347 lines) - `src/client/cards.old.js` - Original monolithic client (817 lines) These can be used for reference or rollback if needed. --- ## 🔄 Migration Notes **No migration required!** The refactoring maintains full backward compatibility: 1. Existing code continues to work without changes 2. Import statements remain valid 3. All API signatures unchanged 4. Test suites run without modification **Optional improvements:** - Can now use structured logging instead of console.error - Can use validators explicitly for better error messages - Can use custom error types for better error handling - Can configure log levels via LOG_LEVEL environment variable --- ## 🚀 Next Steps ### Immediate: 1. ✅ All refactoring complete 2. ✅ All tests passing 3. ✅ Code structure validated ### Recommended Future Enhancements: 1. Add unit tests for individual handlers 2. Add integration tests 3. Add ESLint and Prettier configuration 4. Add CI/CD pipeline (GitHub Actions) 5. Add TypeScript definitions or JSDoc types 6. Add request rate limiting 7. Add response caching 8. Add request retry logic with exponential backoff --- ## 📝 Summary Successfully completed a major refactoring of the Metabase MCP Server: ✅ **Monolithic File Structure** → Modular, focused components ✅ **No Error Handling** → Comprehensive error framework with 6 custom error types ✅ **Code Duplication** → Shared utilities and DRY principles ✅ **Console Logging** → Structured JSON logging with levels ✅ **Scattered Validation** → Centralized, reusable validators ✅ **Hard to Maintain** → Easy to understand, modify, and test **Result:** A cleaner, more maintainable codebase that's built to last, with zero breaking changes and 100% backward compatibility. --- ## 👥 Contributors - Refactoring completed on November 3, 2025 - All changes tested and validated - Documentation updated

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/MikelA92/metabase-mcp-mab'

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