|
| 1 | +import { Tool } from '@modelcontextprotocol/sdk/types.js' |
| 2 | +import { DevCycleApiClient, handleZodiosValidationErrors } from '../utils/api' |
| 3 | +import { |
| 4 | + fetchFeatureTotalEvaluations, |
| 5 | + fetchProjectTotalEvaluations, |
| 6 | +} from '../../api/results' |
| 7 | +import { |
| 8 | + GetFeatureTotalEvaluationsArgsSchema, |
| 9 | + GetProjectTotalEvaluationsArgsSchema, |
| 10 | + FeatureTotalEvaluationsQuerySchema, |
| 11 | + ProjectTotalEvaluationsQuerySchema, |
| 12 | +} from '../types' |
| 13 | +import { ToolHandler } from '../server' |
| 14 | +import { |
| 15 | + DASHBOARD_LINK_PROPERTY, |
| 16 | + FEATURE_KEY_PROPERTY, |
| 17 | + EVALUATION_QUERY_PROPERTIES, |
| 18 | + EVALUATION_DATA_POINT_SCHEMA, |
| 19 | + PROJECT_DATA_POINT_SCHEMA, |
| 20 | +} from './commonSchemas' |
| 21 | + |
| 22 | +// Helper functions to generate dashboard links |
| 23 | +const generateFeatureAnalyticsDashboardLink = ( |
| 24 | + orgId: string, |
| 25 | + projectKey: string, |
| 26 | + featureKey: string, |
| 27 | +): string => { |
| 28 | + return `https://app.devcycle.com/o/${orgId}/p/${projectKey}/features/${featureKey}/analytics` |
| 29 | +} |
| 30 | + |
| 31 | +const generateProjectAnalyticsDashboardLink = ( |
| 32 | + orgId: string, |
| 33 | + projectKey: string, |
| 34 | +): string => { |
| 35 | + return `https://app.devcycle.com/o/${orgId}/p/${projectKey}/analytics` |
| 36 | +} |
| 37 | + |
| 38 | +// ============================================================================= |
| 39 | +// INPUT SCHEMAS |
| 40 | +// ============================================================================= |
| 41 | + |
| 42 | +const FEATURE_EVALUATION_QUERY_PROPERTIES = { |
| 43 | + featureKey: FEATURE_KEY_PROPERTY, |
| 44 | + ...EVALUATION_QUERY_PROPERTIES, |
| 45 | +} |
| 46 | + |
| 47 | +const PROJECT_EVALUATION_QUERY_PROPERTIES = EVALUATION_QUERY_PROPERTIES |
| 48 | + |
| 49 | +// ============================================================================= |
| 50 | +// OUTPUT SCHEMAS |
| 51 | +// ============================================================================= |
| 52 | + |
| 53 | +const FEATURE_EVALUATIONS_OUTPUT_SCHEMA = { |
| 54 | + type: 'object' as const, |
| 55 | + properties: { |
| 56 | + result: { |
| 57 | + type: 'object' as const, |
| 58 | + description: 'Feature evaluation data aggregated by time period', |
| 59 | + properties: { |
| 60 | + evaluations: { |
| 61 | + type: 'array' as const, |
| 62 | + description: 'Array of evaluation data points', |
| 63 | + items: EVALUATION_DATA_POINT_SCHEMA, |
| 64 | + }, |
| 65 | + cached: { |
| 66 | + type: 'boolean' as const, |
| 67 | + description: 'Whether this result came from cache', |
| 68 | + }, |
| 69 | + updatedAt: { |
| 70 | + type: 'string' as const, |
| 71 | + format: 'date-time' as const, |
| 72 | + description: 'When the data was last updated', |
| 73 | + }, |
| 74 | + }, |
| 75 | + required: ['evaluations', 'cached', 'updatedAt'], |
| 76 | + }, |
| 77 | + dashboardLink: DASHBOARD_LINK_PROPERTY, |
| 78 | + }, |
| 79 | + required: ['result', 'dashboardLink'], |
| 80 | +} |
| 81 | + |
| 82 | +const PROJECT_EVALUATIONS_OUTPUT_SCHEMA = { |
| 83 | + type: 'object' as const, |
| 84 | + properties: { |
| 85 | + result: { |
| 86 | + type: 'object' as const, |
| 87 | + description: 'Project evaluation data aggregated by time period', |
| 88 | + properties: { |
| 89 | + evaluations: { |
| 90 | + type: 'array' as const, |
| 91 | + description: 'Array of evaluation data points', |
| 92 | + items: PROJECT_DATA_POINT_SCHEMA, |
| 93 | + }, |
| 94 | + cached: { |
| 95 | + type: 'boolean' as const, |
| 96 | + description: 'Whether this result came from cache', |
| 97 | + }, |
| 98 | + updatedAt: { |
| 99 | + type: 'string' as const, |
| 100 | + format: 'date-time' as const, |
| 101 | + description: 'When the data was last updated', |
| 102 | + }, |
| 103 | + }, |
| 104 | + required: ['evaluations', 'cached', 'updatedAt'], |
| 105 | + }, |
| 106 | + dashboardLink: DASHBOARD_LINK_PROPERTY, |
| 107 | + }, |
| 108 | + required: ['result', 'dashboardLink'], |
| 109 | +} |
| 110 | + |
| 111 | +// ============================================================================= |
| 112 | +// TOOL DEFINITIONS |
| 113 | +// ============================================================================= |
| 114 | + |
| 115 | +export const resultsToolDefinitions: Tool[] = [ |
| 116 | + { |
| 117 | + name: 'get_feature_total_evaluations', |
| 118 | + description: |
| 119 | + 'Get total variable evaluations per time period for a specific feature. Include dashboard link in the response.', |
| 120 | + inputSchema: { |
| 121 | + type: 'object', |
| 122 | + properties: FEATURE_EVALUATION_QUERY_PROPERTIES, |
| 123 | + required: ['featureKey'], |
| 124 | + }, |
| 125 | + outputSchema: FEATURE_EVALUATIONS_OUTPUT_SCHEMA, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: 'get_project_total_evaluations', |
| 129 | + description: |
| 130 | + 'Get total variable evaluations per time period for the entire project. Include dashboard link in the response.', |
| 131 | + inputSchema: { |
| 132 | + type: 'object', |
| 133 | + properties: PROJECT_EVALUATION_QUERY_PROPERTIES, |
| 134 | + }, |
| 135 | + outputSchema: PROJECT_EVALUATIONS_OUTPUT_SCHEMA, |
| 136 | + }, |
| 137 | +] |
| 138 | + |
| 139 | +export const resultsToolHandlers: Record<string, ToolHandler> = { |
| 140 | + get_feature_total_evaluations: async ( |
| 141 | + args: unknown, |
| 142 | + apiClient: DevCycleApiClient, |
| 143 | + ) => { |
| 144 | + const validatedArgs = GetFeatureTotalEvaluationsArgsSchema.parse(args) |
| 145 | + |
| 146 | + return await apiClient.executeWithDashboardLink( |
| 147 | + 'getFeatureTotalEvaluations', |
| 148 | + validatedArgs, |
| 149 | + async (authToken, projectKey) => { |
| 150 | + const { featureKey, ...apiQueries } = validatedArgs |
| 151 | + |
| 152 | + return await handleZodiosValidationErrors( |
| 153 | + () => |
| 154 | + fetchFeatureTotalEvaluations( |
| 155 | + authToken, |
| 156 | + projectKey, |
| 157 | + featureKey, |
| 158 | + apiQueries, |
| 159 | + ), |
| 160 | + 'fetchFeatureTotalEvaluations', |
| 161 | + ) |
| 162 | + }, |
| 163 | + (orgId, projectKey) => |
| 164 | + generateFeatureAnalyticsDashboardLink( |
| 165 | + orgId, |
| 166 | + projectKey, |
| 167 | + validatedArgs.featureKey, |
| 168 | + ), |
| 169 | + ) |
| 170 | + }, |
| 171 | + get_project_total_evaluations: async ( |
| 172 | + args: unknown, |
| 173 | + apiClient: DevCycleApiClient, |
| 174 | + ) => { |
| 175 | + const validatedArgs = GetProjectTotalEvaluationsArgsSchema.parse(args) |
| 176 | + |
| 177 | + return await apiClient.executeWithDashboardLink( |
| 178 | + 'getProjectTotalEvaluations', |
| 179 | + validatedArgs, |
| 180 | + async (authToken, projectKey) => { |
| 181 | + return await handleZodiosValidationErrors( |
| 182 | + () => |
| 183 | + fetchProjectTotalEvaluations( |
| 184 | + authToken, |
| 185 | + projectKey, |
| 186 | + validatedArgs, |
| 187 | + ), |
| 188 | + 'fetchProjectTotalEvaluations', |
| 189 | + ) |
| 190 | + }, |
| 191 | + generateProjectAnalyticsDashboardLink, |
| 192 | + ) |
| 193 | + }, |
| 194 | +} |
0 commit comments