File size: 29,798 Bytes
7009aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Base Server Handler - Shared functionality for MCP servers
 * Eliminates code duplication between stdio and HTTP server implementations
 * 
 * IMPORTANT FOR LLMs/AI ASSISTANTS:
 * =================================
 * The function names in this MCP server may appear with different prefixes depending on your MCP client:
 * - Simple names: search, fetch, sap_community_search, sap_help_search, sap_help_get
 * - Prefixed names: mcp_sap-docs-remote_search, mcp_sap-docs-remote_fetch, etc.
 * 
 * Try the simple names first, then the prefixed versions if they don't work.
 * 
 * Note: sap_docs_search and sap_docs_get are legacy aliases for backward compatibility.
 */

import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import {
  ListResourcesRequestSchema,
  ReadResourceRequestSchema,
  CallToolRequestSchema,
  ListToolsRequestSchema,
  ListPromptsRequestSchema,
  GetPromptRequestSchema
} from "@modelcontextprotocol/sdk/types.js";
import {
  searchLibraries,
  fetchLibraryDocumentation,
  listDocumentationResources,
  readDocumentationResource,
  searchCommunity
} from "./localDocs.js";
import { searchSapHelp, getSapHelpContent } from "./sapHelp.js";

import { SearchResponse } from "./types.js";
import { logger } from "./logger.js";
import { search } from "./search.js";
import { CONFIG } from "./config.js";
import { loadMetadata, getDocUrlConfig } from "./metadata.js";
import { generateDocumentationUrl, formatSearchResult } from "./url-generation/index.js";

/**
 * Helper functions for creating structured JSON responses compatible with ChatGPT and all MCP clients
 */

interface SearchResult {
  id: string;
  title: string;
  url: string;
  snippet?: string;
  score?: number;
  metadata?: Record<string, any>;
}

interface DocumentResult {
  id: string;
  title: string;
  text: string;
  url: string;
  metadata?: Record<string, any>;
}

/**
 * Create structured JSON response for search results (ChatGPT-compatible)
 */
function createSearchResponse(results: SearchResult[]): any {
  // Clean the results to avoid JSON serialization issues in MCP protocol
  const cleanedResults = results.map(result => ({
    // ChatGPT requires: id, title, url (other fields optional)
    id: result.id,
    title: result.title ? result.title.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n') : result.title,
    url: result.url,
    // Additional fields for enhanced functionality
    snippet: result.snippet ? result.snippet.replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '').replace(/\r\n/g, '\n').replace(/\r/g, '\n') : result.snippet,
    score: result.score,
    metadata: result.metadata
  }));
  
  // ChatGPT expects: { "results": [...] } in JSON-encoded text content
  return {
    content: [
      {
        type: "text",
        text: JSON.stringify({ results: cleanedResults })
      }
    ]
  };
}

/**
 * Create structured JSON response for document fetch (ChatGPT-compatible)
 */
function createDocumentResponse(document: DocumentResult): any {
  // Clean the text content to avoid JSON serialization issues in MCP protocol
  const cleanedDocument = {
    // ChatGPT requires: id, title, text, url, metadata
    id: document.id,
    title: document.title,
    text: document.text
      .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, '') // Remove control chars except \n, \r, \t
      .replace(/\r\n/g, '\n') // Normalize line endings
      .replace(/\r/g, '\n'), // Convert remaining \r to \n
    url: document.url,
    metadata: document.metadata
  };
  
  // ChatGPT expects document object as JSON-encoded text content
  return {
    content: [
      {
        type: "text", 
        text: JSON.stringify(cleanedDocument)
      }
    ]
  };
}

/**
 * Create error response in structured JSON format
 */
function createErrorResponse(error: string, requestId?: string): any {
  return {
    content: [
      {
        type: "text",
        text: JSON.stringify({ 
          error,
          requestId: requestId || 'unknown'
        })
      }
    ]
  };
}

export interface ServerConfig {
  name: string;
  description: string;
  version: string;
}

/**
 * Helper function to extract client metadata from request
 */
