- Standardized error handling across tool implementations - Improved return type consistency for tool execution results - Simplified configuration parsing and type definitions - Enhanced type safety for various configuration schemas - Cleaned up and normalized tool response structures - Updated SSE and event subscription tool implementations
68 lines
1.5 KiB
TypeScript
68 lines
1.5 KiB
TypeScript
import { EventEmitter } from "events";
|
|
|
|
export class LiteMCP extends EventEmitter {
|
|
private static instance: LiteMCP;
|
|
private constructor() {
|
|
super();
|
|
// Initialize with default configuration
|
|
this.configure({});
|
|
}
|
|
|
|
public static getInstance(): LiteMCP {
|
|
if (!LiteMCP.instance) {
|
|
LiteMCP.instance = new LiteMCP();
|
|
}
|
|
return LiteMCP.instance;
|
|
}
|
|
|
|
public configure(config: Record<string, any>): void {
|
|
// Store configuration
|
|
this.config = {
|
|
...this.defaultConfig,
|
|
...config,
|
|
};
|
|
}
|
|
|
|
private config: Record<string, any> = {};
|
|
private defaultConfig = {
|
|
maxRetries: 3,
|
|
retryDelay: 1000,
|
|
timeout: 5000,
|
|
};
|
|
|
|
public async execute(
|
|
command: string,
|
|
params: Record<string, any> = {},
|
|
): Promise<any> {
|
|
try {
|
|
// Emit command execution event
|
|
this.emit("command", { command, params });
|
|
|
|
// Execute command logic here
|
|
const result = await this.processCommand(command, params);
|
|
|
|
// Emit success event
|
|
this.emit("success", { command, params, result });
|
|
|
|
return result;
|
|
} catch (error) {
|
|
// Emit error event
|
|
this.emit("error", { command, params, error });
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
private async processCommand(
|
|
command: string,
|
|
params: Record<string, any>,
|
|
): Promise<any> {
|
|
// Command processing logic
|
|
return { command, params, status: "processed" };
|
|
}
|
|
|
|
public async shutdown(): Promise<void> {
|
|
// Cleanup logic
|
|
this.removeAllListeners();
|
|
}
|
|
}
|