"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testConnection = testConnection;
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/client/stdio.js");
const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
/**
 * Test connectivity to an MCP server without persisting.
 * Creates a temporary client, connects, lists tools, then disconnects.
 */
async function testConnection(transportType, transportConfig) {
    const client = new index_js_1.Client({ name: 'long-tail-test', version: '1.0.0' });
    const timeout = setTimeout(() => { throw new Error('Connection timed out (10s)'); }, 10_000);
    try {
        let transport;
        if (transportType === 'stdio') {
            transport = new stdio_js_1.StdioClientTransport({
                command: transportConfig.command,
                args: transportConfig.args || [],
                env: transportConfig.env,
            });
        }
        else if (transportType === 'streamable-http') {
            transport = new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(transportConfig.url));
        }
        else {
            transport = new sse_js_1.SSEClientTransport(new URL(transportConfig.url));
        }
        await client.connect(transport);
        const { tools } = await client.listTools();
        const manifest = tools.map((t) => ({
            name: t.name,
            description: t.description || '',
            inputSchema: t.inputSchema || {},
        }));
        return { success: true, tools: manifest };
    }
    catch (err) {
        return { success: false, tools: [], error: err.message };
    }
    finally {
        clearTimeout(timeout);
        try {
            await client.close();
        }
        catch { /* ignore close errors */ }
    }
}
