All files / src index.ts

63.63% Statements 49/77
38.88% Branches 7/18
73.33% Functions 11/15
65.75% Lines 48/73

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                                                                                                                                                                                                                                                                                    3x                           3x                                           3x 1x           3x 2x   2x 2x       2x           2x     2x 2x             3x 1x           3x 3x   3x 3x   2x 2x 2x             3x 1x           3x 1x   1x 1x   1x 1x 1x                                     2x                                                           2x   2x 2x   1x 1x     1x 47x 1x 1x 1x     1x 1x 1x 1x 1x               2x     2x                                                                                
#!/usr/bin/env node
/**
 * European Parliament MCP Server
 * 
 * Model Context Protocol server providing access to European Parliament open data.
 * 
 * **Intelligence Perspective:** Serves as the primary OSINT platform for EU parliamentary
 * intelligence—enabling MEP profiling, voting pattern analysis, coalition detection,
 * legislative monitoring, and political risk assessment through structured MCP tools.
 * 
 * **Business Perspective:** Core product offering structured access to EU parliamentary
 * data via MCP protocol—targeting AI developers, civic tech platforms, journalists,
 * researchers, and enterprise government affairs teams.
 * 
 * **Marketing Perspective:** First MCP server for European Parliament data—positioned
 * at the intersection of AI/LLM tooling and democratic transparency, targeting the
 * growing MCP ecosystem and EU civic tech market.
 * 
 * @see https://data.europarl.europa.eu/
 * @see https://spec.modelcontextprotocol.io/
 */
 
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
  CallToolRequestSchema,
  ListToolsRequestSchema,
  ListPromptsRequestSchema,
  GetPromptRequestSchema,
  ListResourceTemplatesRequestSchema,
  ReadResourceRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import { fileURLToPath } from 'url';
import { resolve } from 'path';
import { realpathSync } from 'fs';
 
// ── Extracted modules ─────────────────────────────────────────────
import { getToolMetadataArray, dispatchToolCall } from './server/toolRegistry.js';
import { showHelp, showVersion, showHealth, parseCLIArgs } from './server/cli.js';
import { ToolError } from './tools/shared/errors.js';
import { handleToolError } from './tools/shared/errorHandler.js';
import { SERVER_NAME, SERVER_VERSION } from './config.js';
// MCP Prompts
import { getPromptMetadataArray, handleGetPrompt } from './prompts/index.js';
// MCP Resources
import { getResourceTemplateArray, handleReadResource } from './resources/index.js';
/** Re-export all public types, branded identifiers, and error classes */
export type * from './types/index.js';
/** Re-export all runtime type guards, factory functions, and error utilities */
export * from './types/index.js';
/** Re-export tool registry for consumers */
export { getToolMetadataArray } from './server/toolRegistry.js';
/** Re-export CLI utilities */
export { sanitizeUrl } from './server/cli.js';
/** Re-export server types */
export type { ToolHandler, ToolMetadata, ToolCategory, CLIOptions, ToolResult } from './server/types.js';
/** Re-export DI token registry */
export type { DIToken } from './di/tokens.js';
/** Re-export DI container and default factory for consumers */
export { createDefaultContainer } from './di/container.js';
/** Re-export metric names and key type for consumers using the metrics service */
export { MetricName } from './services/MetricsService.js';
export type { MetricKey } from './services/MetricsService.js';
/** Re-export performance thresholds, timeout defaults, and related config types for consumers */
export { DEFAULT_PERFORMANCE_THRESHOLDS } from './utils/performance.js';
export type { PerformanceThresholds } from './utils/performance.js';
export { DEFAULT_TIMEOUTS } from './utils/timeout.js';
export type { TimeoutConfig } from './utils/timeout.js';
export type { RateLimiterConfig, RateLimiterStatus } from './utils/rateLimiter.js';
export type { AuditEvent, AuditLogEntry, LogLevel } from './utils/auditLogger.js';
/** Re-export audit sink public API for consumer-configurable pluggable sinks */
export type {
  AuditFilter,
  AuditLoggerOptions,
  AuditSink,
  AuthToken,
  FileAuditSinkOptions,
} from './utils/auditLogger.js';
export {
  DEFAULT_SENSITIVE_KEYS,
  FileAuditSink,
  MemoryAuditSink,
  RetentionPolicy,
  sanitizeParams,
  StderrAuditSink,
  StructuredJsonSink,
} from './utils/auditLogger.js';
/** Re-export prompt argument schemas for integration tests and client validation */
export {
  MepBriefingArgsSchema,
  CoalitionAnalysisArgsSchema,
  LegislativeTrackingArgsSchema,
  PoliticalGroupComparisonArgsSchema,
  CommitteeActivityArgsSchema,
  VotingPatternArgsSchema,
  CountryDelegationArgsSchema,
} from './prompts/index.js';
 
