import { z } from 'zod';
import { zodToJsonSchema } from 'zod-to-json-schema';
export function mcpJsonSchemaToZod(schema) {
    if (!schema.properties || Object.keys(schema.properties).length === 0) {
        return z.object({}).passthrough();
    }
    const shape = {};
    const required = new Set(schema.required ?? []);
    for (const [key, propSchema] of Object.entries(schema.properties)) {
        let field = jsonSchemaPropertyToZod(propSchema);
        if (!required.has(key)) {
            field = field.optional();
        }
        shape[key] = field;
    }
    return z.object(shape).passthrough();
}
function jsonSchemaPropertyToZod(prop) {
    if (typeof prop !== 'object' || prop === null)
        return z.unknown();
    const schema = prop;
    switch (schema.type) {
        case 'string':
            return z.string();
        case 'number':
        case 'integer':
            return z.number();
        case 'boolean':
            return z.boolean();
        case 'array':
            return z.array(z.unknown());
        case 'object':
            return z.record(z.unknown());
        default:
            return z.unknown();
    }
}
export function zodToMCPJsonSchema(zodSchema) {
    const jsonSchema = zodToJsonSchema(zodSchema, { target: 'openApi3' });
    return {
        type: 'object',
        ...jsonSchema,
    };
}
export function mcpToolToToolDefinition(tool, client, serverName) {
    const inputSchema = mcpJsonSchemaToZod(tool.inputSchema);
    const toolName = `mcp_${serverName}_${tool.name}`;
    return {
        name: toolName,
        description: tool.description
            ? `[MCP:${serverName}] ${tool.description}`
            : `[MCP:${serverName}] ${tool.name}`,
        inputSchema,
        category: 'network',
        permissions: ['network_access'],
        isReadOnly: () => tool.annotations?.readOnlyHint ?? false,
        isDestructive: () => tool.annotations?.destructiveHint ?? false,
        isConcurrencySafe: () => true,
        async execute(input, _context) {
            const result = await client.callTool(tool.name, input);
            return mcpToolResultToToolResult(result);
        },
    };
}
export function toolDefinitionToMCPTool(tool) {
    return {
        name: tool.name,
        description: tool.description,
        inputSchema: zodToMCPJsonSchema(tool.inputSchema),
        annotations: {
            readOnlyHint: tool.isReadOnly?.(undefined),
            destructiveHint: tool.isDestructive?.(undefined),
        },
    };
}
export function mcpToolResultToToolResult(result) {
    const textContent = result.content
        .filter((block) => block.type === 'text')
        .map((block) => block.text)
        .join('\n');
    return {
        success: !result.isError,
        output: textContent,
        data: result.content,
        error: result.isError ? textContent : undefined,
    };
}
export function toolResultToMCPToolResult(result) {
    const content = [];
    if (result.output) {
        content.push({ type: 'text', text: result.output });
    }
    if (!result.success && result.error) {
        content.push({ type: 'text', text: result.error });
    }
    if (content.length === 0) {
        content.push({ type: 'text', text: '' });
    }
    return {
        content,
        isError: !result.success,
    };
}
//# sourceMappingURL=adapter.js.map