refactor: optimize configuration and tool implementations
- 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
This commit is contained in:
@@ -1,61 +1,67 @@
|
||||
import { EventEmitter } from 'events';
|
||||
import { EventEmitter } from "events";
|
||||
|
||||
export class LiteMCP extends EventEmitter {
|
||||
private static instance: LiteMCP;
|
||||
private constructor() {
|
||||
super();
|
||||
// Initialize with default configuration
|
||||
this.configure({});
|
||||
}
|
||||
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 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 configure(config: Record<string, any>): void {
|
||||
// Store configuration
|
||||
this.config = {
|
||||
...this.defaultConfig,
|
||||
...config,
|
||||
};
|
||||
}
|
||||
|
||||
public async execute(command: string, params: Record<string, any> = {}): Promise<any> {
|
||||
try {
|
||||
// Emit command execution event
|
||||
this.emit('command', { command, params });
|
||||
private config: Record<string, any> = {};
|
||||
private defaultConfig = {
|
||||
maxRetries: 3,
|
||||
retryDelay: 1000,
|
||||
timeout: 5000,
|
||||
};
|
||||
|
||||
// Execute command logic here
|
||||
const result = await this.processCommand(command, params);
|
||||
public async execute(
|
||||
command: string,
|
||||
params: Record<string, any> = {},
|
||||
): Promise<any> {
|
||||
try {
|
||||
// Emit command execution event
|
||||
this.emit("command", { command, params });
|
||||
|
||||
// Emit success event
|
||||
this.emit('success', { command, params, result });
|
||||
// Execute command logic here
|
||||
const result = await this.processCommand(command, params);
|
||||
|
||||
return result;
|
||||
} catch (error) {
|
||||
// Emit error event
|
||||
this.emit('error', { command, params, error });
|
||||
throw error;
|
||||
}
|
||||
// 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' };
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
public async shutdown(): Promise<void> {
|
||||
// Cleanup logic
|
||||
this.removeAllListeners();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user