All files / src/tools getAllGeneratedStats.ts

94.91% Statements 56/59
75.47% Branches 40/53
100% Functions 18/18
96% Lines 48/50

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                                                                    4x                                                                                                             13x                     4x                                               47x 1016x   1016x 47x   2247x 47x   47x       47x                       47x 1034x     47x       1016x 47x 47x   47x 969x     1016x                 47x                                     32x   12x 47x   12x 3x       9x   8x 8x 8x 104x                           32x 32x 32x     32x 704x   473x   471x 471x 471x       32x       32x 50x     32x                                                               32x                 4x                                                                                                                                         7x 7x    
/**
 * MCP Tool: get_all_generated_stats
 *
 * Returns precomputed European Parliament activity statistics covering
 * parliamentary terms EP6–EP10 (2004–2025), including monthly activity
 * breakdowns, category rankings with percentiles, analytical commentary,
 * and trend-based predictions for 2026–2030.
 *
 * The underlying data is static and designed to be refreshed weekly by
 * an agentic workflow. No live EP API calls are made by this tool.
 *
 * **Intelligence Perspective:** Enables rapid longitudinal analysis of
 * EP legislative productivity, committee workload, and parliamentary
 * engagement metrics across two decades—supporting trend identification,
 * term-over-term comparisons, and predictive modelling.
 *
 * **Business Perspective:** Provides pre-built analytics for policy
 * consultancies, think-tanks, and academic researchers who need
 * ready-made historical benchmarks without incurring API latency.
 *
 * **Marketing Perspective:** Showcases the depth of the MCP server's
 * analytical capabilities with rich, pre-formatted intelligence products.
 *
 * ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
 * Data source: European Parliament Open Data Portal — data.europarl.europa.eu
 *
 * @module tools/getAllGeneratedStats
 */
 
import { z } from 'zod';
import { GENERATED_STATS } from '../data/generatedStats.js';
import { buildToolResponse, buildErrorResponse } from './shared/responseBuilder.js';
import type { ToolResult } from './shared/types.js';
 
export const GetAllGeneratedStatsSchema = z
  .object({
    yearFrom: z
      .number()
      .int()
      .min(2004)
      .max(2030)
      .optional()
      .describe('Start year for filtering (default: earliest available, 2004)'),
    yearTo: z
      .number()
      .int()
      .min(2004)
      .max(2030)
      .optional()
      .describe('End year for filtering (default: latest available, 2025)'),
    category: z
      .enum([
        'all',
        'plenary_sessions',
        'legislative_acts',
        'roll_call_votes',
        'committee_meetings',
        'parliamentary_questions',
        'resolutions',
        'speeches',
        'adopted_texts',
        'political_groups',
        'procedures',
        'events',
        'documents',
        'mep_turnover',
        'declarations',
      ])
      .optional()
      .default('all')
      .describe('Activity category to focus on (default: all)'),
    includePredictions: z
      .boolean()
      .optional()
      .default(true)
      .describe('Include trend-based predictions for 2026-2030 (default: true)'),
    includeMonthlyBreakdown: z
      .boolean()
      .optional()
      .default(false)
      .describe('Include month-by-month activity data (default: false for compact output)'),
    includeRankings: z
      .boolean()
      .optional()
      .default(true)
      .describe('Include percentile rankings and statistical analysis (default: true)'),
  })
  .refine(
    (data) =>
      data.yearFrom === undefined ||
      data.yearTo === undefined ||
      data.yearFrom <= data.yearTo,
    {
      message: 'yearFrom must be less than or equal to yearTo',
      path: ['yearFrom'],
    },
  );
 
export type GetAllGeneratedStatsParams = z.infer<typeof GetAllGeneratedStatsSchema>;
 
const CATEGORY_LABEL_MAP: Partial<Record<string, string>> = {
  plenary_sessions: 'Plenary Sessions',
  legislative_acts: 'Legislative Acts Adopted',
  roll_call_votes: 'Roll-Call Votes',
  committee_meetings: 'Committee Meetings',
  parliamentary_questions: 'Parliamentary Questions',
  resolutions: 'Resolutions',
  speeches: 'Speeches',
  adopted_texts: 'Adopted Texts',
  procedures: 'Procedures',
  events: 'Events',
  documents: 'Documents',
  mep_turnover: 'MEP Turnover',
  declarations: 'Declarations',
  // political_groups intentionally omitted — it has no numeric ranking
};
 