function extractClientMetadata(request: any): Record<string, any> {
  const metadata: Record<string, any> = {};
  
  // Try to extract available metadata from the request
  if (request.meta) {
    metadata.meta = request.meta;
  }
  
  // Extract any client identification from headers or other sources
  if (request.headers) {
    metadata.headers = request.headers;
  }
  
  // Extract transport information if available
  if (request.transport) {
    metadata.transport = request.transport;
  }
  
  // Extract session or connection info
  if (request.id) {
    metadata.requestId = request.id;
  }
  
  return metadata;
}

/**
 * Base Server Handler Class
 * Provides shared functionality for all MCP server implementations
 */
export class BaseServerHandler {
  
  /**
   * Configure server with shared resource and tool handlers
   */
  static configureServer(srv: Server): void {
    // Only setup resource handlers if resources capability is enabled
    // DISABLED: Resources capability causes 60,000+ resources which breaks Cursor
    // this.setupResourceHandlers(srv);
    this.setupToolHandlers(srv);

    const capabilities = (srv as unknown as { _capabilities?: { prompts?: object } })._capabilities;
    if (capabilities?.prompts) {
      this.setupPromptHandlers(srv);
    }
  }

  /**
   * Setup resource handlers (shared between all server types)
   */
  private static setupResourceHandlers(srv: Server): void {
    // List available resources
    srv.setRequestHandler(ListResourcesRequestSchema, async () => {
      const resources = await listDocumentationResources();
      return { resources };
    });

    // Read resource contents
    srv.setRequestHandler(ReadResourceRequestSchema, async (request) => {
      const { uri } = request.params;
      try {
        return await readDocumentationResource(uri);
      } catch (error: any) {
        return {
          contents: [{
            uri,
            mimeType: "text/plain",
            text: `Error reading resource: ${error.message}`
          }]
        };
      }
    });
  }

