All files / scripts/mcp-client client.ts

59.76% Statements 150/251
64.31% Branches 155/241
57.57% Functions 19/33
60.17% Lines 136/226

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                                                              13x 13x 13x   13x     104x 104x                                 13x 13x     12x 13x 13x 6x     6x 6x 6x       2x 2x 6x 6x 6x         8x     13x   13x                                           105x 2x 2x 2x 2x 2x   103x 103x 103x 103x 103x     105x 105x 105x                         91x 8x         83x 73x     83x 83x   83x   83x   91x 91x   91x             91x 1x 1x           83x         83x 83x   83x             70x 3x 3x 3x       3x           67x         91x       67x     67x 2x     2x         2x 1x 1x 1x     1x                     1x     65x 91x 91x                                               65x   17x 17x   17x               9x 9x     9x 9x 9x     8x   8x 8x             8x       2x             8x   83x                 6x                           1x   1x 1x   1x 1x         1x   1x                               1x         1x       1x         1x                         1x                           4x 4x 4x   4x 4x               4x 4x 4x   4x 4x       5x 5x   5x 5x       5x 5x   5x 5x                                   10x       10x 10x       4x       4x       5x       5x       4x       4x                           4x       4x                                                                                                                                                                                                                                                                                             12x                     2x 2x      
/**
 * @module mcp-client/client
 * @description MCPClient class providing typed access to 32 riksdag-regering
 * intelligence tools via JSON-RPC 2.0.
 *
 * @author Hack23 AB
 * @license Apache-2.0
 */
 
import fs from 'fs';
import type {
  MCPClientConfig,
  MCPStats,
  JsonRpcRequest,
  JsonRpcResponse,
  SearchDocumentsParams,
  SearchSpeechesParams,
  FetchMPsFilters,
  FetchVotingFilters,
  FetchVotingGroupFilters,
  GovDocSearchParams,
  RiksdagDocument,
} from '../types/mcp.js';
import { performPost } from './transport.js';
import { annotateDocumentTypes } from './document-types.js';
 
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
 
const DEFAULT_MCP_SERVER_URL: string =
  process.env['MCP_SERVER_URL'] ?? 'https://riksdag-regering-ai.onrender.com/mcp';
const DEFAULT_MAX_RETRIES = 3;
const RETRY_DELAY = 2000;
/** Timeout in milliseconds for fetching external URLs (GitHub, etc.) */
const EXTERNAL_URL_FETCH_TIMEOUT_MS = 15_000;
 
function getDefaultTimeout(): number {
  const envVal = process.env['MCP_CLIENT_TIMEOUT_MS'];
  return envVal ? (Number.parseInt(envVal, 10) || 30_000) : 30_000;
}
 
/**
 * Resolve the default MCP auth token.
 * Priority:
 *   1. MCP_AUTH_TOKEN env var (strips "Bearer " prefix if present)
 *   2. MCP_GATEWAY_API_KEY env var (raw API key)
 *   3. gateway.apiKey from MCP config file (legacy — raw API key)
 *   4. mcpServers['riksdag-regering'].headers.Authorization from MCP config file
 *      (raw API key — used as-is)
 *
 * The MCP gateway expects a raw API key (no "Bearer " prefix). If a legacy
 * "Bearer <key>" value is stored in the config, the prefix is stripped
 * automatically.
 */
function getDefaultAuthToken(): string {
  Iif (process.env['MCP_AUTH_TOKEN']) return process.env['MCP_AUTH_TOKEN'].replace(/^Bearer\s+/i, '');
  if (process.env['MCP_GATEWAY_API_KEY']) return process.env['MCP_GATEWAY_API_KEY'];
 
  // Try reading from MCP config file
  const configPath = process.env['GH_AW_MCP_CONFIG'] ?? '/home/runner/.copilot/mcp-config.json';
  try {
    if (fs.existsSync(configPath)) {
      const raw = JSON.parse(fs.readFileSync(configPath, 'utf8')) as Record<string, unknown>;
 
      // Priority 3: legacy gateway.apiKey
      const gateway = raw['gateway'] as Record<string, unknown> | undefined;
      const apiKey = gateway?.['apiKey'] as string | undefined;
      if (apiKey) return apiKey.replace(/^Bearer\s+/i, '');
 
      // Priority 4: mcpServers['riksdag-regering'].headers.Authorization
      // Strip legacy "Bearer " prefix if present — gateway expects raw API key
      const mcpServers = raw['mcpServers'] as Record<string, unknown> | undefined;
      const rrServer = mcpServers?.['riksdag-regering'] as Record<string, unknown> | undefined;
      const headers = rrServer?.['headers'] as Record<string, unknown> | undefined;
      const authHeader = headers?.['Authorization'] as string | undefined;
      if (authHeader) return authHeader.replace(/^Bearer\s+/i, '');
    }
  } catch {
    // Config file read is best-effort — fall through to empty token
  }
  return '';
}
 
