All files / src/tools/shared responseBuilder.ts

100% Statements 11/11
100% Branches 8/8
100% Functions 2/2
100% Lines 11/11

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                                  624x                                               43x     43x 41x 2x 1x   1x     43x               43x 11x     43x          
/**
 * Shared response builder utilities for MCP tool handlers.
 *
 * ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
 */
 
import type { ToolResult } from './types.js';
import { classifyError } from './errorClassifier.js';
import type { ErrorClassification } from './errorClassifier.js';
 
/**
 * Build a standard success response wrapping data as formatted JSON text.
 *
 * @param data - Data payload to serialize
 * @returns MCP-compliant ToolResult
 */
export function buildToolResponse(data: unknown): ToolResult {
  return {
    content: [{ type: 'text', text: JSON.stringify(data, null, 2) }]
  };
}
 
/**
 * Build an error response from an error value or message string.
 * Never exposes raw stack traces to MCP clients.
 *
 * When no explicit classification is provided, the error is automatically
 * classified via {@link classifyError} so all error responses — whether
 * routed through `handleToolError` or built directly by tool handlers —
 * include consistent `errorCode`, `errorCategory`, and `retryable` metadata.
 *
 * @param error - Error instance or message string
 * @param toolName - Name of the tool that produced the error
 * @param classification - Optional pre-computed classification (auto-classified if omitted)
 * @returns MCP-compliant ToolResult with isError flag set
 */
export function buildErrorResponse(
  error: unknown,
  toolName: string,
  classification?: ErrorClassification
): ToolResult {
  const cls = classification ?? classifyError(error);
 
  let message: string;
  if (error instanceof Error) {
    message = error.message;
  } else if (typeof error === 'string') {
    message = error;
  } else {
    message = 'Unknown error occurred';
  }
 
  const payload: Record<string, unknown> = {
    error: message,
    toolName,
    errorType: cls.errorCategory,
    retryable: cls.retryable,
    errorCode: cls.errorCode,
    errorCategory: cls.errorCategory,
  };
  if (cls.httpStatus !== undefined) {
    payload['httpStatus'] = cls.httpStatus;
  }
 
  return {
    content: [{ type: 'text', text: JSON.stringify(payload, null, 2) }],
    isError: true
  };
}