phase-2a-handle-get-prompt.md•7.08 kB
# Refactoring: handle_get_prompt() - Phase 2, Function #5
## Summary
Refactored `server.py::handle_get_prompt()` to reduce cyclomatic complexity and improve maintainability through Extract Method pattern.
**Metrics:**
- **Original Complexity:** 33
- **Refactored Main Function:** Complexity 6 (82% reduction)
- **Original Lines:** 208
- **Refactored Main Function:** 41 lines
- **Helper Functions Created:** 5
## Refactoring Strategy: Extract Method Pattern
The function contained a long if/elif/else chain handling 5 different prompt types. Each prompt type required 25-40 lines of specialized logic with high nesting and branching.
### Helper Functions Extracted
#### 1. `_prompt_memory_review()` - CC: 5
**Purpose:** Handle "memory_review" prompt type
**Responsibilities:**
- Parse time_period and focus_area arguments
- Retrieve memories from specified time period
- Format memories as prompt text with tags
**Location:** Lines ~1320-1347
**Input:** arguments dict
**Output:** List of PromptMessage objects
---
#### 2. `_prompt_memory_analysis()` - CC: 8
**Purpose:** Handle "memory_analysis" prompt type
**Responsibilities:**
- Parse tags and time_range arguments
- Retrieve relevant memories
- Analyze patterns (tag counts, memory types)
- Build analysis report text
**Location:** Lines ~1349-1388
**Input:** arguments dict
**Output:** List of PromptMessage objects
**Complexity Source:** Double-nested loops for pattern analysis (2 for loops)
---
#### 3. `_prompt_knowledge_export()` - CC: 8
**Purpose:** Handle "knowledge_export" prompt type
**Responsibilities:**
- Parse format_type and filter criteria
- Retrieve memories based on filter
- Format export in JSON/Markdown/Text based on format_type
- Build export text
**Location:** Lines ~1390-1428
**Input:** arguments dict
**Output:** List of PromptMessage objects
**Complexity Source:** Multiple format branches (if/elif/else)
---
#### 4. `_prompt_memory_cleanup()` - CC: 6
**Purpose:** Handle "memory_cleanup" prompt type
**Responsibilities:**
- Parse cleanup parameters
- Find duplicate memories
- Build cleanup report
- Provide recommendations
**Location:** Lines ~1430-1458
**Input:** arguments dict
**Output:** List of PromptMessage objects
**Complexity Source:** Nested loop for duplicate detection
---
#### 5. `_prompt_learning_session()` - CC: 5
**Purpose:** Handle "learning_session" prompt type
**Responsibilities:**
- Parse topic, key_points, and questions
- Create structured learning note
- Store as memory
- Return formatted response
**Location:** Lines ~1460-1494
**Input:** arguments dict
**Output:** List of PromptMessage objects
---
## Refactored `handle_get_prompt()` Function - CC: 6
**New Structure:**
```python
async def handle_get_prompt(self, name: str, arguments: dict):
await self._ensure_storage_initialized()
# Simple dispatch to specialized handlers
if name == "memory_review":
messages = await self._prompt_memory_review(arguments)
elif name == "memory_analysis":
messages = await self._prompt_memory_analysis(arguments)
# ... etc
else:
messages = [unknown_prompt_message]
return GetPromptResult(description=..., messages=messages)
```
**Lines:** 41 (vs 208 original)
**Control Flow:** Reduced from 33 branches to 6 (if/elif chain only)
## Benefits
### Code Quality
- ✅ **Single Responsibility:** Each function handles one prompt type
- ✅ **Testability:** Each prompt type can be unit tested independently
- ✅ **Readability:** Main function is now a simple dispatcher
- ✅ **Maintainability:** Changes to one prompt type isolated to its handler
- ✅ **Extensibility:** Adding new prompt types requires just another elif
### Complexity Distribution
```
handle_get_prompt: CC 6 (dispatcher)
_prompt_memory_review: CC 5 (simple retrieval + format)
_prompt_memory_analysis: CC 8 (pattern analysis)
_prompt_knowledge_export: CC 8 (multiple format branches)
_prompt_memory_cleanup: CC 6 (duplicate detection)
_prompt_learning_session: CC 5 (create + store)
```
**Total distributed complexity:** 38 (vs 33 monolithic)
**Max function complexity:** 8 (vs 33 monolithic) - 75% reduction in peak complexity
### Maintainability Improvements
- Prompt handlers are now 27-39 lines each (vs 208 for entire function)
- Clear naming convention (`_prompt_<type>`) makes intent obvious
- Easier to locate specific prompt logic
- Reduces cognitive load when reading main function
- New developers can understand each handler independently
## Backward Compatibility
✅ **Fully compatible** - No changes to:
- Function signature: `handle_get_prompt(name, arguments) -> GetPromptResult`
- Return values: Same GetPromptResult structure
- Argument processing: Same argument parsing
- All prompt types: Same behavior
## Testing Recommendations
### Unit Tests
- `test_prompt_memory_review()` - Test memory retrieval + formatting
- `test_prompt_memory_analysis()` - Test pattern analysis logic
- `test_prompt_knowledge_export()` - Test each format (JSON/MD/text)
- `test_prompt_memory_cleanup()` - Test duplicate detection
- `test_prompt_learning_session()` - Test storage logic
### Integration Tests
- Test all 5 prompt types through handle_get_prompt()
- Verify error handling for unknown prompts
- Test with various argument combinations
## Related Issues
- **Issue #246:** Code Quality Phase 2 - Reduce Function Complexity
- **Phase 2 Progress:** 4/27 high-risk functions completed
- ✅ `install.py::main()` - Complexity 62 → ~8
- ✅ `sqlite_vec.py::initialize()` - Complexity 38 → Reduced
- ✅ `install_package()` - Complexity 33 → 7
- ✅ `handle_get_prompt()` - Complexity 33 → 6 (THIS REFACTORING)
## Files Modified
- `src/mcp_memory_service/server.py`: Refactored `handle_get_prompt()` with 5 helper methods
## Git Commit
Use semantic commit message:
```
refactor: reduce handle_get_prompt() complexity from 33 to 6 (82% reduction)
Extract prompt type handlers:
- _prompt_memory_review (CC 5) - Memory retrieval + formatting
- _prompt_memory_analysis (CC 8) - Pattern analysis
- _prompt_knowledge_export (CC 8) - Multi-format export
- _prompt_memory_cleanup (CC 6) - Duplicate detection
- _prompt_learning_session (CC 5) - Learning note creation
Main dispatcher now 41 lines (vs 208 original) with CC 6.
All handlers individually testable and maintainable.
Addresses issue #246 Phase 2, function #5 in refactoring plan.
```
## Code Review Checklist
- [x] Code compiles without errors
- [x] All handlers extract correctly
- [x] Dispatcher logic correct
- [x] No changes to external API
- [x] Backward compatible
- [x] Complexity reduced
- [ ] All tests pass (manual verification needed)
- [ ] Integration tested
## Future Improvements
1. **Prompt Registry:** Create a dictionary-based prompt registry for even simpler dispatch
2. **Configuration:** Make prompt definitions configurable
3. **Validation:** Add argument schema validation for each prompt type
4. **Documentation:** Auto-generate prompt documentation from handler implementations