"use strict";
// =============================================================================
// Pure Agent - MCP Tool Registration
// Ported from Hermes tools/mcp_tool.py (registration + dispatch)
// =============================================================================
Object.defineProperty(exports, "__esModule", { value: true });
exports.initMCPTools = initMCPTools;
exports.cleanupMCP = cleanupMCP;
const tool_registry_1 = require("./tool-registry");
const result_types_1 = require("./result-types");
const mcp_client_1 = require("./mcp-client");
const logger_1 = require("./logger");
// ---------------------------------------------------------------------------
// Dynamic tool registration
// ---------------------------------------------------------------------------
let _mcpToolsRegistered = false;
async function initMCPTools() {
    if (_mcpToolsRegistered)
        return;
    const config = (0, mcp_client_1.loadMCPConfig)();
    if (Object.keys(config).length === 0) {
        (0, logger_1.getLogger)().info('MCP: no servers configured');
        return;
    }
    try {
        const toolNames = await (0, mcp_client_1.connectMCPServers)(config);
        (0, logger_1.getLogger)().info(`MCP: connected to ${Object.keys(config).length} servers, ${toolNames.length} tools`);
        // Register each MCP tool in the registry
        const tools = (0, mcp_client_1.getMCPTools)();
        for (const tool of tools) {
            registerMCPTool(tool);
        }
        _mcpToolsRegistered = true;
    }
    catch (err) {
        (0, logger_1.getLogger)().error(`MCP initialization failed: ${err.message}`);
    }
}
function registerMCPTool(tool) {
    const serverName = tool.serverName;
    const toolName = tool.name;
    tool_registry_1.registry.register({
        name: toolName,
        toolset: `mcp-${serverName}`,
        schema: {
            description: tool.description || `MCP tool from ${serverName}`,
            parameters: tool.inputSchema,
        },
        handler: async (args, context) => {
            try {
                const result = await (0, mcp_client_1.callMCPTool)(toolName, args, context.signal);
                return result;
            }
            catch (err) {
                return (0, result_types_1.toolError)(`MCP tool error: ${err.message}`);
            }
        },
        isAsync: true,
        description: tool.description || `MCP: ${toolName}`,
        emoji: '🔌',
        maxResultSizeChars: 100000,
    });
}
// ---------------------------------------------------------------------------
// MCP status tool
// ---------------------------------------------------------------------------
tool_registry_1.registry.register({
    name: 'mcp_status',
    toolset: 'mcp',
    schema: {
        description: 'Show status of all configured MCP servers and their available tools.',
        parameters: {
            type: 'object',
            properties: {},
            required: [],
        },
    },
    handler: async () => {
        const status = (0, mcp_client_1.getMCPStatus)();
        return (0, result_types_1.toolResult)({
            servers: status,
            total_tools: status.reduce((sum, s) => sum + s.tools, 0),
        });
    },
    isAsync: true,
    description: 'MCP server status',
    emoji: '🔌',
});
// ---------------------------------------------------------------------------
// Cleanup
// ---------------------------------------------------------------------------
async function cleanupMCP() {
    await (0, mcp_client_1.disconnectMCPServers)();
    _mcpToolsRegistered = false;
}
//# sourceMappingURL=mcp-tool.js.map