elasticsearch-mcp
Version:
Secure MCP server for Elasticsearch integration with comprehensive tools and Elastic Cloud support
166 lines • 5.6 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ErrorHandler = exports.ElasticsearchError = exports.RateLimitError = exports.NotFoundError = exports.AuthenticationError = exports.ConnectionError = exports.ValidationError = exports.ElasticMCPError = void 0;
class ElasticMCPError extends Error {
code;
statusCode;
context;
constructor(message, code, statusCode = 500, context) {
super(message);
this.name = 'ElasticMCPError';
this.code = code;
this.statusCode = statusCode;
this.context = context;
Error.captureStackTrace(this, ElasticMCPError);
}
}
exports.ElasticMCPError = ElasticMCPError;
class ValidationError extends ElasticMCPError {
constructor(message, context) {
super(message, 'VALIDATION_ERROR', 400, context);
this.name = 'ValidationError';
}
}
exports.ValidationError = ValidationError;
class ConnectionError extends ElasticMCPError {
constructor(message, context) {
super(message, 'CONNECTION_ERROR', 503, context);
this.name = 'ConnectionError';
}
}
exports.ConnectionError = ConnectionError;
class AuthenticationError extends ElasticMCPError {
constructor(message, context) {
super(message, 'AUTHENTICATION_ERROR', 401, context);
this.name = 'AuthenticationError';
}
}
exports.AuthenticationError = AuthenticationError;
class NotFoundError extends ElasticMCPError {
constructor(message, context) {
super(message, 'NOT_FOUND', 404, context);
this.name = 'NotFoundError';
}
}
exports.NotFoundError = NotFoundError;
class RateLimitError extends ElasticMCPError {
constructor(message, context) {
super(message, 'RATE_LIMIT_EXCEEDED', 429, context);
this.name = 'RateLimitError';
}
}
exports.RateLimitError = RateLimitError;
class ElasticsearchError extends ElasticMCPError {
constructor(message, originalError, context) {
super(message, 'ELASTICSEARCH_ERROR', 500, {
...context,
originalError: originalError ? {
name: originalError.name,
message: originalError.message,
} : undefined,
});
this.name = 'ElasticsearchError';
}
}
exports.ElasticsearchError = ElasticsearchError;
class ErrorHandler {
logger;
constructor(logger) {
this.logger = logger.child({ component: 'error-handler' });
}
handleError(error, requestId) {
if (error instanceof ElasticMCPError) {
this.logger.warn('Handled error occurred', {
code: error.code,
message: error.message,
statusCode: error.statusCode,
context: error.context,
requestId,
});
return {
error: {
code: error.code,
message: error.message,
statusCode: error.statusCode,
context: error.context,
timestamp: new Date().toISOString(),
requestId,
},
};
}
if (error instanceof Error) {
this.logger.error('Unhandled error occurred', {
name: error.name,
message: error.message,
stack: error.stack,
requestId,
}, error);
return {
error: {
code: 'INTERNAL_ERROR',
message: 'An unexpected error occurred',
statusCode: 500,
context: undefined,
timestamp: new Date().toISOString(),
requestId,
},
};
}
this.logger.error('Unknown error occurred', {
error: String(error),
requestId,
});
return {
error: {
code: 'UNKNOWN_ERROR',
message: 'An unknown error occurred',
statusCode: 500,
context: undefined,
timestamp: new Date().toISOString(),
requestId,
},
};
}
isRetryableError(error) {
if (error instanceof ConnectionError) {
return true;
}
if (error instanceof ElasticsearchError) {
return true;
}
if (error instanceof Error) {
const message = error.message.toLowerCase();
return (message.includes('timeout') ||
message.includes('connection') ||
message.includes('network') ||
message.includes('503') ||
message.includes('502') ||
message.includes('504'));
}
return false;
}
sanitizeError(error) {
const sanitized = { ...error };
if (sanitized.error.context) {
const context = { ...sanitized.error.context };
const sensitiveKeys = [
'password',
'apiKey',
'token',
'secret',
'auth',
'authorization',
'credential',
];
for (const key of Object.keys(context)) {
if (sensitiveKeys.some(sensitive => key.toLowerCase().includes(sensitive))) {
context[key] = '***';
}
}
sanitized.error.context = context;
}
return sanitized;
}
}
exports.ErrorHandler = ErrorHandler;
//# sourceMappingURL=handlers.js.map