  /**
   * Setup tool handlers (shared between all server types)
   */
  private static setupToolHandlers(srv: Server): void {
    // List available tools
    srv.setRequestHandler(ListToolsRequestSchema, async () => {
      return {
        tools: [
          {
            name: "sap_community_search", 
            description: `SEARCH SAP COMMUNITY: sap_community_search(query="search terms")

FUNCTION NAME: sap_community_search (or mcp_sap-docs-remote_sap_community_search)

FINDS: Blog posts, discussions, solutions from SAP Community
INCLUDES: Engagement data (kudos), ranked by "Best Match"

TYPICAL WORKFLOW:
1. sap_community_search(query="your problem + error code")
2. fetch(id="community-12345") for full posts

BEST FOR TROUBLESHOOTING:
• Include error codes: "415 error", "500 error"
• Be specific: "CAP action binary upload 415"
• Use real scenarios: "wizard implementation issues"`,
            inputSchema: {
              type: "object",
              properties: {
                query: {
                  type: "string",
                  description: "Search terms for SAP Community. Include error codes and specific technical details.",
                  examples: [
                    "CAP action parameter binary file upload 415 error",
                    "wizard implementation best practices",
                    "fiori elements authentication",
                    "UI5 deployment issues",
                    "wdi5 test automation problems"
                  ]
                }
              },
              required: ["query"]
            }
          },
          {
            name: "sap_help_search",
            description: `SEARCH SAP HELP PORTAL: sap_help_search(query="product + topic")

FUNCTION NAME: sap_help_search (or mcp_sap-docs-remote_sap_help_search)

SEARCHES: Official SAP Help Portal (help.sap.com)
COVERS: Product guides, implementation guides, technical documentation

TYPICAL WORKFLOW:
1. sap_help_search(query="product name + configuration topic")
2. sap_help_get(result_id="sap-help-12345abc")

BEST PRACTICES:
• Include product names: "S/4HANA", "BTP", "Fiori"
• Add specific tasks: "configuration", "setup", "deployment"
• Use official SAP terminology`,
            inputSchema: {
              type: "object",
              properties: {
                query: {
                  type: "string",
                  description: "Search terms for SAP Help Portal. Include product names and specific topics.",
                  examples: [
                    "S/4HANA configuration",
                    "Fiori Launchpad setup", 
                    "BTP integration",
                    "ABAP development guide",
                    "SAP Analytics Cloud setup"
                  ]
                }
              },
              required: ["query"]
            }
          },
          {
            name: "sap_help_get", 
            description: `GET SAP HELP PAGE: sap_help_get(result_id="sap-help-12345abc")

FUNCTION NAME: sap_help_get (or mcp_sap-docs-remote_sap_help_get)

RETRIEVES: Complete SAP Help Portal page content
REQUIRES: Exact result_id from sap_help_search

USAGE PATTERN:
1. Get ID from sap_help_search results  
2. Use exact ID (don't modify the format)
3. Receive full page content + metadata`,
            inputSchema: {
              type: "object",
              properties: {
                result_id: {
                  type: "string",
                  description: "Exact ID from sap_help_search results. Copy the ID exactly as returned.",
                  examples: [
                    "sap-help-12345abc",
                    "sap-help-98765def"
                  ]
                }
              },
              required: ["result_id"]
            }
          },
          {
            name: "search",
            description: `SEARCH SAP DOCS: search(query="search terms")

FUNCTION NAME: search

COVERS: ABAP (all versions), UI5, CAP, wdi5, OpenUI5 APIs, Cloud SDK
AUTO-DETECTS: ABAP versions from query (e.g. "LOOP 7.57", defaults to 7.58)

TYPICAL WORKFLOW:
1. search(query="your search terms") 
2. fetch(id="result_id_from_step_1")

QUERY TIPS:
• Be specific: "CAP action binary parameter" not just "CAP"
• Include error codes: "415 error CAP action"
• Use technical terms: "LargeBinary MediaType XMLHttpRequest"
• For ABAP: Include version like "7.58" or "latest"`,
            inputSchema: {
              type: "object",
              properties: {
                query: {
                  type: "string",
                  description: "Search terms using natural language. Be specific and include technical terms.",
                  examples: [
                    "CAP binary data LargeBinary MediaType",
                    "UI5 button properties",
                    "wdi5 testing locators", 
                    "ABAP SELECT statements 7.58",
                    "415 error CAP action parameter"
                  ]
                }
              },
              required: ["query"]
            }
          },
          {
            name: "fetch",
            description: `GET SPECIFIC DOCS: fetch(id="result_id")

FUNCTION NAME: fetch

RETRIEVES: Full content from search results
WORKS WITH: Document IDs returned by search

ChatGPT COMPATIBLE:
• Uses "id" parameter (required by ChatGPT)
• Returns structured JSON content
• Includes full document text and metadata`,
            inputSchema: {
              type: "object",
              properties: {
                id: {
                  type: "string",
                  description: "Unique document ID from search results. Use exact IDs returned by search.",
                  examples: [
                    "/cap/guides/domain-modeling",
                    "/sapui5/controls/button-properties", 
                    "/openui5-api/sap/m/Button",
                    "/abap-docs-758/inline-declarations",
                    "community-12345"
                  ]
                }
              },
              required: ["id"]
            }
          },

        ]
      };
    });

    // Handle tool execution
    srv.setRequestHandler(CallToolRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
      const clientMetadata = extractClientMetadata(request);

      if (name === "sap_docs_search" || name === "search") {
        const { query } = args as { query: string };
        
        // Enhanced logging with timing
        const timing = logger.logToolStart(name, query, clientMetadata);
        
        try {
          // Use hybrid search with reranking
          const results = await search(query, { 
            k: CONFIG.RETURN_K 
          });
          
          const topResults = results;
          
          if (topResults.length === 0) {
            logger.logToolSuccess(name, timing.requestId, timing.startTime, 0, { fallback: false });
            return createErrorResponse(
              `No results for "${query}". Try UI5 controls ("button", "table"), CAP topics ("actions", "binary"), testing ("wdi5", "e2e"), ABAP with versions ("SELECT 7.58"), or include error codes ("415 error").`,
              timing.requestId
            );
          }
          
          // Transform results to ChatGPT-compatible format with id, title, url
          const searchResults: SearchResult[] = topResults.map((r, index) => {
            // Extract library_id and topic from document ID
            const libraryIdMatch = r.id.match(/^(\/[^\/]+)/);
            const libraryId = libraryIdMatch ? libraryIdMatch[1] : (r.sourceId ? `/${r.sourceId}` : r.id);
            const topic = r.id.startsWith(libraryId) ? r.id.slice(libraryId.length + 1) : '';
            
            const config = getDocUrlConfig(libraryId);
            const docUrl = config ? generateDocumentationUrl(libraryId, '', r.text, config) : null;
            
            return {
              // ChatGPT-required format: id, title, url
              id: r.id,  // Use full document ID as required by ChatGPT
              title: r.text.split('\n')[0] || r.id,
              url: docUrl || `#${r.id}`,
              // Additional fields for backward compatibility
              library_id: libraryId,
              topic: topic,
              snippet: r.text ? r.text.substring(0, CONFIG.EXCERPT_LENGTH_MAIN) + '...' : '',
              score: r.finalScore,
              metadata: {
                source: r.sourceId || 'sap-docs',
                library: libraryId,
                bm25Score: r.bm25,
                rank: index + 1
              }
            };
          });
          
          logger.logToolSuccess(name, timing.requestId, timing.startTime, topResults.length, { fallback: false });
          
          return createSearchResponse(searchResults);
        } catch (error) {
          logger.logToolError(name, timing.requestId, timing.startTime, error, false);
          logger.info('Attempting fallback to original search after hybrid search failure');
          
          // Fallback to original search
          try {
            const res: SearchResponse = await searchLibraries(query);
            
            if (!res.results.length) {
              logger.logToolSuccess(name, timing.requestId, timing.startTime, 0, { fallback: true });
              return createErrorResponse(
                res.error || `No fallback results for "${query}". Try UI5 controls ("button", "table"), CAP topics ("actions", "binary"), testing ("wdi5", "e2e"), ABAP with versions ("SELECT 7.58"), or include error codes.`,
                timing.requestId
              );
            }
            
            // Transform fallback results to structured format
            const fallbackResults: SearchResult[] = res.results.map((r, index) => ({
              id: r.id || `fallback-${index}`,
              title: r.title || 'SAP Documentation',
              url: r.url || `#${r.id}`,
              snippet: r.description ? r.description.substring(0, 200) + '...' : '',
              metadata: {
                source: 'fallback-search',
                rank: index + 1
              }
            }));
            
            logger.logToolSuccess(name, timing.requestId, timing.startTime, res.results.length, { fallback: true });
            
            return createSearchResponse(fallbackResults);
          } catch (fallbackError) {
            logger.logToolError(name, timing.requestId, timing.startTime, fallbackError, true);
            return createErrorResponse(
              `Search temporarily unavailable. Wait 30 seconds and retry, try sap_community_search instead, or use more specific search terms.`,
              timing.requestId
            );
          }
        }
      }

      if (name === "sap_community_search") {
        const { query } = args as { query: string };
        
        // Enhanced logging with timing
        const timing = logger.logToolStart(name, query, clientMetadata);
        
        try {
          const res: SearchResponse = await searchCommunity(query);
          
          if (!res.results.length) {
            logger.logToolSuccess(name, timing.requestId, timing.startTime, 0);
            return createErrorResponse(
              res.error || `No SAP Community posts found for "${query}". Try different keywords or check your connection.`,
              timing.requestId
            );
          }
          
          // Transform community search results to ChatGPT-compatible format
          const communityResults: SearchResult[] = res.results.map((r: any, index) => ({
            // ChatGPT-required format: id, title, url
            id: r.id || `community-${index}`,
            title: r.title || 'SAP Community Post',
            url: r.url || `#${r.id}`,
            // Additional fields for enhanced functionality
            library_id: r.library_id || `community-${index}`,
            topic: r.topic || '',
            snippet: r.snippet || (r.description ? r.description.substring(0, 200) + '...' : ''),
            score: r.score || 0,
            metadata: r.metadata || {
              source: 'sap-community',
              likes: r.likes,
              author: r.author,
              postTime: r.postTime,
              rank: index + 1
            }
          }));
          
          logger.logToolSuccess(name, timing.requestId, timing.startTime, res.results.length);
          
          return createSearchResponse(communityResults);
        } catch (error) {
          logger.logToolError(name, timing.requestId, timing.startTime, error);
          return createErrorResponse(
            `SAP Community search service temporarily unavailable. Please try again later.`,
            timing.requestId
          );
        }
      }

      if (name === "sap_docs_get" || name === "fetch") {
        // Handle both old format (library_id) and new ChatGPT format (id)
        const library_id = (args as any).library_id || (args as any).id;
        const topic = (args as any).topic || "";
        
        if (!library_id) {
          const timing = logger.logToolStart(name, 'missing_id', clientMetadata);
          logger.logToolError(name, timing.requestId, timing.startTime, new Error('Missing id parameter'));
          return createErrorResponse(
            `Missing required parameter: id. Please provide a document ID from search results.`,
            timing.requestId
          );
        }
        
        // Enhanced logging with timing
        const searchKey = library_id + (topic ? `/${topic}` : '');
        const timing = logger.logToolStart(name, searchKey, clientMetadata);
        
        try {
          const text = await fetchLibraryDocumentation(library_id, topic);
          
          if (!text) {
            logger.logToolSuccess(name, timing.requestId, timing.startTime, 0);
            return createErrorResponse(
              `Nothing found for ${library_id}`,
              timing.requestId
            );
          }
          
          // Transform document content to ChatGPT-compatible format
          const config = getDocUrlConfig(library_id);
          const docUrl = config ? generateDocumentationUrl(library_id, '', text, config) : null;
          const document: DocumentResult = {
            id: library_id,
            title: library_id.replace(/^\//, '').replace(/\//g, ' > ') + (topic ? ` (${topic})` : ''),
            text: text,
            url: docUrl || `#${library_id}`,
            metadata: {
              source: 'sap-docs',
              library: library_id,
              topic: topic || undefined,
              contentLength: text.length
            }
          };
          
          logger.logToolSuccess(name, timing.requestId, timing.startTime, 1, { 
            contentLength: text.length,
            libraryId: library_id,
            topic: topic || undefined
          });
          
          return createDocumentResponse(document);
        } catch (error) {
          logger.logToolError(name, timing.requestId, timing.startTime, error);
          return createErrorResponse(
            `Error retrieving documentation for ${library_id}. Please try again later.`,
            timing.requestId
          );
        }
      }

      if (name === "sap_help_search") {
        const { query } = args as { query: string };
        
        // Enhanced logging with timing
        const timing = logger.logToolStart(name, query, clientMetadata);
        
        try {
          const res: SearchResponse = await searchSapHelp(query);
          
          if (!res.results.length) {
            logger.logToolSuccess(name, timing.requestId, timing.startTime, 0);
            return createErrorResponse(
              res.error || `No SAP Help results found for "${query}". Try different keywords or check your connection.`,
              timing.requestId
            );
          }
          
          // Transform SAP Help search results to ChatGPT-compatible format
          const helpResults: SearchResult[] = res.results.map((r, index) => ({
            // ChatGPT-required format: id, title, url
            id: r.id || `sap-help-${index}`,
            title: r.title || 'SAP Help Document',
            url: r.url || `#${r.id}`,
            // Additional fields for enhanced functionality
            snippet: r.description ? r.description.substring(0, 200) + '...' : '',
            metadata: {
              source: 'sap-help',
              totalSnippets: r.totalSnippets,
              rank: index + 1
            }
          }));
          
          logger.logToolSuccess(name, timing.requestId, timing.startTime, res.results.length);
          
          return createSearchResponse(helpResults);
        } catch (error) {
          logger.logToolError(name, timing.requestId, timing.startTime, error);
          return createErrorResponse(
            `SAP Help search service temporarily unavailable. Please try again later.`,
            timing.requestId
          );
        }
      }

      if (name === "sap_help_get") {
        const { result_id } = args as { result_id: string };
        
        // Enhanced logging with timing
        const timing = logger.logToolStart(name, result_id, clientMetadata);
        
        try {
          const content = await getSapHelpContent(result_id);
          
          // Transform SAP Help content to structured format
          const document: DocumentResult = {
            id: result_id,
            title: `SAP Help Document (${result_id})`,
            text: content,
            url: `https://help.sap.com/#${result_id}`,
            metadata: {
              source: 'sap-help',
              resultId: result_id,
              contentLength: content.length
            }
          };
          
          logger.logToolSuccess(name, timing.requestId, timing.startTime, 1, { 
            contentLength: content.length,
            resultId: result_id
          });
          
          return createDocumentResponse(document);
        } catch (error) {
          logger.logToolError(name, timing.requestId, timing.startTime, error);
          return createErrorResponse(
            `Error retrieving SAP Help content. Please try again later.`,
            timing.requestId
          );
        }
      }



      throw new Error(`Unknown tool: ${name}`);
    });
  }

