export { StdioClientTransport, getDefaultEnvironment, } from '@modelcontextprotocol/sdk/client/stdio.js';
export { StreamableHTTPClientTransport, StreamableHTTPError, } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { StdioClientTransport, } from '@modelcontextprotocol/sdk/client/stdio.js';
import { StreamableHTTPClientTransport, } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
import { createLogger, getVerbose } from '../lib/logger.js';
import { ClientError } from '../lib/errors.js';
export function createStdioTransport(config) {
    const logger = createLogger('StdioTransport');
    logger.debug('Creating stdio transport', { command: config.command, args: config.args });
    const params = {
        ...config,
        stderr: getVerbose() ? 'inherit' : 'ignore',
    };
    return new StdioClientTransport(params);
}
export function createStreamableHttpTransport(url, options = {}) {
    const logger = createLogger('StreamableHttpTransport');
    logger.debug('Creating Streamable HTTP transport', { url });
    logger.debug('Transport options:', {
        hasAuthProvider: !!options.authProvider,
        hasRequestInit: !!options.requestInit,
    });
    const defaultReconnectionOptions = {
        initialReconnectionDelay: 1000,
        maxReconnectionDelay: 30000,
        reconnectionDelayGrowFactor: 2,
        maxRetries: 10,
    };
    const transport = new StreamableHTTPClientTransport(new URL(url), {
        reconnectionOptions: defaultReconnectionOptions,
        ...options,
    });
    const hasAuthProvider = !!transport._authProvider;
    logger.debug('Transport created, authProvider attached:', hasAuthProvider);
    if (hasAuthProvider) {
        const authProvider = transport._authProvider;
        if (typeof authProvider.tokens === 'function') {
            logger.debug('authProvider.tokens() is a function - verification passed');
        }
        else {
            logger.error('authProvider.tokens() is NOT a function - this is a bug!');
        }
    }
    return transport;
}
export function createTransportFromConfig(config, options = {}) {
    if (config.command) {
        const stdioConfig = {
            command: config.command,
        };
        if (config.args !== undefined) {
            stdioConfig.args = config.args;
        }
        if (config.env !== undefined) {
            stdioConfig.env = config.env;
        }
        return createStdioTransport(stdioConfig);
    }
    if (config.url) {
        const logger = createLogger('TransportFactory');
        const transportOptions = {};
        if (options.authProvider) {
            transportOptions.authProvider = options.authProvider;
            logger.debug('Setting authProvider on transport options');
            logger.debug(`  authProvider type: ${options.authProvider.constructor.name}`);
            logger.debug(`  authProvider has tokens method: ${typeof options.authProvider.tokens === 'function'}`);
        }
        else {
            logger.debug('No authProvider provided for HTTP transport');
        }
        if (options.mcpSessionId) {
            transportOptions.sessionId = options.mcpSessionId;
            logger.debug(`Setting mcpSessionId for session resumption: ${options.mcpSessionId}`);
        }
        if (config.headers !== undefined) {
            transportOptions.requestInit = {
                headers: config.headers,
            };
        }
        if (config.timeout !== undefined) {
            transportOptions.requestInit = {
                ...transportOptions.requestInit,
                signal: AbortSignal.timeout(config.timeout * 1000),
            };
        }
        return createStreamableHttpTransport(config.url, transportOptions);
    }
    throw new ClientError('Invalid ServerConfig: must have either url or command');
}
//# sourceMappingURL=transports.js.map