Spaces:
Sleeping
Sleeping
File size: 3,898 Bytes
8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 def69a7 8372659 |
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 |
"""
Script to run either the MCP server or the Gradio interface
"""
import argparse
import importlib.util
import os
import sys
import time
import subprocess
def load_module(name, path):
"""Load a module from path"""
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
def run_mcp_server(host="127.0.0.1", port=8000, transport="streamable-http"):
"""Run the MCP server with specified configuration"""
print(f"Starting TutorX MCP Server on {host}:{port} using {transport} transport...")
# Set environment variables for MCP server
os.environ["MCP_HOST"] = host
os.environ["MCP_PORT"] = str(port)
os.environ["MCP_TRANSPORT"] = transport
main_module = load_module("main", "main.py")
# Access the mcp instance and run it
if hasattr(main_module, "run_server"):
main_module.run_server()
else:
print("Error: run_server function not found in main.py")
sys.exit(1)
def run_gradio_interface():
"""Run the Gradio interface"""
print("Starting TutorX Gradio Interface...")
app_module = load_module("app", "app.py")
# Run the Gradio demo
if hasattr(app_module, "demo"):
app_module.demo.launch()
else:
print("Error: Gradio demo not found in app.py")
sys.exit(1)
def check_port_available(port):
"""Check if a port is available"""
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(('127.0.0.1', port))
sock.close()
return True
except:
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Run TutorX MCP Server or Gradio Interface")
parser.add_argument(
"--mode",
choices=["mcp", "gradio", "both"],
default="mcp",
help="Run mode: 'mcp' for MCP server, 'gradio' for Gradio interface, 'both' for both"
)
parser.add_argument(
"--host",
default="127.0.0.1",
help="Host address to use"
)
parser.add_argument(
"--port",
type=int,
default=8000,
help="Port to use"
)
parser.add_argument(
"--transport",
choices=["stdio", "streamable-http", "sse"],
default="streamable-http",
help="Transport protocol to use"
)
args = parser.parse_args()
if args.mode == "mcp":
if not check_port_available(args.port):
print(f"Warning: Port {args.port} is already in use. Trying to use the server anyway...")
run_mcp_server(args.host, args.port, args.transport)
elif args.mode == "gradio":
run_gradio_interface()
elif args.mode == "both":
# For 'both' mode, we'll start MCP server in a separate process
if not check_port_available(args.port):
print(f"Warning: Port {args.port} is already in use. Trying to use the server anyway...")
# Start MCP server in a background process
mcp_process = subprocess.Popen(
[
sys.executable,
"run.py",
"--mode", "mcp",
"--host", args.host,
"--port", str(args.port),
"--transport", args.transport
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
# Give the MCP server a moment to start up
print("Starting MCP server in background...")
time.sleep(2)
try:
# Then start Gradio interface
run_gradio_interface()
finally:
# Make sure to terminate the MCP server process when exiting
print("Shutting down MCP server...")
mcp_process.terminate()
mcp_process.wait(timeout=5)
|