/**
* Custom error types for better error handling and debugging
*/
export class MCPError extends Error {
constructor(
message: string,
public readonly code: string,
public readonly details?: Record<string, unknown>
) {
super(message);
this.name = 'MCPError';
Error.captureStackTrace(this, this.constructor);
}
}
export class ValidationError extends MCPError {
constructor(message: string, details?: Record<string, unknown>) {
super(message, 'VALIDATION_ERROR', details);
this.name = 'ValidationError';
}
}
export class AuthenticationError extends MCPError {
constructor(message: string, details?: Record<string, unknown>) {
super(message, 'AUTHENTICATION_ERROR', details);
this.name = 'AuthenticationError';
}
}
export class APIError extends MCPError {
constructor(
message: string,
public readonly statusCode?: number,
details?: Record<string, unknown>
) {
super(message, 'API_ERROR', details);
this.name = 'APIError';
}
}
export class ConfigurationError extends MCPError {
constructor(message: string, details?: Record<string, unknown>) {
super(message, 'CONFIGURATION_ERROR', details);
this.name = 'ConfigurationError';
}
}
/**
* Format error for MCP response
*/
export function formatErrorResponse(error: unknown): {
message: string;
code?: string;
details?: Record<string, unknown>;
} {
if (error instanceof MCPError) {
return {
message: error.message,
code: error.code,
details: error.details,
};
}
if (error instanceof Error) {
return {
message: error.message,
code: 'UNKNOWN_ERROR',
};
}
return {
message: 'An unknown error occurred',
code: 'UNKNOWN_ERROR',
details: { error: String(error) },
};
}
/**
* Check if error is a specific type
*/
export function isValidationError(error: unknown): error is ValidationError {
return error instanceof ValidationError;
}
export function isAuthenticationError(error: unknown): error is AuthenticationError {
return error instanceof AuthenticationError;
}
export function isAPIError(error: unknown): error is APIError {
return error instanceof APIError;
}
export function isConfigurationError(error: unknown): error is ConfigurationError {
return error instanceof ConfigurationError;
}