marketing-post-generator-mcp
Version:
A powerful MCP server for AI-powered marketing blog post generation with Claude integration
90 lines (89 loc) • 3.19 kB
JavaScript
import { createLogger } from '../../utils/logger.js';
/**
* Factory for creating and managing search adapter instances.
* Provides centralized registration and creation of search adapters.
*/
export class SearchAdapterFactory {
static adapters = new Map();
static logger = createLogger({
level: 'info',
format: 'simple',
});
/**
* Register a new adapter implementation
* @param name Name/identifier for the adapter
* @param adapterClass Class constructor for the adapter
*/
static registerAdapter(name, adapterClass) {
const normalizedName = name.toLowerCase();
if (this.adapters.has(normalizedName)) {
this.logger.warn(`Adapter '${name}' is already registered, overwriting`);
}
this.adapters.set(normalizedName, adapterClass);
this.logger.info(`Search adapter registered: ${name}`);
}
/**
* Create an instance of a specific adapter
* @param name Name of the adapter to create
* @returns New instance of the specified adapter
* @throws Error if adapter is not found
*/
static createAdapter(name) {
const normalizedName = name.toLowerCase();
const AdapterClass = this.adapters.get(normalizedName);
if (!AdapterClass) {
const availableAdapters = Array.from(this.adapters.keys()).join(', ');
throw new Error(`Search adapter '${name}' not found. Available adapters: ${availableAdapters}`);
}
try {
const adapter = new AdapterClass();
this.logger.debug(`Created search adapter instance: ${name}`);
return adapter;
}
catch (error) {
this.logger.error(`Failed to create search adapter '${name}'`, {
error: error instanceof Error ? error.message : String(error),
});
throw new Error(`Failed to create search adapter '${name}': ${error}`);
}
}
/**
* List all available adapters
* @returns Array of registered adapter names
*/
static getAvailableAdapters() {
return Array.from(this.adapters.keys());
}
/**
* Check if an adapter is registered
* @param name Name of the adapter to check
* @returns True if the adapter is registered
*/
static hasAdapter(name) {
return this.adapters.has(name.toLowerCase());
}
/**
* Unregister an adapter
* @param name Name of the adapter to unregister
* @returns True if the adapter was found and removed
*/
static unregisterAdapter(name) {
const normalizedName = name.toLowerCase();
const removed = this.adapters.delete(normalizedName);
if (removed) {
this.logger.info(`Search adapter unregistered: ${name}`);
}
else {
this.logger.warn(`Attempted to unregister non-existent adapter: ${name}`);
}
return removed;
}
/**
* Clear all registered adapters
*/
static clearAdapters() {
const count = this.adapters.size;
this.adapters.clear();
this.logger.info(`Cleared ${count} registered search adapters`);
}
}