UNPKG

@chinchillaenterprises/mcp-ai-assistant

Version:

MCP server for AI assistant with ElevenLabs text-to-speech integration

121 lines 4.86 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ElevenLabsClient = void 0; const axios_1 = __importDefault(require("axios")); const types_js_1 = require("@modelcontextprotocol/sdk/types.js"); class ElevenLabsClient { accountState; client; baseURL = 'https://api.elevenlabs.io/v1'; constructor(accountState) { this.accountState = accountState; this.client = axios_1.default.create({ baseURL: this.baseURL, }); this.client.interceptors.request.use((config) => { const account = this.accountState.getActiveAccount(); if (!account) { throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'No active ElevenLabs account. Please add an account first.'); } config.headers['xi-api-key'] = account.apiKey; return config; }); this.client.interceptors.response.use((response) => response, (error) => { if (error.response?.status === 401) { throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'Invalid API key. Please check your ElevenLabs credentials.'); } if (error.response?.status === 403) { throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'Insufficient permissions or quota exceeded.'); } if (error.response?.status === 429) { throw new types_js_1.McpError(types_js_1.ErrorCode.InvalidRequest, 'Rate limit exceeded. Please try again later.'); } throw error; }); } async getVoices() { const response = await this.client.get('/voices'); return response.data.voices; } async getVoice(voiceId) { const response = await this.client.get(`/voices/${voiceId}`); return response.data; } async textToSpeech(text, voiceId, options = {}) { const { modelId = 'eleven_monolingual_v1', voiceSettings, outputFormat = 'mp3_44100_128', optimizeStreamingLatency, pronunciationDictionaryLocators } = options; const payload = { text, model_id: modelId, voice_settings: voiceSettings || { stability: 0.5, similarity_boost: 0.5 } }; if (pronunciationDictionaryLocators) { payload.pronunciation_dictionary_locators = pronunciationDictionaryLocators; } const params = {}; if (outputFormat) { params.output_format = outputFormat; } if (optimizeStreamingLatency !== undefined) { params.optimize_streaming_latency = optimizeStreamingLatency; } const response = await this.client.post(`/text-to-speech/${voiceId}`, payload, { params, responseType: 'arraybuffer', headers: { 'Accept': 'audio/mpeg', 'Content-Type': 'application/json' } }); return Buffer.from(response.data); } async textToSpeechStream(text, voiceId, options = {}) { const { modelId = 'eleven_monolingual_v1', voiceSettings, outputFormat = 'mp3_44100_128', optimizeStreamingLatency = 3, pronunciationDictionaryLocators } = options; const payload = { text, model_id: modelId, voice_settings: voiceSettings || { stability: 0.5, similarity_boost: 0.5 } }; if (pronunciationDictionaryLocators) { payload.pronunciation_dictionary_locators = pronunciationDictionaryLocators; } const params = {}; if (outputFormat) { params.output_format = outputFormat; } if (optimizeStreamingLatency !== undefined) { params.optimize_streaming_latency = optimizeStreamingLatency; } const response = await this.client.post(`/text-to-speech/${voiceId}/stream`, payload, { params, responseType: 'stream', headers: { 'Accept': 'audio/mpeg', 'Content-Type': 'application/json' } }); return response.data; } async getModels() { const response = await this.client.get('/models'); return response.data; } async getUserInfo() { const response = await this.client.get('/user'); return response.data; } async getSubscriptionInfo() { const response = await this.client.get('/user/subscription'); return response.data; } } exports.ElevenLabsClient = ElevenLabsClient; //# sourceMappingURL=elevenlabs-client.js.map