|
| 1 | +"""Migration status tool for Basic Memory MCP server.""" |
| 2 | + |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +from loguru import logger |
| 6 | + |
| 7 | +from basic_memory.mcp.server import mcp |
| 8 | +from basic_memory.mcp.project_session import get_active_project |
| 9 | + |
| 10 | + |
| 11 | +@mcp.tool( |
| 12 | + description="""Check the status of system migration and background operations. |
| 13 | + |
| 14 | + Use this tool to: |
| 15 | + - Check if migration is in progress or completed |
| 16 | + - Get detailed migration progress information |
| 17 | + - Understand if the system is ready for normal operations |
| 18 | + - Get specific error details if migration failed |
| 19 | + """, |
| 20 | +) |
| 21 | +async def migration_status(project: Optional[str] = None) -> str: |
| 22 | + """Get current migration status and system readiness information. |
| 23 | +
|
| 24 | + This tool provides detailed information about any ongoing or completed |
| 25 | + migration operations, helping users understand system availability. |
| 26 | +
|
| 27 | + Args: |
| 28 | + project: Optional project name (included for consistency with other tools) |
| 29 | +
|
| 30 | + Returns: |
| 31 | + Detailed migration status including: |
| 32 | + - Current migration state (ready, in progress, failed, etc.) |
| 33 | + - Progress information if migration is running |
| 34 | + - Error details if migration failed |
| 35 | + - Estimated completion information |
| 36 | + - Guidance on next steps |
| 37 | +
|
| 38 | + Examples: |
| 39 | + # Check current migration status |
| 40 | + migration_status() |
| 41 | +
|
| 42 | + # Get migration status for specific project context |
| 43 | + migration_status(project="work-project") |
| 44 | + """ |
| 45 | + logger.info("MCP tool call tool=migration_status") |
| 46 | + |
| 47 | + try: |
| 48 | + from basic_memory.services.migration_service import migration_manager |
| 49 | + |
| 50 | + # Get current migration state |
| 51 | + state = migration_manager.state |
| 52 | + |
| 53 | + # Build detailed status response |
| 54 | + status_lines = [ |
| 55 | + "# Migration Status", |
| 56 | + "", |
| 57 | + f"**Current Status**: {state.status.value.replace('_', ' ').title()}", |
| 58 | + "", |
| 59 | + ] |
| 60 | + |
| 61 | + if migration_manager.is_ready: |
| 62 | + status_lines.extend( |
| 63 | + [ |
| 64 | + "✅ **System Ready**: All migrations completed successfully", |
| 65 | + "", |
| 66 | + "The system is fully operational and ready for normal use. All MCP tools", |
| 67 | + "are available and functioning normally.", |
| 68 | + ] |
| 69 | + ) |
| 70 | + else: |
| 71 | + # Migration in progress or failed |
| 72 | + status_lines.append(f"**Message**: {state.message}") |
| 73 | + |
| 74 | + if state.status.value == "in_progress": |
| 75 | + status_lines.extend( |
| 76 | + [ |
| 77 | + "", |
| 78 | + "🔄 **Migration in Progress**", |
| 79 | + "", |
| 80 | + ] |
| 81 | + ) |
| 82 | + |
| 83 | + if state.projects_total > 0: |
| 84 | + progress_pct = (state.projects_migrated / state.projects_total) * 100 |
| 85 | + status_lines.extend( |
| 86 | + [ |
| 87 | + f"- **Progress**: {state.projects_migrated}/{state.projects_total} projects ({progress_pct:.0f}%)", |
| 88 | + f"- **Remaining**: {state.projects_total - state.projects_migrated} projects", |
| 89 | + ] |
| 90 | + ) |
| 91 | + |
| 92 | + status_lines.extend( |
| 93 | + [ |
| 94 | + "", |
| 95 | + "**What's happening**: Basic Memory is migrating legacy project data", |
| 96 | + "to the new unified database format. This process runs in the background", |
| 97 | + "and most tools will show status messages until completion.", |
| 98 | + "", |
| 99 | + "**Estimated time**: Usually 1-3 minutes depending on knowledge base size", |
| 100 | + "", |
| 101 | + "**What you can do**: Wait for migration to complete, or check status", |
| 102 | + "again in a few moments. The system will be fully operational once finished.", |
| 103 | + ] |
| 104 | + ) |
| 105 | + |
| 106 | + elif state.status.value == "failed": |
| 107 | + status_lines.extend( |
| 108 | + [ |
| 109 | + "", |
| 110 | + "❌ **Migration Failed**", |
| 111 | + "", |
| 112 | + f"**Error**: {state.error or 'Unknown error occurred'}", |
| 113 | + "", |
| 114 | + "**What this means**: The automatic migration encountered an issue.", |
| 115 | + "Basic Memory may still work, but some legacy data might not be available.", |
| 116 | + "", |
| 117 | + "**Recommended actions**:", |
| 118 | + "1. Try running `basic-memory sync` manually from the command line", |
| 119 | + "2. Check the logs for more detailed error information", |
| 120 | + "3. If issues persist, consider filing a support issue", |
| 121 | + ] |
| 122 | + ) |
| 123 | + |
| 124 | + elif state.status.value == "pending": |
| 125 | + status_lines.extend( |
| 126 | + [ |
| 127 | + "", |
| 128 | + "⏳ **Migration Pending**", |
| 129 | + "", |
| 130 | + "Migration has been detected as needed but hasn't started yet.", |
| 131 | + "This usually resolves automatically within a few seconds.", |
| 132 | + ] |
| 133 | + ) |
| 134 | + |
| 135 | + # Add project context if provided |
| 136 | + if project: |
| 137 | + try: |
| 138 | + active_project = get_active_project(project) |
| 139 | + status_lines.extend( |
| 140 | + [ |
| 141 | + "", |
| 142 | + "---", |
| 143 | + "", |
| 144 | + f"**Active Project**: {active_project.name}", |
| 145 | + f"**Project Path**: {active_project.path}", |
| 146 | + ] |
| 147 | + ) |
| 148 | + except Exception as e: |
| 149 | + logger.debug(f"Could not get project info: {e}") |
| 150 | + # Don't fail the tool for project info issues |
| 151 | + |
| 152 | + return "\n".join(status_lines) |
| 153 | + |
| 154 | + except Exception as e: |
| 155 | + logger.error(f"Error checking migration status: {e}") |
| 156 | + return f"""# Migration Status - Error |
| 157 | +
|
| 158 | +❌ **Unable to check migration status** |
| 159 | +
|
| 160 | +**Error**: {str(e)} |
| 161 | +
|
| 162 | +**What this means**: There was a technical issue checking the migration status. |
| 163 | +The system is likely functioning normally, but status information is unavailable. |
| 164 | +
|
| 165 | +**Recommended action**: Try again in a moment, or proceed with normal operations. |
| 166 | +""" |
0 commit comments