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 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 | 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 12x 6x 6x 6x 4x 2x 1x 12x 1x 1x 12x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 6x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 6x 6x 6x 2x 4x 1x 1x 1x 2x 3x 2x 2x 1x 1x 1x 1x | import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { ListToolsRequestSchema, CallToolRequestSchema, ListResourcesRequestSchema, ReadResourceRequestSchema, ListPromptsRequestSchema, GetPromptRequestSchema, Tool, } from '@modelcontextprotocol/sdk/types.js'; import { ProviderManager } from '../providers/ProviderManager.js'; import { WorkItemsManager } from '../abstraction/WorkItemsManager.js'; import { RepositoryManager } from '../abstraction/RepositoryManager.js'; import { ProjectManager } from '../abstraction/ProjectManager.js'; import { MergeRequestManager } from '../abstraction/MergeRequestManager.js'; import { PipelineManager } from '../abstraction/PipelineManager.js'; import { SearchManager } from '../abstraction/SearchManager.js'; import { BranchManager } from '../abstraction/BranchManager.js'; import { CommitManager } from '../abstraction/CommitManager.js'; import { NexusConfig, ProviderInstance } from '../types/index.js'; import { createServer } from 'http'; import express from 'express'; import { logger } from '../utils/logger.js'; export class NexusProxyServer { private server: Server; private providerManager: ProviderManager; private workItemsManager: WorkItemsManager; private repositoryManager: RepositoryManager; private projectManager: ProjectManager; private mergeRequestManager: MergeRequestManager; private pipelineManager: PipelineManager; private searchManager: SearchManager; private branchManager: BranchManager; private commitManager: CommitManager; private config: NexusConfig; private httpServer?: ReturnType<typeof createServer>; private expressApp?: express.Application; constructor() { this.server = new Server( { name: 'project-nexus-mcp', version: '1.0.0', description: 'Unified MCP proxy for DevOps platforms', }, { capabilities: { tools: {}, resources: {}, prompts: {}, }, }, ); this.providerManager = new ProviderManager(); this.workItemsManager = new WorkItemsManager(this.providerManager); this.repositoryManager = new RepositoryManager(this.providerManager); this.projectManager = new ProjectManager(this.providerManager); this.mergeRequestManager = new MergeRequestManager(this.providerManager); this.pipelineManager = new PipelineManager(this.providerManager); this.searchManager = new SearchManager(this.providerManager); this.branchManager = new BranchManager(this.providerManager); this.commitManager = new CommitManager(this.providerManager); this.config = { providers: [], projects: {}, }; this.setupHandlers(); this.setupProviderEventHandlers(); } private setupHandlers(): void { this.server.setRequestHandler(ListToolsRequestSchema, () => { // DO NOT expose provider tools - only unified nexus_* tools const workItemTools = this.workItemsManager.createUnifiedTools(); const repositoryTools = this.repositoryManager.createUnifiedTools(); const projectTools = this.projectManager.createUnifiedTools(); const mergeRequestTools = this.mergeRequestManager.createUnifiedTools(); const pipelineTools = this.pipelineManager.createUnifiedTools(); const searchTools = this.searchManager.createUnifiedTools(); const branchTools = this.branchManager.createUnifiedTools(); const commitTools = this.commitManager.createUnifiedTools(); const additionalTools = this.createAdditionalTools(); return { tools: [ ...workItemTools, ...repositoryTools, ...projectTools, ...mergeRequestTools, ...pipelineTools, ...searchTools, ...branchTools, ...commitTools, ...additionalTools, ], }; }); this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; const safeArgs = args ?? {}; if (name.startsWith('nexus_')) { return await this.handleNexusTool(name, safeArgs); } const result = await this.providerManager.callTool(name, safeArgs); return result as { [x: string]: unknown; _meta?: { [x: string]: unknown } | undefined }; }); this.server.setRequestHandler(ListResourcesRequestSchema, () => { const resources = this.providerManager.getAllResources(); return { resources }; }); this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => { const { uri } = request.params; const result = await this.providerManager.readResource(uri); return result as { [x: string]: unknown; _meta?: { [x: string]: unknown } | undefined }; }); this.server.setRequestHandler(ListPromptsRequestSchema, () => { const prompts = this.providerManager.getAllPrompts(); return { prompts }; }); this.server.setRequestHandler(GetPromptRequestSchema, async (request) => { const { name, arguments: args } = request.params; const result = await this.providerManager.getPrompt(name, args); return result as { [x: string]: unknown; _meta?: { [x: string]: unknown } | undefined }; }); } private setupProviderEventHandlers(): void { this.providerManager.on('provider:connected', (provider: { id: string }) => { logger.log(`š Provider connected: ${provider.id}`); // Show detailed tool information when provider connects const providerInstance = this.providerManager .getAllProviders() .find((p) => p.id === provider.id); if (providerInstance && providerInstance.status === 'connected') { this.printProviderToolsOnConnect(providerInstance); } }); this.providerManager.on('provider:disconnected', (provider: { id: string }) => { logger.log(`š Provider disconnected: ${provider.id}`); }); this.providerManager.on( 'provider:error', ({ provider, error }: { provider: { id: string }; error: unknown }) => { const errorMessage = error instanceof Error ? error.message : String(error); logger.error(`ā Provider error (${provider.id}): ${errorMessage}`); }, ); this.providerManager.on('provider:auth_failed', (provider: { id: string; error?: string }) => { const missingTokensInfo = this.getMissingTokensInfo(provider.id); logger.error( `š Provider authentication failed (${provider.id}): ${provider.error ?? 'Missing authentication tokens'}`, ); if (missingTokensInfo) { logger.log(`š” ${missingTokensInfo}`); } }); this.providerManager.on('provider:update-check', (provider: { id: string }) => { logger.log(`š Checking updates for provider: ${provider.id}`); }); } /** * Get information about missing tokens for a specific provider */ private getMissingTokensInfo(providerId: string): string | null { switch (providerId) { case 'github': Eif (!process.env.GITHUB_TOKEN) { return 'Set GITHUB_TOKEN environment variable with your GitHub personal access token'; } break; case 'gitlab': Eif (!process.env.GITLAB_TOKEN) { return 'Set GITLAB_TOKEN environment variable with your GitLab personal access token'; } break; case 'azure': { const missingAzure: string[] = []; Eif (!process.env.AZURE_TOKEN) { missingAzure.push('AZURE_TOKEN'); } Eif (!process.env.AZURE_ORG) { missingAzure.push('AZURE_ORG'); } Eif (missingAzure.length > 0) { return `Set environment variables: ${missingAzure.join(', ')}`; } break; } } return null; } /** * Get information about all missing tokens */ private getAllMissingTokensInfo(): Record<string, string> { const missing: Record<string, string> = {}; const providers = ['github', 'gitlab', 'azure']; for (const providerId of providers) { const info = this.getMissingTokensInfo(providerId); Eif (info) { missing[providerId] = info; } } return missing; } private async handleNexusTool( name: string, args: Record<string, unknown>, ): Promise<{ content: Array<{ type: string; text: string }> }> { switch (name) { case 'nexus_list_work_items': { const project = typeof args.project === 'string' ? args.project : undefined; const items = await this.workItemsManager.listWorkItems(project, args); return { content: [ { type: 'text', text: JSON.stringify(items, null, 2), }, ], }; } case 'nexus_create_work_item': { if (typeof args.project !== 'string') { throw new Error('Project parameter must be a string'); } const created = await this.workItemsManager.createWorkItem(args.project, args); return { content: [ { type: 'text', text: JSON.stringify(created, null, 2), }, ], }; } case 'nexus_update_work_item': { if (typeof args.id !== 'string') { throw new Error('ID parameter must be a string'); } const updated = await this.workItemsManager.updateWorkItem(args.id, args); return { content: [ { type: 'text', text: JSON.stringify(updated, null, 2), }, ], }; } case 'nexus_search_projects': { const query = typeof args.query === 'string' ? args.query : undefined; const projects = await this.workItemsManager.searchProjects(query); return { content: [ { type: 'text', text: JSON.stringify(projects, null, 2), }, ], }; } case 'nexus_search_users': { const query = typeof args.query === 'string' ? args.query : undefined; const users = await this.workItemsManager.searchUsers(query); return { content: [ { type: 'text', text: JSON.stringify(users, null, 2), }, ], }; } case 'nexus_get_project_users': { if (typeof args.project_id !== 'string') { throw new Error('project_id parameter must be a string'); } const users = this.workItemsManager.getProjectUsers(args.project_id); return { content: [ { type: 'text', text: JSON.stringify(users, null, 2), }, ], }; } case 'nexus_cache_stats': { const stats = this.workItemsManager.getCacheStats(); return { content: [ { type: 'text', text: JSON.stringify(stats, null, 2), }, ], }; } case 'nexus_transfer_work_item': { if (typeof args.source_id !== 'string' || typeof args.target_project !== 'string') { throw new Error('source_id and target_project parameters must be strings'); } const transferred = await this.workItemsManager.transferWorkItem( args.source_id, args.target_project, ); return { content: [ { type: 'text', text: JSON.stringify(transferred, null, 2), }, ], }; } // Repository Manager Tools case 'nexus_list_repositories': { const project = typeof args.project === 'string' ? args.project : undefined; const repositories = await this.repositoryManager.listRepositories(project, args); return { content: [ { type: 'text', text: JSON.stringify(repositories, null, 2), }, ], }; } case 'nexus_get_repository': { if (typeof args.project !== 'string' || typeof args.repository !== 'string') { throw new Error('project and repository parameters must be strings'); } const repository = await this.repositoryManager.getRepository( args.project, args.repository, ); return { content: [ { type: 'text', text: JSON.stringify(repository, null, 2), }, ], }; } case 'nexus_list_files': { if (typeof args.project !== 'string' || typeof args.repository !== 'string') { throw new Error('project and repository parameters must be strings'); } const path = typeof args.path === 'string' ? args.path : ''; const ref = typeof args.ref === 'string' ? args.ref : undefined; const files = await this.repositoryManager.listFiles( args.project, args.repository, path, ref, ); return { content: [ { type: 'text', text: JSON.stringify(files, null, 2), }, ], }; } case 'nexus_get_file_content': { if ( typeof args.project !== 'string' || typeof args.repository !== 'string' || typeof args.file_path !== 'string' ) { throw new Error('project, repository, and file_path parameters must be strings'); } const ref = typeof args.ref === 'string' ? args.ref : undefined; const content = await this.repositoryManager.getFileContent( args.project, args.repository, args.file_path, ref, ); return { content: [ { type: 'text', text: JSON.stringify(content, null, 2), }, ], }; } // Project Manager Tools case 'nexus_list_projects': { const provider = typeof args.provider === 'string' ? args.provider : undefined; const projects = await this.projectManager.listProjects(provider, args); return { content: [ { type: 'text', text: JSON.stringify(projects, null, 2), }, ], }; } case 'nexus_get_project': { if (typeof args.project !== 'string') { throw new Error('project parameter must be a string'); } const project = await this.projectManager.getProject(args.project); return { content: [ { type: 'text', text: JSON.stringify(project, null, 2), }, ], }; } case 'nexus_list_project_members': { if (typeof args.project !== 'string') { throw new Error('project parameter must be a string'); } const members = await this.projectManager.listProjectMembers(args.project); return { content: [ { type: 'text', text: JSON.stringify(members, null, 2), }, ], }; } case 'nexus_add_project_member': { if ( typeof args.project !== 'string' || typeof args.username !== 'string' || typeof args.role !== 'string' ) { throw new Error('project, username, and role parameters must be strings'); } const added = await this.projectManager.addProjectMember( args.project, args.username, args.role, ); return { content: [ { type: 'text', text: JSON.stringify({ success: added }, null, 2), }, ], }; } case 'nexus_remove_project_member': { if (typeof args.project !== 'string' || typeof args.username !== 'string') { throw new Error('project and username parameters must be strings'); } const removed = await this.projectManager.removeProjectMember(args.project, args.username); return { content: [ { type: 'text', text: JSON.stringify({ success: removed }, null, 2), }, ], }; } case 'nexus_reload_provider': { Iif (typeof args.provider_id !== 'string') { throw new Error('provider_id parameter must be a string'); } await this.providerManager.reloadProvider(args.provider_id); return { content: [ { type: 'text', text: `Provider ${args.provider_id} reloaded successfully`, }, ], }; } case 'nexus_provider_status': { const providers = this.providerManager.getAllProviders(); const status = providers.map((p) => ({ id: p.id, name: p.config.name, status: p.status, tools: p.tools.size, resources: p.resources.size, error: p.error, errorType: p.errorType, shouldReconnect: p.shouldReconnect, reconnectAttempts: p.reconnectAttempts ?? 0, lastUpdated: p.lastUpdated?.toISOString(), })); // Add helpful information about missing tokens const statusWithHelp = { providers: status, missingTokens: this.getAllMissingTokensInfo(), }; return { content: [ { type: 'text', text: JSON.stringify(statusWithHelp, null, 2), }, ], }; } case 'nexus_restart_all_providers': { // Check if debug tools are enabled if (process.env.NEXUS_DEBUG_TOOLS !== 'true') { throw new Error('Debug tools are not enabled. Set NEXUS_DEBUG_TOOLS=true to enable.'); } await this.providerManager.restartAllProviders(); return { content: [ { type: 'text', text: 'All providers have been restarted', }, ], }; } case 'nexus_exit_server': { // Check if debug tools are enabled if (process.env.NEXUS_DEBUG_TOOLS !== 'true') { throw new Error('Debug tools are not enabled. Set NEXUS_DEBUG_TOOLS=true to enable.'); } // Initiate graceful shutdown logger.log('Debug exit requested - shutting down server...'); // Return response first const response = { content: [ { type: 'text', text: 'Server shutdown initiated', }, ], }; // Schedule shutdown after response is sent setTimeout(() => { void (async () => { try { await this.shutdown(); process.exit(0); } catch (error) { logger.error( `Error during shutdown: ${error instanceof Error ? error.message : String(error)}`, ); process.exit(1); } })(); }, 100); return response; } default: throw new Error(`Unknown tool: ${name}`); } } private createAdditionalTools(): Tool[] { const tools: Tool[] = [ { name: 'nexus_reload_provider', description: 'Reload a specific provider (useful for updating to new versions)', inputSchema: { type: 'object', required: ['provider_id'], properties: { provider_id: { type: 'string', description: 'Provider ID to reload (e.g., "github", "gitlab", "azure")', }, }, }, }, { name: 'nexus_provider_status', description: 'Get status of all configured providers', inputSchema: { type: 'object', properties: {}, }, }, ]; // Add debug tools only if environment variable is set Iif (process.env.NEXUS_DEBUG_TOOLS === 'true') { tools.push( { name: 'nexus_restart_all_providers', description: '[DEBUG] Restart all child MCP server providers', inputSchema: { type: 'object', properties: {}, }, }, { name: 'nexus_exit_server', description: '[DEBUG] Shut down the Nexus proxy server (allows restart from Claude Code)', inputSchema: { type: 'object', properties: {}, }, }, ); } return tools; } loadConfig(_configPath?: string): void { // Always use environment variables only - no .mcp.json support logger.log('Loading configuration from environment variables'); this.config = this.loadConfigFromEnv(); } private loadConfigFromEnv(): NexusConfig { const config: NexusConfig = { providers: [], projects: {}, }; if (process.env.DEFAULT_REPOSITORY) { config.defaultRepository = process.env.DEFAULT_REPOSITORY; config.projects[''] = process.env.DEFAULT_REPOSITORY; } if (process.env.DEFAULT_TASK) { config.defaultTask = process.env.DEFAULT_TASK; } const providers = []; if (process.env.GITHUB_TOKEN) { providers.push({ id: 'github', name: 'GitHub', type: 'http' as const, url: 'https://api.githubcopilot.com/mcp/', headers: { Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, 'X-MCP-Toolsets': process.env.GITHUB_TOOLSETS ?? 'all', 'X-MCP-Readonly': 'false', }, enabled: true, }); } if (process.env.GITLAB_TOKEN) { providers.push({ id: 'gitlab', name: 'GitLab', type: 'stdio' as const, command: 'uv', args: [ 'run', '--python', '3.13', '--with', 'git+https://github.com/polaz/gitlab-mcp.git', 'python', '-m', 'gitlab_mcp', ], env: { GITLAB_PERSONAL_ACCESS_TOKEN: process.env.GITLAB_TOKEN, GITLAB_API_URL: process.env.GITLAB_URL ?? 'https://gitlab.com', }, enabled: true, autoUpdate: true, }); } if (process.env.AZURE_TOKEN && process.env.AZURE_ORG) { providers.push({ id: 'azure', name: 'Azure DevOps', type: 'stdio' as const, command: 'yarn', args: ['dlx', '-q', '@azure-devops/mcp', process.env.AZURE_ORG], env: { AZURE_DEVOPS_PAT: process.env.AZURE_TOKEN, YARN_NODE_LINKER: 'node-modules', YARN_ENABLE_PROGRESS_BARS: 'false', }, enabled: true, autoUpdate: true, }); } config.providers = providers; return config; } async initialize(): Promise<void> { this.loadConfig(); // Read version from package.json const fs = await import('fs/promises'); const path = await import('path'); try { const packageJsonPath = path.join(process.cwd(), 'package.json'); const packageData = await fs.readFile(packageJsonPath, 'utf-8'); const { version } = JSON.parse(packageData) as { version: string }; logger.log(`\n=== Project Nexus MCP Server v${version} ===`); } catch { logger.log(`\n=== Project Nexus MCP Server Initialization ===`); } logger.log(`Found ${this.config.providers.length} provider(s) in configuration`); // Initialize all providers asynchronously without blocking const initPromises = this.config.providers.map(async (providerConfig) => { if (providerConfig.enabled) { logger.log(`Initializing provider: ${providerConfig.name} (${providerConfig.id})...`); try { await this.providerManager.initializeProvider(providerConfig); logger.log(`ā ${providerConfig.name} initialized successfully`); } catch (error) { logger.error( `ā Failed to initialize provider ${providerConfig.id}: ${error instanceof Error ? error.message : String(error)}`, ); } } else { logger.log(`- ${providerConfig.name} is disabled`); } }); // Wait for all providers to attempt initialization (but don't fail if they error) await Promise.allSettled(initPromises); // Print final status this.printProviderStatus(); // Start cache warming after providers are initialized this.startCacheWarming(); this.providerManager.startAutoUpdate(3600000); // 1 hour } /** * Start cache warming process for projects and users */ private startCacheWarming(): void { const connectedProviders = this.providerManager .getAllProviders() .filter((p) => p.status === 'connected'); if (connectedProviders.length === 0) { logger.log('[cache] No connected providers, skipping cache warming'); return; } // Start cache warming in the background (don't await) setTimeout(() => { void (async () => { try { await this.providerManager.warmupCaches(); } catch (error) { logger.error( `[cache] Cache warmup failed: ${error instanceof Error ? error.message : String(error)}`, ); } })(); }, 5000); // Wait 5 seconds after server startup to avoid interfering with initialization } async runStdio(): Promise<void> { await this.initialize(); const transport = new StdioServerTransport(); await this.server.connect(transport); logger.log('\nš Project Nexus MCP Server running in STDIO mode'); logger.log('š” Ready to receive MCP requests\n'); } async runHttp(port: number = 3000): Promise<void> { await this.initialize(); const { SSEServerTransport } = await import('@modelcontextprotocol/sdk/server/sse.js'); this.expressApp = express(); this.expressApp.use(express.json()); this.expressApp.get('/sse', async (_req, res) => { const transport = new SSEServerTransport('/message', res); await this.server.connect(transport); }); this.expressApp.post('/message', (_req, res) => { // HTTP transport not yet available res.status(501).json({ error: 'HTTP transport not yet implemented' }); }); this.expressApp.get('/health', (_req, res) => { const providers = this.providerManager.getAllProviders(); res.json({ status: 'healthy', providers: providers.map((p) => ({ id: p.id, status: p.status, tools: p.tools.size, })), }); }); this.httpServer = createServer(this.expressApp); this.httpServer.listen(port, () => { process.stdout.write(`Project Nexus MCP Server running on http://localhost:${port}\n`); process.stdout.write(`SSE endpoint: http://localhost:${port}/sse\n`); process.stdout.write(`HTTP endpoint: http://localhost:${port}/message\n`); process.stdout.write(`Health check: http://localhost:${port}/health\n`); }); } private printProviderStatus(): void { const providers = this.providerManager.getAllProviders(); logger.log(`\\n=== Provider Status Summary ===`); if (providers.length === 0) { logger.warn(`ā ļø No providers configured. Set environment variables:`); logger.log(` GITHUB_TOKEN - for GitHub integration`); logger.log(` GITLAB_TOKEN - for GitLab integration`); logger.log(` AZURE_TOKEN - for Azure DevOps integration\\n`); return; } let connectedCount = 0; for (const provider of providers) { const status = provider.status === 'connected' ? 'š¢' : provider.status === 'error' ? 'š“' : 'š”'; logger.log(`${status} ${provider.config.name} (${provider.id}): ${provider.status}`); if (provider.status === 'connected') { connectedCount++; logger.log( ` Tools: ${provider.tools.size}, Resources: ${provider.resources.size}, Prompts: ${provider.prompts.size}`, ); } else if (provider.status === 'error' && provider.error) { logger.error(` Error: ${provider.error}`); } } logger.log(`\\n⨠${connectedCount}/${providers.length} providers connected successfully`); if (connectedCount > 0) { const totalTools = this.providerManager.getAllTools().length; const unifiedTools = this.workItemsManager.createUnifiedTools().length; const additionalTools = this.createAdditionalTools().length; logger.log(`\\nš Available tools: ${totalTools + unifiedTools + additionalTools} total`); logger.log(` - Provider tools: ${totalTools}`); logger.log(` - Unified work item tools: ${unifiedTools}`); logger.log(` - Management tools: ${additionalTools}`); // Show detailed tool breakdown with unified annotations this.printDetailedToolBreakdown(); } logger.log(`=====================================`); } private printProviderToolsOnConnect(provider: ProviderInstance): void { if (provider.tools.size === 0) { return; } // Get all unified tools from all managers const workItemTools = this.workItemsManager.createUnifiedTools(); const repositoryTools = this.repositoryManager.createUnifiedTools(); const projectTools = this.projectManager.createUnifiedTools(); const mergeRequestTools = this.mergeRequestManager.createUnifiedTools(); const pipelineTools = this.pipelineManager.createUnifiedTools(); const searchTools = this.searchManager.createUnifiedTools(); const branchTools = this.branchManager.createUnifiedTools(); const commitTools = this.commitManager.createUnifiedTools(); const allUnifiedTools = [ ...workItemTools, ...repositoryTools, ...projectTools, ...mergeRequestTools, ...pipelineTools, ...searchTools, ...branchTools, ...commitTools, ]; const unifiedToolPatterns = new Map<string, string>(); // Build comprehensive unified tool pattern map for (const tool of allUnifiedTools) { switch (tool.name) { // Work Items case 'nexus_list_work_items': unifiedToolPatterns.set('list.*work.*item', 'nexus_list_work_items'); unifiedToolPatterns.set('get.*work.*item', 'nexus_list_work_items'); unifiedToolPatterns.set('list.*issue', 'nexus_list_work_items'); unifiedToolPatterns.set('get.*issue', 'nexus_list_work_items'); break; case 'nexus_create_work_item': unifiedToolPatterns.set('create.*work.*item', 'nexus_create_work_item'); unifiedToolPatterns.set('create.*issue', 'nexus_create_work_item'); break; case 'nexus_update_work_item': unifiedToolPatterns.set('update.*work.*item', 'nexus_update_work_item'); unifiedToolPatterns.set('update.*issue', 'nexus_update_work_item'); break; case 'nexus_transfer_work_item': unifiedToolPatterns.set('transfer.*work.*item', 'nexus_transfer_work_item'); unifiedToolPatterns.set('move.*issue', 'nexus_transfer_work_item'); break; // Repositories case 'nexus_list_repositories': unifiedToolPatterns.set('list.*repositor', 'nexus_list_repositories'); unifiedToolPatterns.set('get.*repositor', 'nexus_list_repositories'); unifiedToolPatterns.set('list.*repo', 'nexus_list_repositories'); unifiedToolPatterns.set('search.*repositor', 'nexus_list_repositories'); break; case 'nexus_get_repository': unifiedToolPatterns.set('get.*repository$', 'nexus_get_repository'); unifiedToolPatterns.set('get.*repo$', 'nexus_get_repository'); unifiedToolPatterns.set('repository.*info', 'nexus_get_repository'); break; case 'nexus_list_files': unifiedToolPatterns.set('list.*file', 'nexus_list_files'); unifiedToolPatterns.set('browse.*repo', 'nexus_list_files'); unifiedToolPatterns.set('get.*content.*list', 'nexus_list_files'); break; case 'nexus_get_file_content': unifiedToolPatterns.set('get.*file.*content', 'nexus_get_file_content'); unifiedToolPatterns.set('read.*file', 'nexus_get_file_content'); unifiedToolPatterns.set('get.*blob', 'nexus_get_file_content'); break; // Projects case 'nexus_list_projects': unifiedToolPatterns.set('list.*project', 'nexus_list_projects'); unifiedToolPatterns.set('list.*organization', 'nexus_list_projects'); unifiedToolPatterns.set('list.*group', 'nexus_list_projects'); unifiedToolPatterns.set('list.*org', 'nexus_list_projects'); break; case 'nexus_get_project': unifiedToolPatterns.set('get.*project$', 'nexus_get_project'); unifiedToolPatterns.set('get.*organization$', 'nexus_get_project'); unifiedToolPatterns.set('get.*group$', 'nexus_get_project'); unifiedToolPatterns.set('get.*org$', 'nexus_get_project'); break; case 'nexus_list_project_members': unifiedToolPatterns.set('list.*member', 'nexus_list_project_members'); unifiedToolPatterns.set('get.*member', 'nexus_list_project_members'); unifiedToolPatterns.set('list.*collaborator', 'nexus_list_project_members'); break; // Merge Requests / Pull Requests case 'nexus_list_merge_requests': unifiedToolPatterns.set('list.*pull.*request', 'nexus_list_merge_requests'); unifiedToolPatterns.set('list.*merge.*request', 'nexus_list_merge_requests'); unifiedToolPatterns.set('list.*pr', 'nexus_list_merge_requests'); unifiedToolPatterns.set('list.*mr', 'nexus_list_merge_requests'); break; case 'nexus_get_merge_request': unifiedToolPatterns.set('get.*pull.*request', 'nexus_get_merge_request'); unifiedToolPatterns.set('get.*merge.*request', 'nexus_get_merge_request'); unifiedToolPatterns.set('get.*pr$', 'nexus_get_merge_request'); unifiedToolPatterns.set('get.*mr$', 'nexus_get_merge_request'); break; case 'nexus_create_merge_request': unifiedToolPatterns.set('create.*pull.*request', 'nexus_create_merge_request'); unifiedToolPatterns.set('create.*merge.*request', 'nexus_create_merge_request'); unifiedToolPatterns.set('create.*pr$', 'nexus_create_merge_request'); unifiedToolPatterns.set('create.*mr$', 'nexus_create_merge_request'); break; case 'nexus_merge_merge_request': unifiedToolPatterns.set('merge.*pull.*request', 'nexus_merge_merge_request'); unifiedToolPatterns.set('merge.*merge.*request', 'nexus_merge_merge_request'); unifiedToolPatterns.set('merge.*pr$', 'nexus_merge_merge_request'); unifiedToolPatterns.set('merge.*mr$', 'nexus_merge_merge_request'); break; // Pipelines / Workflows / Builds case 'nexus_list_pipelines': unifiedToolPatterns.set('list.*pipeline', 'nexus_list_pipelines'); unifiedToolPatterns.set('list.*workflow', 'nexus_list_pipelines'); unifiedToolPatterns.set('list.*build', 'nexus_list_pipelines'); unifiedToolPatterns.set('list.*action', 'nexus_list_pipelines'); break; case 'nexus_trigger_pipeline': unifiedToolPatterns.set('trigger.*pipeline', 'nexus_trigger_pipeline'); unifiedToolPatterns.set('run.*workflow', 'nexus_trigger_pipeline'); unifiedToolPatterns.set('start.*build', 'nexus_trigger_pipeline'); unifiedToolPatterns.set('dispatch.*workflow', 'nexus_trigger_pipeline'); break; case 'nexus_cancel_pipeline': unifiedToolPatterns.set('cancel.*pipeline', 'nexus_cancel_pipeline'); unifiedToolPatterns.set('cancel.*workflow', 'nexus_cancel_pipeline'); unifiedToolPatterns.set('cancel.*build', 'nexus_cancel_pipeline'); unifiedToolPatterns.set('stop.*workflow', 'nexus_cancel_pipeline'); break; // Search case 'nexus_search_code': unifiedToolPatterns.set('search.*code', 'nexus_search_code'); unifiedToolPatterns.set('search.*file', 'nexus_search_code'); unifiedToolPatterns.set('code.*search', 'nexus_search_code'); break; case 'nexus_search_repositories': unifiedToolPatterns.set('search.*repositor', 'nexus_search_repositories'); unifiedToolPatterns.set('search.*repo', 'nexus_search_repositories'); unifiedToolPatterns.set('repository.*search', 'nexus_search_repositories'); break; case 'nexus_search_issues': unifiedToolPatterns.set('search.*issue', 'nexus_search_issues'); unifiedToolPatterns.set('search.*work.*item', 'nexus_search_issues'); unifiedToolPatterns.set('issue.*search', 'nexus_search_issues'); break; case 'nexus_search_users': unifiedToolPatterns.set('search.*user', 'nexus_search_users'); unifiedToolPatterns.set('user.*search', 'nexus_search_users'); unifiedToolPatterns.set('find.*user', 'nexus_search_users'); break; // Branches case 'nexus_list_branches': unifiedToolPatterns.set('list.*branch', 'nexus_list_branches'); unifiedToolPatterns.set('get.*branch', 'nexus_list_branches'); unifiedToolPatterns.set('list.*ref', 'nexus_list_branches'); break; case 'nexus_create_branch': unifiedToolPatterns.set('create.*branch', 'nexus_create_branch'); unifiedToolPatterns.set('create.*ref', 'nexus_create_branch'); break; case 'nexus_delete_branch': unifiedToolPatterns.set('delete.*branch', 'nexus_delete_branch'); unifiedToolPatterns.set('delete.*ref', 'nexus_delete_branch'); break; // Commits case 'nexus_list_commits': unifiedToolPatterns.set('list.*commit', 'nexus_list_commits'); unifiedToolPatterns.set('get.*commit', 'nexus_list_commits'); unifiedToolPatterns.set('get.*repository.*commit', 'nexus_list_commits'); break; case 'nexus_get_commit': unifiedToolPatterns.set('get.*commit$', 'nexus_get_commit'); unifiedToolPatterns.set('get.*repository.*commit$', 'nexus_get_commit'); break; case 'nexus_get_commit_diff': unifiedToolPatterns.set('get.*commit.*diff', 'nexus_get_commit_diff'); unifiedToolPatterns.set('get.*diff', 'nexus_get_commit_diff'); unifiedToolPatterns.set('compare.*commit', 'nexus_get_commit_diff'); break; } } logger.log(` š Provider tools (${provider.tools.size} total, showing first 10):`); const toolsList = Array.from(provider.tools.values()) .sort((a: Tool, b: Tool) => a.name.localeCompare(b.name)) .slice(0, 10); for (const tool of toolsList) { const originalToolName = tool.name.substring(provider.id.length + 1); let annotation = ''; // Check if this tool is unified/replaced for (const [pattern, unifiedTool] of unifiedToolPatterns) { const regex = new RegExp(pattern, 'i'); if (regex.test(originalToolName)) { annotation = ` [unified ā ${unifiedTool}]`; break; } } logger.log(` ⢠${originalToolName}${annotation}`); } if (provider.tools.size > 10) { logger.log(` ... and ${provider.tools.size - 10} more tools`); } // Show resources and prompts summary if any if (provider.resources.size > 0) { logger.log(` š Resources: ${provider.resources.size}`); } if (provider.prompts.size > 0) { logger.log(` š Prompts: ${provider.prompts.size}`); } // After all providers are connected, show total tool reduction stats const connectedProviders = this.providerManager .getAllProviders() .filter((p) => p.status === 'connected'); if ( connectedProviders.length > 0 && connectedProviders.every((p) => p.id === provider.id || p.lastUpdated) ) { setTimeout(() => { this.printToolReductionStats(); }, 100); // Small delay to ensure all providers have finished connecting } } private printToolReductionStats(): void { const providers = this.providerManager.getAllProviders(); const connectedProviders = providers.filter((p) => p.status === 'connected'); if (connectedProviders.length === 0) return; // Count total provider tools const totalProviderTools = connectedProviders.reduce( (sum, provider) => sum + provider.tools.size, 0, ); // Count unified tools const workItemTools = this.workItemsManager.createUnifiedTools(); const repositoryTools = this.repositoryManager.createUnifiedTools(); const projectTools = this.projectManager.createUnifiedTools(); const mergeRequestTools = this.mergeRequestManager.createUnifiedTools(); const pipelineTools = this.pipelineManager.createUnifiedTools(); const searchTools = this.searchManager.createUnifiedTools(); const branchTools = this.branchManager.createUnifiedTools(); const commitTools = this.commitManager.createUnifiedTools(); const managementTools = this.createAdditionalTools(); const totalUnifiedTools = workItemTools.length + repositoryTools.length + projectTools.length + mergeRequestTools.length + pipelineTools.length + searchTools.length + branchTools.length + commitTools.length + managementTools.length; const reductionPercentage = totalProviderTools > 0 ? Math.round(((totalProviderTools - totalUnifiedTools) / totalProviderTools) * 100) : 0; logger.log(`\nšÆ === TOOL AGGREGATION SUMMARY ===`); logger.log(`š Total provider tools: ${totalProviderTools}`); logger.log(`š§ Unified nexus tools: ${totalUnifiedTools}`); logger.log( `š Tool reduction: ${totalProviderTools - totalUnifiedTools} tools (-${reductionPercentage}%)`, ); logger.log(`\nš” Benefits:`); logger.log(` ⢠Single interface across ${connectedProviders.length} DevOps platforms`); logger.log( ` ⢠Reduced complexity for AI agents (${totalUnifiedTools} vs ${totalProviderTools} tools)`, ); logger.log(` ⢠Fits within AI tool limits (GitHub Copilot: 128 tools max)`); logger.log(` ⢠Consistent naming and parameter patterns`); logger.log(`=====================================\n`); } private printDetailedToolBreakdown(): void { const providers = this.providerManager.getAllProviders(); const unifiedTools = this.workItemsManager.createUnifiedTools(); const managementTools = this.createAdditionalTools(); // Create a map of unified tool patterns to detect which provider tools are replaced const unifiedToolPatterns = new Map<string, string>(); for (const tool of unifiedTools) { switch (tool.name) { case 'nexus_list_work_items': // These provider tools would be unified under this unifiedToolPatterns.set('list.*work.*item', 'nexus_list_work_items'); unifiedToolPatterns.set('get.*work.*item', 'nexus_list_work_items'); unifiedToolPatterns.set('list.*issue', 'nexus_list_work_items'); unifiedToolPatterns.set('get.*issue', 'nexus_list_work_items'); break; case 'nexus_create_work_item': unifiedToolPatterns.set('create.*work.*item', 'nexus_create_work_item'); unifiedToolPatterns.set('create.*issue', 'nexus_create_work_item'); break; case 'nexus_update_work_item': unifiedToolPatterns.set('update.*work.*item', 'nexus_update_work_item'); unifiedToolPatterns.set('update.*issue', 'nexus_update_work_item'); break; case 'nexus_transfer_work_item': unifiedToolPatterns.set('transfer.*work.*item', 'nexus_transfer_work_item'); unifiedToolPatterns.set('move.*issue', 'nexus_transfer_work_item'); break; } } logger.log(`\\nš§ Tool Breakdown by Provider:`); for (const provider of providers) { if (provider.status !== 'connected' || provider.tools.size === 0) { continue; } logger.log(`\\n š¦ ${provider.config.name} (${provider.id}):`); const toolsList = Array.from(provider.tools.values()).sort((a, b) => a.name.localeCompare(b.name), ); for (const tool of toolsList) { const originalToolName = tool.name.substring(provider.id.length + 1); // Remove prefix let annotation = ''; // Check if this tool is unified/replaced for (const [pattern, unifiedTool] of unifiedToolPatterns) { const regex = new RegExp(pattern, 'i'); if (regex.test(originalToolName)) { annotation = ` [unified ā ${unifiedTool}]`; break; } } logger.log(` ⢠${originalToolName}${annotation}`); } } if (unifiedTools.length > 0) { logger.log(`\\n š Unified Tools:`); for (const tool of unifiedTools) { logger.log(` ⢠${tool.name} - ${tool.description}`); } } if (managementTools.length > 0) { logger.log(`\\n āļø Management Tools:`); for (const tool of managementTools) { logger.log(` ⢠${tool.name} - ${tool.description}`); } } } async shutdown(): Promise<void> { logger.log(`\\nš Shutting down Project Nexus MCP Server...`); // Provider manager will handle its own cache cleanup during shutdown await this.providerManager.shutdown(); if (this.httpServer) { this.httpServer.close(); } await this.server.close(); logger.log(`ā Server shutdown complete`); } } |