File size: 4,776 Bytes
8372659
1af10cc
8372659
 
 
 
1af10cc
 
 
 
8372659
1af10cc
 
 
 
 
 
 
 
 
def69a7
1af10cc
def69a7
 
 
1af10cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8372659
 
1af10cc
 
 
8372659
1af10cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8372659
 
def69a7
1af10cc
 
 
 
 
 
 
 
 
 
 
def69a7
8372659
1af10cc
 
 
 
 
8372659
 
1af10cc
8372659
bbd9cd6
1af10cc
8372659
 
 
1af10cc
bbd9cd6
1af10cc
8372659
 
1af10cc
 
bbd9cd6
1af10cc
bbd9cd6
 
 
1af10cc
bbd9cd6
1af10cc
def69a7
8372659
 
 
1af10cc
 
 
 
8372659
1af10cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8372659
1af10cc
 
 
 
 
 
 
 
 
 
 
8372659
1af10cc
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
"""
Script to run either the MCP server or the Gradio interface for TutorX
"""

import os
import sys
import argparse
import uvicorn
from pathlib import Path
import socket

def run_mcp_server(host="0.0.0.0", port=8001):
    """
    Run the MCP server using uvicorn
    
    Args:
        host: Host to bind the server to
        port: Port to run the server on
    """
    print(f"Starting TutorX MCP Server on {host}:{port}...")
    
    # Set environment variables
    os.environ["MCP_HOST"] = host
    os.environ["MCP_PORT"] = str(port)
    
    try:
        # Add the mcp-server directory to Python path
        mcp_server_dir = str(Path(__file__).parent / "mcp-server")
        if mcp_server_dir not in sys.path:
            sys.path.insert(0, mcp_server_dir)
        
        # Import the FastAPI app
        from server import api_app
        
        # Run the server using uvicorn
        uvicorn.run(
            "mcp-server.server:api_app",
            host=host,
            port=port,
            reload=True,
            reload_dirs=[mcp_server_dir],
            log_level="info"
        )
    except ImportError as e:
        print(f"Error: {e}")
        print("Make sure you have installed all required dependencies:")
        print("  pip install uvicorn fastapi")
        sys.exit(1)
    except Exception as e:
        print(f"Error starting MCP server: {e}")
        sys.exit(1)

def run_gradio_interface(port=7860):
    """
    Run the Gradio interface
    
    Args:
        port: Port to run the Gradio interface on
    """
    print(f"Starting TutorX Gradio Interface on port {port}...")
    
    try:
        # Make sure the mcp-server directory is in the path
        mcp_server_dir = str(Path(__file__).parent / "mcp-server")
        if mcp_server_dir not in sys.path:
            sys.path.insert(0, mcp_server_dir)
            
        # Import and run the Gradio app
        from app import demo
        
        # Launch the Gradio interface
        demo.launch(
            server_name="0.0.0.0",
            server_port=port,
            share=False
        )
    except Exception as e:
        print(f"Failed to start Gradio interface: {e}")
        sys.exit(1)

def check_port_available(port):
    """
    Check if a port is available
    
    Args:
        port: Port number to check
        
    Returns:
        bool: True if port is available, False otherwise
    """
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(('localhost', port)) != 0

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="TutorX - Run MCP Server and/or Gradio Interface"
    )
    
    # Add command line arguments
    parser.add_argument(
        "--mode", 
        type=str, 
        choices=["mcp", "gradio", "both"], 
        default="both",
        help="Run mode: 'mcp' for MCP server, 'gradio' for Gradio interface, 'both' for both (default)"
    )
    parser.add_argument(
        "--host", 
        type=str, 
        default="0.0.0.0",
        help="Host to bind the server to (default: 0.0.0.0)"
    )
    parser.add_argument(
        "--mcp-port", 
        type=int, 
        default=8001,
        help="Port for MCP server (default: 8001)"
    )
    parser.add_argument(
        "--gradio-port", 
        type=int, 
        default=7860,
        help="Port for Gradio interface (default: 7860)"
    )
    
    args = parser.parse_args()
    
    # Check if ports are available
    if args.mode in ["mcp", "both"] and not check_port_available(args.mcp_port):
        print(f"Error: Port {args.mcp_port} is already in use (MCP server)")
        sys.exit(1)
        
    if args.mode in ["gradio", "both"] and not check_port_available(args.gradio_port):
        print(f"Error: Port {args.gradio_port} is already in use (Gradio interface)")
        sys.exit(1)
    
    try:
        if args.mode in ["mcp", "both"]:
            # Start MCP server in a separate process
            mcp_process = multiprocessing.Process(
                target=run_mcp_server,
                kwargs={
                    "host": args.host,
                    "port": args.mcp_port
                }
            )
            mcp_process.start()
            
            # Give the server a moment to start
            time.sleep(2)
        
        if args.mode in ["gradio", "both"]:
            # Run Gradio in the main process
            run_gradio_interface(port=args.gradio_port)
            
    except KeyboardInterrupt:
        print("\nShutting down...")
    except Exception as e:
        print(f"Error: {e}")
        sys.exit(1)
    finally:
        if 'mcp_process' in locals() and mcp_process.is_alive():
            mcp_process.terminate()
            mcp_process.join(timeout=5)