All files / src/providers ProviderManager.ts

60.64% Statements 319/526
37.13% Branches 140/377
74.13% Functions 43/58
61.12% Lines 316/517

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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 12925x 5x 5x 5x     5x 5x   5x 5x   5x 43x   43x     43x 43x     5x     70x 35x 35x 35x 35x 35x     35x 35x   35x 3x                       3x 3x 3x 3x     32x                         32x 32x   32x 32x 28x                 28x 28x 28x 28x         28x 28x 28x 28x 28x 28x   4x     4x   4x 4x 4x 4x 4x   4x   4x 1x   3x     3x 3x       4x         32x             32x                   32x 32x     32x 32x   3944x 3942x     32x     32x                 32x           32x 32x 28x       4x         4x     28x                                                                                                         28x   28x       28x 28x 28x 27x 26x 3x 3x             2x         28x 28x 28x 28x 27x 1x 1x             1x         28x 28x 28x 28x 27x 1x 1x             1x             3x 3x 1x     2x     2x   2x 2x     2x 2x 2x 2x 2x   2x   2x     1x     1x 1x 1x 1x 1x   1x 1x 1x 1x   1x           1x     1x         6x 6x         6x 6x 1x 1x     6x 6x 6x   1x           6x 6x 6x       1x 1x 2x 1x   1x     1x   1x     1x                                               6x 6x   6x 6x 1x       5x 2x           3x 1x     2x       2x 2x         1x   1x     1x         2x 2x   2x 2x         2x 1x         1x       1x       1x 1x       1x                   2x 2x   2x 2x         2x             2x       2x       2x 2x   6x                             2x         2x                   2x 1x         2x 2x 2x         1x                                 1x 1x     1x 1x   1x 1x     1x       1x       3x                   35x   1x 1x 1x     1x 1x 1x     1x 1x 1x 1x 1x       32x 32x                 6x 6x 6x   6x 1x     1x       5x 5x         5x 5x   5x       5x 1x 1x   1x 1x     1x 1x 1x     1x                 5x             3x 3x       3x               3x 3x 3x 3x 3x   3x           3x 3x   3x 3x                 28x           28x   28x       28x 2x 1x 1x 1x         28x 2x 1x     1x                                         3x 3x 3x                   3x   3x   3x         3x   1x 1x 1x 1x 1x                   3x               3x 1x     2x       2x 2x   2x   2x 2x     2x     2x   1x 1x         1x   1x 1x       1x                                                       2x                     5x 5x       5x 5x   5x 2x 2x   3x 3x 3x               1x   1x 1x 2x     2x 1x   1x   1x 1x 2x 2x 1x   1x               1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { Tool, Resource, Prompt } from '@modelcontextprotocol/sdk/types.js';
import { ProviderConfig, ProviderInstance, QueuedRequest } from '../types/index.js';
import { classifyError, shouldAttemptReconnection } from '../utils/errorClassifier.js';
import { hasTextContent } from '../utils/typeGuards.js';
import { ProjectCacheData, UserRole } from '../cache/CacheManager.js';
import { EventEmitter } from 'events';
import { randomUUID } from 'crypto';
 
export class ProviderManager extends EventEmitter {
  private providers: Map<string, ProviderInstance> = new Map();
  private updateCheckInterval?: NodeJS.Timeout;
  private reconnectionTimers: Map<string, NodeJS.Timeout> = new Map();
 
  // Cache structure: [provider -> projects] and [provider -> project -> users]
  private projectsCache: Map<string, ProjectCacheData[]> = new Map();
  private usersCache: Map<string, Map<string, UserRole[]>> = new Map();
 
  // Request queuing constants
  private static readonly REQUEST_TIMEOUT_MS = 30000; // 30 seconds
 
