UNPKG

@turbot/guardrails-mcp

Version:

MCP server for interacting with Turbot Guardrails.

73 lines (72 loc) 2.46 kB
import { formatJson } from './jsonFormatter.mjs'; /** * Format a response with content array wrapper * @param text The text to wrap in a content array * @param isError Whether this is an error response * @returns Formatted response object */ export function formatToolResponse(text, isError = false) { return { content: [ { type: 'text', text } ], ...(isError && { isError }) }; } /** * Format an error response with content array wrapper * @param errorMessage The error message to wrap * @returns Formatted error response object */ export function errorResponse(errorMessage) { return formatToolResponse(errorMessage, true); } /** * Format a JSON response with content array wrapper * @param data The data to stringify and wrap in a content array * @param isError Whether this is an error response * @returns Formatted response object */ export function formatJsonToolResponse(data, isError = false) { return formatToolResponse(formatJson(data), isError); } /** * Format a common GraphQL error into a user-friendly message * @param error The error to format * @param id The ID that was used in the query * @returns Formatted error message */ export function formatGraphQLError(error, id) { if (error instanceof Error) { if (error.message.includes('Not Found')) { return `'${id}' not found. Please verify the value is correct and try again.`; } else if (error.message.includes('Invalid input')) { return `'${id}' has an invalid format. Please provide a valid ID or URI.`; } return `${error.name}: ${error.message}`; } return String(error); } /** * Format a GraphQL result, logging and surfacing errors if present. * @param result The GraphQL result object (with optional errors array) * @param logger The logger to use for error logging * @returns Formatted response object, with isError=true if errors are present */ export function formatGraphQLResultWithErrors(result, logger) { if (result.errors?.length) { result.errors.forEach((error) => { logger.error({ error: error.message, path: error.path, locations: error.locations }, "GraphQL execution error"); }); return formatJsonToolResponse(result, true); } return formatJsonToolResponse(result); }