/**
 * Security MCP Tools - AIDefence Integration
 *
 * Provides MCP tools for AI manipulation defense:
 * - aidefence_scan: Scan input for threats
 * - aidefence_analyze: Deep analysis of threats
 * - aidefence_stats: Get detection statistics
 * - aidefence_learn: Learn from detection feedback
 *
 * Created with ❤️ by ruv.io
 */
// Lazy-loaded AIDefence instance
let aidefenceInstance = null;
async function getAIDefence() {
    if (!aidefenceInstance) {
        try {
            const { createAIDefence } = await import('@claude-flow/aidefence');
            aidefenceInstance = createAIDefence({ enableLearning: true });
        }
        catch {
            throw new Error('AIDefence package not available. Install with: npm install @claude-flow/aidefence');
        }
    }
    return aidefenceInstance;
}
/**
 * Scan input for AI manipulation threats
 */
const aidefenceScanTool = {
    name: 'aidefence_scan',
    description: 'Scan input text for AI manipulation threats (prompt injection, jailbreaks, PII). Returns threat assessment with <10ms latency.',
    inputSchema: {
        type: 'object',
        properties: {
            input: {
                type: 'string',
                description: 'Text to scan for threats',
            },
            quick: {
                type: 'boolean',
                description: 'Quick scan mode (faster, less detailed)',
                default: false,
            },
        },
        required: ['input'],
    },
    handler: async (args) => {
        const input = args.input;
        const quick = args.quick;
        try {
            const defender = await getAIDefence();
            if (quick) {
                const result = defender.quickScan(input);
                return {
                    content: [{
                            type: 'text',
                            text: JSON.stringify({
                                safe: !result.threat,
                                threatDetected: result.threat,
                                confidence: result.confidence,
                                mode: 'quick',
                            }, null, 2),
                        }],
                };
            }
            const result = await defender.detect(input);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            safe: result.safe,
                            threats: result.threats.map(t => ({
                                type: t.type,
                                severity: t.severity,
                                confidence: t.confidence,
                                description: t.description,
                            })),
                            piiFound: result.piiFound,
                            detectionTimeMs: result.detectionTimeMs,
                        }, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Deep analysis of specific threat
 */
const aidefenceAnalyzeTool = {
    name: 'aidefence_analyze',
    description: 'Deep analysis of input for specific threat types with similar pattern search and mitigation recommendations.',
    inputSchema: {
        type: 'object',
        properties: {
            input: {
                type: 'string',
                description: 'Text to analyze',
            },
            searchSimilar: {
                type: 'boolean',
                description: 'Search for similar known threats',
                default: true,
            },
            k: {
                type: 'number',
                description: 'Number of similar patterns to retrieve',
                default: 5,
            },
        },
        required: ['input'],
    },
    handler: async (args) => {
        const input = args.input;
        const searchSimilar = args.searchSimilar !== false;
        const k = args.k || 5;
        try {
            const defender = await getAIDefence();
            const result = await defender.detect(input);
            const analysis = {
                detection: {
                    safe: result.safe,
                    threats: result.threats,
                    piiFound: result.piiFound,
                },
                mitigations: [],
                similarPatterns: [],
            };
            // Get mitigations for detected threats
            for (const threat of result.threats) {
                const mitigation = await defender.getBestMitigation(threat.type);
                if (mitigation) {
                    analysis.mitigations.push({
                        threatType: threat.type,
                        strategy: mitigation.strategy,
                        effectiveness: mitigation.effectiveness,
                    });
                }
            }
            // Search similar patterns
            if (searchSimilar) {
                const similar = await defender.searchSimilarThreats(input, { k });
                analysis.similarPatterns = similar.map(p => ({
                    pattern: p.pattern,
                    type: p.type,
                    effectiveness: p.effectiveness,
                }));
            }
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify(analysis, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Get detection statistics
 */
const aidefenceStatsTool = {
    name: 'aidefence_stats',
    description: 'Get AIDefence detection and learning statistics.',
    inputSchema: {
        type: 'object',
        properties: {},
    },
    handler: async () => {
        try {
            const defender = await getAIDefence();
            const stats = await defender.getStats();
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            detectionCount: stats.detectionCount,
                            avgDetectionTimeMs: stats.avgDetectionTimeMs,
                            learnedPatterns: stats.learnedPatterns,
                            mitigationStrategies: stats.mitigationStrategies,
                            avgMitigationEffectiveness: stats.avgMitigationEffectiveness,
                        }, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Record detection feedback for learning
 */
const aidefenceLearnTool = {
    name: 'aidefence_learn',
    description: 'Record detection feedback for pattern learning. Improves future detection accuracy.',
    inputSchema: {
        type: 'object',
        properties: {
            input: {
                type: 'string',
                description: 'Original input that was scanned',
            },
            wasAccurate: {
                type: 'boolean',
                description: 'Whether the detection was accurate',
            },
            verdict: {
                type: 'string',
                description: 'User verdict or correction',
            },
            threatType: {
                type: 'string',
                description: 'Threat type for mitigation recording',
            },
            mitigationStrategy: {
                type: 'string',
                description: 'Mitigation strategy used',
                enum: ['block', 'sanitize', 'warn', 'log', 'escalate', 'transform', 'redirect'],
            },
            mitigationSuccess: {
                type: 'boolean',
                description: 'Whether the mitigation was successful',
            },
        },
        required: ['input', 'wasAccurate'],
    },
    handler: async (args) => {
        const input = args.input;
        const wasAccurate = args.wasAccurate;
        const verdict = args.verdict;
        const threatType = args.threatType;
        const mitigationStrategy = args.mitigationStrategy;
        const mitigationSuccess = args.mitigationSuccess;
        try {
            const defender = await getAIDefence();
            // Re-detect to get result for learning
            const result = await defender.detect(input);
            // Learn from detection
            await defender.learnFromDetection(input, result, {
                wasAccurate,
                userVerdict: verdict,
            });
            // Record mitigation if provided
            if (threatType && mitigationStrategy && mitigationSuccess !== undefined) {
                await defender.recordMitigation(threatType, mitigationStrategy, mitigationSuccess);
            }
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({
                            success: true,
                            message: 'Feedback recorded for pattern learning',
                            learnedFrom: {
                                input: input.slice(0, 50) + (input.length > 50 ? '...' : ''),
                                wasAccurate,
                                threatCount: result.threats.length,
                            },
                        }, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Check if input is safe (simple boolean check)
 */
const aidefenceIsSafeTool = {
    name: 'aidefence_is_safe',
    description: 'Quick boolean check if input is safe. Fastest option for simple validation.',
    inputSchema: {
        type: 'object',
        properties: {
            input: {
                type: 'string',
                description: 'Text to check',
            },
        },
        required: ['input'],
    },
    handler: async (args) => {
        const input = args.input;
        try {
            const { isSafe } = await import('@claude-flow/aidefence');
            const safe = isSafe(input);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ safe }, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Check for PII in input
 */
const aidefenceHasPIITool = {
    name: 'aidefence_has_pii',
    description: 'Check if input contains PII (emails, SSNs, API keys, passwords, etc.).',
    inputSchema: {
        type: 'object',
        properties: {
            input: {
                type: 'string',
                description: 'Text to check for PII',
            },
        },
        required: ['input'],
    },
    handler: async (args) => {
        const input = args.input;
        try {
            const defender = await getAIDefence();
            const hasPII = defender.hasPII(input);
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ hasPII }, null, 2),
                    }],
            };
        }
        catch (error) {
            return {
                content: [{
                        type: 'text',
                        text: JSON.stringify({ error: String(error) }),
                    }],
                isError: true,
            };
        }
    },
};
/**
 * Export all security tools
 */
export const securityTools = [
    aidefenceScanTool,
    aidefenceAnalyzeTool,
    aidefenceStatsTool,
    aidefenceLearnTool,
    aidefenceIsSafeTool,
    aidefenceHasPIITool,
];
export default securityTools;
//# sourceMappingURL=security-tools.js.map