All files / src/config instances-schema.ts

100% Statements 79/79
96.22% Branches 51/53
100% Functions 5/5
100% Lines 78/78

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326              113x               113x       48x     48x     48x 42x     6x 1x       6x 9x 5x   5x 1x   5x         48x             113x                           113x                                                   113x                               113x                           113x                         113x                                                                                                     113x 23x 23x 1x     22x     22x 19x 4x 4x               22x 22x   12x 12x     10x       22x   10x 10x 9x   9x 9x 9x 5x 5x 4x 4x 4x 4x 4x                     22x 6x 6x 5x   5x 5x 5x 5x 5x 5x                     22x 1x       21x           21x 9x         9x 4x       21x           113x 11x           113x       15x 10x     5x     5x 2x       5x 1x           5x    
/**
 * Instance Configuration Schemas
 *
 * Zod schemas for multi-instance GitLab configuration.
 * Supports YAML/JSON configuration files and environment variable formats.
 */
 
import { z } from "zod";
 
/**
 * URL validation and normalization
 * Accepts http/https URLs, preserves subpath for relative URL root deployments,
 * removes trailing slashes and /api/v4 suffix.
 * Supports both https://gitlab.com and https://example.com/gitlab
 */
const GitLabUrlSchema = z
  .string()
  .url()
  .transform(url => {
    const parsed = new URL(url);
 
    // Start with origin (protocol + host + port)
    let path = parsed.pathname;
 
    // Normalize root path to empty
    if (path === "/") {
      path = "";
    } else {
      // Remove single trailing slash for non-root paths
      if (path.endsWith("/")) {
        path = path.slice(0, -1);
      }
 
      // Strip /api/v4 or /api/graphql suffix if present
      for (const apiSuffix of ["/api/v4", "/api/graphql"]) {
        if (path.endsWith(apiSuffix)) {
          path = path.slice(0, -apiSuffix.length);
          // Normalize any resulting "/" back to empty
          if (path === "/") {
            path = "";
          }
          break;
        }
      }
    }
 
    return `${parsed.origin}${path}`;
  })
  .describe("GitLab instance URL (e.g., https://gitlab.com or https://example.com/gitlab)");
 
/**
 * OAuth configuration for an instance
 */
export const InstanceOAuthConfigSchema = z
  .object({
    clientId: z.string().min(1).describe("OAuth Application ID"),
    clientSecret: z.string().optional().describe("OAuth Secret (only for confidential apps)"),
    scopes: z
      .string()
      .default("api read_user")
      .describe("OAuth scopes to request (space-separated)"),
  })
  .describe("OAuth configuration for this GitLab instance");
 
/**
 * Rate limiting configuration for an instance
 */
export const InstanceRateLimitConfigSchema = z
  .object({
    maxConcurrent: z
      .number()
      .int()
      .positive()
      .default(100)
      .describe("Maximum parallel requests to this instance"),
    queueSize: z
      .number()
      .int()
      .positive()
      .default(500)
      .describe("Maximum requests to queue when at capacity"),
    queueTimeout: z
      .number()
      .int()
      .positive()
      .default(60000)
      .describe("Queue wait timeout in milliseconds"),
  })
  .describe("Rate limiting configuration for this instance");
 
/**
 * Single GitLab instance configuration
 */
export const GitLabInstanceConfigSchema = z
  .object({
    url: GitLabUrlSchema,
    label: z.string().optional().describe("Human-readable name for UI display"),
    oauth: InstanceOAuthConfigSchema.optional(),
    rateLimit: InstanceRateLimitConfigSchema.optional(),
    insecureSkipVerify: z
      .boolean()
      .default(false)
      .describe("Skip TLS certificate verification (development only!)"),
  })
  .describe("Configuration for a single GitLab instance");
 
/**
 * Default configuration applied to all instances
 */
export const InstanceDefaultsSchema = z
  .object({
    rateLimit: InstanceRateLimitConfigSchema.optional(),
    oauth: z
      .object({
        scopes: z.string().default("api read_user").describe("Default OAuth scopes"),
      })
      .optional(),
  })
  .describe("Default configuration applied to all instances");
 
/**
 * Complete instances configuration file schema
 */
export const InstancesConfigFileSchema = z
  .object({
    instances: z
      .array(GitLabInstanceConfigSchema)
      .min(1)
      .describe("List of GitLab instances to connect to"),
    defaults: InstanceDefaultsSchema.optional(),
  })
  .describe("GitLab MCP instances configuration file");
 
/**
 * Connection status for runtime state
 */
export const ConnectionStatusSchema = z.enum(["healthy", "degraded", "offline"]);
 
/**
 * Inferred types from schemas
 */
export type InstanceOAuthConfig = z.infer<typeof InstanceOAuthConfigSchema>;
export type InstanceRateLimitConfig = z.infer<typeof InstanceRateLimitConfigSchema>;
export type GitLabInstanceConfig = z.infer<typeof GitLabInstanceConfigSchema>;
export type InstanceDefaults = z.infer<typeof InstanceDefaultsSchema>;
export type InstancesConfigFile = z.infer<typeof InstancesConfigFileSchema>;
export type ConnectionStatus = z.infer<typeof ConnectionStatusSchema>;
 
/**
 * Runtime instance state (extends config with runtime data)
 */
export interface GitLabInstanceState extends GitLabInstanceConfig {
  /** Connection health status */
  connectionStatus: ConnectionStatus;
  /** Last health check timestamp */
  lastHealthCheck: Date | null;
  /** Cached introspection result */
  introspectionCache: CachedIntrospection | null;
}
 
/**
 * Cached introspection data
 */
