/**
 * MCP tool integration for Pi Agent.
 * Converts MCP tools to Clawx AgentTool format.
 */
import { getMcpManager } from './manager.js';
import { log } from '../utils/logger.js';
/**
 * Create Clawx tools from MCP servers
 */
export async function createMcpTools(config) {
    const tools = [];
    if (!config.mcpServers || Object.keys(config.mcpServers).length === 0) {
        return tools;
    }
    // Initialize manager
    const manager = getMcpManager();
    manager.initialize(config);
    // Wait a bit for connections to establish
    await new Promise(resolve => setTimeout(resolve, 1000));
    // Get all tools from connected servers
    const mcpTools = manager.getAllTools();
    for (const { server, tool } of mcpTools) {
        const toolName = `mcp_${server}_${tool.name}`;
        const clawxTool = {
            name: toolName,
            description: tool.description || `MCP tool from ${server}`,
            label: `MCP: ${server}/${tool.name}`,
            parameters: tool.inputSchema || {
                type: 'object',
                properties: {},
                additionalProperties: true,
            },
            async execute(toolCallId, params) {
                try {
                    log.info(`Calling MCP tool ${server}.${tool.name}`);
                    const result = await manager.callTool(server, tool.name, params);
                    // Format the result for Clawx
                    let text = '';
                    if (result.content && Array.isArray(result.content)) {
                        for (const item of result.content) {
                            if (item.type === 'text') {
                                text += item.text + '\n';
                            }
                            else if (item.type === 'image') {
                                text += `[Image: ${item.data?.slice?.(0, 50) || 'binary data'}...]\n`;
                            }
                        }
                    }
                    if (!text.trim()) {
                        text = `MCP tool ${tool.name} executed successfully`;
                    }
                    return {
                        content: [{
                                type: 'text',
                                text: text.trim(),
                            }],
                        details: {
                            server,
                            tool: tool.name,
                            success: true,
                        },
                    };
                }
                catch (error) {
                    return {
                        content: [{
                                type: 'text',
                                text: `Error calling MCP tool ${tool.name}: ${error instanceof Error ? error.message : String(error)}`,
                            }],
                        details: {
                            server,
                            tool: tool.name,
                            success: false,
                            error: error instanceof Error ? error.message : String(error),
                        },
                    };
                }
            },
        };
        tools.push(clawxTool);
        log.info(`Registered MCP tool: ${toolName}`);
    }
    log.info(`Loaded ${tools.length} MCP tools`);
    return tools;
}
/**
 * Get MCP server status for display
 */
export function getMcpStatus() {
    const manager = getMcpManager();
    const states = manager.getServerStates();
    return states.map(state => ({
        name: state.name,
        status: state.status,
        tools: state.tools.length,
        error: state.error,
    }));
}
//# sourceMappingURL=tools.js.map