Add play_audio_base64 tool to MCP server

This commit is contained in:
Adolfo Reyna
2026-06-01 14:53:22 -04:00
parent 612ba63c59
commit 3cd3428e94
+45
View File
@@ -251,6 +251,18 @@ class MCPServer:
"filename": {"type": "string", "description": "Destination file path (e.g. 'recording.pcm')", "default": "recording.pcm"}
}
}
},
{
"name": "play_audio_base64",
"description": "Decode a base64-encoded WAV file and play it directly on the speaker.",
"inputSchema": {
"type": "object",
"properties": {
"wav_base64": {"type": "string", "description": "Base64 encoded string of the WAV audio file"},
"volume": {"type": "integer", "description": "Volume from 0 to 100 (default 50)", "default": 50}
},
"required": ["wav_base64"]
}
}
]
},
@@ -477,5 +489,38 @@ class MCPServer:
else:
raise RuntimeError("Audio recording failed.")
elif name == "play_audio_base64":
b64_data = args.get("wav_base64")
vol = int(args.get("volume", 50))
vol = max(0, min(100, vol))
if not b64_data:
raise ValueError("Missing wav_base64 parameter.")
import binascii
try:
audio_bytes = binascii.a2b_base64(b64_data)
except Exception as e:
raise ValueError(f"Failed to decode base64 audio: {e}")
filename = "temp_play.wav"
with open(filename, "wb") as f:
f.write(audio_bytes)
try:
from audio_util import play_wav
success = play_wav(filename, volume=vol)
finally:
import os
try:
os.remove(filename)
except:
pass
if success:
return "Successfully played base64 audio stream."
else:
raise RuntimeError("Failed to play decoded audio stream. Check format (16kHz 16-bit PCM WAV recommended).")
else:
raise ValueError(f"Unknown tool: {name}")