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
128 lines • 4 kB
TypeScript
/**
* BC WebSocket Manager
*
* Manages raw WebSocket connection and JSON-RPC request/response lifecycle.
*
* Responsibilities:
* - WebSocket connection lifecycle (connect/disconnect)
* - JSON-RPC request/response matching
* - Raw message routing to subscribers (for protocol adapter)
*
* IMPORTANT: Does NOT parse BC protocol. Protocol parsing is delegated to
* BCProtocolAdapter which subscribes to raw messages via onRawMessage().
*
* Usage:
* ```ts
* const wsManager = new BCWebSocketManager(config, authService, timeouts);
* await wsManager.connect();
*
* // Protocol adapter subscribes to raw messages
* const unsubscribe = wsManager.onRawMessage((msg) => {
* // Parse BC protocol here
* });
*
* // Send JSON-RPC requests
* const result = await wsManager.sendRpcRequest('OpenSession', [params]);
* ```
*/
import { type TimeoutsConfig } from '../../core/timeouts.js';
import type { IBCWebSocketManager, IBCAuthenticationService } from '../interfaces.js';
import type { BCConfig } from '../../types.js';
/**
* WebSocket manager implementation for Business Central.
*
* Week 3: Extracted from BCRawWebSocketClient to separate transport
* from protocol parsing.
*/
export declare class BCWebSocketManager implements IBCWebSocketManager {
private readonly config;
private readonly authService;
private readonly timeouts;
private ws;
private connected;
private pendingRequests;
private rawMessageHandlers;
constructor(config: BCConfig, authService: IBCAuthenticationService, timeouts: TimeoutsConfig);
/**
* Build WebSocket URL with query parameters.
*/
private buildWebSocketUrl;
/**
* Handle JSON-RPC response with explicit ID match.
*/
private handleJsonRpcResponse;
/**
* Handle async Message events (BC's primary response format).
*/
private handleAsyncMessage;
/**
* Handle incoming WebSocket message.
*/
private handleWebSocketMessage;
/**
* Handle WebSocket close event.
*/
private handleWebSocketClose;
/**
* Set up persistent message and close handlers on WebSocket.
*/
private setupPersistentHandlers;
/**
* Connect to WebSocket with session cookies.
*
* Establishes WebSocket connection using authenticated session cookies.
* Must call authService.authenticateWeb() first.
*
* @param options Optional cancellation signal and timeout override
* @param options.signal Optional AbortSignal for external cancellation
* @param options.timeoutMs Optional timeout override (default: 10s from config)
*/
connect(options?: {
signal?: AbortSignal;
timeoutMs?: number;
}): Promise<void>;
/**
* Disconnect WebSocket.
*
* Closes the WebSocket connection if open.
*/
disconnect(): Promise<void>;
/**
* Send JSON-RPC request and wait for response.
*
* @param method RPC method name
* @param params RPC parameters
* @param options Optional cancellation signal and timeout
* @returns Promise resolving to RPC result
*/
sendRpcRequest(method: string, params: readonly unknown[], options?: {
signal?: AbortSignal;
timeoutMs?: number;
}): Promise<unknown>;
/**
* Check if WebSocket is connected.
*
* @returns true if connected, false otherwise
*/
isConnected(): boolean;
/**
* Subscribe to raw WebSocket messages.
*
* Allows protocol adapter to receive all raw messages for BC protocol parsing.
*
* @param handler Callback receiving raw message
* @returns Unsubscribe function
*
* @example
* ```ts
* const unsubscribe = wsManager.onRawMessage((msg) => {
* // Parse BC protocol
* });
*
* // Later: unsubscribe when done
* unsubscribe();
* ```
*/
onRawMessage(handler: (msg: unknown) => void): () => void;
}
//# sourceMappingURL=BCWebSocketManager.d.ts.map