Skip to content

Commit a0980e4

Browse files
committed
Add MCP debugging scripts for SSH testing
1 parent a03e0dd commit a0980e4

6 files changed

Lines changed: 422 additions & 0 deletions

File tree

debug_scripts/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# MCP Debugging Scripts
2+
3+
This directory contains diagnostic scripts to help debug MCP (Model Context Protocol) connectivity issues in Azure App Service.
4+
5+
## Scripts
6+
7+
### 🔧 `test_mcp_local.py`
8+
Tests local MCP server connection within the App Service instance.
9+
- Tests `http://localhost:8000/mcp/stream`
10+
- Verifies MCP JSON-RPC tools/list
11+
- Tests tool execution
12+
13+
### 🌐 `test_mcp_external.py`
14+
Tests external MCP connection (how Azure AI Agents would connect).
15+
- Tests public app URL MCP endpoint
16+
- Simulates external client connectivity
17+
- Identifies network/CORS issues
18+
19+
### ☁️ `test_azure_ai.py`
20+
Tests Azure AI Agent integration and MCP tool creation.
21+
- Verifies environment variables
22+
- Tests Azure AI SDK imports
23+
- Tests credential and client initialization
24+
- Tests MCP tool creation
25+
26+
### 📊 `test_system_info.py`
27+
Collects system information and network diagnostics.
28+
- System and Python version info
29+
- Environment variables
30+
- Process and network status
31+
- File system information
32+
33+
### 🏃‍♂️ `run_all_tests.py`
34+
Runs all diagnostic tests in sequence with organized output.
35+
36+
## Usage in SSH Session
37+
38+
Once you SSH into your Azure App Service:
39+
40+
```bash
41+
# Navigate to the app directory
42+
cd /home/site/wwwroot
43+
44+
# Run individual tests
45+
python3 debug_scripts/test_mcp_local.py
46+
python3 debug_scripts/test_mcp_external.py
47+
python3 debug_scripts/test_azure_ai.py
48+
python3 debug_scripts/test_system_info.py
49+
50+
# Or run all tests at once
51+
python3 debug_scripts/run_all_tests.py
52+
```
53+
54+
## Expected Outputs
55+
56+
### ✅ Success Indicators
57+
- MCP server responds with JSON
58+
- Tools list shows 5 tools (create_todo, list_todos, etc.)
59+
- Azure AI client initializes successfully
60+
- External URL is accessible
61+
62+
### ❌ Failure Indicators
63+
- Connection refused errors
64+
- Timeout errors
65+
- Missing environment variables
66+
- Azure credential issues
67+
- Network policy restrictions
68+
69+
## Troubleshooting
70+
71+
Based on test results:
72+
73+
1. **Local works, External fails**: Network/CORS issue
74+
2. **Azure AI client fails**: Authentication/endpoint issue
75+
3. **All connections fail**: App not running or port issue
76+
4. **Intermittent failures**: Resource constraints or networking
77+
78+
These tests will help identify the exact point of failure in the MCP integration chain.

