Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 | 4x 4x 4x 4x 4x 27x 27x 23x 27x 27x 26x 23x 3x 3x 3x 5x 1x 1x 1x 27x 27x 27x 27x 27x 27x 28x 28x 27x 705x 27x 27x 27x 2x 27x 1x 27x 5x 27x 1x 1x 27x 27x 705x 27x 17x 17x 701x 701x 701x 701x 16x 17x 17x 705x 705x 705x 705x 705x 17x 17x 17x 705x 705x 705x 705x 705x 17x 4x 4x 4x 27x 512x 512x 512x 508x 508x 508x 4x 4x 24x 4864x 4864x 4864x 24x 2432x 2432x 2432x 1513x 25x 25x 25x 25x 25x 25x 25x 25x 25x 25x 555x 555x 555x 535x 5x 4x 25x 25x 25x 25x 25x 25x 513x 512x 512x 512x 4x 4x 508x 508x 508x 11360x 11360x 11360x 595x 25x 24x 24x 595x 595x 595x 595x 595x 24x 25x 25x 25x 25x 25x 25x 1x 1x 24x 24x 24x 24x 24x 24x 25x 25x 28x 28x 28x 28x 28x 3x 3x 25x 25x 24x 24x 25x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 27x 24x 23x 23x 4x 4x 4x 4x 4x 4x 4x 27x 27x 27x 27x 27x 27x 27x 27x 1x 26x 9x 17x 3x 27x 20x 27x 9x 27x 27x 17x 17x 17x 17x 27x 16x 16x 16x 16x 27x 17x 17x 16x 27x 27x 27x 27x 27x 27x 27x 30x 30x 30x 30x 30x 30x 28x 28x 28x 4x 4x 1x 1x 4x | /**
* MCP Tool: detect_voting_anomalies
*
* Flag unusual voting patterns — party defections, sudden alignment shifts,
* abstention spikes, and cross-party movement signals — using authoritative
* DOCEO roll-call (RCV) records.
*
* **Intelligence Perspective:** Anomaly detection tool identifying deviations
* from each MEP's *own* historical voting baseline within the requested
* period. Surfaces early warning signals for party splits, political
* realignments, and emerging cross-party movements. Consumed by
* `correlate_intelligence` and `early_warning_system`.
*
* **Data source:** EP DOCEO XML (`PV-{term}-{date}-RCV_{lang}.xml`) via the
* shared bounded/cached aggregator. Falls back gracefully to LOW confidence
* with `dataQualityWarnings` when DOCEO is unreachable.
*
* ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege),
* AU-002 (Audit Logging). GDPR Article 5(1)(c)/(d) — minimisation & accuracy.
*
* @since 0.8.0
*/
import { DetectVotingAnomaliesSchema } from '../schemas/europeanParliament.js';
import { epClient } from '../clients/europeanParliamentClient.js';
import { doceoClient } from '../clients/ep/doceoClient.js';
import { auditLogger, toErrorMessage } from '../utils/auditLogger.js';
import { normalizePoliticalGroup } from '../utils/politicalGroupNormalization.js';
import { withTimeoutAndAbort } from '../utils/timeout.js';
import {
bucketByWeek,
classifyMepVote,
coverageConfidence,
findCrossPartyAlignmentWindows,
findOutlierWeeks,
findWoWShifts,
isoWeekStart,
iteratePlenaryWeeks,
MAX_PLENARY_WEEKS,
DEFAULT_CROSS_PARTY_SHARE,
DEFAULT_WOW_THRESHOLD_PP,
DEFAULT_Z_SCORE_THRESHOLD,
} from '../utils/votingBaseline.js';
import { buildToolResponse } from './shared/responseBuilder.js';
import type { ToolResult } from './shared/types.js';
import type { LatestVoteRecord } from '../clients/ep/doceoXmlParser.js';
import type { ClassifiedVote, WeekBucket } from '../utils/votingBaseline.js';
/** Max DOCEO RCV records aggregated per request (2 pages × 100 per page). */
const DOCEO_RCV_FETCH_LIMIT = 200;
/** Per-page limit for DOCEO getLatestVotes (API enforces ≤ 100). */
const DOCEO_PAGE_LIMIT = 100;
/** DOCEO call timeout, mirrors the shared aggregator guard. */
const DOCEO_TIMEOUT_MS = 2_000;
/** Cap MEPs inspected per group/all-MEPs request to bound runtime. */
const MAX_MEPS_PER_REQUEST = 50;
/** Window (hours) for the NEAR_REALTIME freshness label. */
const NEAR_REALTIME_WINDOW_HOURS = 72;
/**
* Voting anomaly surface, preserves the schema consumed by
* `correlate_intelligence` and adds `evidenceVoteIds` for traceability.
*/
interface VotingAnomaly {
type:
| 'PARTY_DEFECTION'
| 'ABSTENTION_SPIKE'
| 'ALIGNMENT_SHIFT'
| 'CROSS_PARTY_ALIGNMENT_SHIFT';
severity: 'HIGH' | 'MEDIUM' | 'LOW';
mepId: string;
mepName: string;
description: string;
metrics: { expectedValue: number; actualValue: number; deviation: number };
detectedDate: string;
/**
* DOCEO `LatestVoteRecord.id` values supporting the anomaly.
* These are the unique vote identifiers returned by `get_latest_votes`
* (e.g. `"RCV-10-2026-01-15-001"`).
*/
evidenceVoteIds: string[];
}
interface VotingAnomalyAnalysis {
period: { from: string; to: string };
targetScope: string;
anomalies: VotingAnomaly[];
summary: { totalAnomalies: number; highSeverity: number; mediumSeverity: number; lowSeverity: number };
computedAttributes: {
anomalyRate: number;
severityIndex: number;
groupStabilityScore: number;
defectionTrend: string;
riskLevel: string;
};
confidenceLevel: 'HIGH' | 'MEDIUM' | 'LOW';
/**
* Source of voting records consumed. Always `'DOCEO'` for this tool —
* matches the closed set used by sibling OSINT tools (`'EP_API' | 'DOCEO'
* | 'EP_API+DOCEO'`). Use {@link dataAvailable} = `false` to discriminate
* the "DOCEO unreachable / empty period" branch.
*/
dataSource: 'DOCEO';
dataFreshness: string;
sourceAttribution: string;
methodology: string;
dataQualityWarnings: string[];
/**
* Minimum per-MEP RCV votes inspected across the analysed scope (worst-case
* coverage). Drives {@link confidenceLevel} so that group/all-MEP scopes
* are not inflated by a single MEP with high coverage.
*/
rcvVotesInspected: number;
/**
* Maximum per-MEP RCV votes inspected across the analysed scope (best-case
* coverage). Surfaced for observability so callers can see the spread
* between the worst- and best-covered MEP.
*/
rcvVotesInspectedMax: number;
mepsAnalyzed: number;
/**
* Number of distinct plenary weeks from which DOCEO RCV records were
* successfully aggregated. Drives the HIGH-confidence ladder (HIGH requires
* ≥3 weeks contributing AND ≥50 per-MEP RCVs inspected).
*/
weeksInspected: number;
/**
* `true` when the requested `[from, to]` window spans more than the
* server-side cap of {@link MAX_PLENARY_WEEKS} plenary weeks and the fetch
* was truncated to the most recent weeks in scope. Accompanied by a
* `dataQualityWarnings` entry.
*/
weeksTruncated?: boolean;
/**
* `false` when DOCEO was unreachable or returned no records for the
* requested period; absent / `true` otherwise. Replaces the previous
* `dataSource: 'NONE'` sentinel so the `dataSource` field stays a closed
* set across OSINT tools.
*/
dataAvailable?: boolean;
}
/** Trend assessment derived from high-severity counts. */
function classifyDefectionTrend(highCount: number): string {
Iif (highCount > 2) return 'INCREASING';
if (highCount > 0) return 'STABLE';
return 'DECREASING';
}
/** Risk-level classifier derived from high-severity counts. */
function classifyRiskLevel(highCount: number): string {
Iif (highCount > 3) return 'CRITICAL';
if (highCount > 1) return 'ELEVATED';
if (highCount > 0) return 'MODERATE';
return 'LOW';
}
/** Map z-score magnitude to severity (preserves prior HIGH/MEDIUM/LOW semantics). */
function severityFromZ(z: number): 'HIGH' | 'MEDIUM' | 'LOW' {
Iif (z >= 3) return 'HIGH';
Iif (z >= 2) return 'MEDIUM';
return 'LOW';
}
/** Map percentage-point deltas to severity. */
function severityFromDelta(delta: number): 'HIGH' | 'MEDIUM' | 'LOW' {
if (delta >= 40) return 'HIGH';
Iif (delta >= 30) return 'MEDIUM';
return 'LOW';
}
/** Map cross-party alignment share to severity. */
function severityFromShare(share: number): 'HIGH' | 'MEDIUM' | 'LOW' {
Eif (share >= 90) return 'HIGH';
if (share >= 75) return 'MEDIUM';
return 'LOW';
}
/**
* Build the human-readable methodology string emitted alongside each
* response. Reports the *derived* thresholds actually applied for the
* request so callers that override `sensitivityThreshold` see the scaled
* values (not the spec defaults).
*
* @internal
*/
function buildMethodologyDescription(
thresholds: SensitivityThresholds,
sensitivity: number
): string {
const z = Math.round(thresholds.zScore * 100) / 100;
const wow = Math.round(thresholds.wowDelta * 100) / 100;
const crossPct = Math.round(thresholds.crossPartyShare * 100);
const isDefault = sensitivity === 0.3;
const scalingNote = isDefault
? '(spec defaults)'
: `(scaled from spec defaults — z≥${String(DEFAULT_Z_SCORE_THRESHOLD)}, WoW≥`
+ `${String(DEFAULT_WOW_THRESHOLD_PP)}pp, cross-party≥`
+ `${String(Math.round(DEFAULT_CROSS_PARTY_SHARE * 100))}% — `
+ `via sensitivityThreshold=${String(sensitivity)})`;
return 'Per-MEP defection / abstention / cross-party alignment anomaly detection on DOCEO RCV '
+ 'records — iterates weekly DOCEO RCV pages across the requested period (deduplicated by '
+ `record id, capped at 200 records, ${String(MAX_PLENARY_WEEKS)}-week hard limit). Per-vote group `
+ 'majority resolved by plurality with alphabetical tie-breaking. Each MEP vote classified as '
+ 'aligned / defected / abstained / absent against their home-group majority. Anomalies emitted '
+ `when defection-rate z-score ≥ ${String(z)}, `
+ `abstention-rate z-score ≥ ${String(z)}, week-over-week defection delta ≥ ${String(wow)}pp, `
+ `or non-home-group alignment share ≥ ${String(crossPct)}% in a weekly sub-window `
+ `${scalingNote}. Confidence: HIGH requires ≥50 RCVs inspected AND ≥3 contributing weeks; `
+ 'otherwise MEDIUM (10-49 RCVs or <3 weeks) or LOW (<10 RCVs). Source: EP DOCEO XML.';
}
interface MepRef {
id: string;
name: string;
politicalGroup: string;
}
/**
* Per-request thresholds derived from `sensitivityThreshold`.
*
* Defaults match the issue specification: z ≥ 1.5, WoW ≥ 20pp, cross-party
* share ≥ 0.6. The sensitivity parameter scales these so lower values yield
* more anomalies (backward-compatible with prior callers).
*/
interface SensitivityThresholds {
zScore: number;
wowDelta: number;
crossPartyShare: number;
}
function deriveThresholds(sensitivity: number): SensitivityThresholds {
// Linear scale anchored at the default 0.3 → spec defaults.
const factor = sensitivity / 0.3;
return {
zScore: Math.max(0.5, DEFAULT_Z_SCORE_THRESHOLD * factor),
wowDelta: Math.max(5, DEFAULT_WOW_THRESHOLD_PP * factor),
crossPartyShare: Math.min(0.95, Math.max(0.3, DEFAULT_CROSS_PARTY_SHARE * factor)),
};
}
/**
* Detect anomalies for a single MEP from the supplied DOCEO RCV records.
*
* @param mep - MEP reference (id/name/politicalGroup).
* @param votes - All DOCEO RCV records in the inspection window.
* @param thresholds - Sensitivity-adjusted detection thresholds.
* @param detectedDate - Period end date stamped onto anomaly records.
* @returns Detected anomalies and the per-MEP RCV inspection count.
*/
function detectMepAnomaliesFromDoceo(
mep: MepRef,
votes: LatestVoteRecord[],
thresholds: SensitivityThresholds,
detectedDate: string
): { anomalies: VotingAnomaly[]; classified: ClassifiedVote[]; buckets: WeekBucket[] } {
const homeGroup = mep.politicalGroup !== '' ? normalizePoliticalGroup(mep.politicalGroup) : null;
const classified = votes.map(v => classifyMepVote(v, mep.id, homeGroup));
const buckets = bucketByWeek(classified);
const anomalies: VotingAnomaly[] = [];
for (const outlier of findOutlierWeeks(buckets, 'defectionRate', thresholds.zScore)) {
anomalies.push({
type: 'PARTY_DEFECTION',
severity: severityFromZ(outlier.z),
mepId: mep.id,
mepName: mep.name,
description: `Defection rate of ${String(outlier.value)}% during week of ${outlier.weekStart} `
+ `(z=${String(outlier.z)} vs ${mep.politicalGroup || 'home group'} majority on DOCEO RCVs)`,
metrics: {
expectedValue: outlier.baselineMean,
actualValue: outlier.value,
deviation: Math.round((outlier.value - outlier.baselineMean) * 100) / 100,
},
detectedDate,
evidenceVoteIds: outlier.voteIds,
});
}
for (const outlier of findOutlierWeeks(buckets, 'abstentionRate', thresholds.zScore)) {
anomalies.push({
type: 'ABSTENTION_SPIKE',
severity: severityFromZ(outlier.z),
mepId: mep.id,
mepName: mep.name,
description: `Abstention rate of ${String(outlier.value)}% during week of ${outlier.weekStart} `
+ `(z=${String(outlier.z)} vs MEP's own baseline)`,
metrics: {
expectedValue: outlier.baselineMean,
actualValue: outlier.value,
deviation: Math.round((outlier.value - outlier.baselineMean) * 100) / 100,
},
detectedDate,
evidenceVoteIds: outlier.voteIds,
});
}
for (const shift of findWoWShifts(buckets, thresholds.wowDelta)) {
anomalies.push({
type: 'ALIGNMENT_SHIFT',
severity: severityFromDelta(shift.delta),
mepId: mep.id,
mepName: mep.name,
description: `Week-over-week defection rate jumped by ${String(shift.delta)}pp `
+ `(${shift.fromWeek} → ${shift.toWeek})`,
metrics: {
expectedValue: shift.previousRate,
actualValue: Math.round((shift.previousRate + shift.delta) * 100) / 100,
deviation: shift.delta,
},
detectedDate,
evidenceVoteIds: shift.voteIds,
});
}
for (const window of findCrossPartyAlignmentWindows(buckets, thresholds.crossPartyShare)) {
const thresholdPct = Math.round(thresholds.crossPartyShare * 100);
anomalies.push({
type: 'CROSS_PARTY_ALIGNMENT_SHIFT',
severity: severityFromShare(window.sharePercent),
mepId: mep.id,
mepName: mep.name,
description: `Voted with non-home group majorities on ${String(window.sharePercent)}% of `
+ `${String(window.decisive)} decisive RCVs during week of ${window.weekStart}`,
metrics: {
expectedValue: thresholdPct,
actualValue: window.sharePercent,
deviation: Math.round((window.sharePercent - thresholdPct) * 100) / 100,
},
detectedDate,
evidenceVoteIds: window.voteIds,
});
}
return { anomalies, classified, buckets };
}
/** Count RCV votes where the MEP actually appeared on the roll. */
function countVotesInspected(classified: ClassifiedVote[]): number {
let n = 0;
for (const c of classified) Eif (c.alignment !== 'absent') n += 1;
return n;
}
/**
* Determine whether the DOCEO source can be labelled NEAR_REALTIME.
*
* NEAR_REALTIME requires at least one DOCEO record with a sitting date within
* the configured window (default 72h) of `referenceDate`.
*/
function isNearRealtime(votes: LatestVoteRecord[], referenceDate: Date): boolean {
const cutoff = referenceDate.getTime() - NEAR_REALTIME_WINDOW_HOURS * 3_600_000;
for (const v of votes) {
const date = v.sittingDate ?? v.date;
Iif (date === '') continue;
const ts = Date.parse(date);
if (!Number.isNaN(ts) && ts >= cutoff) return true;
}
return false;
}
/**
* Return the most recent valid sitting date in the supplied record list,
* or `null` when none of the records have a parseable date.
*
* @internal
*/
function mostRecentSittingDate(votes: LatestVoteRecord[]): Date | null {
let latest: number | null = null;
for (const v of votes) {
const date = v.sittingDate ?? v.date;
Iif (date === '') continue;
const ts = Date.parse(date);
Iif (Number.isNaN(ts)) continue;
if (latest === null || ts > latest) latest = ts;
}
return latest === null ? null : new Date(latest);
}
/**
* Return the oldest valid sitting date in the supplied record list, or `null`
* when none of the records have a parseable date. Used by
* {@link resolveDataFreshness} to surface the span actually covered by a
* multi-week DOCEO fetch.
*
* @internal
*/
function oldestSittingDate(votes: LatestVoteRecord[]): Date | null {
let oldest: number | null = null;
for (const v of votes) {
const date = v.sittingDate ?? v.date;
Iif (date === '') continue;
const ts = Date.parse(date);
Iif (Number.isNaN(ts)) continue;
if (oldest === null || ts < oldest) oldest = ts;
}
return oldest === null ? null : new Date(oldest);
}
/**
* Result of the multi-week DOCEO fetch loop.
*
* @internal
*/
interface DoceoCorpus {
records: LatestVoteRecord[];
doceoAvailable: boolean;
/** Plenary-week Mondays from which at least one RCV record was retrieved. */
weeksContributing: number;
/** Total plenary weeks attempted in the [from, to] window after truncation. */
weeksAttempted: number;
/** `true` when the requested window was truncated to {@link MAX_PLENARY_WEEKS}. */
weeksTruncated: boolean;
}
/** Corpus cache TTL — 5 minutes per issue spec. */
const CORPUS_CACHE_TTL_MS = 5 * 60 * 1_000;
/**
* Bound on the multi-week corpus cache to prevent unbounded memory growth in
* long-lived servers as varied `${scope}|${from}|${to}` windows accumulate
* — mirrors the convention used by `analyzeCommitteeActivity` and
* `doceoMepAggregator`.
*/
const MAX_CORPUS_CACHE_ENTRIES = 200;
interface CorpusCacheEntry {
corpus: DoceoCorpus;
expiresAt: number;
}
/**
* Multi-week DOCEO corpus cache, keyed by `${scope}|${from}|${to}`. Bounded
* with a 5-minute TTL so back-to-back calls within the same window reuse the
* weekly fan-out. Cache keys include both `from` and `to` so that windows
* shifted even by one day re-fetch correctly.
*
* @internal
*/
const corpusCache = new Map<string, CorpusCacheEntry>();
/**
* Clear the multi-week DOCEO corpus cache.
*
* @internal Exported for unit testing.
*/
export function clearDoceoCorpusCache(): void {
corpusCache.clear();
}
/**
* Fetch DOCEO RCV records for a single plenary week (Mon-Fri) anchored at
* `weekStart`. Returns `null` when the week is entirely unreachable so the
* caller can record per-week resilience without aborting the multi-week fan-out.
*
* Implements within-week pagination (2 × 100) only when the first page is
* full *and* this is the only week being fetched — for multi-week requests
* we prefer breadth (more weeks) over depth (more votes per week) to seed
* a meaningful baseline.
*
* @internal
*/
async function fetchSinglePlenaryWeek(
weekStart: string,
paginateWithinWeek: boolean
): Promise<LatestVoteRecord[] | null> {
try {
const firstPage = await withTimeoutAndAbort(
(signal) => doceoClient.getLatestVotes({
includeIndividualVotes: true,
limit: DOCEO_PAGE_LIMIT,
offset: 0,
weekStart,
abortSignal: signal,
}),
DOCEO_TIMEOUT_MS,
`DOCEO RCV fetch (week ${weekStart}, page 1) timed out`
);
let weekRecords = firstPage.data;
Iif (paginateWithinWeek && firstPage.data.length >= DOCEO_PAGE_LIMIT) {
try {
const secondPage = await withTimeoutAndAbort(
(signal) => doceoClient.getLatestVotes({
includeIndividualVotes: true,
limit: DOCEO_PAGE_LIMIT,
offset: DOCEO_PAGE_LIMIT,
weekStart,
abortSignal: signal,
}),
DOCEO_TIMEOUT_MS,
`DOCEO RCV fetch (week ${weekStart}, page 2) timed out`
);
weekRecords = [...weekRecords, ...secondPage.data];
} catch {
// Second page failure is non-fatal; continue with first page only.
}
}
return weekRecords;
} catch (error: unknown) {
auditLogger.logError(
'detect_voting_anomalies.doceo_week_fetch',
{ weekStart },
toErrorMessage(error)
);
return null;
}
}
/**
* Sort RCV records chronologically (sittingDate desc) so the
* `DOCEO_RCV_FETCH_LIMIT` cap keeps the freshest votes when truncating.
*
* @internal
*/
function sortRecordsDesc(records: LatestVoteRecord[]): LatestVoteRecord[] {
const toTs = (d: string | undefined): number => {
Iif (d === undefined || d === '' || !/^\d{4}-\d{2}-\d{2}$/.test(d)) return 0;
const ts = Date.parse(`${d}T00:00:00Z`);
return Number.isNaN(ts) ? 0 : ts;
};
return [...records].sort((a, b) => {
const aTs = toTs(a.sittingDate ?? a.date);
const bTs = toTs(b.sittingDate ?? b.date);
if (bTs !== aTs) return bTs - aTs;
return a.id.localeCompare(b.id);
});
}
/**
* Detect whether the requested `[from, to]` window spans more plenary weeks
* than {@link MAX_PLENARY_WEEKS}. Pure helper extracted to keep
* {@link fetchDoceoRcvRecords} below the cyclomatic-complexity ceiling.
*
* @internal
*/
function detectWindowTruncation(
period: { from: string; to: string }
): boolean {
const fromMs = Date.parse(`${period.from}T00:00:00Z`);
const toMs = Date.parse(`${period.to}T00:00:00Z`);
Iif (Number.isNaN(fromMs) || Number.isNaN(toMs) || fromMs > toMs) return false;
// Count intersecting plenary weeks using the same Mon–Fri intersection logic
// as iteratePlenaryWeeks — stop early once we've confirmed more than
// MAX_PLENARY_WEEKS intersecting weeks exist so the result matches actual
// truncation rather than a raw day-span heuristic.
const DAY_MS = 24 * 3_600_000;
const WEEK_MS = 7 * DAY_MS;
const FRIDAY_OFFSET_MS = 4 * DAY_MS;
const endMonday = isoWeekStart(period.to);
let cursorMs = Date.parse(`${endMonday}T00:00:00Z`);
let intersectingCount = 0;
while (cursorMs + FRIDAY_OFFSET_MS >= fromMs) {
Eif (cursorMs <= toMs) {
intersectingCount += 1;
if (intersectingCount > MAX_PLENARY_WEEKS) return true;
}
cursorMs -= WEEK_MS;
}
return false;
}
/** Stop the corpus build after this many consecutive per-week fetch failures. */
const MAX_CONSECUTIVE_FETCH_FAILURES = 3;
/**
* Run the per-week DOCEO fetch loop sequentially, deduplicating RCV records
* by record `id` (the top-level `LatestVoteRecord.id`). Returns the merged
* map plus a `anyWeekReached` flag used by the caller to discriminate the
* "all weeks failed" branch from the "empty corpus" branch, and
* `weeksAttempted` reflecting the number of weeks the loop actually tried
* to fetch (which can be less than `weeks.length` under the
* {@link MAX_CONSECUTIVE_FETCH_FAILURES} early-exit).
*
* @internal
*/
async function fetchAndDedupWeeks(
weeks: string[],
paginateWithinWeek: boolean
): Promise<{
dedup: Map<string, LatestVoteRecord>;
anyWeekReached: boolean;
rawFetched: number;
weeksAttempted: number;
}> {
const dedup = new Map<string, LatestVoteRecord>();
let anyWeekReached = false;
let rawFetched = 0;
let weeksAttempted = 0;
let consecutiveFailures = 0;
for (const week of weeks) {
if (consecutiveFailures >= MAX_CONSECUTIVE_FETCH_FAILURES) break;
weeksAttempted += 1;
const weekRecords = await fetchSinglePlenaryWeek(week, paginateWithinWeek);
if (weekRecords === null) {
consecutiveFailures += 1;
continue;
}
consecutiveFailures = 0;
anyWeekReached = true;
for (const v of weekRecords) {
Iif (v.dataSource !== 'RCV') continue;
rawFetched += 1;
if (!dedup.has(v.id)) {
dedup.set(v.id, v);
}
}
}
return { dedup, anyWeekReached, rawFetched, weeksAttempted };
}
/**
* Count distinct ISO-week buckets (by `sittingDate`) present in the corpus.
* This is the spec-aligned signal — "≥3 distinct weeks contributed votes" —
* and is robust against fixtures where the same DOCEO XML fixture is returned
* for multiple `weekStart` anchors.
*
* Malformed sitting dates (anything not matching `YYYY-MM-DD` or that
* `Date.parse` cannot resolve) are skipped so they cannot inflate the
* distinct-week count with bogus week keys — see review on
* https://github.com/Hack23/European-Parliament-MCP-Server/pull/490.
*
* @internal
*/
function countDistinctSittingWeeks(corpus: LatestVoteRecord[]): number {
const distinctWeeks = new Set<string>();
for (const v of corpus) {
const date = v.sittingDate ?? v.date;
Iif (date === '') continue;
Iif (!/^\d{4}-\d{2}-\d{2}$/.test(date)) continue;
Iif (Number.isNaN(Date.parse(`${date}T00:00:00Z`))) continue;
distinctWeeks.add(isoWeekStart(date));
}
return distinctWeeks.size;
}
/**
* Build the multi-week DOCEO RCV corpus for the requested `[period.from,
* period.to]` window.
*
* Replaces the prior single-week fetch (which silently dropped any votes
* outside the latest plenary week before `period.to`, degenerating the
* per-MEP baseline to a single-week sample with zero stdev — see
* Hack23/European-Parliament-MCP-Server#462 follow-up). Now iterates every
* plenary-week Monday between `from` and `to`, sequentially under the
* existing rate limiter, deduplicates by record `id`
* (the top-level `LatestVoteRecord.id`), and caps the corpus at
* {@link DOCEO_RCV_FETCH_LIMIT}.
*
* Resilience: per-week timeouts/errors are absorbed (the week is recorded as
* non-contributing) so a single bad week does not invalidate the whole
* request.
*
* @internal
*/
async function fetchDoceoRcvRecords(
period: { from: string; to: string }
): Promise<DoceoCorpus> {
const allWeeks = iteratePlenaryWeeks(period.from, period.to);
const weeksTruncated = detectWindowTruncation(period);
// Single-week fallback: empty iteration (e.g. malformed dates) still hits
// DOCEO at period.to to preserve the legacy "no [from,to]" path.
const weeks = allWeeks.length === 0 ? [period.to] : allWeeks;
const paginateWithinWeek = weeks.length === 1;
const { dedup, anyWeekReached, rawFetched, weeksAttempted } = await fetchAndDedupWeeks(weeks, paginateWithinWeek);
if (!anyWeekReached) {
auditLogger.logError(
'detect_voting_anomalies.doceo_fetch',
{ from: period.from, to: period.to, weeksAttempted },
'all weekly fetches failed'
);
return {
records: [],
doceoAvailable: false,
weeksContributing: 0,
weeksAttempted,
weeksTruncated,
};
}
const merged = [...dedup.values()];
const corpus = sortRecordsDesc(merged).slice(0, DOCEO_RCV_FETCH_LIMIT);
const weeksContributing = countDistinctSittingWeeks(corpus);
// `duplicatesRemoved` reflects RCV records collapsed by record `id`
// (the top-level `LatestVoteRecord.id`; pre-dedup fetched − unique merged).
// `truncatedRecords` reports the post-cap drop from
// `DOCEO_RCV_FETCH_LIMIT`. These were previously conflated into a single
// `dedupeRatio` computed against the *capped* corpus, which mostly
// measured truncation rather than true de-duplication
// — see review on https://github.com/Hack23/European-Parliament-MCP-Server/pull/490.
const duplicatesRemoved = Math.max(0, rawFetched - merged.length);
const truncatedRecords = Math.max(0, merged.length - corpus.length);
const dedupeRatio = rawFetched > 0
? Math.round((duplicatesRemoved / rawFetched) * 1000) / 1000
: 0;
auditLogger.logDataAccess(
'detect_voting_anomalies.doceo_corpus',
{
from: period.from,
to: period.to,
weeksAttempted,
weeksContributing,
rawRecords: rawFetched,
uniqueRecords: merged.length,
duplicatesRemoved,
cappedRecords: corpus.length,
truncatedRecords,
dedupeRatio,
},
corpus.length
);
return {
records: corpus,
doceoAvailable: true,
weeksContributing,
weeksAttempted,
weeksTruncated,
};
}
/**
* Cached wrapper around {@link fetchDoceoRcvRecords}. Cache key includes the
* MEP/group scope AND the period boundaries so windows shifted by even one
* day refetch correctly. TTL is 5 minutes per the issue spec.
*
* @internal
*/
async function getDoceoCorpus(
scope: { mepId?: string; groupId?: string },
period: { from: string; to: string }
): Promise<DoceoCorpus> {
const scopeKey = scope.mepId ?? scope.groupId ?? 'all';
const key = `${scopeKey}|${period.from}|${period.to}`;
const now = Date.now();
const cached = corpusCache.get(key);
if (cached !== undefined) {
Eif (cached.expiresAt > now) {
return cached.corpus;
}
// Expired entries are removed eagerly so the bounded cache reclaims slots
// promptly instead of waiting for the FIFO eviction below.
corpusCache.delete(key);
}
const corpus = await fetchDoceoRcvRecords(period);
// Only cache successful fetches so a transient outage doesn't lock the
// window into LOW confidence for 5 minutes.
if (corpus.doceoAvailable) {
Iif (corpusCache.size >= MAX_CORPUS_CACHE_ENTRIES) {
// FIFO eviction: Map iteration order is insertion order in JS, so the
// first key is the oldest insertion. Sufficient for a small bounded
// corpus cache; full LRU is not warranted at this scale.
const firstKey = corpusCache.keys().next().value;
if (firstKey !== undefined) corpusCache.delete(firstKey);
}
corpusCache.set(key, { corpus, expiresAt: now + CORPUS_CACHE_TTL_MS });
}
return corpus;
}
interface DetectionResult {
scope: string;
anomalies: VotingAnomaly[];
/** Minimum per-MEP RCV votes inspected (worst-case coverage). */
rcvVotesInspected: number;
/** Maximum per-MEP RCV votes inspected (best-case coverage). */
rcvVotesInspectedMax: number;
mepsAnalyzed: number;
}
/**
* Aggregate per-MEP anomaly detection over a list of MEPs.
*
* Returns *minimum* and *maximum* RCV votes inspected across the MEP list.
* The minimum drives confidence/warnings so that group/all-MEP scopes are not
* inflated by a single high-coverage MEP — when most MEPs have few or zero
* inspected votes (e.g. broad scope, narrow DOCEO window) the response
* correctly reports LOW/MEDIUM confidence instead of HIGH.
*/
function runDetectionForMeps(
meps: MepRef[],
records: LatestVoteRecord[],
thresholds: SensitivityThresholds,
detectedDate: string
): { anomalies: VotingAnomaly[]; rcvVotesInspected: number; rcvVotesInspectedMax: number } {
const allAnomalies: VotingAnomaly[] = [];
const perMepInspected: number[] = [];
for (const mep of meps) {
const { anomalies, classified } = detectMepAnomaliesFromDoceo(mep, records, thresholds, detectedDate);
const inspected = countVotesInspected(classified);
perMepInspected.push(inspected);
allAnomalies.push(...anomalies);
auditLogger.logDataAccess(
'detect_voting_anomalies.mep_analysis',
{ mepId: mep.id, rcvVotesInspected: inspected },
inspected
);
}
// Worst-case coverage drives confidence; best-case is reported separately
// for observability. When no MEPs were analysed both values are 0.
const rcvVotesInspected = perMepInspected.length > 0 ? Math.min(...perMepInspected) : 0;
const rcvVotesInspectedMax = perMepInspected.length > 0 ? Math.max(...perMepInspected) : 0;
return { anomalies: allAnomalies, rcvVotesInspected, rcvVotesInspectedMax };
}
/**
* Detect anomalies for a single MEP.
*
* @internal Exported for unit testing.
*/
async function detectSingleMepAnomalies(
mepId: string,
thresholds: SensitivityThresholds,
period: { from: string; to: string },
records: LatestVoteRecord[]
): Promise<DetectionResult> {
const mep = await epClient.getMEPDetails(mepId);
const { anomalies, rcvVotesInspected, rcvVotesInspectedMax } = runDetectionForMeps(
[{ id: mep.id, name: mep.name, politicalGroup: mep.politicalGroup }],
records,
thresholds,
period.to
);
return { scope: `MEP: ${mepId}`, anomalies, rcvVotesInspected, rcvVotesInspectedMax, mepsAnalyzed: 1 };
}
/** Detect anomalies for a political group or all MEPs (bounded scan). */
async function detectGroupAnomalies(
groupId: string | undefined,
thresholds: SensitivityThresholds,
period: { from: string; to: string },
records: LatestVoteRecord[]
): Promise<DetectionResult> {
const groupFilter: { group?: string } = {};
if (groupId !== undefined) groupFilter.group = groupId;
const scope = groupId !== undefined ? `Group: ${groupId}` : 'All MEPs';
const mepsResult = await epClient.getCurrentMEPs({ ...groupFilter, limit: MAX_MEPS_PER_REQUEST });
const meps: MepRef[] = mepsResult.data.map(m => ({
id: m.id,
name: m.name,
politicalGroup: m.politicalGroup,
}));
const { anomalies, rcvVotesInspected, rcvVotesInspectedMax } =
runDetectionForMeps(meps, records, thresholds, period.to);
return { scope, anomalies, rcvVotesInspected, rcvVotesInspectedMax, mepsAnalyzed: meps.length };
}
/** Roll up anomalies into severity counts and derived attributes. */
function buildAnomalySummary(anomalies: VotingAnomaly[]): {
highSeverity: number;
mediumSeverity: number;
lowSeverity: number;
anomalyRate: number;
severityIndex: number;
} {
const highSeverity = anomalies.filter(a => a.severity === 'HIGH').length;
const mediumSeverity = anomalies.filter(a => a.severity === 'MEDIUM').length;
const lowSeverity = anomalies.filter(a => a.severity === 'LOW').length;
const anomalyRate = anomalies.length > 0
? Math.round((highSeverity / anomalies.length) * 100) / 100
: 0;
const severityIndex = anomalies.length > 0
? Math.round(((highSeverity * 3 + mediumSeverity * 2 + lowSeverity) / anomalies.length) * 100) / 100
: 0;
return { highSeverity, mediumSeverity, lowSeverity, anomalyRate, severityIndex };
}
/**
* Compute data-quality warnings for a given fetch outcome.
*
* @internal
*/
function collectDataQualityWarnings(
doceoAvailable: boolean,
recordCount: number,
rcvVotesInspected: number,
weeksContributing: number,
weeksTruncated: boolean
): string[] {
const warnings: string[] = [];
if (!doceoAvailable) {
warnings.push('DOCEO RCV source unavailable — anomaly detection deferred (LOW confidence).');
} else if (recordCount === 0) {
warnings.push('No DOCEO RCV records returned for the requested period.');
} else if (rcvVotesInspected < 10) {
warnings.push('Fewer than 10 RCV votes inspected — confidence reduced to LOW.');
}
if (doceoAvailable && weeksTruncated) {
warnings.push(
`Requested window exceeds the ${String(MAX_PLENARY_WEEKS)}-week cap — `
+ `fetched the most recent ${String(MAX_PLENARY_WEEKS)} plenary weeks only (weeksTruncated=true).`
);
}
if (doceoAvailable && recordCount > 0 && weeksContributing < 3) {
warnings.push(
`Only ${String(weeksContributing)} plenary week(s) contributed RCV records — `
+ 'baseline lacks multi-week dispersion (confidence capped at MEDIUM).'
);
}
return warnings;
}
/**
* Resolve the data freshness label.
*
* - `NEAR_REALTIME` when at least one DOCEO sitting falls within
* {@link NEAR_REALTIME_WINDOW_HOURS} of "now".
* - Otherwise, surface the most recent sitting date and the actual age in
* hours so consumers can reason about staleness without inflating the
* window. When multiple plenary weeks contributed, the oldest contributing
* week is also reported so callers can see the multi-week span covered.
*/
function resolveDataFreshness(
dataAvailable: boolean,
records: LatestVoteRecord[]
): string {
if (!dataAvailable) return 'No DOCEO RCV data available for the requested period';
const now = new Date();
const latest = mostRecentSittingDate(records);
const oldest = oldestSittingDate(records);
const oldestSuffix = oldest !== null && latest !== null && oldest.getTime() < latest.getTime()
? ` (oldest contributing week ${oldest.toISOString().slice(0, 10)})`
: '';
if (isNearRealtime(records, now)) return `NEAR_REALTIME${oldestSuffix}`;
Iif (latest === null) return `DOCEO RCV data — outside ${String(NEAR_REALTIME_WINDOW_HOURS)}h NEAR_REALTIME window`;
const ageMs = now.getTime() - latest.getTime();
const ageHours = Math.max(0, Math.round(ageMs / 3_600_000));
return `DOCEO RCV data — latest sitting ${String(ageHours)}h old`
+ ` (outside ${String(NEAR_REALTIME_WINDOW_HOURS)}h NEAR_REALTIME window)${oldestSuffix}`;
}
/**
* Resolve the confidence level from the fetch outcome and coverage.
*
* `HIGH` requires *both* ≥50 per-MEP RCVs *and* ≥3 distinct plenary weeks
* contributing records — without multi-week dispersion the rolling baseline
* has zero stdev and z-score outliers collapse (the failure mode this
* follow-up was opened to fix). When coverage is sufficient but only 1-2
* weeks contributed, confidence is capped at MEDIUM.
*/
function resolveConfidenceLevel(
dataAvailable: boolean,
rcvVotesInspected: number,
weeksContributing: number
): 'HIGH' | 'MEDIUM' | 'LOW' {
if (!dataAvailable) return 'LOW';
const coverage = coverageConfidence(rcvVotesInspected);
if (coverage === 'HIGH' && weeksContributing < 3) return 'MEDIUM';
return coverage;
}
/**
* Inputs assembled by {@link handleDetectVotingAnomalies} and consumed by
* {@link buildVotingAnomalyAnalysis} to construct the response envelope.
*
* @internal
*/
interface AnalysisInputs {
period: { from: string; to: string };
result: DetectionResult;
corpus: DoceoCorpus;
thresholds: SensitivityThresholds;
sensitivity: number;
records: LatestVoteRecord[];
}
/**
* Assemble the `VotingAnomalyAnalysis` response envelope from the detection
* result, the multi-week DOCEO corpus, and the sensitivity-derived thresholds.
* Extracted from {@link handleDetectVotingAnomalies} to keep its cyclomatic
* complexity below the project ceiling.
*
* @internal
*/
function buildVotingAnomalyAnalysis(input: AnalysisInputs): VotingAnomalyAnalysis {
const { period, result, corpus, thresholds, sensitivity, records } = input;
const summary = buildAnomalySummary(result.anomalies);
const dataAvailable = corpus.doceoAvailable && records.length > 0;
const analysis: VotingAnomalyAnalysis = {
period,
targetScope: result.scope,
anomalies: result.anomalies,
summary: {
totalAnomalies: result.anomalies.length,
highSeverity: summary.highSeverity,
mediumSeverity: summary.mediumSeverity,
lowSeverity: summary.lowSeverity,
},
computedAttributes: {
anomalyRate: summary.anomalyRate,
severityIndex: summary.severityIndex,
groupStabilityScore: Math.round((1 - summary.severityIndex / 3) * 100 * 100) / 100,
defectionTrend: classifyDefectionTrend(summary.highSeverity),
riskLevel: classifyRiskLevel(summary.highSeverity),
},
confidenceLevel: resolveConfidenceLevel(dataAvailable, result.rcvVotesInspected, corpus.weeksContributing),
dataSource: 'DOCEO',
dataFreshness: resolveDataFreshness(dataAvailable, records),
sourceAttribution: 'European Parliament DOCEO XML — europarl.europa.eu/doceo',
methodology: buildMethodologyDescription(thresholds, sensitivity),
dataQualityWarnings: collectDataQualityWarnings(
corpus.doceoAvailable,
records.length,
result.rcvVotesInspected,
corpus.weeksContributing,
corpus.weeksTruncated
),
rcvVotesInspected: result.rcvVotesInspected,
rcvVotesInspectedMax: result.rcvVotesInspectedMax,
mepsAnalyzed: result.mepsAnalyzed,
weeksInspected: corpus.weeksContributing,
};
if (corpus.doceoAvailable && corpus.weeksTruncated) analysis.weeksTruncated = true;
if (!dataAvailable) analysis.dataAvailable = false;
return analysis;
}
/**
* Handles the `detect_voting_anomalies` MCP tool request.
*
* See file-level JSDoc for methodology details. Each anomaly carries
* `evidenceVoteIds` referencing the contributing DOCEO RCV records.
*
* @param args - Raw tool arguments, validated against {@link DetectVotingAnomaliesSchema}
* @returns MCP tool result containing detected anomalies with evidence vote IDs
* @throws If `args` fails schema validation or the EP API is unreachable.
*
* @security Input validated with Zod. Audit logs record only `mepId` and
* sanitised counts (no MEP names or vote contents).
* @since 0.8.0
*/
export async function handleDetectVotingAnomalies(
args: unknown
): Promise<ToolResult> {
const params = DetectVotingAnomaliesSchema.parse(args);
try {
const period = { from: params.dateFrom ?? '2024-01-01', to: params.dateTo ?? '2024-12-31' };
const thresholds = deriveThresholds(params.sensitivityThreshold);
const scope: { mepId?: string; groupId?: string } = {};
if (params.mepId !== undefined) scope.mepId = params.mepId;
if (params.groupId !== undefined) scope.groupId = params.groupId;
const corpus = await getDoceoCorpus(scope, period);
const result = params.mepId !== undefined
? await detectSingleMepAnomalies(params.mepId, thresholds, period, corpus.records)
: await detectGroupAnomalies(params.groupId, thresholds, period, corpus.records);
const analysis = buildVotingAnomalyAnalysis({
period,
result,
corpus,
thresholds,
sensitivity: params.sensitivityThreshold,
records: corpus.records,
});
return buildToolResponse(analysis);
} catch (error: unknown) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
throw new Error(`Failed to detect voting anomalies: ${errorMessage}`);
}
}
/**
* Tool metadata for MCP registration
*/
export const detectVotingAnomaliesToolMetadata = {
name: 'detect_voting_anomalies',
description: 'Detect unusual voting patterns including party defections, abstention spikes, week-over-week alignment shifts, and cross-party movement signals. Uses DOCEO RCV roll-call records and per-MEP rolling baselines (defection z ≥1.5, abstention z ≥1.5, WoW Δ ≥20pp, cross-party share ≥60%). Returns anomalies with evidenceVoteIds, severity classification (HIGH/MEDIUM/LOW), group stability score, defection trend, and risk level.',
inputSchema: {
type: 'object' as const,
properties: {
mepId: {
type: 'string',
description: 'MEP identifier (omit for broad analysis)',
minLength: 1,
maxLength: 100
},
groupId: {
type: 'string',
description: 'Political group to analyze',
minLength: 1,
maxLength: 50
},
dateFrom: {
type: 'string',
description: 'Analysis start date (YYYY-MM-DD format)',
pattern: '^\\d{4}-\\d{2}-\\d{2}$'
},
dateTo: {
type: 'string',
description: 'Analysis end date (YYYY-MM-DD format)',
pattern: '^\\d{4}-\\d{2}-\\d{2}$'
},
sensitivityThreshold: {
type: 'number',
description: 'Anomaly sensitivity (0-1, lower = more anomalies detected). Default 0.3 matches the spec thresholds (z≥1.5, WoW≥20pp, cross-party≥60%).',
minimum: 0,
maximum: 1,
default: 0.3
}
}
}
};
|