// tools/suggest-mcp-server.ts — MCP tool handler that generates a full MCP server recommendation.
import { knowledgeStore } from '../storage/index.js';
import { scoreMcpCandidate, detectAppDomains } from '../engine/detectors/mcp-candidate-detector.js';
import { t } from '../i18n/index.js';
import { readFile } from 'node:fs/promises';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
// Load archetypes lazily
let archetypesCache = null;
async function loadArchetypes() {
    if (archetypesCache) {
        return archetypesCache;
    }
    try {
        const configDir = join(dirname(fileURLToPath(import.meta.url)), '..', 'config');
        const raw = await readFile(join(configDir, 'mcp-archetypes.json'), 'utf-8');
        const parsed = JSON.parse(raw);
        archetypesCache = parsed.archetypes;
        return archetypesCache;
    }
    catch {
        return [];
    }
}
export async function handleSuggestMcpServer(args) {
    const { projectId } = args;
    const knowledge = await knowledgeStore.getKnowledge(projectId);
    if (!knowledge) {
        return {
            content: [{ type: 'text', text: t('project.notFound') }],
            isError: true,
        };
    }
    const candidate = scoreMcpCandidate(knowledge);
    const domains = detectAppDomains(knowledge);
    if (!candidate.isStrongCandidate && !candidate.isSoftCandidate) {
        return {
            content: [
                {
                    type: 'text',
                    text: `This project scored ${candidate.score}/15 for MCP server candidacy (threshold: 3). Not enough indicators to suggest an MCP server layer at this time.`,
                },
            ],
        };
    }
    // Load matching archetypes
    const allArchetypes = await loadArchetypes();
    const matchingArchetypes = allArchetypes.filter((a) => domains.includes(a.domain));
    // Collect all suggested tools and resources from matching archetypes
    const suggestedTools = matchingArchetypes.flatMap((a) => a.tools);
    const suggestedResources = matchingArchetypes.flatMap((a) => a.resources);
    // Determine transport suggestion
    const stack = knowledge.stack;
    const suggestedTransport = stack.some((s) => ['express', 'fastify', 'nestjs', 'koa', 'hapi', 'django', 'flask', 'fastapi'].includes(s.toLowerCase()))
        ? 'http-streamable'
        : 'stdio';
    // Estimate effort
    const toolCount = suggestedTools.length;
    const hours = Math.max(8, Math.min(40, toolCount * 2));
    const recommendation = {
        projectId,
        candidate,
        archetypes: matchingArchetypes,
        suggestedTransport,
        suggestedTools,
        suggestedResources,
        estimatedEffort: { hours, difficulty: domains.length > 2 ? 4 : 3 },
        generatedAt: new Date().toISOString(),
    };
    // Build summary text
    const strength = candidate.isStrongCandidate ? 'Strong' : 'Moderate';
    const summary = [
        `MCP Server Recommendation: ${strength} candidate (score: ${candidate.score}/15)`,
        `Detected domains: ${domains.join(', ')}`,
        `Suggested tools: ${toolCount}`,
        `Suggested transport: ${suggestedTransport}`,
        `Estimated effort: ${hours}h`,
    ].join('\n');
    return {
        content: [
            { type: 'text', text: summary },
            { type: 'text', text: JSON.stringify(recommendation, null, 2) },
        ],
    };
}
//# sourceMappingURL=suggest-mcp-server.js.map