import type { Transport } from "@modelcontextprotocol/sdk/shared/transport.js"; import type { ILogger } from "../core/logger.js"; import type { Result } from "../core/result.js"; import type { TransportConfig } from "./config.js"; import type { ISessionManager } from "./sessionManager.js"; /** * Factory for creating MCP transport instances based on configuration * * This factory implements the Factory pattern to encapsulate transport creation logic. * It validates configuration and creates the appropriate transport type (stdio or HTTP). */ export declare class TransportFactory { /** * Reset the internal state of a StreamableHTTPServerTransport instance * * WARNING: This method manipulates the private field '_initialized' using Reflect.set(). * This is fragile and may break if the MCP SDK implementation changes. * There is currently no public API to reset the initialized state, so this is required * to ensure the transport can be reused safely after a session is closed. * If the SDK adds a public reset/init method, use that instead. * If the SDK changes the field name or its semantics, update this code accordingly. */ private static resetStreamableHttpState; /** * Create a transport instance from configuration * * @param config - Transport configuration (stdio or streamable-http) * @param logger - Optional logger for HTTP transport session events (ignored for stdio) * @param sessionManager - Optional session manager for HTTP transport (ignored for stdio) * @returns Result with Transport instance or Error * * @example * ```typescript * // Create stdio transport * const stdioResult = TransportFactory.create({ type: 'stdio' }); * * // Create HTTP transport with logger and session manager * const httpResult = TransportFactory.create({ * type: 'streamable-http', * port: 6280, * host: '127.0.0.1', * endpoint: '/mcp' * }, logger, sessionManager); * ``` */ static create(config: TransportConfig, logger?: ILogger, sessionManager?: ISessionManager | null): Result; /** * Create a stdio transport instance * * @param _config - Stdio transport configuration (unused, kept for API consistency) * @returns Result with StdioServerTransport instance */ private static createStdio; /** * Create a streamable HTTP transport instance * * Creates a StreamableHTTPServerTransport with session management support. * The transport instance is stateful and manages session lifecycle through callbacks. * * Note: DNS rebinding protection is handled by Express middleware (createMcpExpressApp) * in the ExpressHttpManager, not by the transport itself. * * @param config - Streamable HTTP transport configuration * @param logger - Optional logger for session lifecycle events * @param sessionManager - Optional session manager for tracking sessions * @returns Result with StreamableHTTPServerTransport instance or Error * * @example * ```typescript * const config: StreamableHttpTransportConfig = { * type: 'streamable-http', * port: 6280, * host: '127.0.0.1', * endpoint: '/mcp', * sessionConfig: { enabled: true } * }; * const result = TransportFactory.createStreamableHttp(config, logger, sessionManager); * ``` */ private static createStreamableHttp; }