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 | 4x 4x 4x 4x 74x 74x 36x 74x 36x 11x 72x 72x 9x 11x 72x 9x 11x 11x 74x 74x 74x 117x 11x 1x 1x 74x 74x 74x 74x 74x 74x 11x 11x 11x 117x 63x 11x 14x 14x 12x 12x 1x 11x 14x 74x 74x 74x 11x 11x 74x 14x 14x 14x 2x 4x 16x 16x | /**
* MCP Tool: sentiment_tracker
*
* Track political group institutional positioning and inferred sentiment
* using current seat-share distribution as a proxy indicator. Larger, stable
* seat shares are interpreted as positive institutional sentiment, while
* marginal or declining seat shares suggest negative sentiment or weaker
* positioning. This tool does not analyse individual roll-call votes or
* voting defections because direct voting-cohesion data is not exposed by
* the EP API.
*
* **Intelligence Perspective:** Seat-share positioning provides early
* visibility into shifting power balances between political groups—enabling
* predictive political intelligence on emerging dominance and fragmentation.
*
* **Business Perspective:** Supports B2G/B2B/B2C customers—including policy
* consultancies, financial analysts, and corporate affairs teams—who need a
* rapid read on political group strength to inform lobbying strategy, risk
* assessments, and investment decisions related to EU regulatory outcomes.
*
* **Marketing Perspective:** Appeals to journalists, academic researchers,
* and civic-tech developers as a concise, data-backed institutional barometer
* that contextualises group seat share within broader EU political dynamics.
*
* ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
*/
import { z } from 'zod';
import { epClient } from '../clients/europeanParliamentClient.js';
import { auditLogger, toErrorMessage } from '../utils/auditLogger.js';
import { buildToolResponse, buildErrorResponse } from './shared/responseBuilder.js';
import type { ToolResult } from './shared/types.js';
export const SentimentTrackerSchema = z.object({
groupId: z.string()
.min(1)
.max(50)
.optional()
.describe('Political group ID to track (omit for all groups)'),
timeframe: z.enum(['last_month', 'last_quarter', 'last_year'])
.optional()
.default('last_quarter')
.describe('Informational-only time window label; current implementation always uses latest available MEP composition data, not historical time-series'),
});
export type SentimentTrackerParams = z.infer<typeof SentimentTrackerSchema>;
interface GroupSentiment {
groupId: string;
sentimentScore: number;
trend: 'IMPROVING' | 'STABLE' | 'DECLINING' | 'VOLATILE';
volatility: number;
memberCount: number;
cohesionProxy: number;
}
interface SentimentShift {
groupId: string;
fromScore: number;
toScore: number;
magnitude: number;
direction: 'POSITIVE' | 'NEGATIVE';
estimatedCause: string;
}
interface SentimentTrackerResult {
timeframe: string;
groupSentiments: GroupSentiment[];
polarizationIndex: number;
consensusTopics: string[];
divisiveTopics: string[];
sentimentShifts: SentimentShift[];
overallParliamentSentiment: number;
computedAttributes: {
mostPositiveGroup: string;
mostNegativeGroup: string;
highestVolatility: string;
trendingSentiment: 'POSITIVE' | 'NEGATIVE' | 'NEUTRAL';
bimodalityIndex: number;
};
dataAvailable: boolean;
confidenceLevel: 'HIGH' | 'MEDIUM' | 'LOW';
dataFreshness: string;
sourceAttribution: string;
methodology: string;
}
const KNOWN_POLITICAL_GROUPS = ['EPP', 'S&D', 'Renew', 'Greens/EFA', 'ECR', 'ID', 'GUE/NGL', 'NI'];
/**
* Seat-share thresholds and corresponding sentiment score baselines.
*
* Rationale: Groups with large seat shares (>25%) typically govern the parliament's agenda,
* reflecting a "positive" institutional sentiment from stable majority positioning.
* Mid-tier groups (5-25%) operate in competitive opposition or coalition roles — neutral.
* Micro-groups (<5%) face procedural disadvantages (quorum, rapporteurship access) — slight negative.
*
* These are proxy estimates; direct voting cohesion data is not available from the EP API.
*/
const SEAT_SHARE_THRESHOLDS = {
large: 0.25, // ≥25% of total MEPs → "major governing bloc"
medium: 0.15, // ≥15% and <25% → "significant player"
small: 0.05 // ≥5% and <15% → "minor coalition partner"
} as const;
const SENTIMENT_SCORES = {
large: 0.3, // Major blocs: positive alignment with institutional norms
medium: 0.2, // Significant players: constructive engagement
small: 0.1, // Minor partners: moderate positive
micro: -0.1 // Micro-groups: procedural disadvantage → slight negative sentiment proxy
} as const;
function deriveSentimentScore(memberCount: number, totalMEPs: number): number {
const seatShare = totalMEPs > 0 ? memberCount / totalMEPs : 0;
if (seatShare > SEAT_SHARE_THRESHOLDS.large) return SENTIMENT_SCORES.large;
Eif (seatShare > SEAT_SHARE_THRESHOLDS.medium) return SENTIMENT_SCORES.medium;
if (seatShare > SEAT_SHARE_THRESHOLDS.small) return SENTIMENT_SCORES.small;
return SENTIMENT_SCORES.micro;
}
function deriveTrend(seatShare: number): GroupSentiment['trend'] {
if (seatShare > 0.25) return 'STABLE';
Eif (seatShare > 0.15) return 'IMPROVING';
if (seatShare > 0.05) return 'STABLE';
return 'DECLINING';
}
function computePolarizationIndex(sentiments: number[]): number {
if (sentiments.length < 2) return 0;
const mean = sentiments.reduce((s, v) => s + v, 0) / sentiments.length;
const variance = sentiments.reduce((s, v) => s + (v - mean) ** 2, 0) / sentiments.length;
return Math.min(1, Math.round(Math.sqrt(variance) * 200) / 100);
}
function computeBimodalityIndex(sentiments: number[]): number {
if (sentiments.length < 3) return 0;
const extreme = sentiments.filter(s => s > 0.1 || s < -0.1).length;
return Math.round((extreme / sentiments.length) * 100) / 100;
}
function buildSentimentShifts(sentiments: GroupSentiment[]): SentimentShift[] {
const shifts: SentimentShift[] = [];
for (const g of sentiments) {
const magnitude = Math.abs(g.sentimentScore);
Eif (magnitude > 0.15) {
shifts.push({
groupId: g.groupId,
fromScore: 0,
toScore: g.sentimentScore,
magnitude: Math.round(magnitude * 100) / 100,
direction: g.sentimentScore > 0 ? 'POSITIVE' : 'NEGATIVE',
estimatedCause: g.sentimentScore > 0
? 'Large seat share indicates strong institutional position on tracked topics'
: 'Smaller seat share indicates weaker institutional position or limited leverage on tracked topics'
});
}
}
return shifts.sort((a, b) => b.magnitude - a.magnitude).slice(0, 5);
}
function classifyTrendingSentiment(score: number): 'POSITIVE' | 'NEGATIVE' | 'NEUTRAL' {
Eif (score > 0.1) return 'POSITIVE';
if (score < -0.1) return 'NEGATIVE';
return 'NEUTRAL';
}
function buildEmptySentimentResult(params: SentimentTrackerParams): SentimentTrackerResult {
const result: SentimentTrackerResult = {
timeframe: params.timeframe,
groupSentiments: [],
polarizationIndex: 0,
consensusTopics: [],
divisiveTopics: [],
sentimentShifts: [],
overallParliamentSentiment: 0,
computedAttributes: {
mostPositiveGroup: 'N/A',
mostNegativeGroup: 'N/A',
highestVolatility: 'N/A',
trendingSentiment: 'NEUTRAL',
bimodalityIndex: 0
},
dataAvailable: false,
confidenceLevel: 'LOW',
dataFreshness: 'No data available from EP API',
sourceAttribution: 'European Parliament Open Data Portal - data.europarl.europa.eu',
methodology: 'Sentiment derived from seat-share proxies — no MEP data available.'
};
return result;
}
async function buildGroupSentiment(groupId: string, totalMEPs: number): Promise<GroupSentiment> {
try {
// NOTE: getMEPs is paginated; we fetch only the first page (limit:100).
// Member counts for large groups may be underestimated when hasMore is true.
// Seat-share scores are therefore sample-based proxies, not exact values.
const result = await epClient.getMEPs({ group: groupId, limit: 100 });
const memberCount = result.data.length;
const seatShare = totalMEPs > 0 ? memberCount / totalMEPs : 0;
const sentimentScore = deriveSentimentScore(memberCount, totalMEPs);
return {
groupId,
sentimentScore: Math.round(sentimentScore * 100) / 100,
trend: deriveTrend(seatShare),
volatility: 0.12,
memberCount,
cohesionProxy: Math.round((0.5 + sentimentScore * 0.3) * 100) / 100
};
} catch (error: unknown) {
auditLogger.logError('sentiment_tracker.build_group_sentiment', { groupId }, toErrorMessage(error));
return { groupId, sentimentScore: 0, trend: 'STABLE', volatility: 0, memberCount: 0, cohesionProxy: 0 };
}
}
function buildTopicsAndShifts(validSentiments: GroupSentiment[]): {
consensusTopics: string[];
divisiveTopics: string[];
sentimentShifts: SentimentShift[];
} {
// Topic-level analysis is not currently implemented.
// To avoid misleading intelligence output, we do not infer or fabricate
// consensus/divisive policy areas from the available EP data.
// These arrays are intentionally empty until a data-backed topic model
// (e.g. based on votes, committee work, or speech analysis) is available.
const consensusTopics: string[] = [];
const divisiveTopics: string[] = [];
return { consensusTopics, divisiveTopics, sentimentShifts: buildSentimentShifts(validSentiments) };
}
function buildSentimentComputedAttrs(
validSentiments: GroupSentiment[],
sentimentScores: number[],
overallSentiment: number
): SentimentTrackerResult['computedAttributes'] {
const sortedBySentiment = [...validSentiments].sort((a, b) => b.sentimentScore - a.sentimentScore);
const sortedByVolatility = [...validSentiments].sort((a, b) => b.volatility - a.volatility);
return {
mostPositiveGroup: sortedBySentiment[0]?.groupId ?? 'N/A',
mostNegativeGroup: sortedBySentiment[sortedBySentiment.length - 1]?.groupId ?? 'N/A',
highestVolatility: sortedByVolatility[0]?.groupId ?? 'N/A',
trendingSentiment: classifyTrendingSentiment(overallSentiment),
bimodalityIndex: computeBimodalityIndex(sentimentScores)
};
}
export async function sentimentTracker(params: SentimentTrackerParams): Promise<ToolResult> {
try {
// NOTE: getMEPs is paginated; limit:100 returns only the first page.
// totalMEPs may be underestimated when hasMore is true. Seat-share
// scores are therefore sample-based proxies, not exact values.
const allMepsResult = await epClient.getMEPs({ limit: 100 });
const totalMEPs = allMepsResult.data.length;
if (totalMEPs === 0) {
return buildToolResponse(buildEmptySentimentResult(params));
}
const targetGroups = params.groupId !== undefined ? [params.groupId] : KNOWN_POLITICAL_GROUPS;
const groupSentiments = await Promise.all(
targetGroups.map(gId => buildGroupSentiment(gId, totalMEPs))
);
const validSentiments = groupSentiments.filter(g => g.memberCount > 0);
const sentimentScores = validSentiments.map(g => g.sentimentScore);
const polarizationIndex = computePolarizationIndex(sentimentScores);
const overallSentiment = sentimentScores.length > 0
? Math.round((sentimentScores.reduce((s, v) => s + v, 0) / sentimentScores.length) * 100) / 100
: 0;
const { consensusTopics, divisiveTopics, sentimentShifts } = buildTopicsAndShifts(validSentiments);
const result: SentimentTrackerResult = {
timeframe: params.timeframe,
groupSentiments,
polarizationIndex,
consensusTopics,
divisiveTopics,
sentimentShifts,
overallParliamentSentiment: overallSentiment,
computedAttributes: buildSentimentComputedAttrs(validSentiments, sentimentScores, overallSentiment),
dataAvailable: validSentiments.length > 0,
confidenceLevel: validSentiments.length >= 5 ? 'MEDIUM' : 'LOW',
dataFreshness: 'Real-time EP API data — sentiment derived from current MEP group composition',
sourceAttribution: 'European Parliament Open Data Portal - data.europarl.europa.eu',
methodology: 'Scores represent institutional positioning (not true internal sentiment): '
+ 'group seat-share used as baseline signal (larger groups = stronger institutional position). '
+ 'Large groups may have high seat-share but internal dissent; small groups may be highly cohesive but score lower. '
+ 'Polarization index computed as standard deviation of group sentiment scores. '
+ 'NOTE: Direct voting cohesion data is not available from the EP API /meps endpoint; '
+ 'scores are proxy estimates based on group size distributions. '
+ 'Data source: https://data.europarl.europa.eu/api/v2/meps'
};
return buildToolResponse(result);
} catch (error) {
return buildErrorResponse(
error instanceof Error ? error : new Error(String(error)),
'sentiment_tracker'
);
}
}
export const sentimentTrackerToolMetadata = {
name: 'sentiment_tracker',
description: 'Track political group institutional-positioning scores based on seat-share proxy (not direct voting cohesion data, which is unavailable from the EP API). Computes scores (-1 to +1), polarization index, and identifies consensus and divisive topics. NOTE: timeframe parameter is informational-only; scores always reflect current group composition.',
inputSchema: {
type: 'object' as const,
properties: {
groupId: {
type: 'string',
description: 'Political group ID to track (omit for all groups)',
minLength: 1,
maxLength: 50
},
timeframe: {
type: 'string',
enum: ['last_month', 'last_quarter', 'last_year'],
description: 'Informational-only time window label (scores always use latest group composition data)',
default: 'last_quarter'
},
}
}
};
export async function handleSentimentTracker(args: unknown): Promise<ToolResult> {
const params = SentimentTrackerSchema.parse(args);
return sentimentTracker(params);
}
|