import { Type as T } from "typebox";
import { MCPServerManager } from "./client.js";
export { MCPServerManager };
const serverManager = new MCPServerManager();
export function getMCPServerManager() {
    return serverManager;
}
export function configureMCPServers(servers) {
    for (const [name, config] of Object.entries(servers)) {
        if (config.enabled !== false) {
            serverManager.addServer(name, config);
        }
    }
}
export async function createAllMCPToolDefinitions() {
    const tools = [];
    for (const [serverName, client] of serverManager.getAllClients()) {
        if (!client.connected)
            continue;
        let mcpTools;
        try {
            mcpTools = await client.listTools();
        }
        catch {
            continue;
        }
        for (const mcpTool of mcpTools) {
            const toolName = `${serverName}_${mcpTool.name}`;
            const toolDef = createMCPToolDefinition(toolName, mcpTool.name, mcpTool.description ?? "", mcpTool.inputSchema, client);
            tools.push(toolDef);
        }
    }
    return tools;
}
function createMCPToolDefinition(toolName, mcpToolName, description, inputSchema, client) {
    const properties = inputSchema.properties ?? {};
    const _required = inputSchema.required ?? [];
    const typeBoxProps = {};
    for (const [key, prop] of Object.entries(properties)) {
        const propType = prop.type;
        const propDesc = prop.description;
        if (propType === "string" || propType === undefined) {
            typeBoxProps[key] = T.Optional(T.String({ description: propDesc ?? "" }));
        }
        else if (propType === "number" || propType === "integer") {
            typeBoxProps[key] = T.Optional(T.Number({ description: propDesc ?? "" }));
        }
        else if (propType === "boolean") {
            typeBoxProps[key] = T.Optional(T.Boolean({ description: propDesc ?? "" }));
        }
        else {
            typeBoxProps[key] = T.Optional(T.String({ description: propDesc ?? "" }));
        }
    }
    const schema = T.Object(typeBoxProps);
    return {
        name: toolName,
        label: toolName,
        description,
        parameters: schema,
        promptSnippet: `${toolName}: ${description}`,
        renderShell: "default",
        async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
            try {
                const result = await client.callTool(mcpToolName, params);
                const text = result.content
                    .filter((c) => c.type === "text")
                    .map((c) => c.text ?? "")
                    .join("\n");
                return { content: [{ type: "text", text: text || "(no text output)" }], details: result };
            }
            catch (error) {
                return {
                    content: [
                        { type: "text", text: `MCP error: ${error instanceof Error ? error.message : String(error)}` },
                    ],
                    details: null,
                };
            }
        },
    };
}
//# sourceMappingURL=index.js.map