text_to_speech
Convert text into speech and save as MP3 files for accessibility or content creation purposes.
Instructions
Convert the given text into speech and save as MP3 file
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| text | Yes | ||
| voice_id | No | JBFqnCBsd6RMkjVDRZzb |
Implementation Reference
- server.py:31-31 (registration)The @mcp.tool() decorator registers the text_to_speech tool with the MCP server.@mcp.tool()
- server.py:32-57 (handler)The handler function that implements the text_to_speech tool logic: converts text to speech using ElevenLabs API, saves the audio as an MP3 file with a unique name, and returns the filename or error.def text_to_speech(text: str, voice_id: str = "JBFqnCBsd6RMkjVDRZzb") -> str: """Convert the given text into speech and save as MP3 file""" import uuid try: # Generate unique filename audio_filename = f"speech_{uuid.uuid4().hex[:8]}.mp3" # Generate audio using ElevenLabs audio = client_elevenlabs.text_to_speech.convert( text=text, voice_id=voice_id, model_id="eleven_multilingual_v2" ) # Save audio to file with open(audio_filename, "wb") as f: for chunk in audio: if chunk: f.write(chunk) return f"Audio generated successfully! Saved as: {audio_filename}" except Exception as e: return f"Error generating speech: {str(e)}"