UNPKG

@toolprint/hypertool-mcp

Version:

HyperTool MCP proxy server for routing requests between clients and multiple underlying MCP servers

274 lines • 7.36 kB
/** * Custom error classes */ /** * Base error class for all application errors */ export class MetaMCPError extends Error { code; category; isRetryable; timestamp; context; constructor(message, code, category, isRetryable = false, context) { super(message); this.name = this.constructor.name; this.code = code; this.category = category; this.isRetryable = isRetryable; this.timestamp = new Date(); this.context = context; // Maintains proper stack trace for where error was thrown if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } } /** * Get error details as object */ toJSON() { return { name: this.name, message: this.message, code: this.code, category: this.category, isRetryable: this.isRetryable, timestamp: this.timestamp.toISOString(), context: this.context, stack: this.stack, }; } /** * Get user-friendly error message */ getUserMessage() { return this.message; } } /** * Configuration-related errors */ export class ConfigurationError extends MetaMCPError { constructor(message, context) { super(message, "CONFIG_ERROR", "configuration", false, context); } getUserMessage() { return `Configuration issue: ${this.message}`; } } /** * Connection-related errors */ export class ConnectionError extends MetaMCPError { serverName; constructor(message, serverName, isRetryable = true, context) { super(message, "CONNECTION_ERROR", "connection", isRetryable, { ...context, serverName, }); this.serverName = serverName; } getUserMessage() { if (this.serverName) { return `Cannot connect to server '${this.serverName}': ${this.message}`; } return `Connection error: ${this.message}`; } } /** * Server unavailable errors */ export class ServerUnavailableError extends MetaMCPError { serverName; constructor(serverName, reason, context) { const message = reason ? `Server '${serverName}' is unavailable: ${reason}` : `Server '${serverName}' is unavailable`; super(message, "SERVER_UNAVAILABLE", "availability", true, { ...context, serverName, reason, }); this.serverName = serverName; } getUserMessage() { return `Server '${this.serverName}' is currently unavailable. Please try again later.`; } } /** * Tool-related errors */ export class ToolError extends MetaMCPError { toolName; serverName; constructor(message, toolName, serverName, context) { super(message, "TOOL_ERROR", "tool", false, { ...context, toolName, serverName, }); this.toolName = toolName; this.serverName = serverName; } getUserMessage() { return `Tool '${this.toolName}' error: ${this.message}`; } } /** * Tool not found errors */ export class ToolNotFoundError extends MetaMCPError { toolName; constructor(toolName, context) { super(`Tool '${toolName}' not found`, "TOOL_NOT_FOUND", "tool", false, { ...context, toolName, }); this.toolName = toolName; } getUserMessage() { return `Tool '${this.toolName}' is not available. Check your configuration or try a different tool.`; } } /** * Validation errors */ export class ValidationError extends MetaMCPError { field; value; constructor(message, field, value, context) { super(message, "VALIDATION_ERROR", "validation", false, { ...context, field, value, }); this.field = field; this.value = value; } getUserMessage() { if (this.field) { return `Invalid ${this.field}: ${this.message}`; } return `Validation error: ${this.message}`; } } /** * Request routing errors */ export class RoutingError extends MetaMCPError { toolName; serverName; constructor(message, toolName, serverName, context) { super(message, "ROUTING_ERROR", "routing", false, { ...context, toolName, serverName, }); this.toolName = toolName; this.serverName = serverName; } getUserMessage() { return `Request routing failed: ${this.message}`; } } /** * Health check errors */ export class HealthCheckError extends MetaMCPError { serverName; constructor(message, serverName, context) { super(message, "HEALTH_CHECK_ERROR", "health", true, { ...context, serverName, }); this.serverName = serverName; } getUserMessage() { return `Health check failed for server '${this.serverName}'`; } } /** * Timeout errors */ export class TimeoutError extends MetaMCPError { operation; timeoutMs; constructor(operation, timeoutMs, context) { super(`Operation '${operation}' timed out after ${timeoutMs}ms`, "TIMEOUT_ERROR", "timeout", true, { ...context, operation, timeoutMs }); this.operation = operation; this.timeoutMs = timeoutMs; } getUserMessage() { return `Operation timed out. Please try again.`; } } /** * Discovery errors */ export class DiscoveryError extends MetaMCPError { serverName; constructor(message, serverName, context) { super(message, "DISCOVERY_ERROR", "discovery", true, { ...context, serverName, }); this.serverName = serverName; } getUserMessage() { return `Tool discovery failed: ${this.message}`; } } /** * Error codes for easy reference */ export const ERROR_CODES = { CONFIG_ERROR: "CONFIG_ERROR", CONNECTION_ERROR: "CONNECTION_ERROR", SERVER_UNAVAILABLE: "SERVER_UNAVAILABLE", TOOL_ERROR: "TOOL_ERROR", TOOL_NOT_FOUND: "TOOL_NOT_FOUND", VALIDATION_ERROR: "VALIDATION_ERROR", ROUTING_ERROR: "ROUTING_ERROR", HEALTH_CHECK_ERROR: "HEALTH_CHECK_ERROR", TIMEOUT_ERROR: "TIMEOUT_ERROR", DISCOVERY_ERROR: "DISCOVERY_ERROR", }; /** * Error categories for filtering and handling */ export const ERROR_CATEGORIES = { CONFIGURATION: "configuration", CONNECTION: "connection", AVAILABILITY: "availability", TOOL: "tool", VALIDATION: "validation", ROUTING: "routing", HEALTH: "health", TIMEOUT: "timeout", DISCOVERY: "discovery", }; /** * Utility function to check if error is retryable */ export function isRetryableError(error) { return error instanceof MetaMCPError && error.isRetryable; } /** * Utility function to extract error code */ export function getErrorCode(error) { if (error instanceof MetaMCPError) { return error.code; } return "UNKNOWN_ERROR"; } /** * Utility function to get user-friendly message */ export function getUserFriendlyMessage(error) { if (error instanceof MetaMCPError) { return error.getUserMessage(); } return "An unexpected error occurred. Please try again."; } //# sourceMappingURL=index.js.map