|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test MCP functionality with non-auth toolkits. |
| 4 | +
|
| 5 | +This script tests MCP server creation and usage with toolkits that don't require authentication: |
| 6 | +- composio_search: COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH |
| 7 | +- text_to_pdf: TEXT_TO_PDF_CONVERT_TEXT_TO_PDF |
| 8 | +""" |
| 9 | + |
| 10 | +import os |
| 11 | +import time |
| 12 | +from composio import Composio |
| 13 | + |
| 14 | +def test_mcp_with_no_auth_toolkits(): |
| 15 | + """Test MCP with toolkits that don't require authentication.""" |
| 16 | + |
| 17 | + # Check API key |
| 18 | + api_key = os.getenv('COMPOSIO_API_KEY') |
| 19 | + if not api_key: |
| 20 | + print("❌ COMPOSIO_API_KEY environment variable not set") |
| 21 | + return False |
| 22 | + |
| 23 | + print("🔧 Testing MCP with Non-Auth Toolkits") |
| 24 | + print("=" * 50) |
| 25 | + |
| 26 | + try: |
| 27 | + # Initialize Composio |
| 28 | + composio = Composio() |
| 29 | + print("✅ Composio client initialized") |
| 30 | + |
| 31 | + # Create MCP server with non-auth toolkits |
| 32 | + server_name = f'no-auth-test-{int(time.time()) % 1000000}' |
| 33 | + print(f"🚀 Creating MCP server: {server_name}") |
| 34 | + |
| 35 | + mcp_server = composio.experimental.mcp.create(server_name, { |
| 36 | + 'toolkits': [ |
| 37 | + { |
| 38 | + 'toolkit': 'composio_search', |
| 39 | + 'allowed_tools': ['COMPOSIO_SEARCH_DUCK_DUCK_GO_SEARCH'] |
| 40 | + }, |
| 41 | + { |
| 42 | + 'toolkit': 'text_to_pdf', |
| 43 | + 'allowed_tools': ['TEXT_TO_PDF_CONVERT_TEXT_TO_PDF'] |
| 44 | + } |
| 45 | + ], |
| 46 | + 'manually_manage_connections': False |
| 47 | + }) |
| 48 | + |
| 49 | + print("✅ MCP server created successfully!") |
| 50 | + print(f" Server ID: {mcp_server.id}") |
| 51 | + print(f" Server Name: {mcp_server.name}") |
| 52 | + print(f" Toolkits: {getattr(mcp_server, 'toolkits', 'N/A')}") |
| 53 | + |
| 54 | + # Generate server instance for a test user |
| 55 | + test_user_id = 'test_user_no_auth_123' |
| 56 | + print(f"\n🔗 Generating server instance for user: {test_user_id}") |
| 57 | + |
| 58 | + server_instance = mcp_server.generate(test_user_id) |
| 59 | + |
| 60 | + print("✅ Server instance generated successfully!") |
| 61 | + print(f" Instance ID: {server_instance['id']}") |
| 62 | + print(f" Instance Type: {server_instance['type']}") |
| 63 | + print(f" Instance URL: {server_instance['url']}") |
| 64 | + print(f" User ID: {server_instance['user_id']}") |
| 65 | + print(f" Allowed Tools: {server_instance['allowed_tools']}") |
| 66 | + print(f" Auth Configs: {server_instance['auth_configs']}") |
| 67 | + |
| 68 | + # Test direct generate method as well |
| 69 | + print(f"\n🔄 Testing direct generate method...") |
| 70 | + |
| 71 | + direct_instance = composio.experimental.mcp.generate( |
| 72 | + test_user_id + '_direct', |
| 73 | + mcp_server.id, |
| 74 | + {'manually_manage_connections': False} |
| 75 | + ) |
| 76 | + |
| 77 | + print("✅ Direct generate method successful!") |
| 78 | + print(f" Direct Instance URL: {direct_instance['url']}") |
| 79 | + print(f" Direct Instance User ID: {direct_instance['user_id']}") |
| 80 | + |
| 81 | + # Test URL connectivity (basic check) |
| 82 | + print(f"\n🌐 Testing MCP URL connectivity...") |
| 83 | + |
| 84 | + import requests |
| 85 | + mcp_url = server_instance['url'] |
| 86 | + |
| 87 | + try: |
| 88 | + # Test with SSE-appropriate headers |
| 89 | + headers = { |
| 90 | + 'Accept': 'text/event-stream, application/json, */*', |
| 91 | + 'Cache-Control': 'no-cache', |
| 92 | + 'User-Agent': 'Composio-Python-MCP-Test' |
| 93 | + } |
| 94 | + |
| 95 | + response = requests.get(mcp_url, headers=headers, timeout=5, stream=True) |
| 96 | + |
| 97 | + print(f"✅ MCP URL is accessible!") |
| 98 | + print(f" Status Code: {response.status_code}") |
| 99 | + print(f" Content-Type: {response.headers.get('Content-Type', 'N/A')}") |
| 100 | + |
| 101 | + # Try to read first event |
| 102 | + try: |
| 103 | + chunk = next(response.iter_content(chunk_size=1024, decode_unicode=True)) |
| 104 | + print(f" First Event: {chunk.strip()[:100]}...") |
| 105 | + except StopIteration: |
| 106 | + print(" No initial content received") |
| 107 | + |
| 108 | + except requests.exceptions.Timeout: |
| 109 | + print("⚠️ MCP URL timeout (normal for SSE endpoints)") |
| 110 | + except Exception as e: |
| 111 | + print(f"⚠️ MCP URL test failed: {e}") |
| 112 | + |
| 113 | + print(f"\n📊 Test Summary:") |
| 114 | + print(f" ✅ MCP Server Created: {mcp_server.id}") |
| 115 | + print(f" ✅ Server Instance Generated: {server_instance['type']}") |
| 116 | + print(f" ✅ Direct Generate Method: Working") |
| 117 | + print(f" ✅ Available Tools: {len(server_instance['allowed_tools'])} tools") |
| 118 | + print(f" ✅ No Auth Required: {len(server_instance['auth_configs'])} auth configs") |
| 119 | + |
| 120 | + return True |
| 121 | + |
| 122 | + except Exception as e: |
| 123 | + print(f"❌ Test failed: {e}") |
| 124 | + import traceback |
| 125 | + traceback.print_exc() |
| 126 | + return False |
| 127 | + |
| 128 | +def main(): |
| 129 | + """Main test function.""" |
| 130 | + print("🎯 MCP Non-Auth Toolkits Test") |
| 131 | + print("Testing composio_search and text_to_pdf toolkits") |
| 132 | + print() |
| 133 | + |
| 134 | + success = test_mcp_with_no_auth_toolkits() |
| 135 | + |
| 136 | + if success: |
| 137 | + print("\n🎉 All tests passed! MCP is working with non-auth toolkits.") |
| 138 | + else: |
| 139 | + print("\n💥 Tests failed. Check the error messages above.") |
| 140 | + |
| 141 | + return success |
| 142 | + |
| 143 | +if __name__ == "__main__": |
| 144 | + main() |
| 145 | + |
0 commit comments