"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.createHttpFetchServer = createHttpFetchServer;
const mcp_js_1 = require("@modelcontextprotocol/sdk/server/mcp.js");
const zod_1 = require("zod");
const logger_1 = require("../../lib/logger");
const http = __importStar(require("../activities/http"));
const httpRequestSchema = zod_1.z.object({
    url: zod_1.z.string().describe('URL to request'),
    method: zod_1.z.string().optional().default('GET').describe('HTTP method'),
    headers: zod_1.z.record(zod_1.z.string()).optional().describe('Request headers'),
    body: zod_1.z.string().optional().describe('Request body'),
    timeout_ms: zod_1.z.number().optional().describe('Request timeout in milliseconds'),
});
const fetchJsonSchema = zod_1.z.object({
    url: zod_1.z.string().describe('URL to fetch JSON from'),
    headers: zod_1.z.record(zod_1.z.string()).optional().describe('Request headers'),
});
const fetchTextSchema = zod_1.z.object({
    url: zod_1.z.string().describe('URL to fetch text from'),
    headers: zod_1.z.record(zod_1.z.string()).optional().describe('Request headers'),
});
async function createHttpFetchServer(options) {
    const name = options?.name || 'long-tail-http-fetch';
    const instance = new mcp_js_1.McpServer({ name, version: '1.0.0' });
    instance.registerTool('http_request', {
        title: 'HTTP Request',
        description: 'Make an HTTP request to any URL. Supports all methods, custom headers, and request bodies. Returns status, headers, and body.',
        inputSchema: httpRequestSchema,
    }, async (args) => {
        try {
            const result = await http.httpRequest(args);
            return { content: [{ type: 'text', text: JSON.stringify(result) }] };
        }
        catch (err) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: err.message }) }], isError: true };
        }
    });
    instance.registerTool('fetch_json', {
        title: 'Fetch JSON',
        description: 'GET a URL and parse the response as JSON. Convenience wrapper around http_request.',
        inputSchema: fetchJsonSchema,
    }, async (args) => {
        try {
            const result = await http.fetchJson(args);
            return { content: [{ type: 'text', text: JSON.stringify(result) }] };
        }
        catch (err) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: err.message }) }], isError: true };
        }
    });
    instance.registerTool('fetch_text', {
        title: 'Fetch Text',
        description: 'GET a URL and return the response as text. Returns content, status, and content type.',
        inputSchema: fetchTextSchema,
    }, async (args) => {
        try {
            const result = await http.fetchText(args);
            return { content: [{ type: 'text', text: JSON.stringify(result) }] };
        }
        catch (err) {
            return { content: [{ type: 'text', text: JSON.stringify({ error: err.message }) }], isError: true };
        }
    });
    logger_1.loggerRegistry.info(`[lt-mcp:http-fetch] ${name} ready (3 tools registered)`);
    return instance;
}