  async initializeProvider(config: ProviderConfig): Promise<ProviderInstance> {
    const { logger } = await import('../utils/logger.js');
    logger.debug(`=== Initializing ${config.id} Provider ===`);
    logger.debug(`  Provider: ${config.name}`);
    logger.debug(`  Type: ${config.type}`);
    logger.debug(`  Command: ${config.command}`);
    logger.debug(`  Args: [${config.args?.join(', ') ?? 'none'}]`);
 
    // Validate tokens before attempted to initialize
    const hasRequiredTokens = this.validateProviderTokens(config, logger);
    logger.debug(`  Token validation: ${hasRequiredTokens ? 'PASSED' : 'FAILED'}`);
 
    if (!hasRequiredTokens) {
      const instance: ProviderInstance = {
        id: config.id,
        config,
        tools: new Map(),
        resources: new Map(),
        prompts: new Map(),
        status: 'auth_failed',
        error: 'Required authentication tokens are missing',
        errorType: 'auth',
        shouldReconnect: false,
        lastUpdated: new Date(),
      };
      logger.warn(`  ❌ Skipping initialization - missing required tokens`);
      this.providers.set(config.id, instance);
      this.emit('provider:auth_failed', instance);
      return instance;
    }
 
    const instance: ProviderInstance = {
      id: config.id,
      config,
      tools: new Map(),
      resources: new Map(),
      prompts: new Map(),
      status: 'starting',
      reconnectAttempts: 0,
      lastUpdated: new Date(),
      isUpdating: false,
      requestQueue: [],
    };
 
    try {
      logger.debug(`  Creating client transport...`);
 
      if (config.type === 'stdio' && config.command) {
        instance.client = await this.createStdioClient(config, logger);
        logger.debug(`  ✓ STDIO client created successfully`);
      } else Eif (config.type === 'sse' && config.url) {
        instance.client = await this.createSSEClient(config);
        logger.debug(`  ✓ SSE client created successfully`);
      } else if (config.type === 'http' && config.url) {
        instance.client = await this.createHTTPClient(config);
        logger.debug(`  ✓ HTTP client created successfully`);
      }
 
      if (instance.client) {
        logger.debug(`  Loading capabilities from ${config.name}...`);
        await this.connectAndLoadCapabilities(instance, logger);
        logger.debug(`  ✓ Capabilities loaded from ${config.name}`);
      } else E{
        throw new Error(`Failed to create client for ${config.id}`);
      }
 
      instance.status = 'connected';
      instance.reconnectAttempts = 0; // Reset on successful connection
      this.setupProviderMonitoring(instance);
      this.providers.set(config.id, instance);
      logger.log(`  ✅ Provider ${config.id} initialized successfully`);
      this.emit('provider:connected', instance);
    } catch (error) {
      logger.error(
        `  ❌ Provider ${config.id} initialization failed: ${error instanceof Error ? error.message : String(error)}`,
      );
      const classification = classifyError(error);
 
      instance.status = classification.type === 'auth' ? 'auth_failed' : 'error';
      instance.error = classification.message;
      instance.errorType = classification.type;
      instance.shouldReconnect = classification.shouldReconnect;
      instance.lastUpdated = new Date();
 
      this.providers.set(config.id, instance);
 
      if (classification.type === 'auth') {
        this.emit('provider:auth_failed', instance);
      } else {
        this.emit('provider:error', { provider: instance, error });
 
        // Schedule reconnection for non-auth errors if appropriate
        Eif (classification.shouldReconnect) {
          this.scheduleReconnection(instance);
        }
      }
 
      process.stderr.write(
        `Failed to initialize provider ${config.id}: ${classification.message}\n`,
      );
    }
 
    return instance;
  }
 
  private async createStdioClient(
    config: ProviderConfig,
    logger: { debug: (msg: string) => void },
  ): Promise<Client> {
    const client = new Client(
      {
        name: `nexus-proxy-${config.id}`,
        version: '1.0.0',
      },
      {
        capabilities: {},
      },
    );
 
    const command = config.command;
    Iif (!command) {
      throw new Error('Command is required for STDIO transport');
    }
    const args = config.args ?? [];
    const env = Object.fromEntries(
      Object.entries({ ...process.env, ...config.env })
        .filter(([, value]) => value !== undefined && typeof value === 'string')
        .map(([key, value]) => [key, value as string]),
    );
 
    logger.debug(`  Spawning: ${command} ${args.join(' ')}`);
 
    // Add debug logging for Azure specifically
    Iif (config.id === 'azure') {
      logger.debug(`  Azure environment variables:`);
      for (const [key, value] of Object.entries(env)) {
        if (key.includes('AZURE')) {
          logger.debug(`    ${key}: ${value ? '[SET]' : '[NOT SET]'}`);
        }
      }
    }
 
    const transport = new StdioClientTransport({
      command,
      args,
      env,
    });
 
    try {
      await client.connect(transport);
      Iif (config.id === 'azure') {
        logger.debug(`  Azure MCP client connected successfully`);
      }
    } catch (error) {
      Iif (config.id === 'azure') {
        logger.debug(
          `  Azure MCP connection failed: ${error instanceof Error ? error.message : String(error)}`,
        );
      }
      throw error;
    }
 
    return client;
  }
 
  private async createSSEClient(config: ProviderConfig): Promise<Client> {
    const client = new Client(
      {
        name: `nexus-proxy-${config.id}`,
        version: '1.0.0',
      },
      {
        capabilities: {},
      },
    );
 
    if (!config.url) {
      throw new Error('URL is required for SSE transport');
    }
    const transport = new SSEClientTransport(new URL(config.url));
 
    await client.connect(transport);
 
    return client;
  }
 
  private async createHTTPClient(config: ProviderConfig): Promise<Client> {
    if (!config.url) {
      throw new Error('URL is required for HTTP transport');
    }
 
    const client = new Client(
      {
        name: 'nexus-proxy-client',
        version: '1.0.0',
      },
      {
        capabilities: {},
      },
    );
 
    // Use Streamable HTTP transport for MCP communication
    const transport = new StreamableHTTPClientTransport(new URL(config.url), {
      requestInit: {
        headers: config.headers ?? {},
      },
    });
    await client.connect(transport);
    return client;
  }
 
