File size: 3,067 Bytes
d36f611
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3
"""
Simple HTTP server for running MCP data display website
"""

import http.server
import socketserver
import json
import os
import webbrowser
from pathlib import Path

class CustomHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        # Add CORS headers to allow local file access
        self.send_header('Access-Control-Allow-Origin', '*')
        self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
        self.send_header('Access-Control-Allow-Headers', 'Content-Type')
        super().end_headers()

    def do_GET(self):
        # Redirect root path to display page
        if self.path == '/':
            self.path = '/index.html'
        
        # Handle JSON file requests, ensure correct Content-Type
        if self.path.endswith('.json'):
            try:
                # Get file path
                file_path = self.path.lstrip('/')
                
                # Check if file exists
                if os.path.exists(file_path):
                    self.send_response(200)
                    self.send_header('Content-Type', 'application/json; charset=utf-8')
                    self.end_headers()
                    
                    # Read and send JSON file
                    with open(file_path, 'rb') as f:
                        self.wfile.write(f.read())
                    return
                else:
                    self.send_error(404, f"File not found: {file_path}")
                    return
            except Exception as e:
                self.send_error(500, f"Error reading file: {str(e)}")
                return
        
        # Use default handling for other files
        super().do_GET()

def main():
    # Set server parameters
    PORT = 8000
    HOST = 'localhost'
    
    # Check if required files exist
    required_files = [
        'index.html',
        'styles.css', 
        'script.js',
        'mcpso_clients_cleaned.json',
        'mcpso_servers_cleaned.json'
    ]
    
    missing_files = [f for f in required_files if not os.path.exists(f)]
    if missing_files:
        print(f"โŒ Missing required files: {', '.join(missing_files)}")
        return
    
    # Create server
    with socketserver.TCPServer((HOST, PORT), CustomHTTPRequestHandler) as httpd:
        print(f"๐ŸŒ MCP data display website started successfully!")
        print(f"๐Ÿ“ Server address: http://{HOST}:{PORT}")
        print(f"๐Ÿ”— Access link: http://{HOST}:{PORT}/index.html")
        print(f"โน๏ธ  Press Ctrl+C to stop server")
        print()
        
        # Try to automatically open browser
        try:
            webbrowser.open(f'http://{HOST}:{PORT}/index.html')
            print("๐ŸŽ‰ Browser opened automatically")
        except:
            print("๐Ÿ’ก Please manually open the above link in your browser")
        
        print()
        
        try:
            httpd.serve_forever()
        except KeyboardInterrupt:
            print("\n๐Ÿ‘‹ Server stopped")

if __name__ == "__main__":
    main()