  /**
   * Setup prompt handlers (shared between all server types)
   */
  private static setupPromptHandlers(srv: Server): void {
    // List available prompts
    srv.setRequestHandler(ListPromptsRequestSchema, async () => {
      return {
        prompts: [
          {
            name: "sap_search_help",
            title: "SAP Documentation Search Helper",
            description: "Helps users construct effective search queries for SAP documentation",
            arguments: [
              {
                name: "domain",
                description: "SAP domain (UI5, CAP, ABAP, etc.)",
                required: false
              },
              {
                name: "context",
                description: "Specific context or technology area",
                required: false
              }
            ]
          },
          {
            name: "sap_troubleshoot",
            title: "SAP Issue Troubleshooting Guide",
            description: "Guides users through troubleshooting common SAP development issues",
            arguments: [
              {
                name: "error_message",
                description: "Error message or symptom description",
                required: false
              },
              {
                name: "technology",
                description: "SAP technology stack (UI5, CAP, ABAP, etc.)",
                required: false
              }
            ]
          }
        ]
      };
    });

    // Get specific prompt
    srv.setRequestHandler(GetPromptRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
      
      switch (name) {
        case "sap_search_help":
          const domain = args?.domain || "general SAP";
          const context = args?.context || "development";
          
          return {
            description: `Search helper for ${domain} documentation`,
            messages: [
              {
                role: "user",
                content: {
                  type: "text",
                  text: `I need help searching ${domain} documentation for ${context}. What search terms should I use to find the most relevant results?

Here are some tips for effective SAP documentation searches:

**For UI5/Frontend:**
- Include specific control names (e.g., "Table", "Button", "ObjectPage")
- Mention UI5 version if relevant
- Use terms like "properties", "events", "aggregations"

**For CAP/Backend:**
- Include CDS concepts (e.g., "entity", "service", "annotation")
- Mention specific features (e.g., "authentication", "authorization", "events")
- Use terms like "deployment", "configuration"

**For ABAP:**
- Include version number (e.g., "7.58", "latest")
- Use specific statement types (e.g., "SELECT", "LOOP", "MODIFY")
- Include object types (e.g., "class", "method", "interface")

**General Tips:**
- Be specific rather than broad
- Include error codes if troubleshooting
- Use technical terms rather than business descriptions
- Combine multiple related terms

What specific topic are you looking for help with?`
                }
              }
            ]
          };

        case "sap_troubleshoot":
          const errorMessage = args?.error_message || "an issue";
          const technology = args?.technology || "SAP";
          
          return {
            description: `Troubleshooting guide for ${technology}`,
            messages: [
              {
                role: "user", 
                content: {
                  type: "text",
                  text: `I'm experiencing ${errorMessage} with ${technology}. Let me help you troubleshoot this systematically.

**Step 1: Information Gathering**
- What is the exact error message or symptom?
- When does this occur (during development, runtime, deployment)?
- What were you trying to accomplish?
- What technology stack are you using?

**Step 2: Initial Search Strategy**
Let me search the SAP documentation for similar issues:

**For UI5 Issues:**
- Search for the exact error message
- Include control or component names
- Look for browser console errors

**For CAP Issues:**
- Check service definitions and annotations
- Look for deployment configuration
- Verify database connections

**For ABAP Issues:**
- Include ABAP version in search
- Look for syntax or runtime errors
- Check object dependencies

**Step 3: Common Solutions**
Based on the issue type, I'll search for:
- Official SAP documentation
- Community discussions
- Code examples and samples

Please provide more details about your specific issue, and I'll search for relevant solutions.`
                }
              }
            ]
          };

        default:
          throw new Error(`Unknown prompt: ${name}`);
      }
    });
  }

  /**
   * Initialize metadata system (shared initialization logic)
   */
  static initializeMetadata(): void {
    logger.info('Initializing BM25 search system...');
    try {
      loadMetadata();
      logger.info('Search system ready with metadata');
    } catch (error) {
      logger.warn('Metadata loading failed, using defaults', { error: String(error) });
      logger.info('Search system ready');
    }
  }
}