  private async connectAndLoadCapabilities(
    instance: ProviderInstance,
    logger: { debug: (msg: string) => void },
  ): Promise<void> {
    const client = instance.client;
 
    Iif (!client) {
      throw new Error('Client not initialized');
    }
 
    logger.debug(`    Listing tools...`);
    try {
      const listToolsResult = await client.listTools();
      logger.debug(`    ✓ Found ${listToolsResult.tools.length} tools`);
      for (const tool of listToolsResult.tools) {
        const prefixedName = `${instance.id}_${tool.name}`;
        instance.tools.set(prefixedName, {
          ...tool,
          name: prefixedName,
          description: `[${instance.config.name}] ${tool.description ?? ''}`,
        });
      }
    } catch (error) {
      logger.debug(
        `    ❌ Could not list tools for ${instance.id}: ${error instanceof Error ? error.message : String(error)}`,
      );
    }
 
    logger.debug(`    Listing resources...`);
    try {
      const listResourcesResult = await client.listResources();
      logger.debug(`    ✓ Found ${listResourcesResult.resources.length} resources`);
      for (const resource of listResourcesResult.resources) {
        const prefixedUri = `${instance.id}:${resource.uri}`;
        instance.resources.set(prefixedUri, {
          ...resource,
          uri: prefixedUri,
          name: `[${instance.config.name}] ${resource.name}`,
        });
      }
    } catch (error) {
      logger.debug(
        `    ❌ Could not list resources for ${instance.id}: ${error instanceof Error ? error.message : String(error)}`,
      );
    }
 
    logger.debug(`    Listing prompts...`);
    try {
      const listPromptsResult = await client.listPrompts();
      logger.debug(`    ✓ Found ${listPromptsResult.prompts.length} prompts`);
      for (const prompt of listPromptsResult.prompts) {
        const prefixedName = `${instance.id}_${prompt.name}`;
        instance.prompts.set(prefixedName, {
          ...prompt,
          name: prefixedName,
          description: `[${instance.config.name}] ${prompt.description ?? ''}`,
        });
      }
    } catch (error) {
      logger.debug(
        `    ❌ Could not list prompts for ${instance.id}: ${error instanceof Error ? error.message : String(error)}`,
      );
    }
  }
 
  async reloadProvider(providerId: string): Promise<void> {
    const existingProvider = this.providers.get(providerId);
    if (!existingProvider) {
      throw new Error(`Provider ${providerId} not found`);
    }
 
    process.stdout.write(`Reloading provider: ${providerId}\n`);
 
    // Set updating state to queue incoming requests
    await this.setProviderUpdating(providerId, true);
 
    try {
      await this.disconnectProvider(providerId);
 
      // Reset reconnection attempts when manually reloading
      existingProvider.reconnectAttempts = 0;
      existingProvider.lastReconnectTime = undefined;
      existingProvider.error = undefined;
      existingProvider.errorType = undefined;
      existingProvider.shouldReconnect = undefined;
 
      await new Promise((resolve) => setTimeout(resolve, 1000));
 
      await this.initializeProvider(existingProvider.config);
 
      // Clear updating state and process queued requests
      await this.setProviderUpdating(providerId, false);
    } catch (error) {
      // If reload failed, still clear updating state but don't process queue
      const provider = this.providers.get(providerId);
      Eif (provider) {
        provider.isUpdating = false;
        provider.status = 'error';
        provider.updateStartTime = undefined;
        // Clear the request queue with errors
        Eif (provider.requestQueue) {
          for (const request of provider.requestQueue) {
            Eif (request.timeoutId) {
              clearTimeout(request.timeoutId);
            }
            request.reject(
              new Error(
                `Provider reload failed: ${error instanceof Error ? error.message : String(error)}`,
              ),
            );
          }
          provider.requestQueue = [];
        }
      }
      throw error;
    }
  }
 
  async disconnectProvider(providerId: string): Promise<void> {
    const provider = this.providers.get(providerId);
    Iif (!provider) {
      return;
    }
 
    // Clear any pending reconnection timer
    const existingTimer = this.reconnectionTimers.get(providerId);
    if (existingTimer) {
      clearTimeout(existingTimer);
      this.reconnectionTimers.delete(providerId);
    }
 
    Eif (provider.client) {
      try {
        await provider.client.close();
      } catch (error) {
        process.stderr.write(
          `Error closing client for ${providerId}: ${error instanceof Error ? error.message : String(error)}\n`,
        );
      }
    }
 
    provider.status = 'disconnected';
    provider.lastUpdated = new Date();
    this.emit('provider:disconnected', provider);
  }
 
  getAllTools(): Tool[] {
    const allTools: Tool[] = [];
    for (const provider of this.providers.values()) {
      if (provider.status === 'connected') {
        const filteredTools = Array.from(provider.tools.values()).filter((tool) => {
          // Hide internal GitLab tools that should not be exposed to clients
          Iif (tool.name === 'gitlab_list_user_projects') {
            return false;
          }
          return true;
        });
        allTools.push(...filteredTools);
      }
    }
    return allTools;
  }
 
  getAllResources(): Resource[] {
    const allResources: Resource[] = [];
    for (const provider of this.providers.values()) {
      if (provider.status === 'connected') {
        allResources.push(...Array.from(provider.resources.values()));
      }
    }
    return allResources;
  }
 
  getAllPrompts(): Prompt[] {
    const allPrompts: Prompt[] = [];
    for (const provider of this.providers.values()) {
      if (provider.status === 'connected') {
        allPrompts.push(...Array.from(provider.prompts.values()));
      }
    }
    return allPrompts;
  }
 
