UNPKG

bc-webclient-mcp

Version:

Model Context Protocol (MCP) server for Microsoft Dynamics 365 Business Central via WebUI protocol. Enables AI assistants to interact with BC through the web client protocol, supporting Card, List, and Document pages with full line item support and server

157 lines 4.96 kB
/** * Connection Pool for Business Central WebSocket Clients * * Manages a pool of authenticated, ready-to-use BCRawWebSocketClient connections * to eliminate expensive connection setup overhead on every request. * * Features: * - Lazy initialization (creates connections on demand) * - Health checks (validates connections before returning from pool) * - Automatic cleanup (removes stale/unhealthy connections) * - Concurrency control (max pool size, request queuing) * - Idle timeout (closes connections after inactivity) */ import { BCRawWebSocketClient } from '../connection/clients/BCRawWebSocketClient.js'; import type { BCConfig } from '../types.js'; /** * Represents a pooled connection with metadata */ export interface PooledConnection { /** Unique identifier for this pooled connection */ id: string; /** The underlying BC WebSocket client */ client: BCRawWebSocketClient; /** When this connection was created */ createdAt: Date; /** Last time this connection was used */ lastUsedAt: Date; /** Whether this connection passed recent health check */ isHealthy: boolean; /** Whether this connection is currently in use */ inUse: boolean; } /** * Configuration for the connection pool */ export interface ConnectionPoolConfig { /** Minimum number of connections to keep warm (default: 2) */ minConnections?: number; /** Maximum number of connections allowed (default: 10) */ maxConnections?: number; /** Idle timeout in milliseconds (default: 300000 = 5 minutes) */ idleTimeoutMs?: number; /** Health check interval in milliseconds (default: 60000 = 1 minute) */ healthCheckIntervalMs?: number; /** Timeout for acquiring a connection in milliseconds (default: 30000 = 30 seconds) */ acquireTimeoutMs?: number; } /** * Connection pool for BC WebSocket clients * * Usage: * ```typescript * const pool = new BCConnectionPool(config, username, password, tenantId); * await pool.initialize(); * * const connection = await pool.acquire(); * try { * await connection.client.invoke(...); * } finally { * await pool.release(connection); * } * * await pool.shutdown(); * ``` */ export declare class BCConnectionPool { private readonly config; private readonly username; private readonly password; private readonly tenantId; private readonly minConnections; private readonly maxConnections; private readonly idleTimeoutMs; private readonly healthCheckIntervalMs; private readonly acquireTimeoutMs; /** Pool of available (idle) connections */ private availableConnections; /** Set of currently active (in-use) connections */ private activeConnections; /** Queue of requests waiting for connections */ private waitQueue; /** Health check interval handle */ private healthCheckInterval; /** Idle cleanup interval handle */ private idleCleanupInterval; /** Whether the pool has been initialized */ private initialized; /** Whether the pool is shutting down */ private shuttingDown; constructor(config: BCConfig, username: string, password: string, tenantId?: string, poolConfig?: ConnectionPoolConfig); /** * Initialize the connection pool * Creates minimum number of connections and starts background tasks */ initialize(): Promise<void>; /** * Acquire a connection from the pool * * @param attempt Current retry attempt (internal parameter for recursion limit) * @returns Promise that resolves with a pooled connection * @throws TimeoutError if no connection available within timeout * @throws ConnectionError if pool is shutting down or max retries exceeded */ acquire(attempt?: number): Promise<PooledConnection>; /** * Release a connection back to the pool * * @param connection The connection to release */ release(connection: PooledConnection): Promise<void>; /** * Shutdown the connection pool * Closes all connections and stops background tasks */ shutdown(): Promise<void>; /** * Get pool statistics */ getStats(): { available: number; active: number; total: number; queued: number; maxConnections: number; }; /** * Create a new connection * @private */ private createConnection; /** * Destroy a connection * @private */ private destroyConnection; /** * Wait for a connection to become available * @private */ private waitForConnection; /** * Check if a connection is healthy * @private */ private checkHealth; /** * Start periodic health checks * @private */ private startHealthChecks; /** * Start periodic idle connection cleanup * @private */ private startIdleCleanup; } //# sourceMappingURL=connection-pool.d.ts.map