debug_scripts/run_all_tests.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Run all MCP diagnostic tests
4+
"""
5+
import sys
6+
import os
7+
8+
# Add the current directory to Python path
9+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
10+
11+
from test_system_info import test_system_info
12+
from test_mcp_local import test_mcp_connection
13+
from test_mcp_external import test_external_mcp
14+
from test_azure_ai import test_azure_ai_mcp
15+
16+
17+
def main():
18+
"""Run all diagnostic tests"""
19+
print("🔍 Starting MCP Diagnostic Tests")
20+
print("=" * 60)
21+
22+
try:
23+
print("\n📊 1. System Information")
24+
print("-" * 30)
25+
test_system_info()
26+
27+
print("\n🔧 2. Local MCP Tests")
28+
print("-" * 30)
29+
test_mcp_connection()
30+
31+
print("\n🌐 3. External MCP Tests")
32+
print("-" * 30)
33+
test_external_mcp()
34+
35+
print("\n☁️ 4. Azure AI Tests")
36+
print("-" * 30)
37+
test_azure_ai_mcp()
38+
39+
print("\n" + "=" * 60)
40+
print("✅ All diagnostic tests completed!")
41+
print("Check the output above for any ❌ errors or issues.")
42+
43+
except Exception as e:
44+
print(f"\n❌ Error running diagnostic tests: {e}")
45+
import traceback
46+
traceback.print_exc()
47+
48+
49+
if __name__ == "__main__":
50+
main()

debug_scripts/test_azure_ai.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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()

debug_scripts/test_mcp_external.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test external MCP connection (how Azure AI Agents would connect)
4+
"""
5+
import requests
6+
import json
7+
import os
8+
9+
# Your app's public URL
10+
APP_URL = os.getenv('AZURE_APP_SERVICE_URL', 'https://az-tda-app-wgznky2irncfe.azurewebsites.net')
11+
12+
13+
def test_external_mcp():
14+
"""Test external MCP connection like Azure AI Agents would"""
15+
try:
16+
print("=== Testing External MCP Connection ===")
17+
print(f"App URL: {APP_URL}")
18+
print(f"Testing: {APP_URL}/mcp/stream")
19+
20+
# Test server info from external URL
21+
print("\n--- Testing External Server Info ---")
22+
response = requests.get(f"{APP_URL}/mcp/stream", timeout=30)
23+
print(f"Status: {response.status_code}")
24+
print(f"Response: {response.text}")
25+
26+
# Test tools list from external URL
27+
print("\n--- Testing External Tools List ---")
28+
payload = {
29+
"jsonrpc": "2.0",
30+
"id": 1,
31+
"method": "tools/list"
32+
}
33+
response = requests.post(
34+
f"{APP_URL}/mcp/stream",
35+
json=payload,
36+
headers={"Content-Type": "application/json"},
37+
timeout=30
38+
)
39+
print(f"Status: {response.status_code}")
40+
print(f"Response: {json.dumps(response.json(), indent=2)}")
41+
42+
# Test tool execution from external URL
43+
print("\n--- Testing External Tool Execution ---")
44+
payload = {
45+
"jsonrpc": "2.0",
46+
"id": 2,
47+
"method": "tools/call",
48+
"params": {
49+
"name": "create_todo",
50+
"arguments": {
51+
"title": "External SSH Test Todo",
52+
"description": "Created via external MCP testing",
53+
"priority": "medium"
54+
}
55+
}
56+
}
57+
response = requests.post(
58+
f"{APP_URL}/mcp/stream",
59+
json=payload,
60+
headers={"Content-Type": "application/json"},
61+
timeout=30
62+
)
63+
print(f"Status: {response.status_code}")
64+
print(f"Response: {json.dumps(response.json(), indent=2)}")
65+
66+
print("\n✅ External MCP tests completed")
67+
68+
except Exception as e:
69+
print(f"❌ External MCP Error: {e}")
70+
import traceback
71+
traceback.print_exc()
72+
73+
74+
if __name__ == "__main__":
75+
test_external_mcp()

debug_scripts/test_mcp_local.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test MCP server connection locally within the App Service
4+
"""
5+
import requests
6+
import json
7+
8+
def test_mcp_connection():
9+
"""Test local MCP server connection"""
10+
try:
11+
print("=== Testing Local MCP Server Info ===")
12+
response = requests.get("http://localhost:8000/mcp/stream")
13+
print(f"Status: {response.status_code}")
14+
print(f"Response: {response.text}")
15+
16+
print("\n=== Testing MCP Tools List ===")
17+
payload = {
18+
"jsonrpc": "2.0",
19+
"id": 1,
20+
"method": "tools/list"
21+
}
22+
response = requests.post(
23+
"http://localhost:8000/mcp/stream",
24+
json=payload,
25+
headers={"Content-Type": "application/json"}
26+
)
27+
print(f"Status: {response.status_code}")
28+
print(f"Response: {json.dumps(response.json(), indent=2)}")
29+
30+
print("\n=== Testing create_todo Tool ===")
31+
payload = {
32+
"jsonrpc": "2.0",
33+
"id": 2,
34+
"method": "tools/call",
35+
"params": {
36+
"name": "create_todo",
37+
"arguments": {
38+
"title": "SSH Test Todo",
39+
"description": "Created via SSH testing",
40+
"priority": "high"
41+
}
42+
}
43+
}
44+
response = requests.post(
45+
"http://localhost:8000/mcp/stream",
46+
json=payload,
47+
headers={"Content-Type": "application/json"}
48+
)
49+
print(f"Status: {response.status_code}")
50+
print(f"Response: {json.dumps(response.json(), indent=2)}")
51+
52+
print("\n✅ Local MCP tests completed")
53+
54+
except Exception as e:
55+
print(f"❌ Error: {e}")
56+
import traceback
57+
traceback.print_exc()
58+
59+
if __name__ == "__main__":
60+
test_mcp_connection()

0 commit comments

Comments
 (0)