  async callTool(toolName: string, args: Record<string, unknown>): Promise<unknown> {
    const providerId = toolName.split('_')[0];
    const originalToolName = toolName.substring(providerId.length + 1);
 
    const provider = this.providers.get(providerId);
    if (!provider) {
      throw new Error(`Provider ${providerId} not found`);
    }
 
    // If provider is updating, queue the request
    if (provider.isUpdating || provider.status === 'updating') {
      return this.queueRequest(provider, 'tool', {
        name: originalToolName,
        arguments: args,
      });
    }
 
    if (provider.status !== 'connected') {
      throw new Error(`Provider ${providerId} not available (status: ${provider.status})`);
    }
 
    Iif (!provider.client) {
      throw new Error(`Client not initialized for provider ${providerId}`);
    }
 
    try {
      const result = await provider.client.callTool({
        name: originalToolName,
        arguments: args,
      });
 
      return result;
    } catch (error) {
      process.stderr.write(
        `Error calling tool ${toolName}: ${error instanceof Error ? error.message : String(error)}\n`,
      );
      throw error;
    }
  }
 
  async readResource(uri: string): Promise<unknown> {
    const providerId = uri.split(':')[0];
    const originalUri = uri.substring(providerId.length + 1);
 
    const provider = this.providers.get(providerId);
    Iif (!provider) {
      throw new Error(`Provider ${providerId} not found`);
    }
 
    // If provider is updating, queue the request
    if (provider.isUpdating || provider.status === 'updating') {
      return this.queueRequest(provider, 'resource', {
        uri: originalUri,
      });
    }
 
    Iif (provider.status !== 'connected') {
      throw new Error(`Provider ${providerId} not available (status: ${provider.status})`);
    }
 
    Iif (!provider.client) {
      throw new Error(`Client not initialized for provider ${providerId}`);
    }
 
    try {
      const result = await provider.client.readResource({
        uri: originalUri,
      });
 
      return result;
    } catch (error) {
      process.stderr.write(
        `Error reading resource ${uri}: ${error instanceof Error ? error.message : String(error)}\n`,
      );
      throw error;
    }
  }
 
  async getPrompt(promptName: string, args?: Record<string, unknown>): Promise<unknown> {
    const providerId = promptName.split('_')[0];
    const originalPromptName = promptName.substring(providerId.length + 1);
 
    const provider = this.providers.get(providerId);
    Iif (!provider) {
      throw new Error(`Provider ${providerId} not found`);
    }
 
    // If provider is updating, queue the request
    Iif (provider.isUpdating || provider.status === 'updating') {
      return this.queueRequest(provider, 'prompt', {
        name: originalPromptName,
        arguments: args,
      });
    }
 
    Iif (provider.status !== 'connected') {
      throw new Error(`Provider ${providerId} not available (status: ${provider.status})`);
    }
 
    Iif (!provider.client) {
      throw new Error(`Client not initialized for provider ${providerId}`);
    }
 
    try {
      const promptArgs: Record<string, string> | undefined = args
        ? Object.fromEntries(
            Object.entries(args).map(([key, value]) => [
              key,
              typeof value === 'string'
                ? value
                : value === null || value === undefined
                  ? ''
                  : typeof value === 'object'
                    ? JSON.stringify(value)
                    : typeof value === 'number' || typeof value === 'boolean'
                      ? String(value)
                      : '',
            ]),
          )
        : undefined;
 
      const result = await provider.client.getPrompt({
        name: originalPromptName,
        arguments: promptArgs,
      });
 
      return result;
    } catch (error) {
      process.stderr.write(
        `Error getting prompt ${promptName}: ${error instanceof Error ? error.message : String(error)}\n`,
      );
      throw error;
    }
  }
 
  startAutoUpdate(intervalMs: number = 60000): void {
    this.updateCheckInterval = setInterval(() => {
      this.checkForUpdates();
    }, intervalMs);
  }
 
  stopAutoUpdate(): void {
    Eif (this.updateCheckInterval) {
      clearInterval(this.updateCheckInterval);
      this.updateCheckInterval = undefined;
    }
  }
 
  private checkForUpdates(): void {
    for (const provider of this.providers.values()) {
      if (provider.config.autoUpdate) {
        try {
          process.stdout.write(`Checking for updates for ${provider.id}...\n`);
          // Update check logic would go here
          // For now, we'll just emit an event
          this.emit('provider:update-check', provider);
        } catch (error) {
          process.stderr.write(
            `Error checking updates for ${provider.id}: ${error instanceof Error ? error.message : String(error)}\n`,
          );
        }
      }
    }
  }
 
  async shutdown(): Promise<void> {
    this.stopAutoUpdate();
    this.clearReconnectionTimers();
 
    // Clear provider caches
    this.projectsCache.clear();
    this.usersCache.clear();
 
    for (const providerId of this.providers.keys()) {
      await this.disconnectProvider(providerId);
    }
 
    this.providers.clear();
  }
 
  getProvider(id: string): ProviderInstance | undefined {
    return this.providers.get(id);
  }
 
  getAllProviders(): ProviderInstance[] {
    return Array.from(this.providers.values());
  }
 
