Graham Paasch commited on
Commit
188e027
·
1 Parent(s): 5fb828f

Add diagnostic tool for troubleshooting GNS3/MCP connection

Browse files

- Comprehensive test script to verify all components
- Tests environment variables, GNS3 HTTP API, and MCP server
- Color-coded output with clear pass/fail status
- Helpful troubleshooting hints for common issues
- Run with: python3 test_connection.py

Files changed (1) hide show
  1. test_connection.py +248 -0
test_connection.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Test script to diagnose GNS3 connection and MCP server issues
4
+ """
5
+
6
+ import os
7
+ import sys
8
+ import time
9
+ import requests
10
+ import subprocess
11
+ import json
12
+
13
+ # Colors for terminal output
14
+ GREEN = '\033[92m'
15
+ RED = '\033[91m'
16
+ YELLOW = '\033[93m'
17
+ BLUE = '\033[94m'
18
+ RESET = '\033[0m'
19
+
20
+ def test_env_vars():
21
+ """Test environment variables"""
22
+ print(f"\n{BLUE}=== Testing Environment Variables ==={RESET}")
23
+
24
+ gns3_server = os.getenv('GNS3_SERVER', 'NOT SET')
25
+ project_name = os.getenv('GNS3_PROJECT_NAME', 'NOT SET')
26
+
27
+ print(f"GNS3_SERVER: {gns3_server}")
28
+ print(f"GNS3_PROJECT_NAME: {project_name}")
29
+
30
+ if gns3_server == 'NOT SET':
31
+ print(f"{RED}❌ GNS3_SERVER not set in environment{RESET}")
32
+ return False
33
+
34
+ print(f"{GREEN}✅ Environment variables configured{RESET}")
35
+ return True
36
+
37
+ def test_gns3_connection():
38
+ """Test direct HTTP connection to GNS3 server"""
39
+ print(f"\n{BLUE}=== Testing GNS3 Server Connection ==={RESET}")
40
+
41
+ server = os.getenv('GNS3_SERVER', 'http://localhost:3080')
42
+
43
+ try:
44
+ print(f"Connecting to: {server}/v2/version")
45
+ start_time = time.time()
46
+ response = requests.get(f"{server}/v2/version", timeout=10)
47
+ elapsed = time.time() - start_time
48
+
49
+ print(f"Response time: {elapsed:.2f}s")
50
+ print(f"Status code: {response.status_code}")
51
+
52
+ if response.status_code == 200:
53
+ data = response.json()
54
+ print(f"GNS3 version: {data.get('version', 'unknown')}")
55
+ print(f"{GREEN}✅ GNS3 server is reachable{RESET}")
56
+ return True
57
+ else:
58
+ print(f"{RED}❌ GNS3 server returned error: {response.status_code}{RESET}")
59
+ return False
60
+
61
+ except requests.exceptions.Timeout:
62
+ print(f"{RED}❌ Connection timed out after 10 seconds{RESET}")
63
+ print(f"{YELLOW}This suggests network connectivity issues{RESET}")
64
+ return False
65
+ except requests.exceptions.ConnectionError as e:
66
+ print(f"{RED}❌ Connection failed: {e}{RESET}")
67
+ return False
68
+ except Exception as e:
69
+ print(f"{RED}❌ Unexpected error: {e}{RESET}")
70
+ return False
71
+
72
+ def test_gns3_projects():
73
+ """Test fetching GNS3 projects"""
74
+ print(f"\n{BLUE}=== Testing GNS3 Projects API ==={RESET}")
75
+
76
+ server = os.getenv('GNS3_SERVER', 'http://localhost:3080')
77
+
78
+ try:
79
+ print(f"Fetching projects from: {server}/v2/projects")
80
+ start_time = time.time()
81
+ response = requests.get(f"{server}/v2/projects", timeout=10)
82
+ elapsed = time.time() - start_time
83
+
84
+ print(f"Response time: {elapsed:.2f}s")
85
+
86
+ if response.status_code == 200:
87
+ projects = response.json()
88
+ print(f"Found {len(projects)} projects")
89
+
90
+ # Look for overgrowth project
91
+ overgrowth = [p for p in projects if 'overgrowth' in p['name'].lower()]
92
+ if overgrowth:
93
+ print(f"{GREEN}✅ Found overgrowth project: {overgrowth[0]['name']}{RESET}")
94
+ print(f" Project ID: {overgrowth[0]['project_id']}")
95
+ print(f" Status: {overgrowth[0].get('status', 'unknown')}")
96
+ return True
97
+ else:
98
+ print(f"{YELLOW}⚠️ overgrowth project not found{RESET}")
99
+ return False
100
+ else:
101
+ print(f"{RED}❌ Failed to fetch projects: {response.status_code}{RESET}")
102
+ return False
103
+
104
+ except Exception as e:
105
+ print(f"{RED}❌ Error: {e}{RESET}")
106
+ return False
107
+
108
+ def test_mcp_server():
109
+ """Test MCP server directly"""
110
+ print(f"\n{BLUE}=== Testing MCP Server ==={RESET}")
111
+
112
+ # Check if server file exists
113
+ server_path = "mcp-server/server.py"
114
+ python_path = "/home/gpaasch/overgrowth-mcp-server/venv/bin/python"
115
+
116
+ if not os.path.exists(server_path):
117
+ print(f"{RED}❌ MCP server not found at: {server_path}{RESET}")
118
+ return False
119
+
120
+ if not os.path.exists(python_path):
121
+ print(f"{YELLOW}⚠️ Venv python not found, using system python{RESET}")
122
+ python_path = sys.executable
123
+
124
+ print(f"Server: {server_path}")
125
+ print(f"Python: {python_path}")
126
+
127
+ # Prepare MCP request
128
+ init_request = {
129
+ "jsonrpc": "2.0",
130
+ "id": 1,
131
+ "method": "initialize",
132
+ "params": {
133
+ "protocolVersion": "2024-11-05",
134
+ "capabilities": {},
135
+ "clientInfo": {"name": "test", "version": "1.0"}
136
+ }
137
+ }
138
+
139
+ projects_request = {
140
+ "jsonrpc": "2.0",
141
+ "id": 2,
142
+ "method": "tools/call",
143
+ "params": {
144
+ "name": "get_projects",
145
+ "arguments": {}
146
+ }
147
+ }
148
+
149
+ input_data = json.dumps(init_request) + "\n" + json.dumps(projects_request) + "\n"
150
+
151
+ try:
152
+ print("Calling MCP server...")
153
+ start_time = time.time()
154
+
155
+ result = subprocess.run(
156
+ [python_path, server_path],
157
+ input=input_data,
158
+ capture_output=True,
159
+ text=True,
160
+ timeout=30
161
+ )
162
+
163
+ elapsed = time.time() - start_time
164
+ print(f"Response time: {elapsed:.2f}s")
165
+ print(f"Exit code: {result.returncode}")
166
+
167
+ # Parse response
168
+ lines = result.stdout.strip().split('\n')
169
+ found_response = False
170
+
171
+ for line in lines:
172
+ if line.startswith('INFO:') or line.startswith('WARNING:'):
173
+ continue
174
+
175
+ try:
176
+ response = json.loads(line)
177
+ if response.get('id') == 2:
178
+ found_response = True
179
+ if 'error' in response:
180
+ print(f"{RED}❌ MCP server returned error: {response['error']}{RESET}")
181
+ return False
182
+
183
+ content = response.get('result', {}).get('content', [])
184
+ if content:
185
+ text = content[0].get('text', '')
186
+ projects = json.loads(text)
187
+ print(f"{GREEN}✅ MCP server working! Found {len(projects)} projects{RESET}")
188
+ return True
189
+ except json.JSONDecodeError:
190
+ continue
191
+
192
+ if not found_response:
193
+ print(f"{RED}❌ No valid response from MCP server{RESET}")
194
+ if result.stderr:
195
+ print(f"Stderr: {result.stderr[:500]}")
196
+ return False
197
+
198
+ except subprocess.TimeoutExpired:
199
+ print(f"{RED}❌ MCP server timed out after 30 seconds{RESET}")
200
+ return False
201
+ except Exception as e:
202
+ print(f"{RED}❌ Error: {e}{RESET}")
203
+ return False
204
+
205
+ def main():
206
+ print(f"{BLUE}{'='*60}")
207
+ print("Overgrowth GNS3 Connection Diagnostic Tool")
208
+ print(f"{'='*60}{RESET}")
209
+
210
+ # Load .env if available
211
+ try:
212
+ from dotenv import load_dotenv
213
+ load_dotenv()
214
+ print(f"{GREEN}✅ Loaded .env file{RESET}")
215
+ except ImportError:
216
+ print(f"{YELLOW}⚠️ python-dotenv not installed{RESET}")
217
+
218
+ results = []
219
+
220
+ # Run tests
221
+ results.append(("Environment Variables", test_env_vars()))
222
+ results.append(("GNS3 Connection", test_gns3_connection()))
223
+ results.append(("GNS3 Projects", test_gns3_projects()))
224
+ results.append(("MCP Server", test_mcp_server()))
225
+
226
+ # Summary
227
+ print(f"\n{BLUE}=== Test Summary ==={RESET}")
228
+ passed = sum(1 for _, result in results if result)
229
+ total = len(results)
230
+
231
+ for name, result in results:
232
+ status = f"{GREEN}✅ PASS{RESET}" if result else f"{RED}❌ FAIL{RESET}"
233
+ print(f"{name:.<40} {status}")
234
+
235
+ print(f"\n{passed}/{total} tests passed")
236
+
237
+ if passed == total:
238
+ print(f"\n{GREEN}🎉 All tests passed! Your setup is working correctly.{RESET}")
239
+ else:
240
+ print(f"\n{RED}❌ Some tests failed. Check the output above for details.{RESET}")
241
+ print(f"\n{YELLOW}Common fixes:{RESET}")
242
+ print("1. Ensure GNS3 server is running at lab.grahampaasch.com:3080")
243
+ print("2. Check network connectivity (firewall, VPN, etc.)")
244
+ print("3. Verify .env file has correct GNS3_SERVER URL")
245
+ print("4. Check that overgrowth project exists in GNS3")
246
+
247
+ if __name__ == "__main__":
248
+ main()