/** @internal Server name constant */
export { SERVER_NAME, SERVER_VERSION } from './config.js';
 
/**
 * Main MCP Server class for European Parliament data access
 * 
 * Implements the Model Context Protocol (MCP) to provide AI assistants,
 * IDEs, and other MCP clients with structured access to European Parliament
 * open data, including information about MEPs, plenary sessions, committees,
 * legislative documents, and parliamentary questions.
 * 
 * **Features:**
 * - MCP protocol implementation with tools, resources, and prompts
 * - Type-safe TypeScript implementation with strict mode
 * - GDPR-compliant data handling
 * - Rate limiting and security controls
 * - Comprehensive error handling
 * 
 * **Data Sources:**
 * - European Parliament Open Data Portal (https://data.europarl.europa.eu/)
 * - API v2: https://data.europarl.europa.eu/api/v2/
 * 
 * @example
 * ```typescript
 * const server = new EuropeanParliamentMCPServer();
 * await server.start();
 * ```
 * 
 * @see https://spec.modelcontextprotocol.io/ - MCP specification
 * @see https://data.europarl.europa.eu/ - EP Open Data Portal
 * @see https://github.com/Hack23/ISMS-PUBLIC - ISMS compliance policies
 */
export class EuropeanParliamentMCPServer {
  // Using Server for now until McpServer is available in the SDK version
  // eslint-disable-next-line @typescript-eslint/no-deprecated
  private readonly server: Server;
 
  constructor() {
    // Using Server for now until McpServer is available in the SDK version
    // eslint-disable-next-line @typescript-eslint/no-deprecated
    this.server = new Server(
      {
        name: SERVER_NAME,
        version: SERVER_VERSION,
      },
      {
        capabilities: {
          tools: {},
          resources: {},
          prompts: {},
        },
      }
    );
 
    this.setupHandlers();
  }
 
