The sgraph-mcp-server has been designed with a modular architecture that separates concerns, improves testability, and enhances maintainability. This document describes the current architectural patterns and design decisions.
Each module has one clear purpose:
- Core: Fundamental components (model management, data conversion)
- Services: Business logic (search, dependency analysis, overview generation)
- Tools: MCP tool definitions and request/response handling
- Utils: Cross-cutting concerns (logging, validation)
- Services depend on abstractions, not concrete implementations
- Tools depend on services, not directly on data structures
- Clear separation between business logic and presentation layer
- Each layer can be tested independently
- Service logic is separated from MCP protocol concerns
- Minimal external dependencies in core business logic
src/
├── core/ # Core components
│ ├── model_manager.py # Model loading and caching
│ └── element_converter.py # Element-to-dict conversions
├── services/ # Business logic layer
│ ├── search_service.py # Search algorithms
│ ├── dependency_service.py # Dependency analysis
│ └── overview_service.py # Model structure analysis
├── tools/ # MCP tool definitions
│ ├── model_tools.py # Model management tools
│ ├── search_tools.py # Search-related tools
│ ├── analysis_tools.py # Dependency analysis tools
│ └── navigation_tools.py # Element navigation tools
└── utils/ # Utilities
├── logging.py # Centralized logging
└── validators.py # Input validation
tests/
├── unit/ # Unit tests
├── integration/ # Integration tests
└── performance/ # Performance tests
throwaway-ai-code/ # Temporary AI debugging code
ModelManager (model_manager.py)
- Loads sgraph models from files
- Manages model caching in memory
- Handles model lifecycle (load, get, remove, clear)
- Provides comprehensive logging and error handling
ElementConverter (element_converter.py)
- Converts SElement objects to dictionary representations
- Handles batch conversions
- Manages association serialization
SearchService (search_service.py)
- Implements search algorithms (by name, type, attributes)
- Uses iterative traversal for performance
- Handles regex and glob pattern matching
- Supports scoped searches
DependencyService (dependency_service.py)
- Analyzes dependency chains and subtrees
- Provides transitive dependency analysis
- Supports multiple element retrieval
- Handles both incoming and outgoing dependencies
OverviewService (overview_service.py)
- Generates hierarchical model overviews
- Provides statistics and type distribution
- Supports configurable depth analysis
- Optimized for quick structural understanding
Model Tools (model_tools.py)
sgraph_load_model: Load models with validationsgraph_get_model_overview: Hierarchical structure overview
Search Tools (search_tools.py)
sgraph_search_elements_by_name: Pattern-based searchsgraph_get_elements_by_type: Type-based filteringsgraph_search_elements_by_attributes: Attribute-based search
Analysis Tools (analysis_tools.py)
sgraph_get_subtree_dependencies: Subtree dependency analysissgraph_get_dependency_chain: Transitive dependency chainssgraph_get_multiple_elements: Bulk element retrieval
Navigation Tools (navigation_tools.py)
sgraph_get_root_element: Root element accesssgraph_get_element: Single element retrievalsgraph_get_element_*_associations: Association navigation
Logging (logging.py)
- Centralized logging configuration
- Service-specific logger management
- Consistent log formatting
Validators (validators.py)
- Model ID validation
- Path validation and security checks
- Pattern validation for search operations
- Element type validation
MCP Client Request
↓
[Tools Layer] - Validates input, calls services
↓
[Services Layer] - Implements business logic
↓
[Core Layer] - Manages models and data conversion
↓
SGraph Library - Actual graph operations
- Iterative Traversal: All tree/graph traversals use iterative approaches instead of recursion
- Model Caching: Models are loaded once and cached in memory
- Lazy Loading: Only requested data is processed and converted
- Bulk Operations: Multiple element operations are batched for efficiency
- Model Loading: < 60 seconds (with timeout)
- Search Operations: < 100ms for typical queries
- Overview Generation: < 150ms for depth ≤ 5
- Dependency Analysis: < 200ms for moderate subtrees
- Tools Layer: Catches all exceptions, returns error objects
- Services Layer: Logs business logic errors, raises specific exceptions
- Core Layer: Handles system-level errors (file I/O, timeouts)
{
"error": "Human-readable error message",
"details": "Optional technical details"
}- DEBUG: Detailed operation traces
- INFO: Important state changes and metrics
- WARNING: Recoverable issues
- ERROR: Failures that prevent operation completion
- Test individual components in isolation
- Mock external dependencies
- Focus on business logic correctness
- Test component interactions
- End-to-end workflows
- Real model loading and analysis
- Validate performance targets
- Regression testing for optimizations
- Real-world scenario simulation
- Create tool class in appropriate
tools/module - Use
@mcp.tool()decorator - Implement validation and error handling
- Add corresponding service method if needed
- Create service class in
services/ - Implement static methods for operations
- Add comprehensive logging
- Create unit tests
- Add validation function to
utils/validators.py - Use in tool layer for input validation
- Add unit tests for edge cases
The original server.py (350+ lines) has been refactored into:
- Tools: 4 focused modules (~100 lines each)
- Services: 3 business logic modules (~150 lines each)
- Core: 2 fundamental modules (~100 lines each)
- Utils: 2 utility modules (~50 lines each)
- Maintainability: Clear boundaries, easier to modify
- Testability: Each component can be tested independently
- Extensibility: New features don't require touching core logic
- Performance: Services can be optimized individually
- Reusability: Core services work outside MCP context
Consider implementing a plugin system for:
- Custom search algorithms
- Additional analysis tools
- External data source integrations
- Persistent model caching
- Query result caching
- Cache invalidation strategies
- Async service operations
- Parallel dependency analysis
- Concurrent model loading
- External configuration files
- Environment-specific settings
- Runtime configuration updates