  /**
   * Validate that required tokens are available for the provider
   */
  private validateProviderTokens(
    config: ProviderConfig,
    logger: { debug: (msg: string) => void },
  ): boolean {
    switch (config.id) {
      case 'github': {
        const hasGithubToken = Boolean(process.env.GITHUB_TOKEN);
        logger.debug(`    GITHUB_TOKEN: ${hasGithubToken ? 'SET' : 'MISSING'}`);
        return hasGithubToken;
      }
      case 'gitlab': {
        const hasGitlabToken = Boolean(process.env.GITLAB_TOKEN);
        logger.debug(`    GITLAB_TOKEN: ${hasGitlabToken ? 'SET' : 'MISSING'}`);
        return hasGitlabToken;
      }
      case 'azure': {
        const hasAzureToken = Boolean(process.env.AZURE_TOKEN);
        const hasAzureOrg = Boolean(process.env.AZURE_ORG);
        logger.debug(`    AZURE_TOKEN: ${hasAzureToken ? 'SET' : 'MISSING'}`);
        logger.debug(`    AZURE_ORG: ${hasAzureOrg ? 'SET' : 'MISSING'}`);
        return hasAzureToken && hasAzureOrg;
      }
      default: {
        // For unknown providers, assume tokens are not required
        logger.debug(`    No validation required for ${config.id}`);
        return true;
      }
    }
  }
 
  /**
   * Schedule reconnection attempt for a provider
   */
  private scheduleReconnection(instance: ProviderInstance): void {
    const { id } = instance;
    const attempts = instance.reconnectAttempts ?? 0;
    const lastReconnectTime = instance.lastReconnectTime;
 
    if (!shouldAttemptReconnection(attempts, lastReconnectTime)) {
      process.stderr.write(
        `Skipping reconnection for ${id}: too many attempts or cooldown period not met\n`,
      );
      return;
    }
 
    // Clear existing timer if any
    const existingTimer = this.reconnectionTimers.get(id);
    Iif (existingTimer) {
      clearTimeout(existingTimer);
    }
 
    // Calculate delay: exponential backoff starting at 5 seconds
    const baseDelay = 5000; // 5 seconds
    const delay = baseDelay * Math.pow(2, attempts);
 
    process.stderr.write(
      `Scheduling reconnection for ${id} in ${delay}ms (attempt ${attempts + 1})\n`,
    );
 
    const timer = setTimeout(() => {
      void (async () => {
        this.reconnectionTimers.delete(id);
 
        try {
          process.stderr.write(`Attempting reconnection for ${id}...\n`);
 
          // Update reconnection tracking
          instance.reconnectAttempts = (instance.reconnectAttempts ?? 0) + 1;
          instance.lastReconnectTime = new Date();
          instance.status = 'starting';
 
          // Attempt to reinitialize
          await this.initializeProvider(instance.config);
        } catch (error) {
          process.stderr.write(
            `Reconnection failed for ${id}: ${error instanceof Error ? error.message : String(error)}\n`,
          );
        }
      })();
    }, delay);
 
    this.reconnectionTimers.set(id, timer);
  }
 
  /**
   * Handle provider process failure (e.g., killed externally)
   */
  handleProviderFailure(providerId: string, error?: Error): void {
    const provider = this.providers.get(providerId);
    Iif (!provider) {
      return;
    }
 
    const classification = error
      ? classifyError(error)
      : {
          type: 'network' as const,
          shouldReconnect: true,
          message: 'Provider process terminated unexpectedly',
        };
 
    provider.status = classification.type === 'auth' ? 'auth_failed' : 'error';
    provider.error = classification.message;
    provider.errorType = classification.type;
    provider.shouldReconnect = classification.shouldReconnect;
    provider.lastUpdated = new Date();
 
    Iif (classification.type === 'auth') {
      process.stderr.write(
        `Provider ${providerId} failed with authentication error, will not reconnect\n`,
      );
      this.emit('provider:auth_failed', provider);
    } else {
      process.stderr.write(`Provider ${providerId} failed: ${classification.message}\n`);
      this.emit('provider:error', { provider, error: error ?? new Error(classification.message) });
 
      Eif (classification.shouldReconnect) {
        this.scheduleReconnection(provider);
      }
    }
  }
 
  /**
   * Monitor provider processes for unexpected failures
   */
  private setupProviderMonitoring(instance: ProviderInstance): void {
    Iif (!instance.client) {
      return;
    }
 
    // Listen for transport errors/disconnections if available
    // Note: Not all MCP client transports expose these events
    try {
      const transport = (
        instance.client as unknown as {
          _transport?: { onclose?: () => void; onerror?: (error: unknown) => void };
        }
      )._transport;
      if (transport && typeof transport.onclose === 'function') {
        transport.onclose = () => {
          Eif (instance.status === 'connected') {
            process.stderr.write(`Provider ${instance.id} disconnected unexpectedly\n`);
            this.handleProviderFailure(instance.id);
          }
        };
      }
 
      if (transport && typeof transport.onerror === 'function') {
        transport.onerror = (error: unknown) => {
          process.stderr.write(
            `Provider ${instance.id} error: ${error instanceof Error ? error.message : String(error)}\n`,
          );
          this.handleProviderFailure(instance.id, error instanceof Error ? error : undefined);
        };
      }
    } catch {
      // Transport monitoring not available, that's okay
      process.stderr.write(`Transport monitoring not available for ${instance.id}\n`);
    }
  }
 
