|
| 1 | +import { type FastifyInstance } from 'fastify'; |
| 2 | +import { z } from 'zod'; |
| 3 | +import { createSchema } from 'zod-openapi'; |
| 4 | +import { requireTeamPermission } from '../../../middleware/roleMiddleware'; |
| 5 | +import { McpInstallationService } from '../../../services/mcpInstallationService'; |
| 6 | +import { getDb } from '../../../db'; |
| 7 | + |
| 8 | +// Response schemas |
| 9 | +const installationSchema = z.object({ |
| 10 | + id: z.string(), |
| 11 | + team_id: z.string(), |
| 12 | + server_id: z.string(), |
| 13 | + user_id: z.string(), |
| 14 | + installation_name: z.string(), |
| 15 | + installation_type: z.enum(['local', 'cloud']), |
| 16 | + user_environment_variables: z.record(z.string(), z.string()).optional(), |
| 17 | + created_at: z.string(), |
| 18 | + updated_at: z.string(), |
| 19 | + last_used_at: z.string().nullable(), |
| 20 | + server: z.object({ |
| 21 | + id: z.string(), |
| 22 | + name: z.string(), |
| 23 | + description: z.string(), |
| 24 | + github_url: z.string().nullable(), |
| 25 | + homepage_url: z.string().nullable(), |
| 26 | + author_name: z.string().nullable(), |
| 27 | + language: z.string(), |
| 28 | + runtime: z.string(), |
| 29 | + status: z.enum(['active', 'deprecated', 'maintenance']), |
| 30 | + tags: z.array(z.string()).nullable(), |
| 31 | + environment_variables: z.array(z.any()).nullable(), |
| 32 | + category_id: z.string().nullable() |
| 33 | + }).optional() |
| 34 | +}); |
| 35 | + |
| 36 | +const successResponseSchema = z.object({ |
| 37 | + success: z.boolean().default(true), |
| 38 | + data: installationSchema |
| 39 | +}); |
| 40 | + |
| 41 | +const errorResponseSchema = z.object({ |
| 42 | + success: z.boolean().default(false), |
| 43 | + error: z.string() |
| 44 | +}); |
| 45 | + |
| 46 | +export default async function getInstallationRoute(fastify: FastifyInstance) { |
| 47 | + fastify.get<{ |
| 48 | + Params: { teamId: string; installationId: string }; |
| 49 | + }>('/teams/:teamId/mcp/installations/:installationId', { |
| 50 | + schema: { |
| 51 | + tags: ['MCP Installations'], |
| 52 | + summary: 'Get MCP installation by ID', |
| 53 | + description: 'Retrieves a specific MCP server installation by ID for the specified team.', |
| 54 | + security: [{ cookieAuth: [] }], |
| 55 | + params: createSchema(z.object({ |
| 56 | + teamId: z.string().min(1, 'Team ID is required'), |
| 57 | + installationId: z.string().min(1, 'Installation ID is required') |
| 58 | + }), { |
| 59 | + }), |
| 60 | + response: { |
| 61 | + 200: createSchema(successResponseSchema.describe('Installation details')), |
| 62 | + 401: createSchema(errorResponseSchema.describe('Unauthorized - Authentication required')), |
| 63 | + 403: createSchema(errorResponseSchema.describe('Forbidden - Insufficient permissions')), |
| 64 | + 404: createSchema(errorResponseSchema.describe('Not Found - Installation not found')) |
| 65 | + } |
| 66 | + }, |
| 67 | + preValidation: requireTeamPermission('mcp.installations.view') |
| 68 | + }, async (request, reply) => { |
| 69 | + const { teamId, installationId } = request.params; |
| 70 | + const userId = request.user!.id; |
| 71 | + |
| 72 | + request.log.info({ |
| 73 | + operation: 'get_mcp_installation', |
| 74 | + teamId, |
| 75 | + installationId, |
| 76 | + userId |
| 77 | + }, 'Getting MCP installation by ID'); |
| 78 | + |
| 79 | + try { |
| 80 | + const db = getDb(); |
| 81 | + const installationService = new McpInstallationService(db, request.log); |
| 82 | + |
| 83 | + const installation = await installationService.getInstallationById(installationId, teamId); |
| 84 | + |
| 85 | + if (!installation) { |
| 86 | + request.log.warn({ |
| 87 | + operation: 'get_mcp_installation', |
| 88 | + teamId, |
| 89 | + installationId, |
| 90 | + userId |
| 91 | + }, 'MCP installation not found'); |
| 92 | + |
| 93 | + return reply.status(404).send({ |
| 94 | + success: false, |
| 95 | + error: 'Installation not found' |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + request.log.info({ |
| 100 | + operation: 'get_mcp_installation', |
| 101 | + teamId, |
| 102 | + installationId, |
| 103 | + userId |
| 104 | + }, 'Retrieved MCP installation'); |
| 105 | + |
| 106 | + return reply.status(200).send({ |
| 107 | + success: true, |
| 108 | + data: { |
| 109 | + ...installation, |
| 110 | + created_at: installation.created_at.toISOString(), |
| 111 | + updated_at: installation.updated_at.toISOString(), |
| 112 | + last_used_at: installation.last_used_at?.toISOString() || null |
| 113 | + } |
| 114 | + }); |
| 115 | + |
| 116 | + } catch (error) { |
| 117 | + request.log.error({ |
| 118 | + operation: 'get_mcp_installation', |
| 119 | + error, |
| 120 | + teamId, |
| 121 | + installationId, |
| 122 | + userId |
| 123 | + }, 'Failed to get MCP installation'); |
| 124 | + |
| 125 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; |
| 126 | + |
| 127 | + return reply.status(500).send({ |
| 128 | + success: false, |
| 129 | + error: errorMessage |
| 130 | + }); |
| 131 | + } |
| 132 | + }); |
| 133 | +} |
0 commit comments