/**
 * `mcp` tool — call a tool on a connected MCP server.
 */
import { Type } from '@sinclair/typebox';
import { callTool } from './connection.js';
import { errorResult, resolveServer } from './tool-helpers.js';
const schema = Type.Object({
    server: Type.String({ description: 'MCP server name (from catalog above)' }),
    tool: Type.String({ description: 'Tool name on that server' }),
    arguments: Type.Optional(Type.Record(Type.String(), Type.Unknown(), {
        description: 'Tool arguments as key-value pairs'
    }))
});
/** Create the `mcp` gateway tool that dispatches calls to connected MCP servers. */
export function createMcpExecuteTool(connections, catalog) {
    return {
        name: 'mcp',
        label: 'MCP',
        description: `Call a tool on a connected MCP server.\n\n${catalog}`,
        parameters: schema,
        async execute(_toolCallId, params, signal) {
            const conn = resolveServer(connections, params.server);
            if (typeof conn === 'string')
                return errorResult(conn);
            const tool = conn.tools.find((t) => t.name === params.tool);
            if (!tool) {
                const available = conn.tools.map((t) => t.name).join(', ');
                return errorResult(`Unknown tool '${params.tool}' on server '${params.server}'. Available: ${available || '(none)'}`);
            }
            const result = await callTool(conn, params.tool, params.arguments, signal);
            return {
                content: [{ type: 'text', text: result.text }],
                details: undefined,
                isError: result.isError || undefined
            };
        }
    };
}
//# sourceMappingURL=mcp-execute-tool.js.map