const DEFAULT_MCP_AUTH_TOKEN: string = getDefaultAuthToken();
 
let jsonRpcId = 1;
 
// ---------------------------------------------------------------------------
// MCPClient class
// ---------------------------------------------------------------------------
 
/**
 * MCP (Model Context Protocol) client providing typed access to
 * 32 riksdag-regering intelligence tools via JSON-RPC 2.0.
 */
export class MCPClient {
  readonly baseURL: string;
  readonly timeout: number;
  readonly maxRetries: number;
  readonly customHeaders: Readonly<Record<string, string>>;
  readonly authToken: string;
 
  requestCount: number;
  errorCount: number;
  sessionId: string | null;
 
  constructor(config: MCPClientConfig | string = {}) {
    if (typeof config === 'string') {
      this.baseURL = config;
      this.timeout = getDefaultTimeout();
      this.maxRetries = DEFAULT_MAX_RETRIES;
      this.customHeaders = {};
      this.authToken = DEFAULT_MCP_AUTH_TOKEN;
    } else {
      this.baseURL = config.baseURL ?? config.serverUrl ?? DEFAULT_MCP_SERVER_URL;
      this.timeout = config.timeout ?? getDefaultTimeout();
      this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
      this.customHeaders = config.headers ?? {};
      this.authToken = config.authToken ?? DEFAULT_MCP_AUTH_TOKEN;
    }
 
    this.requestCount = 0;
    this.errorCount = 0;
    this.sessionId = null;
  }
 
  // -----------------------------------------------------------------------
  // Core request
  // -----------------------------------------------------------------------
 