  /**
   * Queue a request during provider update
   */
  private async queueRequest(
    provider: ProviderInstance,
    type: 'tool' | 'resource' | 'prompt',
    data: {
      name?: string;
      uri?: string;
      arguments?: Record<string, unknown>;
    },
  ): Promise<unknown> {
    return new Promise((resolve, reject) => {
      const requestId = randomUUID();
      const queuedRequest: QueuedRequest = {
        id: requestId,
        type,
        data,
        resolve,
        reject,
        timestamp: new Date(),
      };
 
      // Initialize queue if not exists
      provider.requestQueue ??= [];
 
      provider.requestQueue.push(queuedRequest);
 
      process.stderr.write(
        `Request queued for provider ${provider.id} (${type}): ${data.name ?? data.uri ?? 'prompt'}\n`,
      );
 
      // Set timeout for the request
      const timeoutId = setTimeout(() => {
        // Remove from queue and reject with timeout error
        Eif (provider.requestQueue) {
          const index = provider.requestQueue.findIndex((req) => req.id === requestId);
          Eif (index !== -1) {
            provider.requestQueue.splice(index, 1);
            reject(
              new Error(
                `Request timed out after ${ProviderManager.REQUEST_TIMEOUT_MS}ms while provider ${provider.id} was updating`,
              ),
            );
          }
        }
      }, ProviderManager.REQUEST_TIMEOUT_MS);
 
      // Store timeout ID in the request for cleanup
      queuedRequest.timeoutId = timeoutId;
    });
  }
 
  /**
   * Process queued requests after provider is back online
   */
  private async processQueuedRequests(provider: ProviderInstance): Promise<void> {
    if (!provider.requestQueue || provider.requestQueue.length === 0) {
      return;
    }
 
    process.stderr.write(
      `Processing ${provider.requestQueue.length} queued requests for provider ${provider.id}\n`,
    );
 
    const requestsToProcess = [...provider.requestQueue];
    provider.requestQueue = []; // Clear queue
 
    for (const request of requestsToProcess) {
      // Clear timeout
      Eif (request.timeoutId) {
        clearTimeout(request.timeoutId);
      }
 
      try {
        let result: unknown;
 
        switch (request.type) {
          case 'tool':
            Eif (request.data.name && provider.client) {
              result = await provider.client.callTool({
                name: request.data.name,
                arguments: request.data.arguments ?? {},
              });
            }
            break;
          case 'resource':
            Eif (request.data.uri && provider.client) {
              result = await provider.client.readResource({
                uri: request.data.uri,
              });
            }
            break;
          case 'prompt':
            if (request.data.name && provider.client) {
              const promptArgs: Record<string, string> | undefined = request.data.arguments
                ? Object.fromEntries(
                    Object.entries(request.data.arguments).map(([key, value]) => [
                      key,
                      typeof value === 'string'
                        ? value
                        : value === null || value === undefined
                          ? ''
                          : typeof value === 'object'
                            ? JSON.stringify(value)
                            : typeof value === 'number' || typeof value === 'boolean'
                              ? String(value)
                              : '',
                    ]),
                  )
                : undefined;
 
              result = await provider.client.getPrompt({
                name: request.data.name,
                arguments: promptArgs,
              });
            }
            break;
        }
 
        request.resolve(result);
      } catch (error) {
        request.reject(error instanceof Error ? error : new Error(String(error)));
      }
    }
  }
 
  /**
   * Set provider updating state and queue incoming requests
   */
  async setProviderUpdating(providerId: string, updating: boolean): Promise<void> {
    const provider = this.providers.get(providerId);
    Iif (!provider) {
      return;
    }
 
    provider.isUpdating = updating;
    provider.status = updating ? 'updating' : 'connected';
 
    if (updating) {
      provider.updateStartTime = new Date();
      process.stderr.write(`Provider ${providerId} is now updating - queueing new requests\n`);
    } else {
      provider.updateStartTime = undefined;
      process.stderr.write(`Provider ${providerId} update complete - processing queued requests\n`);
      await this.processQueuedRequests(provider);
    }
  }
 
  /**
   * Restart all providers (for debugging)
   */
  async restartAllProviders(): Promise<void> {
    process.stderr.write(`Restarting all ${this.providers.size} providers...\n`);
 
    const providerIds = Array.from(this.providers.keys());
    const results = await Promise.allSettled(
      providerIds.map((providerId) => this.reloadProvider(providerId)),
    );
 
    const successful = results.filter((result) => result.status === 'fulfilled').length;
    const failed = results.length - successful;
 
    process.stderr.write(`Provider restart complete: ${successful} successful, ${failed} failed\n`);
 
    Eif (failed > 0) {
      const failures = results
        .map((result, index) => ({ result, id: providerIds[index] }))
        .filter(({ result }) => result.status === 'rejected')
        .map(({ result, id }) => `${id}: ${(result as PromiseRejectedResult).reason}`);
 
      process.stderr.write(`Failed providers:\n${failures.join('\n')}\n`);
    }
  }
 
  /**
   * Clear all reconnection timers on shutdown
   */
  private clearReconnectionTimers(): void {
    for (const [id, timer] of this.reconnectionTimers.entries()) {
      clearTimeout(timer);
      this.reconnectionTimers.delete(id);
    }
  }
 
