|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Test Azure AI Agent integration with MCP server |
| 4 | +""" |
| 5 | +import os |
| 6 | +import json |
| 7 | + |
| 8 | + |
| 9 | +def test_azure_ai_mcp(): |
| 10 | + """Test Azure AI Agent MCP connection""" |
| 11 | + try: |
| 12 | + print("=== Testing Azure AI Agent MCP Connection ===") |
| 13 | + |
| 14 | + # Check environment variables |
| 15 | + print("\n--- Environment Variables ---") |
| 16 | + endpoint = os.getenv('AZURE_AI_PROJECT_ENDPOINT') |
| 17 | + project_name = os.getenv('AZURE_AI_PROJECT_NAME') |
| 18 | + deployment_name = os.getenv('AZURE_OPENAI_DEPLOYMENT_NAME') |
| 19 | + app_url = os.getenv('AZURE_APP_SERVICE_URL') |
| 20 | + |
| 21 | + print(f"AZURE_AI_PROJECT_ENDPOINT: {endpoint}") |
| 22 | + print(f"AZURE_AI_PROJECT_NAME: {project_name}") |
| 23 | + print(f"AZURE_OPENAI_DEPLOYMENT_NAME: {deployment_name}") |
| 24 | + print(f"AZURE_APP_SERVICE_URL: {app_url}") |
| 25 | + |
| 26 | + if not all([endpoint, project_name, deployment_name, app_url]): |
| 27 | + print("❌ Missing required environment variables") |
| 28 | + return |
| 29 | + |
| 30 | + # Test Azure AI imports |
| 31 | + print("\n--- Testing Azure AI Imports ---") |
| 32 | + try: |
| 33 | + from azure.identity import DefaultAzureCredential |
| 34 | + from azure.ai.agents import AgentsClient |
| 35 | + from azure.ai.agents.models import McpTool |
| 36 | + print("✅ Azure AI packages imported successfully") |
| 37 | + except ImportError as e: |
| 38 | + print(f"❌ Azure AI import failed: {e}") |
| 39 | + return |
| 40 | + |
| 41 | + # Test credential initialization |
| 42 | + print("\n--- Testing Azure Credential ---") |
| 43 | + try: |
| 44 | + credential = DefaultAzureCredential() |
| 45 | + print("✅ DefaultAzureCredential created") |
| 46 | + except Exception as e: |
| 47 | + print(f"❌ Credential error: {e}") |
| 48 | + return |
| 49 | + |
| 50 | + # Test Azure AI client initialization |
| 51 | + print("\n--- Testing Azure AI Client ---") |
| 52 | + try: |
| 53 | + # Get the proper agents endpoint |
| 54 | + if 'cognitiveservices.azure.com' in endpoint: |
| 55 | + resource_name = endpoint.replace('https://', '').replace('.cognitiveservices.azure.com/', '').replace('.cognitiveservices.azure.com', '') |
| 56 | + agents_endpoint = f"https://{resource_name}.services.ai.azure.com/api/projects/{project_name}" |
| 57 | + else: |
| 58 | + agents_endpoint = endpoint |
| 59 | + |
| 60 | + print(f"Agents endpoint: {agents_endpoint}") |
| 61 | + client = AgentsClient(endpoint=agents_endpoint, credential=credential) |
| 62 | + print("✅ AgentsClient created successfully") |
| 63 | + except Exception as e: |
| 64 | + print(f"❌ Azure AI client error: {e}") |
| 65 | + import traceback |
| 66 | + traceback.print_exc() |
| 67 | + return |
| 68 | + |
| 69 | + # Test MCP tool creation |
| 70 | + print("\n--- Testing MCP Tool Creation ---") |
| 71 | + try: |
| 72 | + mcp_url = f"{app_url}/mcp/stream" |
| 73 | + print(f"MCP URL: {mcp_url}") |
| 74 | + |
| 75 | + mcp_tool = McpTool(mcp_server_url=mcp_url) |
| 76 | + print(f"✅ MCP Tool created: {mcp_tool}") |
| 77 | + except Exception as e: |
| 78 | + print(f"❌ MCP Tool creation error: {e}") |
| 79 | + import traceback |
| 80 | + traceback.print_exc() |
| 81 | + return |
| 82 | + |
| 83 | + print("\n✅ Azure AI MCP tests completed successfully") |
| 84 | + |
| 85 | + except Exception as e: |
| 86 | + print(f"❌ Unexpected error: {e}") |
| 87 | + import traceback |
| 88 | + traceback.print_exc() |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + test_azure_ai_mcp() |
0 commit comments