All files / src/tools/shared responseBuilder.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 2/2
100% Lines 7/7

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                              405x                             18x 16x 2x 1x   1x   18x          
/**
 * Shared response builder utilities for MCP tool handlers.
 *
 * ISMS Policy: SC-002 (Input Validation), AC-003 (Least Privilege)
 */
 
import type { ToolResult } from './types.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.
 *
 * @param error - Error instance or message string
 * @param toolName - Name of the tool that produced the error
 * @returns MCP-compliant ToolResult with isError flag set
 */
export function buildErrorResponse(error: unknown, toolName: string): ToolResult {
  let message: string;
  if (error instanceof Error) {
    message = error.message;
  } else if (typeof error === 'string') {
    message = error;
  } else {
    message = 'Unknown error occurred';
  }
  return {
    content: [{ type: 'text', text: JSON.stringify({ error: message, toolName }, null, 2) }],
    isError: true
  };
}