  async request(
    tool: string,
    params: Record<string, unknown> = {},
    retryCount = 0,
    skipPrefix = false,
  ): Promise<Record<string, unknown>> {
    if (!tool || typeof tool !== 'string' || !/^[a-zA-Z0-9_-]+$/.test(tool)) {
      throw new Error(
        `Invalid tool name: ${tool}. Tool names must contain only alphanumeric characters, hyphens, and underscores.`,
      );
    }
 
    if (retryCount === 0 && !skipPrefix) {
      this.requestCount++;
    }
 
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), this.timeout);
 
    try {
      const isGateway =
        this.baseURL.includes('host.docker.internal') ||
        this.baseURL.includes('/mcp/riksdag-regering');
      const shouldPrefix = isGateway && !skipPrefix && !tool.includes('--');
      const toolName = shouldPrefix ? `riksdag-regering--${tool}` : tool;
 
      const jsonRpcRequest: JsonRpcRequest = {
        jsonrpc: '2.0',
        id: jsonRpcId++,
        method: 'tools/call',
        params: { name: toolName, arguments: params },
      };
 
      if (this.authToken && !this.sessionId) {
        try {
          await this.initializeSession();
        } catch {
          // Session init is optional
        }
      }
 
      const headers: Record<string, string> = {
        'Content-Type': 'application/json',
        Accept: 'application/json, text/event-stream',
        ...this.customHeaders,
      };
      if (this.authToken) headers['Authorization'] = this.authToken;
      Iif (this.sessionId) headers['Mcp-Session-Id'] = this.sessionId;
 
      const response = await performPost(
        this.baseURL,
        headers,
        JSON.stringify(jsonRpcRequest),
        controller.signal,
      );
 
      if (!response.ok) {
        let errorBody = '';
        try {
          errorBody = await response.text();
        } catch {
          // ignore
        }
        throw new Error(
          `MCP server error: ${response.status} ${response.statusText}${errorBody ? ' - ' + errorBody : ''}`,
        );
      }
 
      const contentType: string =
        response.headers && typeof response.headers.get === 'function'
          ? (response.headers.get('content-type') ?? '')
          : '';
 
      let jsonRpcResponse: JsonRpcResponse;
      Iif (contentType.includes('text/event-stream')) {
        const text = await response.text();
        jsonRpcResponse = this.parseSSEResponse(text);
      } else {
        jsonRpcResponse = (await response.json()) as JsonRpcResponse;
      }
 
      if (jsonRpcResponse.error) {
        const errorMsg = jsonRpcResponse.error.message || JSON.stringify(jsonRpcResponse.error);
 
        const isToolLookupError =
          errorMsg.includes('not found') ||
          errorMsg.includes('Internal error') ||
          errorMsg.includes('Unknown tool') ||
          errorMsg.includes('unknown tool');
 
        if (isToolLookupError && toolName.startsWith('riksdag-regering--') && !skipPrefix) {
          const bareTool = toolName.replace(/^riksdag-regering--/, '');
          console.warn(`⚠️ Tool '${toolName}' not found, retrying as '${bareTool}'...`);
          return this.request(bareTool, params, retryCount, true);
        }
 
        Iif (errorMsg.includes('session initialization') || errorMsg.includes('Too Many Requests')) {
          this.sessionId = null;
          if (retryCount < 2) {
            const delay = (retryCount + 1) * 2000;
            console.warn(`⚠️ Session error, re-initializing after ${delay}ms...`);
            await new Promise<void>((r) => setTimeout(r, delay));
            await this.initializeSession();
            return this.request(tool, params, retryCount + 1, skipPrefix);
          }
        }
 
        throw new Error(`MCP tool error: ${errorMsg}`);
      }
 
      const result = (jsonRpcResponse.result ?? {}) as Record<string, unknown>;
      const content = result['content'] as Array<{ text?: string }> | undefined;
      Iif (Array.isArray(content) && content[0]?.text) {
        try {
          const parsed = JSON.parse(content[0].text) as Record<string, unknown>;
          if (parsed['payloadPath']) {
            const fs = await import('fs');
            const payloadRaw = JSON.parse(
              fs.readFileSync(parsed['payloadPath'] as string, 'utf8'),
            ) as Record<string, unknown>;
            const payloadContent = payloadRaw['content'] as Array<{ text?: string }> | undefined;
            const payloadText = payloadContent?.[0]?.text;
            if (payloadText) {
              try {
                return JSON.parse(payloadText) as Record<string, unknown>;
              } catch {
                return { text: payloadText };
              }
            }
            return payloadRaw;
          }
          return parsed;
        } catch {
          return { text: content[0].text };
        }
      }
      return result;
    } catch (error: unknown) {
      const err = error as Error;
      const errorMsg = (err.message ?? '').toLowerCase();
 
      if (
        retryCount < this.maxRetries - 1 &&
        (err.name === 'AbortError' ||
          errorMsg.includes('network') ||
          errorMsg.includes('econnrefused') ||
          errorMsg.includes('connection closed') ||
          errorMsg.includes('too many requests'))
      ) {
        const delay = RETRY_DELAY * Math.pow(2, retryCount);
        console.warn(
          `⚠️ Request failed (${err.message.substring(0, 60)}), retrying after ${delay}ms (${retryCount + 1}/${this.maxRetries - 1})...`,
        );
        this.sessionId = null;
        await this.sleep(delay);
        return this.request(tool, params, retryCount + 1, skipPrefix);
      }
 
      this.errorCount++;
 
      let errorMessage = `MCP request failed: ${err.message}`;
      Iif (err.name === 'AbortError' || errorMsg.includes('timeout')) {
        errorMessage += `\n\n💡 Troubleshooting tips:
  - The MCP server may be cold starting (Render.com free tier)
  - Try increasing timeout or waiting a few minutes
  - Server URL: ${this.baseURL}
  - Consider running workflow again in 5-10 minutes`;
      } else if (
        errorMsg.includes('network') ||
        errorMsg.includes('econnrefused') ||
        errorMsg.includes('fetch failed')
      ) {
        errorMessage += `\n\n💡 Troubleshooting tips:
  - Check if MCP server is accessible: ${this.baseURL}
  - Verify network connectivity
  - The server may be temporarily unavailable
  - Try manual workflow dispatch with force_generation=true`;
      }
 
      throw new Error(errorMessage, { cause: error });
    } finally {
      clearTimeout(timeoutId);
    }
  }
 
  // -----------------------------------------------------------------------
  // Utilities
  // -----------------------------------------------------------------------
 
  async sleep(ms: number): Promise<void> {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }
 
  parseSSEResponse(text: string): JsonRpcResponse {
    const lines = text.split('\n');
    for (const line of lines) {
      if (line.startsWith('data: ')) {
        return JSON.parse(line.substring(6)) as JsonRpcResponse;
      }
    }
    return JSON.parse(text) as JsonRpcResponse;
  }
 
  async initializeSession(): Promise<void> {
    Iif (this.sessionId || !this.authToken) return;
 
    const controller = new AbortController();
    const timeoutId = setTimeout(() => controller.abort(), this.timeout);
 
    try {
      const headers: Record<string, string> = {
        'Content-Type': 'application/json',
        Accept: 'application/json, text/event-stream',
        ...this.customHeaders,
      };
      Eif (this.authToken) headers['Authorization'] = this.authToken;
 
      const response = await performPost(
        this.baseURL,
        headers,
        JSON.stringify({
          jsonrpc: '2.0',
          id: jsonRpcId++,
          method: 'initialize',
          params: {
            protocolVersion: '2024-11-05',
            capabilities: {},
            clientInfo: { name: 'riksdagsmonitor-news', version: '1.0.0' },
          },
        }),
        controller.signal,
      );
 
      Iif (!response.ok) {
        throw new Error(`Session init failed: ${response.status} ${response.statusText}`);
      }
 
      const sessionId =
        response.headers && typeof response.headers.get === 'function'
          ? response.headers.get('Mcp-Session-Id')
          : null;
 
      Iif (sessionId) {
        this.sessionId = sessionId;
        console.log(`  🔗 MCP session initialized: ${sessionId.substring(0, 8)}...`);
      }
 
      await performPost(
        this.baseURL,
        {
          ...headers,
          ...(this.sessionId ? { 'Mcp-Session-Id': this.sessionId } : {}),
        },
        JSON.stringify({
          jsonrpc: '2.0',
          method: 'notifications/initialized',
        }),
        controller.signal,
      );
    } finally {
      clearTimeout(timeoutId);
    }
  }
 
  // -----------------------------------------------------------------------
  // Data-fetching methods
  // -----------------------------------------------------------------------
 
  async fetchCalendarEvents(
    from: string,
    tom: string,
    org: string | null = null,
    akt: string | null = null,
  ): Promise<unknown[]> {
    const params: Record<string, unknown> = { from, tom };
    if (org) params['org'] = org;
    if (akt) params['akt'] = akt;
 
    const response = await this.request('get_calendar_events', params);
    return (response['kalender'] ?? response['events'] ?? []) as unknown[];
  }
 
  async fetchCommitteeReports(
    limit = 10,
    rm: string | null = null,
    organ: string | null = null,
  ): Promise<unknown[]> {
    const params: Record<string, unknown> = { limit };
    if (rm) params['rm'] = rm;
    if (organ) params['organ'] = organ;
 
    const response = await this.request('get_betankanden', params);
    return (response['dokument'] ?? response['reports'] ?? []) as unknown[];
  }
 
  async fetchPropositions(limit = 10, rm: string | null = null): Promise<unknown[]> {
    const params: Record<string, unknown> = { limit };
    if (rm) params['rm'] = rm;
 
    const response = await this.request('get_propositioner', params);
    return (response['dokument'] ?? response['propositions'] ?? []) as unknown[];
  }
 
  async fetchMotions(limit = 10, rm: string | null = null): Promise<unknown[]> {
    const params: Record<string, unknown> = { limit };
    if (rm) params['rm'] = rm;
 
    const response = await this.request('get_motioner', params);
    return (response['dokument'] ?? response['motions'] ?? []) as unknown[];
  }
 
  async fetchWrittenQuestions(params: { limit?: number; rm?: string } = {}): Promise<unknown[]> {
    const reqParams: Record<string, unknown> = { limit: params.limit ?? 20 };
    if (params.rm) reqParams['rm'] = params.rm;
    const response = await this.request('get_fragor', reqParams);
    return (response['dokument'] ?? response['questions'] ?? []) as unknown[];
  }
 
  async fetchInterpellations(params: { limit?: number; rm?: string } = {}): Promise<unknown[]> {
    const reqParams: Record<string, unknown> = { limit: params.limit ?? 15 };
    if (params.rm) reqParams['rm'] = params.rm;
    const response = await this.request('get_interpellationer', reqParams);
    return (response['dokument'] ?? response['interpellations'] ?? []) as unknown[];
  }
 
  async searchDocuments(searchParams: SearchDocumentsParams): Promise<unknown[]> {
    const response = await this.request(
      'search_dokument',
      searchParams as unknown as Record<string, unknown>,
    );
    const raw = (response['dokument'] ?? response['documents'] ?? []) as unknown[];
    return raw.map(d => annotateDocumentTypes(d as Record<string, unknown>));
  }
 
  async searchSpeeches(searchParams: SearchSpeechesParams): Promise<unknown[]> {
    const response = await this.request(
      'search_anforanden',
      searchParams as unknown as Record<string, unknown>,
    );
    return (response['anforanden'] ?? response['speeches'] ?? []) as unknown[];
  }
 
  async fetchMPs(filters: FetchMPsFilters = {}): Promise<unknown[]> {
    const response = await this.request(
      'search_ledamoter',
      filters as unknown as Record<string, unknown>,
    );
    return (response['mps'] ?? []) as unknown[];
  }
 
  async fetchVotingRecords(filters: FetchVotingFilters): Promise<unknown[]> {
    const response = await this.request(
      'search_voteringar',
      filters as unknown as Record<string, unknown>,
    );
    return (response['votes'] ?? []) as unknown[];
  }
 
  async fetchVotingGroup(params: FetchVotingGroupFilters = {}): Promise<unknown[]> {
    const response = await this.request(
      'get_voting_group',
      params as unknown as Record<string, unknown>,
    );
    // MCP server returns 'groups' when groupBy is provided (grouped results),
    // or 'votes' when no grouping is applied (flat voting list fallback)
    return (response['groups'] ?? response['votes'] ?? []) as unknown[];
  }
 
  async fetchGovernmentDocuments(searchParams: GovDocSearchParams): Promise<unknown[]> {
    const response = await this.request(
      'search_regering',
      searchParams as unknown as Record<string, unknown>,
    );
    return (response['documents'] ?? []) as unknown[];
  }
 
  async fetchDocumentDetails(
    dok_id: string,
    include_full_text = true,
  ): Promise<Record<string, unknown>> {
    const response = await this.request('get_dokument_innehall', {
      dok_id,
      include_full_text,
    });
    return response;
  }
 
  async enrichDocumentsWithContent(
    documents: RiksdagDocument[],
    concurrency = 3,
  ): Promise<RiksdagDocument[]> {
    const safeConcurrency = Math.max(1, Math.floor(concurrency));
    const enriched: RiksdagDocument[] = [];
 
    for (let i = 0; i < documents.length; i += safeConcurrency) {
      const batch = documents.slice(i, i + safeConcurrency);
 
      const batchResults = await Promise.allSettled(
        batch.map(async (doc): Promise<RiksdagDocument> => {
          const dok_id = doc.dokumentnamn ?? doc.dok_id ?? doc.id;
          if (!dok_id) {
            console.warn('⚠️ Document missing ID:', doc);
            return { ...doc, contentFetchError: 'No document ID' };
          }
 
          try {
            const details = await this.fetchDocumentDetails(dok_id, false);
            const intressent = (details['intressent'] ?? {}) as Record<string, string>;
            const author = intressent['tilltalsnamn']
              ? `${intressent['tilltalsnamn']} ${intressent['efternamn']}`.trim()
              : doc.intressent_namn ?? intressent['namn'] ?? 'Unknown';
            const party = intressent['parti'] ?? doc.parti ?? 'Unknown';
            const summary =
              (details['summary'] as string) ??
              doc.summary ??
              (details['notis'] as string) ??
              doc.notis ??
              '';
 
            return {
              ...doc,
              ...(details as Partial<RiksdagDocument>),
              author,
              parti: party,
              intressent_namn: author,
              summary,
              contentFetched: true,
            } as RiksdagDocument;
          } catch (error: unknown) {
            const errMsg = (error as Error).message;
            console.error(`❌ Failed to enrich document ${dok_id}:`, errMsg);
            return { ...doc, contentFetchError: errMsg };
          }
        }),
      );
 
      for (let idx = 0; idx < batchResults.length; idx++) {
        const result = batchResults[idx]!;
        if (result.status === 'fulfilled') {
          enriched.push(result.value);
        } else {
          const failedDoc = batch[idx]!;
          const failedDokId = failedDoc.dokumentnamn ?? failedDoc.dok_id ?? failedDoc.id ?? 'unknown';
          console.error(`❌ Batch enrichment failed for document ${failedDokId}:`, result.reason);
          enriched.push({ ...failedDoc, contentFetchError: (result.reason as Error).message });
        }
      }
 
      if (i + safeConcurrency < documents.length) {
        await new Promise<void>((resolve) => setTimeout(resolve, 200));
      }
    }
 
    return enriched;
  }
 
  /**
   * Fetch government document content from regeringen.se via g0v.se.
   * Uses the get_g0v_document_content MCP tool to retrieve Markdown content.
   *
   * The g0v MCP tool response typically contains:
   *   - `content` (primary): Markdown content of the document
   *   - `markdown` (fallback): Alias used by some g0v API versions
   *   - `text` (fallback): Plain text content when Markdown is unavailable
   *
   * @param regeringenUrl - Full URL to a document on regeringen.se
   * @returns Markdown content of the document, or null if unavailable
   */
  async fetchGovernmentDocumentContent(
    regeringenUrl: string,
  ): Promise<string | null> {
    try {
      const response = await this.request('get_g0v_document_content', {
        regeringenUrl,
      });
      // g0v API returns content in 'content' field; 'markdown'/'text' are fallbacks
      return (response['content'] ?? response['markdown'] ?? response['text'] ?? null) as string | null;
    } catch (error: unknown) {
      console.warn(`⚠️ Could not fetch government document content for ${regeringenUrl}: ${(error as Error).message}`);
      return null;
    }
  }
 
  /**
   * Fetch raw text content from an external URL (e.g. GitHub raw, other public URLs).
   * Performs a simple HTTP GET and returns the response body as text.
   * Uses a 15-second timeout to avoid hanging on slow external resources.
   *
   * @param rawUrl - Full URL to fetch (must be publicly accessible)
   * @returns Text content of the resource, or null if unavailable
   */
  async fetchExternalUrlContent(rawUrl: string): Promise<string | null> {
    try {
      const controller = new AbortController();
      const timeout = setTimeout(() => controller.abort(), EXTERNAL_URL_FETCH_TIMEOUT_MS);
      const response = await fetch(rawUrl, {
        signal: controller.signal,
        headers: { 'Accept': 'text/plain, text/markdown, text/html, */*' },
      });
      clearTimeout(timeout);
      if (!response.ok) {
        console.warn(`⚠️ HTTP ${response.status} fetching external URL: ${rawUrl}`);
        return null;
      }
      return await response.text();
    } catch (error: unknown) {
      console.warn(`⚠️ Could not fetch external URL ${rawUrl}: ${(error as Error).message}`);
      return null;
    }
  }
 
  // -----------------------------------------------------------------------
  // Statistics
  // -----------------------------------------------------------------------
 
  getStats(): MCPStats {
    return {
      requests: this.requestCount,
      errors: this.errorCount,
      successRate:
        this.requestCount > 0
          ? Math.round(((this.requestCount - this.errorCount) / this.requestCount) * 100) + '%'
          : '0%',
    };
  }
 
  resetStats(): void {
    this.requestCount = 0;
    this.errorCount = 0;
  }
}