  /**
   * Warm up caches for all connected providers
   */
  async warmupCaches(): Promise<void> {
    const { logger } = await import('../utils/logger.js');
    logger.debug('[cache] Starting provider-level cache warmup...');
 
    const providers = Array.from(this.providers.values()).filter((p) => p.status === 'connected');
 
    // Warm up projects and users sequentially for each provider (to avoid race conditions)
    for (const provider of providers) {
      try {
        logger.debug(`[cache] Warming up projects for ${provider.id}...`);
        const projects = await this.fetchProjectsFromProvider(provider.id);
        this.projectsCache.set(provider.id, projects);
 
        logger.debug(`[cache] Warming up users for ${provider.id}...`);
        const users = this.fetchUsersFromProvider(provider.id);
        this.usersCache.set(provider.id, users);
 
        logger.debug(
          `[cache] Warmup completed for ${provider.id}: ${projects.length} projects, ${Array.from(users.values()).reduce((total, projectUsers) => total + projectUsers.length, 0)} users`,
        );
      } catch (error) {
        logger.debug(`[cache] Warmup failed for ${provider.id}: ${String(error)}`);
        // Continue with next provider even if this one fails
      }
    }
    logger.debug('[cache] Provider-level cache warmup completed for all providers');
  }
 
  /**
   * Fetch projects from a specific provider (used for cache warming)
   */
  private async fetchProjectsFromProvider(providerId: string): Promise<ProjectCacheData[]> {
    const provider = this.providers.get(providerId);
    if (!provider || provider.status !== 'connected') {
      return [];
    }
 
    const projects: ProjectCacheData[] = [];
 
    // Try different project listing tools based on provider
    const projectTools =
      providerId === 'gitlab'
        ? [
            'list_user_projects', // GitLab user projects (internal tool)
            'search_globally', // GitLab global search for projects
            'list_projects', // GitLab projects
            'list_groups', // GitLab groups
          ]
        : providerId === 'azure'
          ? [
              'core_list_projects', // Azure DevOps projects (correct tool name)
              'list_projects', // Legacy fallback
              'get_projects', // Alternative fallback
            ]
          : [
              'list_repositories', // GitHub direct repo listing (preferred)
              'list_user_repositories', // GitHub user's repos
              'search_repositories', // GitHub search with user:username
              'list_organizations', // GitHub organizations
            ];
 
    for (const toolSuffix of projectTools) {
      const toolName = `${providerId}_${toolSuffix}`;
      if (provider.tools.has(toolName)) {
        try {
          const { logger } = await import('../utils/logger.js');
          logger.debug(`[cache] Trying tool: ${toolName}`);
 
          // Prepare parameters based on tool type
          let toolParams = {};
          if (toolName.includes('search_globally')) {
            // GitLab search_globally with scope=projects and active=true
            toolParams = { scope: 'projects', active: 'true' };
          } else if (toolName.includes('search_repositories')) {
            // For GitHub search, we need to get the authenticated username first
            let username = '';
            if (providerId === 'github') {
              // Try to get username from get_me tool
              const getMeTool = `${providerId}_get_me`;
              if (provider.tools.has(getMeTool)) {
                try {
                  const meResult = await this.callTool(getMeTool, {});
                  if (hasTextContent(meResult)) {
                    const meJson = meResult.content[0].text;
                    const meData = JSON.parse(meJson) as Record<string, unknown>;
                    username = typeof meData.login === 'string' ? meData.login : '';
                  }
                } catch {
                  // Skip search if we can't get username
                  continue;
                }
              }
            } else {
              // Skip search if we can't get username
              continue;
            }
            toolParams = { query: `user:${username}` };
          }
 
          logger.debug(`[cache] Calling tool: ${toolName} with params:`, toolParams);
          const result = await this.callTool(toolName, toolParams);
          logger.debug(`[cache] Tool call successful: ${toolName}`);
 
          if (hasTextContent(result)) {
            const projectsJson = result.content[0].text;
            const parsedProjects: unknown = JSON.parse(projectsJson);
 
            // Handle both array responses and GitHub-style object responses
            let projectArray: unknown[] = [];
            if (Array.isArray(parsedProjects)) {
              projectArray = parsedProjects;
            } else if (typeof parsedProjects === 'object' && parsedProjects !== null) {
              // GitHub search API returns { items: [...], total_count: number }
              const searchResult = parsedProjects as Record<string, unknown>;
              if (Array.isArray(searchResult.items)) {
                projectArray = searchResult.items;
              }
            }
 
            if (projectArray.length > 0) {
              for (const project of projectArray) {
                if (typeof project === 'object' && project !== null) {
                  const proj = project as Record<string, unknown>;
                  const projectId =
                    typeof proj.full_name === 'string'
                      ? proj.full_name
                      : typeof proj.path_with_namespace === 'string'
                        ? proj.path_with_namespace
                        : typeof proj.name === 'string'
                          ? proj.name
                          : typeof proj.id === 'string' || typeof proj.id === 'number'
                            ? String(proj.id)
                            : '';
                  const projectName =
                    typeof proj.name === 'string'
                      ? proj.name
                      : typeof proj.title === 'string'
                        ? proj.title
                        : projectId;
 
                  if (projectId && projectName) {
                    // Also fetch project members if available
                    const members = await this.fetchProjectMembers(providerId, projectId);
 
                    // Determine project URL based on provider
                    let projectUrl: string | undefined;
                    if (typeof proj.html_url === 'string') {
                      projectUrl = proj.html_url; // GitHub
                    } else if (typeof proj.web_url === 'string') {
                      projectUrl = proj.web_url; // GitLab
                    } else if (typeof proj.url === 'string') {
                      projectUrl = proj.url; // Azure
                    }
 
                    projects.push({
                      id: projectId,
                      name: projectName,
                      provider: providerId,
                      description:
                        typeof proj.description === 'string' ? proj.description : undefined,
                      url: projectUrl,
                      members: members,
                    });
                  }
                }
              }
 
              // Return after first successful tool
              break;
            }
          }
        } catch {
          // Continue with next tool if current one fails
          continue;
        }
      }
    }
 
    return projects;
  }
 
