/** * Request Batcher - Batches multiple tool calls for efficiency * * Provides intelligent batching of MCP tool calls to reduce overhead * and improve throughput. Supports automatic flushing based on: * - Maximum batch size * - Maximum wait time * - Manual flush triggers */ import { EventEmitter } from "events"; import type { BatchConfig, BatchExecutor } from "../../types/index.js"; /** * Request Batcher - Efficient batch processing for MCP tool calls * * @example * ```typescript * const batcher = new RequestBatcher({ * maxBatchSize: 10, * maxWaitMs: 100, * }); * * // Set the batch executor * batcher.setExecutor(async (requests) => { * // Execute all requests in a batch * return await Promise.all(requests.map(r => executeTool(r.tool, r.args))); * }); * * // Add requests - they'll be batched automatically * const result1 = await batcher.add('getUserById', { id: 1 }); * const result2 = await batcher.add('getUserById', { id: 2 }); * ``` */ export declare class RequestBatcher extends EventEmitter { private config; private pending; private serverQueues; private flushTimer?; private executor?; private activeBatches; private batchCounter; private requestCounter; private isDestroyed; constructor(config: BatchConfig); /** * Set the batch executor function */ setExecutor(executor: BatchExecutor): void; /** * Add a request to the batch queue */ add(tool: string, args: unknown, serverId?: string): Promise; /** * Manually flush the current batch */ flush(): Promise; /** * Get current queue size */ get queueSize(): number; /** * Get number of active batches */ get activeBatchCount(): number; /** * Check if the batcher is idle (no pending requests) */ get isIdle(): boolean; /** * Wait for all pending requests to complete */ drain(): Promise; /** * Destroy the batcher and reject all pending requests */ destroy(): void; private generateRequestId; private generateBatchId; private scheduleFlush; private clearFlushTimer; private executeBatch; private selectBatchRequests; } /** * Factory function to create a RequestBatcher instance */ export declare const createRequestBatcher: (config: BatchConfig) => RequestBatcher; /** * Default batcher configuration */ export declare const DEFAULT_BATCH_CONFIG: BatchConfig; /** * Tool Call Batcher - Specialized batcher for MCP tool calls */ export declare class ToolCallBatcher { private batcher; private toolExecutor?; constructor(config?: Partial); /** * Set the tool executor function */ setToolExecutor(executor: (tool: string, args: unknown, serverId?: string) => Promise): void; /** * Execute a tool call (will be batched automatically) */ execute(tool: string, args: unknown, serverId?: string): Promise; /** * Flush pending tool calls */ flush(): Promise; /** * Wait for all pending tool calls to complete */ drain(): Promise; /** * Get current queue size */ get queueSize(): number; /** * Check if idle */ get isIdle(): boolean; /** * Destroy the batcher */ destroy(): void; } /** * Create a tool call batcher instance */ export declare const createToolCallBatcher: (config?: Partial) => ToolCallBatcher;