  /**
   * Set up MCP protocol handlers
   * 
   * Registers handlers for:
   * - Tool listing: Returns available tools with schemas
   * - Tool execution: Routes tool calls to appropriate handlers
   * 
   * **Security:**
   * - All inputs validated before processing
   * - Rate limiting applied per client
   * - GDPR compliance enforced
   * - Audit logging for all data access
   * 
   * @throws {Error} If handler registration fails
   * 
   * @internal
   */
  private setupHandlers(): void {
    // List available tools
    this.server.setRequestHandler(ListToolsRequestSchema, () => {
      return Promise.resolve({
        tools: getToolMetadataArray(),
      });
    });
 
    // Handle tool calls
    this.server.setRequestHandler(CallToolRequestSchema, async (request) => {
      const { name, arguments: args } = request.params;
 
      try {
        return await this.dispatchToolCall(name, args);
      } catch (error) {
        // Convert ToolError to an in-band MCP error response (isError: true)
        // so MCP clients receive a well-formed ToolResult instead of an RPC error.
        Iif (error instanceof ToolError) {
          // Log ToolError so tool failures are visible in server logs (consistent with non-ToolError path)
          console.error(`[ERROR] Tool ${error.toolName} failed:`, error);
          return handleToolError(error, error.toolName);
        }
        // Log error to stderr (stdout is used for MCP protocol)
        console.error(`[ERROR] Tool ${name} failed:`, error);
        
        // Re-throw with clean error message
        Eif (error instanceof Error) {
          throw error;
        }
        throw new Error(`Tool execution failed: ${String(error)}`);
      }
    });
 
    // List available prompts
    this.server.setRequestHandler(ListPromptsRequestSchema, () => {
      return Promise.resolve({
        prompts: getPromptMetadataArray(),
      });
    });
 
    // Handle prompt requests
    this.server.setRequestHandler(GetPromptRequestSchema, (request) => {
      const { name, arguments: args } = request.params;
 
      try {
        return Promise.resolve(handleGetPrompt(name, args));
      } catch (error) {
        console.error(`[ERROR] Prompt ${name} failed:`, error);
        Eif (error instanceof Error) {
          throw error;
        }
        throw new Error(`Prompt execution failed: ${String(error)}`);
      }
    });
 
    // List resource templates
    this.server.setRequestHandler(ListResourceTemplatesRequestSchema, () => {
      return Promise.resolve({
        resourceTemplates: getResourceTemplateArray(),
      });
    });
 
    // Handle resource reads
    this.server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
      const { uri } = request.params;
 
      try {
        return await handleReadResource(uri);
      } catch (error) {
        console.error(`[ERROR] Resource ${uri} failed:`, error);
        Eif (error instanceof Error) {
          throw error;
        }
        throw new Error(`Resource read failed: ${String(error)}`);
      }
    });
  }
 
  /**
   * Dispatch tool calls to appropriate handlers
   * 
   * @internal
   * @param name - Tool name
   * @param args - Tool arguments
   * @returns Tool execution result
   */
  private async dispatchToolCall(
    name: string,
    args: unknown
  ): Promise<{ content: { type: string; text: string }[] }> {
    return await dispatchToolCall(name, args);
  }
 
  /**
   * Start the MCP server
   * 
   * Initializes the server with stdio transport and begins listening for
   * MCP protocol messages. The server communicates via standard input/output
   * following the MCP specification.
   * 
   * **Lifecycle:**
   * 1. Create stdio transport
   * 2. Connect server to transport
   * 3. Log startup message to stderr
   * 4. Begin handling MCP requests
   * 
   * @returns Promise that resolves when server is started
   * 
   * @throws {Error} If server initialization fails
   * 
   * @example
   * ```typescript
   * const server = new EuropeanParliamentMCPServer();
   * await server.start();
   * // Server now listening on stdio
   * ```
   * 
   * @see https://spec.modelcontextprotocol.io/specification/architecture/
   */
  async start(): Promise<void> {
    const transport = new StdioServerTransport();
 
    try {
      await this.server.connect(transport);
    } catch (connectError) {
      console.error('[FATAL] Failed to connect MCP transport:', connectError);
      throw connectError;
    }
 
    const tools = getToolMetadataArray();
    const coreToolCount = tools.filter(t => t.category === 'core').length;
    const nonCoreToolCount = tools.length - coreToolCount;
    const prompts = getPromptMetadataArray();
    const resourceTemplates = getResourceTemplateArray();
 
    // Log to stderr (stdout is used for MCP protocol)
    console.error(`${SERVER_NAME} v${SERVER_VERSION} started`);
    console.error('Server ready to handle requests');
    console.error(`Available tools: ${String(tools.length)} (${String(coreToolCount)} core + ${String(nonCoreToolCount)} additional)`);
    console.error(`Available prompts: ${String(prompts.length)}`);
    console.error(`Available resource templates: ${String(resourceTemplates.length)}`);
  }
}
 
// Only execute CLI logic if this module is the entry point
// This prevents CLI behavior from interfering when imported as a library
// Use realpathSync to handle npm bin symlinks/shims correctly
const isMainModule =
  process.argv[1] !== undefined &&
  realpathSync(resolve(fileURLToPath(import.meta.url))) === realpathSync(resolve(process.argv[1]));
 
Iif (isMainModule) {
  // Parse command-line arguments using typed CLIOptions
  const cliArgs = process.argv.slice(2);
  const opts = parseCLIArgs(cliArgs);
 
  // Handle CLI commands
  if (opts.help === true) {
    showHelp();
    process.exit(0);
  }
 
  if (opts.version === true) {
    showVersion();
    process.exit(0);
  }
 
  if (opts.health === true) {
    showHealth();
    process.exit(0);
  }
 
  // Start the MCP server (default behavior)
  const server = new EuropeanParliamentMCPServer();
 
  // Shutdown signal handlers — exit immediately on SIGTERM / SIGINT
  // (stdio would otherwise keep the event loop alive in containers)
  function handleShutdownSignal(signal: string): void {
    console.error(`[${SERVER_NAME}] Received ${signal} — exiting`);
    // Explicitly exit so that stdio doesn't keep the event loop alive in containers
    process.exit(0);
  }
 
  process.once('SIGTERM', () => { handleShutdownSignal('SIGTERM'); });
  process.once('SIGINT', () => { handleShutdownSignal('SIGINT'); });
 
  server.start().catch((error: unknown) => {
    console.error('[FATAL] Server startup failed:', error);
    process.exit(1);
  });
}