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 | 8x 8x 8x 18x 18x 18x 18x 18x 18x 63x 10x 9x 4x 4x 5x 5x 4x 4x 71x 71x 71x 36x 36x 36x 36x 11x 11x 11x 14x 2x 8x 49x 49x 49x 34x 34x 15x 15x 16x 16x 1x 1x 1x 1x 1x 9x 6x 6x 12x 3x 2x 2x 4x 1x 1x 1x 1x 1x 1x 6x 1x 1x 1x 1x 1x 1x 6x 3x 3x 3x 2x 1x 1x 1x 6x 3x 3x 3x 2x 1x 1x 1x 6x 3x 3x 3x 2x 1x 1x 1x 3x 1x 1x 1x 1x 1x 1x 9x 9x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 12x 5x 5x 5x 5x 4x 3x 3x 3x 4x 1x 1x 2x 12x 5x 5x 5x 5x 4x 3x 3x 3x 3x 1x 1x 2x 12x 5x 5x 5x 5x 4x 3x 3x 3x 3x 1x 1x 2x 11x 4x 4x 4x 4x 3x 2x 2x 1x 2x 1x 1x 3x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 5x 4x 4x 5x 4x 4x 5x 5x 5x 5x 9x 6x 6x 5x 3x 3x 2x 2x 9x 3x 9x 8x 5x 5x 4x 2x 2x 1x 8x 8x 2x 8x 9x 3x 3x 3x 2x 1x 1x 1x 1x 1x | // SPDX-FileCopyrightText: 2024-2026 Hack23 AB
// SPDX-License-Identifier: Apache-2.0
/**
* @module Generators/Pipeline/FetchStage
* @description MCP data-fetching pipeline stage with circuit breaker protection.
*
* All functions are pure with respect to I/O: they accept an explicit
* `client` argument instead of reading module-level state, making them
* straightforward to unit-test with a mock client.
*
* The {@link CircuitBreaker} prevents cascading failures when the MCP server
* is degraded: after {@link CircuitBreakerOptions.failureThreshold} consecutive
* errors the circuit opens and subsequent calls short-circuit immediately.
*/
import type { EuropeanParliamentMCPClient } from '../../mcp/ep-mcp-client.js';
import { getEPMCPClient } from '../../mcp/ep-mcp-client.js';
import type {
WeekAheadData,
DateRange,
CommitteeData,
MCPToolResult,
VotingRecord,
VotingPattern,
VotingAnomaly,
MotionsQuestion,
LegislativeDocument,
} from '../../types/index.js';
import {
parsePlenarySessions,
parseCommitteeMeetings,
parseLegislativeDocuments,
parseLegislativePipeline,
parseParliamentaryQuestions,
parseEPEvents,
PLACEHOLDER_EVENTS,
} from '../week-ahead-content.js';
import { applyCommitteeInfo, applyDocuments, applyEffectiveness } from '../committee-helpers.js';
import { getMotionsFallbackData } from '../motions-content.js';
import { escapeHTML } from '../../utils/file-utils.js';
import type { PipelineData } from '../propositions-content.js';
// ─── Shared string constants ─────────────────────────────────────────────────
/** Log prefix for MCP fetch operations */
const MCP_FETCH_PREFIX = ' 📡';
/** Warning prefix for MCP failures */
const WARN_PREFIX = ' ⚠️';
/** Info prefix for fallback messages */
const INFO_PREFIX = ' ℹ️';
// ─── Circuit Breaker ─────────────────────────────────────────────────────────
/** Possible circuit breaker states */
export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
/** Constructor options for {@link CircuitBreaker} */
export interface CircuitBreakerOptions {
/** Consecutive failures before opening the circuit (default: 3) */
failureThreshold?: number;
/** Milliseconds to wait before probing recovery (default: 60 000) */
resetTimeoutMs?: number;
}
/**
* Circuit breaker preventing cascading MCP failures.
*
* - **CLOSED** — normal operation; all requests pass through.
* - **OPEN** — fast-fail; requests are rejected for `resetTimeoutMs` ms.
* - **HALF_OPEN** — probe state; one request is allowed to test recovery.
*/
export class CircuitBreaker {
private state: CircuitState = 'CLOSED';
private consecutiveFailures = 0;
private nextAttemptAt = 0;
private halfOpenProbeInFlight = false;
private readonly failureThreshold: number;
private readonly resetTimeoutMs: number;
constructor(options: CircuitBreakerOptions = {}) {
this.failureThreshold = options.failureThreshold ?? 3;
this.resetTimeoutMs = options.resetTimeoutMs ?? 60_000;
}
/**
* Whether a request may proceed given the current circuit state.
*
* In HALF_OPEN state only a single probe is allowed at a time; subsequent
* calls return `false` until the in-flight probe records success or failure.
*
* @returns `true` when the circuit is CLOSED, or HALF_OPEN with no probe in flight
*/
canRequest(): boolean {
if (this.state === 'CLOSED') return true;
if (this.state === 'OPEN') {
if (Date.now() >= this.nextAttemptAt) {
this.state = 'HALF_OPEN';
this.halfOpenProbeInFlight = false;
// Fall through to HALF_OPEN probe logic below
} else {
return false;
}
}
// HALF_OPEN: allow exactly one probe in flight at a time
if (this.halfOpenProbeInFlight) return false;
this.halfOpenProbeInFlight = true;
return true;
}
/** Record a successful request and close the circuit */
recordSuccess(): void {
this.consecutiveFailures = 0;
this.halfOpenProbeInFlight = false;
this.state = 'CLOSED';
}
/**
* Record a failed request.
*
* - When in **HALF_OPEN** the circuit re-opens immediately (the probe failed).
* - When in **CLOSED** the circuit opens only once the failure threshold is reached.
*/
recordFailure(): void {
this.halfOpenProbeInFlight = false;
Iif (this.state === 'HALF_OPEN') {
// Probe failed — immediately re-open and back off again
this.state = 'OPEN';
this.nextAttemptAt = Date.now() + this.resetTimeoutMs;
console.warn('⚡ Circuit breaker re-OPEN after HALF_OPEN probe failure');
return;
}
this.consecutiveFailures++;
if (this.consecutiveFailures >= this.failureThreshold) {
this.state = 'OPEN';
this.nextAttemptAt = Date.now() + this.resetTimeoutMs;
console.warn(
`⚡ Circuit breaker OPEN after ${this.consecutiveFailures} consecutive failures`
);
}
}
/**
* Return the current circuit state.
*
* @returns Current circuit state
*/
getState(): CircuitState {
return this.state;
}
/**
* Return current statistics for observability.
*
* @returns Snapshot of state and consecutive failure count
*/
getStats(): Readonly<{ state: CircuitState; consecutiveFailures: number }> {
return { state: this.state, consecutiveFailures: this.consecutiveFailures };
}
}
/** Module-level circuit breaker shared across all MCP fetch operations */
export const mcpCircuitBreaker = new CircuitBreaker();
/**
* Execute a single MCP API call through the module-level circuit breaker.
* Short-circuits with `fallback` whenever the circuit breaker is not
* accepting requests (for example when OPEN, or in HALF_OPEN with no
* probe slots available).
* Records success or failure after each call, opening the circuit when
* {@link CircuitBreakerOptions.failureThreshold} consecutive failures occur.
*
* @param fn - Async factory that performs the MCP call
* @param fallback - Value returned when the circuit is not accepting requests
* @param context - Label used in warning messages
* @returns Result of `fn` or `fallback`
*/
async function callMCP<T>(fn: () => Promise<T>, fallback: T, context: string): Promise<T> {
Iif (!mcpCircuitBreaker.canRequest()) {
console.warn(
`${WARN_PREFIX} Circuit breaker not accepting requests (${mcpCircuitBreaker.getState()}) — skipping ${context}`
);
return fallback;
}
try {
const result = await fn();
mcpCircuitBreaker.recordSuccess();
return result;
} catch (error) {
mcpCircuitBreaker.recordFailure();
throw error;
}
}
// ─── Internal helpers ─────────────────────────────────────────────────────────
/**
* Parse JSON text, returning `null` and logging a warning on parse failure.
*
* @param text - Raw JSON string
* @param context - Label used in the warning message
* @returns Parsed value or null
*/
function parseJSON<T>(text: string, context: string): T | null {
try {
return JSON.parse(text) as T;
} catch {
console.warn(`${WARN_PREFIX} Failed to parse JSON for ${context}`);
return null;
}
}
// ─── MCP client initialisation ───────────────────────────────────────────────
/**
* Attempt to connect to the European Parliament MCP server.
* Returns `null` (with a warning) if the connection fails or MCP is disabled.
*
* @param useMCP - Whether MCP should be used at all
* @returns Connected client or null
*/
export async function initializeMCPClient(
useMCP: boolean
): Promise<EuropeanParliamentMCPClient | null> {
Eif (!useMCP) {
console.log(`${INFO_PREFIX} MCP client disabled via USE_EP_MCP=false`);
return null;
}
try {
console.log('🔌 Attempting to connect to European Parliament MCP Server...');
const client = await getEPMCPClient();
console.log('✅ MCP client connected successfully');
return client;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn('⚠️ Could not connect to MCP server:', message);
console.warn('⚠️ Falling back to placeholder content');
return null;
}
}
// ─── Week-Ahead fetches ──────────────────────────────────────────────────────
/**
* Fetch aggregated week-ahead data from multiple MCP sources in parallel.
* Returns placeholder data when the client is unavailable.
*
* @param client - MCP client or null
* @param dateRange - Date range for the week-ahead period
* @returns Aggregated week-ahead data
*/
export async function fetchWeekAheadData(
client: EuropeanParliamentMCPClient | null,
dateRange: DateRange
): Promise<WeekAheadData> {
if (!client) {
console.log(`${INFO_PREFIX} MCP unavailable — using placeholder events`);
return {
events: PLACEHOLDER_EVENTS.map((e) => ({ ...e, date: dateRange.start })),
committees: [],
documents: [],
pipeline: [],
questions: [],
};
}
if (!mcpCircuitBreaker.canRequest()) {
console.warn(
`${WARN_PREFIX} Circuit breaker not accepting requests (${mcpCircuitBreaker.getState()}) — using placeholder events`
);
return {
events: PLACEHOLDER_EVENTS.map((e) => ({ ...e, date: dateRange.start })),
committees: [],
documents: [],
pipeline: [],
questions: [],
};
}
// Record whether we entered as a HALF_OPEN probe so any rejection triggers
// an immediate re-open (normal circuit-breaker probe semantics).
const wasHalfOpen = mcpCircuitBreaker.getState() === 'HALF_OPEN';
console.log(`${MCP_FETCH_PREFIX} Fetching week-ahead data from MCP (parallel)...`);
const [plenarySessions, committeeInfo, documents, pipeline, questions, epEvents] =
await Promise.allSettled([
client.getPlenarySessions({ startDate: dateRange.start, endDate: dateRange.end, limit: 50 }),
client.getCommitteeInfo({ limit: 20 }),
client.searchDocuments({ query: 'parliament', limit: 20 }),
client.monitorLegislativePipeline({
dateFrom: dateRange.start,
dateTo: dateRange.end,
status: 'ACTIVE',
limit: 20,
}),
client.getParliamentaryQuestions({ startDate: dateRange.start, limit: 20 }),
client.getEvents({ dateFrom: dateRange.start, dateTo: dateRange.end, limit: 20 }),
]);
const allFailed = [
plenarySessions,
committeeInfo,
documents,
pipeline,
questions,
epEvents,
].every((r) => r.status === 'rejected');
const anyFailed = [plenarySessions, committeeInfo, documents, pipeline, questions, epEvents].some(
(r) => r.status === 'rejected'
);
// In HALF_OPEN any single rejection means the probe failed — re-open immediately.
Iif (allFailed || (wasHalfOpen && anyFailed)) {
mcpCircuitBreaker.recordFailure();
} else {
mcpCircuitBreaker.recordSuccess();
}
const plenaryEvents = parsePlenarySessions(plenarySessions, dateRange.start);
const additionalEvents = parseEPEvents(epEvents, dateRange.start);
const events = [...plenaryEvents, ...additionalEvents];
return {
events: events.length > 0 ? events : [{ ...PLACEHOLDER_EVENTS[0]!, date: dateRange.start }],
committees: parseCommitteeMeetings(committeeInfo, dateRange.start),
documents: parseLegislativeDocuments(documents),
pipeline: parseLegislativePipeline(pipeline),
questions: parseParliamentaryQuestions(questions),
};
}
// ─── Breaking-News fetches ───────────────────────────────────────────────────
/**
* Fetch voting anomaly text from MCP, returning empty string on failure.
*
* @param client - MCP client or null
* @returns Raw anomaly data text
*/
export async function fetchVotingAnomalies(
client: EuropeanParliamentMCPClient | null
): Promise<string> {
if (!client) return '';
try {
const result = await callMCP(
() => client.callTool('detect_voting_anomalies', { sensitivityThreshold: 0.3 }),
undefined,
'detect_voting_anomalies'
);
return result?.content?.[0]?.text ?? '';
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} detect_voting_anomalies failed:`, message);
return '';
}
}
/**
* Fetch coalition dynamics analysis text from MCP.
*
* @param client - MCP client or null
* @returns Raw coalition dynamics text
*/
export async function fetchCoalitionDynamics(
client: EuropeanParliamentMCPClient | null
): Promise<string> {
if (!client) return '';
try {
const result = await callMCP(
() => client.callTool('analyze_coalition_dynamics', {}),
undefined,
'analyze_coalition_dynamics'
);
return result?.content?.[0]?.text ?? '';
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} analyze_coalition_dynamics failed:`, message);
return '';
}
}
/**
* Fetch voting statistics report text from MCP.
*
* @param client - MCP client or null
* @returns Raw voting report text
*/
export async function fetchVotingReport(
client: EuropeanParliamentMCPClient | null
): Promise<string> {
if (!client) return '';
try {
const result = await callMCP(
() => client.callTool('generate_report', { reportType: 'VOTING_STATISTICS' }),
undefined,
'generate_report'
);
return result?.content?.[0]?.text ?? '';
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} generate_report failed:`, message);
return '';
}
}
/**
* Fetch MEP influence assessment text from MCP.
* Short-circuits immediately when `mepId` is empty.
*
* @param client - MCP client or null
* @param mepId - MEP identifier; pass empty string to skip the call
* @returns Raw influence data text
*/
export async function fetchMEPInfluence(
client: EuropeanParliamentMCPClient | null,
mepId: string
): Promise<string> {
if (!mepId || !client) return '';
try {
const result = await callMCP(
() => client.callTool('assess_mep_influence', { mepId, includeDetails: true }),
undefined,
'assess_mep_influence'
);
return result?.content?.[0]?.text ?? '';
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} assess_mep_influence failed:`, message);
return '';
}
}
// ─── Committee-Reports fetches ───────────────────────────────────────────────
/**
* Fetch committee data from three MCP sources for the given abbreviation.
* Each source failure is caught individually so partial data is still returned.
*
* @param client - MCP client or null
* @param abbreviation - Committee code (e.g. `"ENVI"`)
* @returns Populated committee data
*/
export async function fetchCommitteeData(
client: EuropeanParliamentMCPClient | null,
abbreviation: string
): Promise<CommitteeData> {
const defaultResult: CommitteeData = {
name: `${abbreviation} Committee`,
abbreviation,
chair: 'N/A',
members: 0,
documents: [],
effectiveness: null,
};
if (!client) return defaultResult;
try {
console.log(`${MCP_FETCH_PREFIX} Fetching committee info for ${abbreviation}...`);
const committeeResult = await callMCP(
() => client.getCommitteeInfo({ committeeId: abbreviation }),
null,
`getCommitteeInfo(${abbreviation})`
);
Iif (committeeResult) applyCommitteeInfo(committeeResult, defaultResult, abbreviation);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(`${WARN_PREFIX} getCommitteeInfo failed for ${abbreviation}:`, message);
}
try {
console.log(`${MCP_FETCH_PREFIX} Fetching documents for ${abbreviation}...`);
const docsResult = await callMCP(
() => client.searchDocuments({ query: abbreviation, limit: 5 }),
null,
`searchDocuments(${abbreviation})`
);
Iif (docsResult) applyDocuments(docsResult, defaultResult);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(`${WARN_PREFIX} searchDocuments failed for ${abbreviation}:`, message);
}
try {
const effectivenessResult = await callMCP(
() =>
client.analyzeLegislativeEffectiveness({
subjectType: 'COMMITTEE',
subjectId: abbreviation,
}),
null,
`analyzeLegislativeEffectiveness(${abbreviation})`
);
if (effectivenessResult) applyEffectiveness(effectivenessResult, defaultResult);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
console.warn(
`${WARN_PREFIX} analyzeLegislativeEffectiveness failed for ${abbreviation}:`,
message
);
}
return defaultResult;
}
// ─── Motions fetches ─────────────────────────────────────────────────────────
/**
* Fetch recent voting records from MCP.
*
* @param client - MCP client or null
* @param dateFromStr - Start date (YYYY-MM-DD)
* @param dateStr - End date (YYYY-MM-DD)
* @returns Array of voting records
*/
export async function fetchVotingRecords(
client: EuropeanParliamentMCPClient | null,
dateFromStr: string,
dateStr: string
): Promise<VotingRecord[]> {
if (!client) return [];
try {
console.log(`${MCP_FETCH_PREFIX} Fetching voting records from MCP server...`);
const votingResult = (await callMCP(
() =>
client.callTool('get_voting_records', {
dateFrom: dateFromStr,
dateTo: dateStr,
limit: 20,
}),
undefined,
'get_voting_records'
)) as MCPToolResult | undefined;
if (votingResult?.content?.[0]) {
const data = parseJSON<{
records?: Array<{
title?: string;
date?: string;
result?: string;
votes?: { for?: number; against?: number; abstain?: number };
}>;
}>(votingResult.content[0].text, 'voting records');
Eif (data?.records && data.records.length > 0) {
console.log(` ✅ Fetched ${data.records.length} voting records from MCP`);
return data.records.map((r) => ({
title: r.title ?? 'Parliamentary Vote',
date: r.date ?? dateStr,
result: r.result ?? 'Adopted',
votes: {
for: r.votes?.for ?? 0,
against: r.votes?.against ?? 0,
abstain: r.votes?.abstain ?? 0,
},
}));
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} MCP voting records fetch failed:`, message);
}
return [];
}
/**
* Fetch voting patterns from MCP.
*
* @param client - MCP client or null
* @param dateFromStr - Start date
* @param dateStr - End date
* @returns Array of voting patterns
*/
export async function fetchVotingPatterns(
client: EuropeanParliamentMCPClient | null,
dateFromStr: string,
dateStr: string
): Promise<VotingPattern[]> {
if (!client) return [];
try {
console.log(`${MCP_FETCH_PREFIX} Fetching voting patterns from MCP server...`);
const patternsResult = (await callMCP(
() =>
client.callTool('analyze_voting_patterns', {
dateFrom: dateFromStr,
dateTo: dateStr,
}),
undefined,
'analyze_voting_patterns'
)) as MCPToolResult | undefined;
if (patternsResult?.content?.[0]) {
const data = parseJSON<{
patterns?: Array<{ group?: string; cohesion?: number; participation?: number }>;
}>(patternsResult.content[0].text, 'voting patterns');
Eif (data?.patterns && data.patterns.length > 0) {
console.log(` ✅ Fetched ${data.patterns.length} voting patterns from MCP`);
return data.patterns.map((p) => ({
group: p.group ?? 'Unknown Group',
cohesion: p.cohesion ?? 0,
participation: p.participation ?? 0,
}));
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} MCP voting patterns fetch failed:`, message);
}
return [];
}
/**
* Fetch voting anomalies for a date range from MCP.
*
* @param client - MCP client or null
* @param dateFromStr - Start date
* @param dateStr - End date
* @returns Array of voting anomalies
*/
export async function fetchMotionsAnomalies(
client: EuropeanParliamentMCPClient | null,
dateFromStr: string,
dateStr: string
): Promise<VotingAnomaly[]> {
if (!client) return [];
try {
console.log(`${MCP_FETCH_PREFIX} Fetching voting anomalies from MCP server...`);
const anomaliesResult = (await callMCP(
() =>
client.callTool('detect_voting_anomalies', {
dateFrom: dateFromStr,
dateTo: dateStr,
}),
undefined,
'detect_voting_anomalies'
)) as MCPToolResult | undefined;
if (anomaliesResult?.content?.[0]) {
const data = parseJSON<{
anomalies?: Array<{ type?: string; description?: string; severity?: string }>;
}>(anomaliesResult.content[0].text, 'voting anomalies');
Eif (data?.anomalies && data.anomalies.length > 0) {
console.log(` ✅ Fetched ${data.anomalies.length} voting anomalies from MCP`);
return data.anomalies.map((a) => ({
type: a.type ?? 'Unusual Pattern',
description: a.description ?? 'No description available',
severity: a.severity ?? 'MEDIUM',
}));
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} MCP voting anomalies fetch failed:`, message);
}
return [];
}
/**
* Fetch parliamentary questions from MCP for the given date range.
*
* @param client - MCP client or null
* @param dateFromStr - Start date
* @param dateStr - End date
* @returns Array of parliamentary questions
*/
export async function fetchParliamentaryQuestionsForMotions(
client: EuropeanParliamentMCPClient | null,
dateFromStr: string,
dateStr: string
): Promise<MotionsQuestion[]> {
if (!client) return [];
try {
console.log(`${MCP_FETCH_PREFIX} Fetching parliamentary questions from MCP server...`);
const questionsResult = await callMCP(
() =>
client.getParliamentaryQuestions({
dateFrom: dateFromStr,
dateTo: dateStr,
limit: 10,
}),
undefined,
'get_parliamentary_questions'
);
if (questionsResult?.content?.[0]) {
const data = parseJSON<{
questions?: Array<{
author?: string;
topic?: string;
subject?: string;
date?: string;
status?: string;
}>;
}>(questionsResult.content[0].text, 'parliamentary questions');
if (data?.questions && data.questions.length > 0) {
console.log(` ✅ Fetched ${data.questions.length} parliamentary questions from MCP`);
return data.questions.map((q) => ({
author: q.author ?? 'Unknown MEP',
topic: q.topic ?? q.subject ?? 'General inquiry',
date: q.date ?? dateStr,
status: q.status ?? 'PENDING',
}));
}
}
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} MCP parliamentary questions fetch failed:`, message);
}
return [];
}
/**
* Fetch all motions data in parallel, applying fallback arrays for any
* section where MCP returned nothing.
*
* @param client - MCP client or null
* @param dateFromStr - Start date
* @param dateStr - End date
* @returns All motions data with fallbacks applied
*/
export async function fetchMotionsData(
client: EuropeanParliamentMCPClient | null,
dateFromStr: string,
dateStr: string
): Promise<{
votingRecords: VotingRecord[];
votingPatterns: VotingPattern[];
anomalies: VotingAnomaly[];
questions: MotionsQuestion[];
}> {
const [votingRecordsResult, votingPatternsResult, anomaliesResult, questionsResult] =
await Promise.allSettled([
fetchVotingRecords(client, dateFromStr, dateStr),
fetchVotingPatterns(client, dateFromStr, dateStr),
fetchMotionsAnomalies(client, dateFromStr, dateStr),
fetchParliamentaryQuestionsForMotions(client, dateFromStr, dateStr),
]);
let votingRecords: VotingRecord[] =
votingRecordsResult.status === 'fulfilled' ? votingRecordsResult.value : [];
Iif (votingRecordsResult.status === 'rejected') {
console.warn(`${WARN_PREFIX} Failed to fetch voting records from MCP`);
}
let votingPatterns: VotingPattern[] =
votingPatternsResult.status === 'fulfilled' ? votingPatternsResult.value : [];
Iif (votingPatternsResult.status === 'rejected') {
console.warn(`${WARN_PREFIX} Failed to fetch voting patterns from MCP`);
}
let anomalies: VotingAnomaly[] =
anomaliesResult.status === 'fulfilled' ? anomaliesResult.value : [];
Iif (anomaliesResult.status === 'rejected') {
console.warn(`${WARN_PREFIX} Failed to fetch voting anomalies from MCP`);
}
let questions: MotionsQuestion[] =
questionsResult.status === 'fulfilled' ? questionsResult.value : [];
Iif (questionsResult.status === 'rejected') {
console.warn(`${WARN_PREFIX} Failed to fetch parliamentary questions from MCP`);
}
const fallback = getMotionsFallbackData(dateStr, dateFromStr);
if (votingRecords.length === 0) {
console.log(`${INFO_PREFIX} Using placeholder voting records`);
votingRecords = fallback.votingRecords;
}
if (votingPatterns.length === 0) {
console.log(`${INFO_PREFIX} Using placeholder voting patterns`);
votingPatterns = fallback.votingPatterns;
}
if (anomalies.length === 0) {
console.log(`${INFO_PREFIX} Using placeholder voting anomalies`);
anomalies = fallback.anomalies;
}
Eif (questions.length === 0) {
console.log(`${INFO_PREFIX} Using placeholder parliamentary questions`);
questions = fallback.questions;
}
return { votingRecords, votingPatterns, anomalies, questions };
}
// ─── Propositions fetches ─────────────────────────────────────────────────────
/**
* Fetch legislative proposals from MCP and build pre-sanitised HTML.
*
* @param client - MCP client or null
* @returns Proposals HTML and the first procedure ID found (if any)
*/
export async function fetchProposalsFromMCP(
client: EuropeanParliamentMCPClient | null
): Promise<{ html: string; firstProcedureId: string }> {
if (!client) return { html: '', firstProcedureId: '' };
const docsResult = await callMCP(
() => client.searchDocuments({ keyword: 'legislative proposal', limit: 10 }),
undefined,
'search_documents(proposals)'
);
if (!docsResult?.content?.[0]) return { html: '', firstProcedureId: '' };
const data = parseJSON<{ documents?: Array<Partial<LegislativeDocument>> }>(
docsResult.content[0].text,
'proposals'
);
if (!data?.documents?.length) return { html: '', firstProcedureId: '' };
console.log(` ✅ Fetched ${data.documents.length} proposals from MCP`);
const firstProcedureId =
data.documents.find((d) => /\d{4}\/\d+\(.+\)/.test(d.id ?? ''))?.id ?? '';
const html = data.documents
.map(
(doc) => `
<div class="proposal-card">
<h3>${escapeHTML(doc.title ?? 'Legislative Proposal')}</h3>
<div class="proposal-meta">
${doc.id ? `<span class="proposal-id">${escapeHTML(doc.id)}</span>` : ''}
${doc.date ? `<span class="proposal-date">${escapeHTML(doc.date)}</span>` : ''}
${doc.status ? `<span class="proposal-status">${escapeHTML(doc.status)}</span>` : ''}
</div>
${doc.committee ? `<p class="proposal-committee">${escapeHTML(doc.committee)}</p>` : ''}
${doc.rapporteur ? `<p class="proposal-rapporteur">${escapeHTML(doc.rapporteur)}</p>` : ''}
</div>`
)
.join('');
return { html, firstProcedureId };
}
/**
* Fetch active legislative pipeline data from MCP.
*
* @param client - MCP client or null
* @returns Structured pipeline data or null when unavailable
*/
export async function fetchPipelineFromMCP(
client: EuropeanParliamentMCPClient | null
): Promise<PipelineData | null> {
if (!client) return null;
const pipelineResult = await callMCP(
() => client.monitorLegislativePipeline({ status: 'ACTIVE', limit: 5 }),
undefined,
'monitor_legislative_pipeline'
);
if (!pipelineResult?.content?.[0]) return null;
const pipeData = parseJSON<{
pipelineHealthScore?: number;
throughputRate?: number;
procedures?: Array<{ id?: string; title?: string; stage?: string }>;
}>(pipelineResult.content[0].text, 'pipeline');
if (!pipeData) return null;
const healthScore = pipeData.pipelineHealthScore ?? 0;
const throughput = pipeData.throughputRate ?? 0;
const procRowsHtml =
pipeData.procedures
?.map(
(proc) => `
<div class="procedure-item">
${proc.id ? `<span class="procedure-id">${escapeHTML(proc.id)}</span>` : ''}
${proc.title ? `<span class="procedure-title">${escapeHTML(proc.title)}</span>` : ''}
${proc.stage ? `<span class="procedure-stage">${escapeHTML(proc.stage)}</span>` : ''}
</div>`
)
.join('') ?? '';
return { healthScore, throughput, procRowsHtml };
}
/**
* Fetch a specific procedure's tracked-status HTML from MCP.
* Returns empty string when `procedureId` is empty or MCP is unavailable.
*
* @param client - MCP client or null
* @param procedureId - Procedure ID (e.g. `"2024/0001(COD)"`)
* @returns HTML snippet for the procedure status section
*/
export async function fetchProcedureStatusFromMCP(
client: EuropeanParliamentMCPClient | null,
procedureId: string
): Promise<string> {
if (!procedureId || !client) return '';
try {
const result = await callMCP(
() => client.trackLegislation(procedureId),
undefined,
`track_legislation(${procedureId})`
);
if (!result?.content?.[0]) return '';
const raw = result.content[0].text;
return `<pre class="data-summary">${escapeHTML(raw.slice(0, 2000))}</pre>`;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
console.warn(`${WARN_PREFIX} track_legislation failed:`, message);
return '';
}
}
|