All files / src/tools getCommitteeDocumentsFeed.ts

72.22% Statements 13/18
58.33% Branches 7/12
50% Functions 1/2
76.47% Lines 13/17

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                                                          10x 10x                             10x 10x 8x 1x 1x 1x                       7x   2x 1x                       1x                   4x            
/**
 * MCP Tool: get_committee_documents_feed
 *
 * Get recently updated committee documents from the feed.
 * This is a **fixed-window feed** — the EP API does not accept a timeframe
 * or start-date parameter. It returns updates from a server-defined default
 * window (typically one month).
 *
 * **EP API Endpoint:**
 * - `GET /committee-documents/feed`
 *
 * ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
 */
 
import { GetCommitteeDocumentsFeedSchema } from '../schemas/europeanParliament.js';
import { epClient } from '../clients/europeanParliamentClient.js';
import { ToolError } from './shared/errors.js';
import { isUpstream404, buildEmptyFeedResponse, isErrorInBody, buildFeedSuccessResponse, FIXED_WINDOW_FEED_INPUT_SCHEMA, extractUpstreamStatusCode } from './shared/feedUtils.js';
import { z } from 'zod';
import type { ToolResult } from './shared/types.js';
 
/**
 * Handles the get_committee_documents_feed MCP tool request.
 *
 * @param args - Raw tool arguments, validated against {@link GetCommitteeDocumentsFeedSchema}
 * @returns MCP tool result containing recently updated committee documents data
 * @security Input is validated with Zod before any API call.
 */
export async function handleGetCommitteeDocumentsFeed(args: unknown): Promise<ToolResult> {
  try {
    GetCommitteeDocumentsFeedSchema.parse(args);
  } catch (error: unknown) {
    if (error instanceof z.ZodError) {
      const fieldErrors = error.issues.map((e) => `${e.path.join('.')}: ${e.message}`).join('; ');
      throw new ToolError({
        toolName: 'get_committee_documents_feed',
        operation: 'validateInput',
        message: `Invalid parameters: ${fieldErrors}`,
        isRetryable: false,
        cause: error,
      });
    }
    throw error;
  }
 
  try {
    const result = await epClient.getCommitteeDocumentsFeed();
    if (isErrorInBody(result)) {
      const errorMessage = typeof result['error'] === 'string' ? result['error'] : 'Unknown upstream error';
      const statusCode = extractUpstreamStatusCode(errorMessage);
      return buildEmptyFeedResponse(
        'EP API returned an error-in-body response for get_committee_documents_feed — the upstream enrichment step may have failed.',
        {
          errorCode: 'ENRICHMENT_FAILED',
          retryable: true,
          upstream: {
            ...(statusCode !== undefined ? { statusCode } : {}),
            errorMessage,
          },
        },
      );
    }
    return buildFeedSuccessResponse(result);
  } catch (error: unknown) {
    if (isUpstream404(error)) {
      return buildEmptyFeedResponse(
        'EP API returned HTTP 404 for get_committee_documents_feed — no committee document feed entries are currently available for the upstream fixed window.',
        {
          errorCode: 'UPSTREAM_ERROR',
          retryable: true,
          upstream: {
            statusCode: 404,
            errorMessage: error instanceof Error ? error.message : 'Not Found',
          },
        },
      );
    }
    throw new ToolError({
      toolName: 'get_committee_documents_feed',
      operation: 'fetchData',
      message: 'Failed to retrieve committee documents feed',
      isRetryable: true,
      cause: error,
    });
  }
}
/** Tool metadata for get_committee_documents_feed */
export const getCommitteeDocumentsFeedToolMetadata = {
  name: 'get_committee_documents_feed',
  description:
    'Get recently updated committee documents from the EP Open Data Portal feed. This is a fixed-window feed — the upstream EP API always returns items updated within a server-defined default window (typically one month). For contract uniformity with sliding-window feed tools, the common feed parameters (timeframe, startDate, limit, offset) are accepted but informational-only — they are silently ignored. Data source: European Parliament Open Data Portal.',
  inputSchema: FIXED_WINDOW_FEED_INPUT_SCHEMA,
};