|
| 1 | +import { type FastifyInstance } from 'fastify'; |
| 2 | +import { RegistrySyncService } from '../../services/registrySyncService'; |
| 3 | +import { JobQueueService } from '../../services/jobQueueService'; |
| 4 | +import { getDb } from '../../db'; |
| 5 | +import { requirePermission } from '../../middleware/roleMiddleware'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Admin routes for MCP Registry synchronization |
| 9 | + * |
| 10 | + * These routes allow administrators to: |
| 11 | + * - Trigger sync from official MCP Registry |
| 12 | + * - Monitor sync progress via existing job queue APIs |
| 13 | + */ |
| 14 | + |
| 15 | +export default async function mcpRegistryRoutes(server: FastifyInstance) { |
| 16 | + /** |
| 17 | + * Trigger MCP Registry sync via job queue |
| 18 | + * |
| 19 | + * This endpoint: |
| 20 | + * 1. Fetches server list from registry.modelcontextprotocol.io |
| 21 | + * 2. Creates a job batch for tracking |
| 22 | + * 3. Creates individual jobs for each server with rate-limited scheduling |
| 23 | + * 4. Returns batch ID for progress monitoring |
| 24 | + * |
| 25 | + * Progress can be monitored via: GET /api/admin/jobs/batches/{batchId} |
| 26 | + */ |
| 27 | + server.post('/admin/mcp-registry/sync', { |
| 28 | + preValidation: requirePermission('mcp.registry.sync'), |
| 29 | + schema: { |
| 30 | + tags: ['Admin - MCP Registry'], |
| 31 | + summary: 'Trigger MCP Registry sync via job queue', |
| 32 | + description: 'Trigger synchronization with official MCP Registry using background jobs for rate limiting. Progress can be monitored via GET /api/admin/jobs/batches/{batchId}', |
| 33 | + security: [{ cookieAuth: [] }], |
| 34 | + |
| 35 | + body: { |
| 36 | + type: 'object', |
| 37 | + properties: { |
| 38 | + maxServers: { |
| 39 | + type: 'number', |
| 40 | + minimum: 1, |
| 41 | + maximum: 1000, |
| 42 | + description: 'Maximum number of servers to sync (for testing). Omit to sync all servers.' |
| 43 | + }, |
| 44 | + skipExisting: { |
| 45 | + type: 'boolean', |
| 46 | + default: true, |
| 47 | + description: 'Skip servers that already exist in the database' |
| 48 | + }, |
| 49 | + forceRefresh: { |
| 50 | + type: 'boolean', |
| 51 | + default: false, |
| 52 | + description: 'Force refresh of existing servers (overrides skipExisting)' |
| 53 | + }, |
| 54 | + rateLimitDelay: { |
| 55 | + type: 'number', |
| 56 | + minimum: 1, |
| 57 | + maximum: 10, |
| 58 | + default: 2, |
| 59 | + description: 'Delay between jobs in seconds (default: 2)' |
| 60 | + } |
| 61 | + }, |
| 62 | + additionalProperties: false |
| 63 | + }, |
| 64 | + |
| 65 | + response: { |
| 66 | + 200: { |
| 67 | + type: 'object', |
| 68 | + properties: { |
| 69 | + success: { type: 'boolean' }, |
| 70 | + data: { |
| 71 | + type: 'object', |
| 72 | + properties: { |
| 73 | + batchId: { type: 'string', description: 'Job batch ID for tracking progress' }, |
| 74 | + totalServers: { type: 'number', description: 'Total servers to sync' }, |
| 75 | + jobsCreated: { type: 'number', description: 'Number of jobs created' }, |
| 76 | + estimatedCompletion: { type: 'string', description: 'Estimated completion time (ISO 8601)' }, |
| 77 | + rateLimitDelay: { type: 'number', description: 'Delay between jobs in seconds' } |
| 78 | + }, |
| 79 | + required: ['batchId', 'totalServers', 'jobsCreated', 'estimatedCompletion'] |
| 80 | + } |
| 81 | + }, |
| 82 | + required: ['success', 'data'] |
| 83 | + }, |
| 84 | + 500: { |
| 85 | + type: 'object', |
| 86 | + properties: { |
| 87 | + success: { type: 'boolean' }, |
| 88 | + error: { type: 'string' } |
| 89 | + }, |
| 90 | + required: ['success', 'error'] |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + }, async (request, reply) => { |
| 95 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 96 | + const config = (request.body || {}) as any; |
| 97 | + |
| 98 | + request.log.info({ |
| 99 | + operation: 'mcp_registry_sync_triggered', |
| 100 | + userId: request.user!.id, |
| 101 | + config, |
| 102 | + }, 'MCP Registry sync via job queue triggered'); |
| 103 | + |
| 104 | + try { |
| 105 | + const db = getDb(); |
| 106 | + const jobQueueService = new JobQueueService(db, request.log); |
| 107 | + const syncService = new RegistrySyncService(db, request.log, jobQueueService); |
| 108 | + |
| 109 | + const stats = await syncService.syncAllServersViaJobQueue({ |
| 110 | + maxServers: config.maxServers || null, |
| 111 | + skipExisting: config.skipExisting !== false, |
| 112 | + forceRefresh: config.forceRefresh || false, |
| 113 | + rateLimitDelay: config.rateLimitDelay || 2, |
| 114 | + }, request.user!.id); |
| 115 | + |
| 116 | + const responseData = { |
| 117 | + success: true, |
| 118 | + data: { |
| 119 | + batchId: stats.batchId, |
| 120 | + totalServers: stats.totalServers, |
| 121 | + jobsCreated: stats.jobsCreated, |
| 122 | + estimatedCompletion: stats.estimatedCompletion.toISOString(), |
| 123 | + rateLimitDelay: config.rateLimitDelay || 2, |
| 124 | + }, |
| 125 | + }; |
| 126 | + const jsonString = JSON.stringify(responseData); |
| 127 | + return reply.status(200).type('application/json').send(jsonString); |
| 128 | + |
| 129 | + } catch (error) { |
| 130 | + request.log.error({ error }, 'MCP Registry sync via job queue failed'); |
| 131 | + const errorResponse = { |
| 132 | + success: false, |
| 133 | + error: error instanceof Error ? error.message : 'Failed to start MCP Registry sync' |
| 134 | + }; |
| 135 | + const jsonString = JSON.stringify(errorResponse); |
| 136 | + return reply.status(500).type('application/json').send(jsonString); |
| 137 | + } |
| 138 | + }); |
| 139 | +} |
0 commit comments