"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMcpToolEntries = createMcpToolEntries;
exports.createMcpHarnessToolName = createMcpHarnessToolName;
const mcpRuntime_1 = require("../extensibility/mcpRuntime");
function createMcpToolEntries(projectDir, definitions = (0, mcpRuntime_1.readMcpServerDefinitionsSync)(projectDir)) {
    const entries = [];
    for (const server of definitions) {
        if (!server.enabled)
            continue;
        for (const remoteTool of server.tools) {
            const toolName = createMcpHarnessToolName(server.id, remoteTool.name);
            const tool = {
                name: toolName,
                description: remoteTool.description || `Invoke MCP tool ${remoteTool.name} on ${server.id}.`,
                parameters: remoteTool.inputSchema && Object.keys(remoteTool.inputSchema).length > 0
                    ? remoteTool.inputSchema
                    : { type: 'object', additionalProperties: true },
                isReadOnly: false,
                riskLevel: 'high',
                permissionCategory: 'shell',
                execute: async (input) => formatMcpToolResult(await (0, mcpRuntime_1.invokeMcpServerTool)(projectDir, server.id, remoteTool.name, input)),
            };
            entries.push({
                tool,
                toolset: `mcp:${server.id}`,
                source: 'runtime',
                enabledByDefault: false,
                riskLevel: 'high',
                permissionCategory: 'shell',
                canDryRun: false,
            });
        }
    }
    return entries;
}
function createMcpHarnessToolName(serverId, toolName) {
    const normalizedServer = serverId.replace(/[^a-zA-Z0-9_-]/g, '_');
    const normalizedTool = toolName.replace(/[^a-zA-Z0-9_-]/g, '_');
    return `mcp_${normalizedServer}__${normalizedTool}`.slice(0, 120);
}
function formatMcpToolResult(result) {
    const output = Array.isArray(result.content) && result.content.length > 0
        ? result.content.map(formatMcpContentBlock).filter(Boolean).join('\n')
        : JSON.stringify(result);
    return {
        success: result.isError !== true,
        output,
        error: result.isError === true ? output : undefined,
    };
}
function formatMcpContentBlock(block) {
    if (typeof block.text === 'string')
        return block.text;
    if (typeof block.type === 'string')
        return JSON.stringify(block);
    return JSON.stringify(block);
}
//# sourceMappingURL=mcpTools.js.map