import { RegistryError } from './types.js';
const BASE = 'https://api.smithery.ai';
export class SmitheryAdapter {
    apiKey;
    descriptor = {
        id: 'smithery',
        label: 'Smithery MCP',
        kinds: ['mcp'],
        requiresAuth: true,
        enabled: true
    };
    constructor(apiKey = process.env.SMITHERY_API_KEY ?? null) {
        this.apiKey = apiKey;
    }
    async search(query, limit = 50) {
        if (!this.apiKey)
            throw new RegistryError('Smithery API key not configured', 401);
        const url = new URL(`${BASE}/servers`);
        if (query)
            url.searchParams.set('q', query);
        url.searchParams.set('pageSize', String(Math.min(limit, 50)));
        const res = await fetch(url, {
            headers: {
                accept: 'application/json',
                authorization: `Bearer ${this.apiKey}`
            }
        });
        if (!res.ok)
            throw new RegistryError(`Smithery search failed: ${res.status}`);
        const data = (await res.json());
        return (data.servers ?? [])
            .filter((server) => server.qualifiedName)
            .map((server) => ({
            id: server.qualifiedName,
            source: 'smithery',
            kind: 'mcp',
            name: server.qualifiedName.split('/').pop() || server.qualifiedName,
            title: server.displayName ?? null,
            description: server.description ?? null,
            version: null,
            repository: null,
            homepage: server.homepage ?? null,
            installManifest: server
        }));
    }
    async resolveInstall(entry) {
        const raw = entry.installManifest;
        const wsUrl = `https://server.smithery.ai/${raw.qualifiedName}/ws`;
        return {
            name: entry.name,
            transport: 'http',
            url: wsUrl,
            source: {
                registry: 'smithery',
                ref: raw.qualifiedName ?? null,
                version: null,
                url: raw.homepage ?? null
            }
        };
    }
}
export const smitheryAdapter = new SmitheryAdapter();
//# sourceMappingURL=smithery.js.map