test_server.py•4.6 kB
"""
Test script for the Resemble AI MCP server.
This script tests the functionality of the MCP server by directly
calling the tool functions that would normally be called by Claude Desktop.
Updated: 2025-03-06 - Updated to support both implementations and the latest API changes
"""
import os
import sys
import json
import argparse
from dotenv import load_dotenv
# Load environment variables from .env
load_dotenv()
# Check if Resemble API key is set
if not os.getenv("RESEMBLE_API_KEY"):
print("Error: RESEMBLE_API_KEY environment variable is not set.")
print("Please create a .env file with your Resemble AI API key.")
exit(1)
# Parse command line arguments
parser = argparse.ArgumentParser(description='Test Resemble AI MCP server tools')
parser.add_argument('--implementation', choices=['direct', 'sdk'], default='direct',
help='Which implementation to test (direct API or SDK)')
parser.add_argument('--voice-id', help='Specific voice ID to test with (optional)')
args = parser.parse_args()
# Import the tools from the appropriate server implementation
try:
if args.implementation == 'sdk':
print("Testing SDK-based implementation...")
from resemble_ai_sdk_server import list_voices, generate_tts
else:
print("Testing direct API implementation...")
from resemble_ai_server import list_voices, generate_tts
print("Successfully imported tools from the MCP server.")
except ImportError as e:
print(f"Error importing tools from the MCP server: {e}")
exit(1)
def test_list_voices():
"""Test the list_voices tool."""
print("\n=== Testing list_voices ===")
try:
result = list_voices()
print(f"Found {len(result.voices)} voices.")
# Print first 3 voices as examples
for i, voice in enumerate(result.voices[:3]):
print(f"\nVoice {i+1}:")
print(f" ID: {voice['id']}")
print(f" Name: {voice['name']}")
print(f" Gender: {voice['gender']}")
print(f" Language: {voice['language']}")
return True, result.voices
except Exception as e:
print(f"Error testing list_voices: {e}")
return False, None
def test_generate_tts(voice_id=None):
"""Test the generate_tts tool."""
print("\n=== Testing generate_tts ===")
# If no voice_id provided, use the first one from list_voices
if not voice_id:
success, voices = test_list_voices()
if not success or not voices:
print("Cannot test generate_tts without available voices.")
return False
voice_id = voices[0]["id"]
print(f"Using voice ID: {voice_id}")
# Test text
test_text = "Hello, this is a test of the Resemble AI voice generation system."
try:
# Test file output
print("\nTesting file output:")
file_result = generate_tts(
text=test_text,
voice_id=voice_id,
return_type="file",
output_filename="test_output"
)
if file_result.success:
print(f"Success! Audio saved to: {file_result.file_path}")
else:
print(f"Failed: {file_result.message}")
# Test base64 output
print("\nTesting base64 output:")
base64_result = generate_tts(
text=test_text,
voice_id=voice_id,
return_type="base64"
)
if base64_result.success:
base64_preview = base64_result.audio_data[:50] + "..." if base64_result.audio_data else None
print(f"Success! Received base64 data: {base64_preview}")
else:
print(f"Failed: {base64_result.message}")
return file_result.success and base64_result.success
except Exception as e:
print(f"Error testing generate_tts: {e}")
return False
if __name__ == "__main__":
print(f"=== Resemble AI MCP Server Test ({args.implementation} implementation) ===")
# Test with specific voice ID if provided
if args.voice_id:
test_generate_tts(args.voice_id)
else:
# Test list_voices
success, voices = test_list_voices()
# Test generate_tts if voices are available
if success and voices:
# Use the first voice for testing
voice_id = voices[0]["id"]
test_generate_tts(voice_id)
else:
print("Skipping generate_tts test due to list_voices failure.")
print("\n=== Test Complete ===")