#!/usr/bin/env python3 """ Test script to validate MCP integration in TTS service. This script tests the MCP functionality without requiring GPU or full model loading. """ import asyncio import json import sys import os # Add the current directory to the path so we can import from app.py sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) async def test_mcp_integration(): """Test the MCP integration functionality""" print("๐Ÿงช Testing TTS MCP Integration...") try: # Try to import the MCP components from our app from app import MCP_AVAILABLE, mcp_server print(f"โœ… MCP Availability: {'Available' if MCP_AVAILABLE else 'Not Available'}") if not MCP_AVAILABLE: print("โš ๏ธ MCP not available - this is expected if mcp package is not installed") print("๐Ÿ” To install MCP: pip install mcp>=1.0.0") return True if mcp_server is None: print("โŒ MCP server not initialized properly") return False print("โœ… MCP server instance created successfully") # Test tool listing (this should work without GPU) try: # Import the handler function from app import handle_list_tools # Get the list of tools tools = await handle_list_tools() print(f"โœ… Available MCP tools ({len(tools)}):") for tool in tools: print(f" - {tool.name}: {tool.description}") # Verify we have the expected tools expected_tools = {"tts_synthesize", "tts_batch_synthesize", "tts_get_info"} actual_tools = {tool.name for tool in tools} if expected_tools == actual_tools: print("โœ… All expected tools are available") else: missing = expected_tools - actual_tools extra = actual_tools - expected_tools if missing: print(f"โŒ Missing tools: {missing}") if extra: print(f"โš ๏ธ Extra tools: {extra}") return False except Exception as e: print(f"โŒ Error testing tool listing: {e}") return False # Test the get_info tool (this should work without GPU) try: from app import handle_call_tool print("\n๐Ÿ” Testing tts_get_info tool...") info_result = await handle_call_tool("tts_get_info", {}) if info_result and len(info_result) > 0: info_data = json.loads(info_result[0].text) print("โœ… System info retrieved successfully") print(f" - MCP Status: {info_data.get('mcp_status', 'Unknown')}") print(f" - Available Tools: {len(info_data.get('available_tools', []))}") print(f" - Voice Presets: {len(info_data.get('voice_presets', []))}") else: print("โŒ Failed to get system info") return False except Exception as e: print(f"โŒ Error testing get_info tool: {e}") return False # Test error handling for invalid tool try: print("\n๐Ÿ” Testing error handling...") error_result = await handle_call_tool("invalid_tool", {}) if error_result and len(error_result) > 0: error_data = json.loads(error_result[0].text) if "error" in error_data and "Unknown tool" in error_data["error"]: print("โœ… Error handling works correctly") else: print("โŒ Unexpected error response format") return False else: print("โŒ No error response received") return False except Exception as e: print(f"โŒ Error testing error handling: {e}") return False print("\n๐ŸŽ‰ All MCP integration tests passed!") return True except ImportError as e: print(f"โŒ Import error: {e}") return False except Exception as e: print(f"โŒ Unexpected error: {e}") return False def test_voice_presets(): """Test that voice presets are properly defined""" try: from app import VOICE_PRESETS print(f"\n๐ŸŽญ Testing Voice Presets ({len(VOICE_PRESETS)} available):") if len(VOICE_PRESETS) == 0: print("โŒ No voice presets defined") return False for code, description in VOICE_PRESETS[:3]: # Show first 3 print(f" - {code}: {description}") if len(VOICE_PRESETS) > 3: print(f" ... and {len(VOICE_PRESETS) - 3} more") print("โœ… Voice presets loaded successfully") return True except Exception as e: print(f"โŒ Error testing voice presets: {e}") return False def main(): """Main test function""" print("=" * 60) print("๐Ÿš€ TTS Service MCP Integration Test") print("=" * 60) # Test voice presets first (synchronous) presets_ok = test_voice_presets() # Test MCP integration (asynchronous) mcp_ok = asyncio.run(test_mcp_integration()) print("\n" + "=" * 60) if presets_ok and mcp_ok: print("โœ… ALL TESTS PASSED - TTS MCP integration is ready!") print("\n๐Ÿ“‹ Summary:") print(" โœ… Voice presets configured") print(" โœ… MCP server setup complete") print(" โœ… MCP tools available") print(" โœ… Error handling working") print("\n๐Ÿš€ Ready to deploy TTS service with dual protocol support!") return 0 else: print("โŒ SOME TESTS FAILED - Check the issues above") return 1 if __name__ == "__main__": sys.exit(main())