|
1 |
| -import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify'; |
2 |
| -import { getUserConfigurationByIdSchema, GetUserConfigurationByIdRequest, formatUserConfigResponse } from './schemas'; |
3 |
| -import { mcpUserConfigurationService } from '../../../services/mcpUserConfigurationService'; |
4 |
| -import { authHook } from '../../../hooks/authHook'; |
5 |
| - |
6 |
| -export default async function getUserConfigurationRoute(fastify: FastifyInstance) { |
7 |
| - fastify.get<GetUserConfigurationByIdRequest>( |
8 |
| - '/teams/:teamId/mcp/installations/:installationId/user-configs/:configId', |
9 |
| - { |
10 |
| - schema: getUserConfigurationByIdSchema, |
11 |
| - preHandler: [authHook] |
12 |
| - }, |
13 |
| - async (request: FastifyRequest<GetUserConfigurationByIdRequest>, reply: FastifyReply) => { |
14 |
| - const { teamId, installationId, configId } = request.params; |
15 |
| - const userId = request.user!.id; |
16 |
| - |
17 |
| - try { |
18 |
| - // Get the user configuration |
19 |
| - const userConfig = await mcpUserConfigurationService.getUserConfigurationById( |
20 |
| - configId, |
21 |
| - userId, |
22 |
| - teamId |
23 |
| - ); |
24 |
| - |
25 |
| - if (!userConfig) { |
26 |
| - return reply.status(404).send({ |
27 |
| - error: 'User configuration not found' |
28 |
| - }); |
29 |
| - } |
30 |
| - |
31 |
| - // Format and return the response |
32 |
| - const response = formatUserConfigResponse(userConfig); |
33 |
| - return reply.send(response); |
34 |
| - |
35 |
| - } catch (error) { |
36 |
| - fastify.log.error({ |
37 |
| - operation: 'get_user_configuration', |
38 |
| - error, |
| 1 | +import { type FastifyInstance } from 'fastify'; |
| 2 | +import { requireAuthenticationAny, requireOAuthScope } from '../../../middleware/oauthMiddleware'; |
| 3 | +import { requireTeamPermission } from '../../../middleware/roleMiddleware'; |
| 4 | +import { McpUserConfigurationService } from '../../../services/mcpUserConfigurationService'; |
| 5 | +import { getDb } from '../../../db'; |
| 6 | +import { |
| 7 | + TEAM_AND_INSTALLATION_AND_CONFIG_PARAMS_SCHEMA, |
| 8 | + USER_CONFIG_SUCCESS_RESPONSE_SCHEMA, |
| 9 | + COMMON_ERROR_RESPONSES, |
| 10 | + DUAL_AUTH_SECURITY, |
| 11 | + formatUserConfigResponse, |
| 12 | + type TeamAndInstallationAndConfigParams, |
| 13 | + type UserConfigSuccessResponse, |
| 14 | + type ErrorResponse |
| 15 | +} from './schemas'; |
| 16 | + |
| 17 | +export default async function getUserConfigurationRoute(server: FastifyInstance) { |
| 18 | + server.get<{ |
| 19 | + Params: TeamAndInstallationAndConfigParams; |
| 20 | + }>('/teams/:teamId/mcp/installations/:installationId/user-configs/:configId', { |
| 21 | + preValidation: [ |
| 22 | + requireAuthenticationAny(), |
| 23 | + requireOAuthScope('mcp:user-configs:read'), |
| 24 | + requireTeamPermission('mcp.installations.view') |
| 25 | + ], |
| 26 | + schema: { |
| 27 | + tags: ['MCP User Configurations'], |
| 28 | + summary: 'Get user configuration by ID', |
| 29 | + description: 'Retrieves a specific user configuration for an MCP server installation. No Content-Type header required for this GET request. Supports both cookie-based authentication (for web users) and OAuth2 Bearer token authentication (for CLI users). Requires mcp:user-configs:read scope for OAuth2 access.', |
| 30 | + security: DUAL_AUTH_SECURITY, |
| 31 | + |
| 32 | + // Fastify validation schema |
| 33 | + params: TEAM_AND_INSTALLATION_AND_CONFIG_PARAMS_SCHEMA, |
| 34 | + |
| 35 | + response: { |
| 36 | + 200: { |
| 37 | + ...USER_CONFIG_SUCCESS_RESPONSE_SCHEMA, |
| 38 | + description: 'User configuration retrieved successfully' |
| 39 | + }, |
| 40 | + ...COMMON_ERROR_RESPONSES |
| 41 | + } |
| 42 | + } |
| 43 | + }, async (request, reply) => { |
| 44 | + const { teamId, installationId, configId } = request.params; |
| 45 | + const userId = request.user!.id; |
| 46 | + const authType = request.tokenPayload ? 'oauth2' : 'cookie'; |
| 47 | + |
| 48 | + request.log.debug({ |
| 49 | + operation: 'mcp_user_config_operation', |
| 50 | + userId, |
| 51 | + authType, |
| 52 | + clientId: request.tokenPayload?.clientId, |
| 53 | + scope: request.tokenPayload?.scope, |
| 54 | + endpoint: request.url |
| 55 | + }, 'Authentication method determined for MCP user configuration operation'); |
| 56 | + |
| 57 | + request.log.info({ |
| 58 | + operation: 'get_mcp_user_config', |
| 59 | + teamId, |
| 60 | + installationId, |
| 61 | + configId, |
| 62 | + userId, |
| 63 | + authType |
| 64 | + }, 'Getting MCP user configuration'); |
| 65 | + |
| 66 | + try { |
| 67 | + const db = getDb(); |
| 68 | + const userConfigService = new McpUserConfigurationService(db, request.log); |
| 69 | + |
| 70 | + const userConfig = await userConfigService.getUserConfigurationById( |
| 71 | + configId, |
| 72 | + userId, |
| 73 | + teamId |
| 74 | + ); |
| 75 | + |
| 76 | + if (!userConfig) { |
| 77 | + request.log.warn({ |
| 78 | + operation: 'get_mcp_user_config', |
39 | 79 | teamId,
|
40 | 80 | installationId,
|
41 | 81 | configId,
|
42 |
| - userId |
43 |
| - }, 'Error getting user configuration'); |
44 |
| - return reply.status(500).send({ |
45 |
| - error: 'Internal server error' |
46 |
| - }); |
| 82 | + userId, |
| 83 | + found: false |
| 84 | + }, 'MCP user configuration not found'); |
| 85 | + |
| 86 | + const errorResponse: ErrorResponse = { |
| 87 | + success: false, |
| 88 | + error: 'User configuration not found' |
| 89 | + }; |
| 90 | + const jsonString = JSON.stringify(errorResponse); |
| 91 | + return reply.status(404).type('application/json').send(jsonString); |
47 | 92 | }
|
| 93 | + |
| 94 | + request.log.info({ |
| 95 | + operation: 'get_mcp_user_config', |
| 96 | + teamId, |
| 97 | + installationId, |
| 98 | + configId, |
| 99 | + userId, |
| 100 | + authType |
| 101 | + }, 'Successfully retrieved MCP user configuration'); |
| 102 | + |
| 103 | + const successResponse: UserConfigSuccessResponse = { |
| 104 | + success: true, |
| 105 | + data: formatUserConfigResponse(userConfig) |
| 106 | + }; |
| 107 | + const jsonString = JSON.stringify(successResponse); |
| 108 | + return reply.status(200).type('application/json').send(jsonString); |
| 109 | + |
| 110 | + } catch (error) { |
| 111 | + request.log.error({ |
| 112 | + operation: 'get_mcp_user_config', |
| 113 | + error, |
| 114 | + teamId, |
| 115 | + installationId, |
| 116 | + configId, |
| 117 | + userId |
| 118 | + }, 'Failed to retrieve MCP user configuration'); |
| 119 | + |
| 120 | + const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred'; |
| 121 | + |
| 122 | + const errorResponse: ErrorResponse = { |
| 123 | + success: false, |
| 124 | + error: errorMessage |
| 125 | + }; |
| 126 | + const jsonString = JSON.stringify(errorResponse); |
| 127 | + return reply.status(500).type('application/json').send(jsonString); |
48 | 128 | }
|
49 |
| - ); |
| 129 | + }); |
50 | 130 | }
|
0 commit comments