UNPKG

mcp-gdrive-enhanced-markov

Version:

MCP server for Google Drive and Sheets with write capabilities, AI-powered PDF analysis, SQLite document indexing, and advanced directory exploration tools

120 lines (119 loc) 5.66 kB
import { z } from 'zod'; import { smartUpdater } from './smart_updater.js'; import { dbV2 } from './database_v2.js'; export const name = 'gdrive_update_index'; export const description = 'Update the Google Drive index with any changes since last sync using the Drive Changes API'; export const inputSchema = z.object({ fullSync: z.boolean().optional().default(false) .describe('Perform a full sync instead of incremental (checks all files)'), processQueue: z.boolean().optional().default(true) .describe('Process the queue after finding changes'), showStats: z.boolean().optional().default(true) .describe('Show database statistics after update') }); export const schema = { name, description, inputSchema: { type: 'object', properties: { fullSync: { type: 'boolean', description: 'Perform a full sync instead of incremental', default: false }, processQueue: { type: 'boolean', description: 'Process the queue after finding changes', default: true }, showStats: { type: 'boolean', description: 'Show database statistics after update', default: true } }, required: [] } }; export async function gdrive_update_index(args) { try { console.log(`Starting ${args.fullSync ? 'full' : 'incremental'} sync...`); // Check for updates const updateSummary = args.fullSync ? await smartUpdater.performFullSync() : await smartUpdater.checkForUpdates(); const output = [ `Update check complete:`, `- New files found: ${updateSummary.new_files}`, `- Updated files: ${updateSummary.updated_files}`, `- Deleted files: ${updateSummary.deleted_files}`, `- Total changes: ${updateSummary.total_changes}`, `- Duration: ${updateSummary.duration_seconds.toFixed(1)} seconds` ]; // Process the queue if there are changes if (args.processQueue && updateSummary.total_changes > 0) { output.push('', 'Processing queue...'); const processingStart = Date.now(); let lastProgress = ''; const progressCallback = (progress) => { const progressStr = `Processed: ${progress.processed}/${progress.total} | Success: ${progress.succeeded} | Failed: ${progress.failed}`; if (progressStr !== lastProgress) { console.log(progressStr); lastProgress = progressStr; } }; const queueResult = await smartUpdater.processQueueWithProgress(progressCallback); const processingDuration = (Date.now() - processingStart) / 1000; output.push(`Queue processing complete:`, `- Processed: ${queueResult.processed}`, `- Failed: ${queueResult.failed}`, `- Duration: ${processingDuration.toFixed(1)} seconds`); } // Show stats if requested if (args.showStats) { const stats = await dbV2.getStats(); const syncState = await dbV2.getSyncState(); output.push('', 'Database Statistics:', `- Total documents: ${stats.total_documents}`, `- Documents by type:`); for (const [type, count] of Object.entries(stats.by_type)) { output.push(` - ${type}: ${count}`); } output.push(`- Total contract value: $${stats.total_contract_value.toLocaleString()}`, `- Unpaid invoices value: $${stats.unpaid_invoices_value.toLocaleString()}`, `- Documents indexed in last 7 days: ${stats.documents_last_7_days}`, '', 'Sync State:', `- Last sync: ${syncState.last_sync_time || 'Never'}`, `- Total files indexed: ${syncState.total_files_indexed}`, `- Total errors: ${syncState.total_errors}`); // Check for expiring contracts const expiringContracts = await dbV2.getExpiringContracts(30); if (expiringContracts.length > 0) { output.push('', `⚠️ ${expiringContracts.length} contracts expiring in next 30 days:`); expiringContracts.slice(0, 5).forEach(contract => { output.push(`- ${contract.name} expires ${contract.expiration_date}`); }); if (expiringContracts.length > 5) { output.push(`... and ${expiringContracts.length - 5} more`); } } // Check for overdue invoices const overdueInvoices = await dbV2.getOverdueInvoices(); if (overdueInvoices.length > 0) { output.push('', `⚠️ ${overdueInvoices.length} overdue invoices:`); overdueInvoices.slice(0, 5).forEach(invoice => { output.push(`- ${invoice.name}: $${invoice.total_amount?.toLocaleString() || 0} (due ${invoice.due_date})`); }); if (overdueInvoices.length > 5) { output.push(`... and ${overdueInvoices.length - 5} more`); } } } return { content: [{ type: 'text', text: output.join('\n') }], isError: false }; } catch (error) { return { content: [{ type: 'text', text: `Error updating index: ${error instanceof Error ? error.message : 'Unknown error'}` }], isError: true }; } }