export interface CachedIntrospection {
  /** GitLab version */
  version: string;
  /** Instance tier (free/premium/ultimate) */
  tier: string;
  /** Available features */
  features: Record<string, boolean>;
  /** Schema info */
  schemaInfo: unknown; // SchemaInfo from SchemaIntrospector
  /** Cache timestamp */
  cachedAt: Date;
}
 
/**
 * Parse and validate a single instance URL string
 * Supports formats:
 * - "https://gitlab.com" (URL only)
 * - "https://gitlab.com/subpath" (URL with subpath)
 * - "https://gitlab.com:client_id" (URL with OAuth client ID)
 * - "https://gitlab.com:client_id:client_secret" (URL with OAuth credentials)
 * - "https://gitlab.com:8080:client_id:secret" (URL with port and OAuth)
 *
 * Uses right-to-left parsing to extract OAuth credentials, preserving subpaths.
 */
export function parseInstanceUrlString(urlString: string): GitLabInstanceConfig {
  const protocolSeparatorIndex = urlString.indexOf("://");
  if (protocolSeparatorIndex === -1) {
    throw new Error(`Invalid GitLab instance URL format: ${urlString}`);
  }
 
  const protocolEnd = protocolSeparatorIndex + 3;
 
  // Helper to check if a string is a valid port number (1-65535)
  const isPortNumber = (str: string): boolean => {
    if (!/^\d+$/.test(str)) return false;
    const num = parseInt(str, 10);
    return num >= 1 && num <= 65535;
  };
 
  let baseUrlString: string | undefined;
  let clientId: string | undefined;
  let clientSecret: string | undefined;
 
  // First, try to parse the entire string as a valid URL (no OAuth)
  try {
    baseUrlString = GitLabUrlSchema.parse(urlString);
    // Success - entire string is a valid URL, no OAuth credentials
    clientId = undefined;
    clientSecret = undefined;
  } catch {
    // Parsing failed - likely has OAuth credentials appended
    baseUrlString = undefined;
  }
 
  // If full URL parsing failed, try extracting OAuth credentials from the end
  if (!baseUrlString) {
    // Try to interpret the string as: <url>:<clientId>:<secret>
    const lastColonIndex = urlString.lastIndexOf(":");
    if (lastColonIndex > protocolEnd) {
      const lastSegment = urlString.slice(lastColonIndex + 1);
      // OAuth segments don't contain slashes and aren't port numbers
      Eif (!lastSegment.includes("/") && !isPortNumber(lastSegment)) {
        const secondLastColonIndex = urlString.lastIndexOf(":", lastColonIndex - 1);
        if (secondLastColonIndex > protocolEnd) {
          const potentialClientId = urlString.slice(secondLastColonIndex + 1, lastColonIndex);
          if (!potentialClientId.includes("/") && !isPortNumber(potentialClientId)) {
            const potentialBaseUrl = urlString.slice(0, secondLastColonIndex);
            try {
              baseUrlString = GitLabUrlSchema.parse(potentialBaseUrl);
              clientId = potentialClientId;
              clientSecret = lastSegment;
            } catch {
              // Fall through to try other patterns
            }
          }
        }
      }
    }
  }
 
  // If two-part OAuth parsing failed, try: <url>:<clientId>
  if (!baseUrlString) {
    const singleLastColonIndex = urlString.lastIndexOf(":");
    if (singleLastColonIndex > protocolEnd) {
      const potentialClientId = urlString.slice(singleLastColonIndex + 1);
      // OAuth clientId doesn't contain slashes and isn't a port number
      Eif (!potentialClientId.includes("/") && !isPortNumber(potentialClientId)) {
        const potentialBaseUrl = urlString.slice(0, singleLastColonIndex);
        try {
          baseUrlString = GitLabUrlSchema.parse(potentialBaseUrl);
          clientId = potentialClientId;
          clientSecret = undefined;
        } catch {
          // Fall through to error
        }
      }
    }
  }
 
  // If we still don't have a base URL, the input is invalid.
  // The multi-strategy parsing above handles: plain URLs, URL:clientId, URL:clientId:secret.
  // This complexity is intentional to support flexible env var formats.
  if (!baseUrlString) {
    throw new Error(`Invalid GitLab instance URL format: ${urlString}`);
  }
 
  // Build config
  const config: GitLabInstanceConfig = {
    url: baseUrlString,
    insecureSkipVerify: false,
  };
 
  // Add OAuth config if client ID provided
  if (clientId) {
    config.oauth = {
      clientId,
      scopes: "api read_user",
    };
 
    if (clientSecret) {
      config.oauth.clientSecret = clientSecret;
    }
  }
 
  return config;
}
 
/**
 * Validate and normalize a complete instances configuration
 */
export function validateInstancesConfig(config: unknown): InstancesConfigFile {
  return InstancesConfigFileSchema.parse(config);
}
 
/**
 * Apply defaults to an instance configuration
 */
export function applyInstanceDefaults(
  instance: GitLabInstanceConfig,
  defaults?: InstanceDefaults
): GitLabInstanceConfig {
  if (!defaults) {
    return instance;
  }
 
  const result: GitLabInstanceConfig = { ...instance };
 
  // Apply rate limit defaults
  if (defaults.rateLimit && !result.rateLimit) {
    result.rateLimit = { ...defaults.rateLimit };
  }
 
  // Apply OAuth scope defaults
  if (defaults.oauth?.scopes && result.oauth && !result.oauth.scopes) {
    result.oauth = {
      ...result.oauth,
      scopes: defaults.oauth.scopes,
    };
  }
 
  return result;
}