import { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js'; import { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'; import { JSONRPCMessage } from '@modelcontextprotocol/sdk/types.js'; import type { LogStorageService } from '../../services/log-storage.service.js'; import { PassThrough } from 'stream'; export interface StdioTransportOptions { compositeKey?: string; logStorage?: LogStorageService; readyPatterns?: string[]; readyTimeout: number; } /** * A transport implementation for communicating with MCP (Model Context Protocol) servers * via standard input/output streams. This transport wraps the official SDK's StdioClientTransport * and provides consistent logging and integration with the MCP Hub Lite system. * * The StdioTransport handles: * - Cross-platform process spawning (including Windows batch file compatibility) * - JSON-RPC message parsing and serialization * - Proper process lifecycle management with graceful shutdown * - Error handling and logging * - Separate handling of stdout (for JSON-RPC) and stderr (for logging) * * This transport is primarily used for local MCP servers that communicate through * standard I/O streams, such as those launched via npx, npm, or other package managers. * * @implements {Transport} */ export declare class StdioTransport implements Transport { private _transport; private _serverName?; private _compositeKey?; private _logStorage?; private _stderrStream; private _readyPatterns?; private _readyTimeout; onclose?: () => void; onerror?: (error: Error) => void; onmessage?: (message: JSONRPCMessage) => void; onstdout?: (data: string) => void; onstderr?: (data: string) => void; /** * Creates a new StdioTransport instance. * * @param {StdioServerParameters} server - Configuration parameters for the child process * @param {string} [serverName] - Optional name for the server used in logging * @param {StdioTransportOptions} [options] - Additional options for the transport */ constructor(server: StdioServerParameters, serverName?: string, options?: StdioTransportOptions); /** * Starts the transport by spawning the child process and establishing communication channels. * * This method delegates to the SDK's StdioClientTransport.start() method which handles: * - Platform-specific process spawning (including Windows batch file compatibility) * - Proper stdio configuration * - Event listeners for process lifecycle events * - Data flow for stdout and stderr handling * * @returns {Promise} Resolves when the child process is successfully spawned * @throws {Error} If the transport is already started */ start(): Promise; /** * Waits for the server to emit a ready pattern in stderr. * * @param patterns - Array of string patterns to match (any match = ready) * @param timeout - Timeout in milliseconds * @returns Promise that resolves when a pattern is matched, or rejects on timeout */ private waitForReady; /** * Gets the stderr stream for the child process. * * Returns our PassThrough stream that captures stderr data. * * @returns {PassThrough | null} The stderr stream or null */ get stderr(): PassThrough | null; /** * Gets the process ID of the spawned child process. * * @returns {number | undefined} The PID of the child process, or undefined if not started */ get pid(): number | undefined; /** * Closes the transport by gracefully terminating the child process. * * This method delegates to the SDK's StdioClientTransport.close() method which handles: * - Graceful shutdown sequence with SIGTERM/SIGKILL * - Proper cleanup of internal state * * @returns {Promise} Resolves when the transport is fully closed */ close(): Promise; /** * Sends a JSON-RPC message to the child process via stdin. * * This method delegates to the SDK's StdioClientTransport.send() method. * * @param {JSONRPCMessage} message - The JSON-RPC message to send * @returns {Promise} Resolves when the message is written to stdin */ send(message: JSONRPCMessage): Promise; } //# sourceMappingURL=stdio-transport.d.ts.map