text_to_speech
Convert text to speech and play it through system audio using customizable voice options for accessibility, content consumption, or audio output needs.
Instructions
Convert text to speech and play it through system audio
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | The text to convert to speech | |
| voice | No | The voice to use for speech synthesis (e.g. 'af_bella'). Use list_voices to see available options. |
Implementation Reference
- src/index.ts:279-292 (handler)MCP CallToolRequest handler for 'text_to_speech' tool: validates args, calls TTSClient.generateAndPlayAudio, returns success message.case "text_to_speech": { const args = request.params.arguments as unknown as TextToSpeechArgs; if (!args.text) { throw new Error("Missing required argument: text"); } await ttsClient.generateAndPlayAudio(args.text, args.voice); return { content: [{ type: "text", text: `Successfully generated and played audio${args.voice ? ` using voice: ${args.voice}` : ''}` }], }; }
- src/index.ts:40-59 (schema)Tool definition including inputSchema (JSON schema) for validating 'text_to_speech' arguments.const textToSpeechTool: Tool = { name: "text_to_speech", description: "Convert text to speech and play it through system audio", inputSchema: { type: "object", properties: { text: { type: "string", description: "The text to convert to speech", minLength: 1, maxLength: 1000, }, voice: { type: "string", description: "The voice to use for speech synthesis (e.g. 'af_bella'). Use list_voices to see available options.", }, }, required: ["text"], }, };
- src/index.ts:230-249 (helper)Core TTS helper method in TTSClient: generates speech audio using KokoroTTS, saves to temp WAV, and plays it synchronously.async generateAndPlayAudio(text: string, voice?: KokoroVoice, speed?: number): Promise<void> { await this.waitForInit(); if (!this.ttsInstance) { throw new Error("TTS model not initialized"); } const audio = await this.ttsInstance.generate(text, { voice: voice || DEFAULT_VOICE, // @ts-ignore-line speed: speed || DEFAULT_SPEECH_SPEED, }); const tempFile = join(tmpdir(), `${Date.now()}.wav`); await audio.save(tempFile); await player.play({ path: tempFile, sync: true }); }
- src/index.ts:29-32 (schema)TypeScript interface defining expected arguments for text_to_speech tool.interface TextToSpeechArgs { text: string; voice?: KokoroVoice; }
- src/index.ts:355-360 (registration)Registration of text_to_speech tool (as textToSpeechTool) in the ListToolsRequest handler response.tools: [ textToSpeechTool, textToSpeechWithOptionsTool, listVoicesTool, getModelStatusTool, ],