type RankingEntry = typeof GENERATED_STATS.categoryRankings[number];
 
/**
 * Compute mean, standard deviation, and median from a numeric array.
 * Used to recalculate ranking summary statistics for filtered year ranges.
 */
function computeStats(values: number[]): { mean: number; stdDev: number; median: number } {
  const n = values.length;
  const mean = values.reduce((s, v) => s + v, 0) / n;
  const variance =
    n > 1 ? values.reduce((s, v) => s + (v - mean) ** 2, 0) / n : 0;
  const stdDev = Math.round(Math.sqrt(variance) * 100) / 100;
 
  const sorted = [...values].sort((a, b) => a - b);
  const mid = Math.floor(sorted.length / 2);
  const median =
    sorted.length % 2 === 0
      ? ((sorted[mid - 1] ?? 0) + (sorted[mid] ?? 0)) / 2
      : sorted[mid] ?? 0;
 
  return { mean: Math.round(mean * 100) / 100, stdDev, median };
}
 
/**
 * Recompute ranking summary fields (mean, stdDev, median, topYear, bottomYear)
 * for a filtered year range. Re-sorts and re-ranks entries with fresh percentiles.
 */
function recomputeRankingSummary(
  r: RankingEntry,
  yearFrom: number,
  yearTo: number,
): RankingEntry {
  const filtered = r.rankings.filter(
    (ry) => ry.year >= yearFrom && ry.year <= yearTo,
  );
 
  Iif (filtered.length === 0) {
    return { ...r, rankings: [], mean: 0, stdDev: 0, median: 0, topYear: 0, bottomYear: 0 };
  }
 
  const values = filtered.map((e) => e.totalActivityScore);
  const n = values.length;
  const stats = computeStats(values);
 
  const reSorted = [...filtered].sort(
    (a, b) => b.totalActivityScore - a.totalActivityScore,
  );
 
  const reRanked = reSorted.map((entry, idx) => ({
    ...entry,
    rank: idx + 1,
    percentile:
      n === 1
        ? 100
        : Math.round(((n - idx - 1) / (n - 1)) * 10000) / 100,
  }));
 
  return {
    ...r,
    rankings: reRanked,
    ...stats,
    topYear: reSorted[0]?.year ?? 0,
    bottomYear: reSorted[n - 1]?.year ?? 0,
  };
}
 
/**
 * Filter category rankings by the requested year range and optional category.
 * Returns recomputed summary statistics for the filtered subset.
 * `political_groups` has no numeric ranking and returns an empty array.
 */
function filterRankings(
  params: GetAllGeneratedStatsParams,
  yearFrom: number,
  yearTo: number
): typeof GENERATED_STATS.categoryRankings {
  if (!params.includeRankings) return [];
 
  const recompute = (r: RankingEntry): RankingEntry =>
    recomputeRankingSummary(r, yearFrom, yearTo);
 
  if (params.category === 'all') {
    return GENERATED_STATS.categoryRankings.map(recompute);
  }
 
  // political_groups has no numeric ranking category
  if (params.category === 'political_groups') return [];
 
  const label = CATEGORY_LABEL_MAP[params.category];
  Iif (label === undefined) return [];
  return GENERATED_STATS.categoryRankings
    .filter((r) => r.category === label)
    .map(recompute);
}
 
/**
 * Retrieve precomputed EP activity statistics with optional year/category filtering.
 *
 * The response always includes `coveragePeriod` (the full dataset range, 2004–2025)
 * and `requestedPeriod` (the user-supplied year filter). The `analysisSummary` covers
 * the full dataset with a `coverageNote` clarifying scope when filters narrow the range.
 */
