All files / src/tools getServerHealth.ts

85% Statements 17/20
72.72% Branches 8/11
100% Functions 3/3
85% Lines 17/20

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                                                        4x             18x   1x   14x     3x                                       20x 20x   2x 2x   2x 2x     2x                     18x 18x   18x                           18x       4x                          
/**
 * MCP Tool: get_server_health
 *
 * Returns server health status and per-feed availability diagnostics.
 * Does NOT make upstream API calls — reports cached status from recent
 * feed tool invocations.
 *
 * **Intelligence Perspective:** Operational awareness of data source
 * availability is critical for reliable intelligence products.
 *
 * **Business Perspective:** Provides dashboard-ready health metrics
 * and enables clients to adapt data collection strategies based on
 * current feed availability.
 *
 * ISMS Policy: MO-001 (Monitoring and Alerting), PE-001 (Performance Standards)
 *
 * @module tools/getServerHealth
 */
 
import { z } from 'zod';
import { SERVER_VERSION } from '../config.js';
import { feedHealthTracker } from '../services/FeedHealthTracker.js';
import type { AvailabilityLevel } from '../services/FeedHealthTracker.js';
import { buildToolResponse } from './shared/responseBuilder.js';
import { ToolError } from './shared/errors.js';
import type { ToolResult } from './shared/types.js';
 
/** Zod schema for the (empty) input of this tool. */
export const GetServerHealthSchema = z.object({});
 
/**
 * Derive the overall server status from the feed availability level.
 * Cyclomatic complexity: 3
 */
function deriveServerStatus(level: AvailabilityLevel): 'healthy' | 'degraded' | 'unhealthy' {
  switch (level) {
    case 'Full':
      return 'healthy';
    case 'Unavailable':
      return 'unhealthy';
    case 'Degraded':
    case 'Sparse':
      return 'degraded';
    default: {
      const exhaustiveCheck: never = level;
      throw new Error(`Unhandled availability level: ${String(exhaustiveCheck)}`);
    }
  }
}
 
/**
 * Handles the get_server_health MCP tool request.
 *
 * Returns a structured health snapshot including:
 * - Server version, uptime, and overall status
 * - Per-feed health status (ok / error / unknown)
 * - Aggregate availability level (Full / Degraded / Sparse / Unavailable)
 *
 * @param args - Validated against empty-object schema (no parameters accepted)
 * @returns MCP tool result with JSON health payload
 */
export async function handleGetServerHealth(args: unknown): Promise<ToolResult> {
  try {
    GetServerHealthSchema.parse(args ?? {});
  } catch (error: unknown) {
    Eif (error instanceof z.ZodError) {
      const fieldErrors = error.issues
        .map((e) => {
          const path = e.path.join('.');
          return path ? `${path}: ${e.message}` : e.message;
        })
        .join('; ');
      throw new ToolError({
        toolName: 'get_server_health',
        operation: 'validateInput',
        message: `Invalid parameters: ${fieldErrors}`,
        isRetryable: false,
        cause: error,
      });
    }
    throw error;
  }
 
  const feeds = feedHealthTracker.getAllStatuses();
  const availability = feedHealthTracker.getAvailability();
 
  const result = {
    server: {
      version: SERVER_VERSION,
      uptime_seconds: feedHealthTracker.getUptimeSeconds(),
      status: deriveServerStatus(availability.level),
    },
    feeds,
    availability: {
      operational_feeds: availability.operationalFeeds,
      total_feeds: availability.totalFeeds,
      level: availability.level,
    },
  };
 
  return await Promise.resolve(buildToolResponse(result));
}
 
/** Tool metadata for MCP registration. */
export const getServerHealthToolMetadata = {
  name: 'get_server_health',
  description:
    'Check server health and feed availability status. Returns server version, uptime, ' +
    'per-feed health status (ok/error/unknown), and overall availability level ' +
    '(Full/Degraded/Sparse/Unavailable). Does not make upstream API calls — reports ' +
    'cached status from recent tool invocations. Use this to check which feeds are ' +
    'healthy before making data requests and to adapt data collection strategy.',
  inputSchema: {
    type: 'object' as const,
    properties: {},
  },
};