  /**
   * Fetch project members from a specific provider
   */
  private async fetchProjectMembers(providerId: string, projectId: string): Promise<UserRole[]> {
    const provider = this.providers.get(providerId);
    if (!provider || provider.status !== 'connected') {
      return [];
    }
 
    const memberTools = [
      'list_project_members', // GitLab
      'list_repository_collaborators', // GitHub
      'list_collaborators', // GitHub alternative
      'list_contributors', // GitHub contributors
      'get_project_members', // Azure DevOps
      'list_team_members', // Azure DevOps alternative
    ];
 
    for (const toolSuffix of memberTools) {
      const toolName = `${providerId}_${toolSuffix}`;
      if (provider.tools.has(toolName)) {
        try {
          const result = await this.callTool(toolName, {
            project_id: projectId,
            id: projectId,
            repo: projectId,
            owner: projectId.split('/')[0], // For GitHub owner/repo format
            repository: projectId,
          });
 
          if (hasTextContent(result)) {
            const membersJson = result.content[0].text;
            const parsedMembers: unknown = JSON.parse(membersJson);
 
            if (Array.isArray(parsedMembers)) {
              const members: UserRole[] = [];
              for (const member of parsedMembers) {
                if (typeof member === 'object' && member !== null) {
                  const mem = member as Record<string, unknown>;
                  const userId =
                    typeof mem.id === 'string' || typeof mem.id === 'number'
                      ? String(mem.id)
                      : typeof mem.login === 'string'
                        ? mem.login
                        : '';
                  const username =
                    typeof mem.username === 'string'
                      ? mem.username
                      : typeof mem.login === 'string'
                        ? mem.login
                        : userId;
                  const displayName =
                    typeof mem.name === 'string'
                      ? mem.name
                      : typeof mem.display_name === 'string'
                        ? mem.display_name
                        : username;
 
                  // Extract role information based on provider
                  let role = 'member'; // default role
                  let accessLevel: number | undefined;
 
                  if (typeof mem.role === 'string') {
                    role = mem.role; // GitLab
                  } else if (typeof mem.permissions === 'string') {
                    role = mem.permissions; // GitHub
                  } else if (typeof mem.access_level === 'number') {
                    accessLevel = mem.access_level; // GitLab access levels
                    // Convert GitLab access levels to roles
                    if (accessLevel >= 50) role = 'maintainer';
                    else if (accessLevel >= 40) role = 'developer';
                    else if (accessLevel >= 30) role = 'developer';
                    else if (accessLevel >= 20) role = 'reporter';
                    else role = 'guest';
                  }
 
                  if (userId && username) {
                    members.push({
                      userId,
                      username,
                      displayName,
                      email: typeof mem.email === 'string' ? mem.email : undefined,
                      role,
                      accessLevel,
                    });
                  }
                }
              }
              return members;
            }
          }
        } catch {
          // Continue with next tool if current one fails
          continue;
        }
      }
    }
 
    return [];
  }
 
  /**
   * Fetch users from a specific provider (used for cache warming)
   */
  private fetchUsersFromProvider(providerId: string): Map<string, UserRole[]> {
    const usersByProject = new Map<string, UserRole[]>();
 
    // Get projects first to populate users per project structure
    const projects = this.projectsCache.get(providerId) ?? [];
 
    for (const project of projects) {
      if (project.members) {
        usersByProject.set(project.id, project.members);
      }
    }
 
    return usersByProject;
  }
 
  /**
   * Get cached projects for a provider
   */
  getCachedProjects(providerId: string): ProjectCacheData[] {
    return this.projectsCache.get(providerId) ?? [];
  }
 
  /**
   * Get cached users for a provider and project
   */
  getCachedUsers(providerId: string, projectId?: string): UserRole[] {
    const providerUsers = this.usersCache.get(providerId);
    if (!providerUsers) return [];
 
    if (projectId) {
      return providerUsers.get(projectId) ?? [];
    }
 
    // Return all users from all projects
    const allUsers: UserRole[] = [];
    for (const projectUsers of providerUsers.values()) {
      allUsers.push(...projectUsers);
    }
 
    // Deduplicate by userId
    const uniqueUsers = new Map<string, UserRole>();
    for (const user of allUsers) {
      uniqueUsers.set(user.userId, user);
    }
 
    return Array.from(uniqueUsers.values());
  }
}