export function getAllGeneratedStats(
  params: GetAllGeneratedStatsParams
): ToolResult {
  try {
    const yearFrom = params.yearFrom ?? GENERATED_STATS.coveragePeriod.from;
    const yearTo = params.yearTo ?? GENERATED_STATS.coveragePeriod.to;
 
    // Filter yearly stats by requested year range
    const filteredYearly = GENERATED_STATS.yearlyStats
      .filter((y) => y.year >= yearFrom && y.year <= yearTo)
      .map((y) => {
        if (params.includeMonthlyBreakdown) return y;
        // Omit monthly breakdown for compact output
        const { monthlyActivity: _monthly, ...rest } = y;
        void _monthly;
        return rest;
      });
 
    // Filter rankings by category if specified
    const filteredRankings = filterRankings(params, yearFrom, yearTo);
 
    // Include predictions only if requested, filtered to the requested year range
    const filteredPredictions =
      params.includePredictions
        ? GENERATED_STATS.predictions.filter((p) => p.year >= yearFrom && p.year <= yearTo)
        : [];
 
    const result = {
      generatedAt: GENERATED_STATS.generatedAt,
      coveragePeriod: GENERATED_STATS.coveragePeriod,
      requestedPeriod: { from: yearFrom, to: yearTo },
      methodologyVersion: GENERATED_STATS.methodologyVersion,
      dataSource: GENERATED_STATS.dataSource,
      totalYearsReturned: filteredYearly.length,
      yearlyStats: filteredYearly,
      ...(filteredRankings.length > 0 && {
        categoryRankings: filteredRankings,
      }),
      ...(filteredPredictions.length > 0 && {
        predictions: filteredPredictions,
      }),
      analysisSummary: {
        ...GENERATED_STATS.analysisSummary,
        coverageNote:
          yearFrom > GENERATED_STATS.coveragePeriod.from ||
          yearTo < GENERATED_STATS.coveragePeriod.to
            ? `This summary reflects the full ${String(GENERATED_STATS.coveragePeriod.from)}-${String(GENERATED_STATS.coveragePeriod.to)} dataset; filtered results cover ${String(yearFrom)}-${String(yearTo)} only.`
            : `Covers the complete ${String(GENERATED_STATS.coveragePeriod.from)}-${String(GENERATED_STATS.coveragePeriod.to)} dataset.`,
      },
      confidenceLevel: 'HIGH' as const,
      methodology:
        'Precomputed statistics from European Parliament Open Data Portal. ' +
        'Rankings use ordinal ranking with percentile scores. ' +
        'Predictions use average-based extrapolation from 2021-2025 with parliamentary term cycle adjustments. ' +
        'Data refreshed weekly by agentic workflow.',
      sourceAttribution:
        'European Parliament Open Data Portal — data.europarl.europa.eu',
    };
 
    return buildToolResponse(result);
  } catch (error) {
    return buildErrorResponse(
      error instanceof Error ? error : new Error(String(error)),
      'get_all_generated_stats'
    );
  }
}
 
export const getAllGeneratedStatsToolMetadata = {
  name: 'get_all_generated_stats',
  description:
    'Retrieve precomputed European Parliament activity statistics (2004-2025) with monthly breakdowns, ' +
    'category rankings, percentile scores, statistical analysis, political landscape history (group composition, ' +
    'fragmentation index, coalition dynamics), analytical commentary, and trend-based predictions for 2026-2030. ' +
    'Data covers parliamentary terms EP6-EP10 including plenary sessions, legislative acts, roll-call votes, ' +
    'committee meetings, parliamentary questions, resolutions, speeches, adopted texts, procedures, events, ' +
    'documents, MEP turnover, and declarations. ' +
    'Static data refreshed weekly by agentic workflow — no live API calls.',
  inputSchema: {
    type: 'object' as const,
    properties: {
      yearFrom: {
        type: 'number',
        description: 'Start year for filtering (default: 2004)',
        minimum: 2004,
        maximum: 2030,
      },
      yearTo: {
        type: 'number',
        description: 'End year for filtering (default: 2025)',
        minimum: 2004,
        maximum: 2030,
      },
      category: {
        type: 'string',
        enum: [
          'all',
          'plenary_sessions',
          'legislative_acts',
          'roll_call_votes',
          'committee_meetings',
          'parliamentary_questions',
          'resolutions',
          'speeches',
          'adopted_texts',
          'political_groups',
          'procedures',
          'events',
          'documents',
          'mep_turnover',
          'declarations',
        ],
        description: 'Activity category to focus on (default: all)',
        default: 'all',
      },
      includePredictions: {
        type: 'boolean',
        description: 'Include trend-based predictions for 2026-2030 (default: true)',
        default: true,
      },
      includeMonthlyBreakdown: {
        type: 'boolean',
        description: 'Include month-by-month activity data (default: false)',
        default: false,
      },
      includeRankings: {
        type: 'boolean',
        description: 'Include percentile rankings and statistics (default: true)',
        default: true,
      },
    },
  },
};
 
export async function handleGetAllGeneratedStats(
  args: unknown
): Promise<ToolResult> {
  const params = GetAllGeneratedStatsSchema.parse(args);
  return Promise.resolve(getAllGeneratedStats(params));
}