import { zodToJsonSchema } from 'zod-to-json-schema';
import { getRootLogger } from '../../../utils/logger.js';
export class MCPConnectorBridge {
    manager;
    prefix;
    mappings = [];
    log;
    constructor(config) {
        this.manager = config.manager;
        this.prefix = config.prefix ?? 'namzu';
        this.log = getRootLogger().child({ component: 'MCPConnectorBridge' });
    }
    listTools(instanceId) {
        const instances = instanceId
            ? [this.manager.getInstance(instanceId)].filter(Boolean)
            : this.manager.listConnectedInstances();
        this.mappings = [];
        const tools = [];
        for (const instance of instances) {
            const definition = this.manager.getRegistry().get(instance.connectorId);
            if (!definition)
                continue;
            for (const method of definition.methods) {
                const mcpTool = this.methodToMCPTool(instance, definition, method);
                tools.push(mcpTool);
            }
        }
        this.log.info(`Bridge generated ${tools.length} MCP tools from ${instances.length} instances`);
        return tools;
    }
    async callTool(name, args) {
        const mapping = this.mappings.find((m) => m.mcpToolName === name);
        if (!mapping) {
            return {
                content: [{ type: 'text', text: `Unknown tool: "${name}"` }],
                isError: true,
            };
        }
        const result = await this.manager.execute({
            instanceId: mapping.instanceId,
            method: mapping.methodName,
            input: args ?? {},
        });
        return this.connectorResultToMCPResult(result);
    }
    getMappings() {
        return [...this.mappings];
    }
    methodToMCPTool(instance, definition, method) {
        const mcpToolName = `${this.prefix}_${instance.connectorId}_${method.name}`;
        this.mappings.push({
            mcpToolName,
            connectorId: instance.connectorId,
            instanceId: instance.id,
            methodName: method.name,
        });
        const inputSchema = this.zodToMCPSchema(method);
        return {
            name: mcpToolName,
            description: `[${definition.name}] ${method.description}`,
            inputSchema,
        };
    }
    zodToMCPSchema(method) {
        try {
            const jsonSchema = zodToJsonSchema(method.inputSchema, { target: 'openApi3' });
            return {
                type: 'object',
                ...jsonSchema,
            };
        }
        catch {
            return { type: 'object' };
        }
    }
    connectorResultToMCPResult(result) {
        if (result.success) {
            const text = typeof result.output === 'string' ? result.output : JSON.stringify(result.output, null, 2);
            return {
                content: [{ type: 'text', text }],
                isError: false,
            };
        }
        return {
            content: [{ type: 'text', text: result.error ?? 'Unknown error' }],
            isError: true,
        };
    }
}
//# sourceMappingURL=adapter.js.map