File size: 4,736 Bytes
1cf571b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
FleetMind MCP Server with Authentication Proxy
Launches both the FastMCP server and the authentication proxy.

This script:
1. Starts FastMCP server on port 7861 (internal)
2. Starts authentication proxy on port 7860 (public)
3. Handles graceful shutdown of both processes

Usage:
    python start_with_proxy.py
"""

import subprocess
import sys
import time
import signal
import os
from pathlib import Path

# Process handles
fastmcp_process = None
proxy_process = None


def signal_handler(sig, frame):
    """Handle Ctrl+C gracefully"""
    print("\n\nShutting down FleetMind MCP Server...")

    if proxy_process:
        print("  -> Stopping proxy...")
        proxy_process.terminate()
        try:
            proxy_process.wait(timeout=5)
        except subprocess.TimeoutExpired:
            proxy_process.kill()

    if fastmcp_process:
        print("  -> Stopping FastMCP server...")
        fastmcp_process.terminate()
        try:
            fastmcp_process.wait(timeout=5)
        except subprocess.TimeoutExpired:
            fastmcp_process.kill()

    print("[OK] FleetMind MCP Server stopped.")
    sys.exit(0)


def main():
    global fastmcp_process, proxy_process

    # Register signal handler
    signal.signal(signal.SIGINT, signal_handler)
    signal.signal(signal.SIGTERM, signal_handler)

    print("\n" + "=" * 70)
    print("FleetMind MCP Server with Multi-Tenant Authentication")
    print("=" * 70)
    print("Starting components:")
    print("  1. FastMCP Server (port 7861) - Internal")
    print("  2. Auth Proxy (port 7860) - Public")
    print("=" * 70 + "\n")

    # Get Python executable
    python_exe = sys.executable

    # Start FastMCP server
    print("[1/2] Starting FastMCP server on port 7861...")
    try:
        # Don't capture output - let it show in console
        fastmcp_process = subprocess.Popen(
            [python_exe, "app.py"]
        )
        print("[OK] FastMCP server starting (output below)...")
    except Exception as e:
        print(f"[ERROR] Failed to start FastMCP server: {e}")
        sys.exit(1)

    # Wait a bit for FastMCP to initialize
    print("   Waiting for FastMCP to initialize...")
    time.sleep(3)

    # Check if FastMCP is still running
    if fastmcp_process.poll() is not None:
        print("[ERROR] FastMCP server failed to start")
        print("   Check the error messages above")
        sys.exit(1)

    print("[OK] FastMCP server running")

    # Start proxy
    print("\n[2/2] Starting authentication proxy on port 7860...")
    try:
        # Don't capture output - let it show in console
        proxy_process = subprocess.Popen(
            [python_exe, "proxy.py"]
        )
        print("[OK] Auth proxy starting (output below)...")
    except Exception as e:
        print(f"[ERROR] Failed to start proxy: {e}")
        if fastmcp_process:
            fastmcp_process.terminate()
        sys.exit(1)

    # Wait a bit for proxy to initialize
    time.sleep(2)

    # Check if proxy is still running
    if proxy_process.poll() is not None:
        print("[ERROR] Proxy failed to start")
        if fastmcp_process:
            fastmcp_process.terminate()
        sys.exit(1)

    print("[OK] Auth proxy running")

    print("\n" + "=" * 70)
    print("[OK] FleetMind MCP Server is READY!")
    print("=" * 70)
    print(f"Connect to: http://localhost:7860/sse")
    print(f"Claude Desktop config:")
    print(f'   "args": ["mcp-remote", "http://localhost:7860/sse?api_key=YOUR_KEY"]')
    print("=" * 70)
    print("\n[AUTH] Multi-tenant authentication is ENABLED")
    print("   Each connection's API key will be captured automatically\n")
    print("Press Ctrl+C to stop...\n")

    # Monitor both processes
    try:
        while True:
            # Check FastMCP
            if fastmcp_process.poll() is not None:
                print("[ERROR] FastMCP server stopped unexpectedly")
                if proxy_process:
                    proxy_process.terminate()
                sys.exit(1)

            # Check proxy
            if proxy_process.poll() is not None:
                print("[ERROR] Proxy stopped unexpectedly")
                if fastmcp_process:
                    fastmcp_process.terminate()
                sys.exit(1)

            # Read and display logs (non-blocking)
            # This keeps the terminal responsive
            time.sleep(1)

    except KeyboardInterrupt:
        signal_handler(signal.SIGINT, None)


if __name__ == "__main__":
    # Check if required files exist
    if not Path("app.py").exists():
        print("[ERROR] app.py not found")
        sys.exit(1)

    if not Path("proxy.py").exists():
        print("[ERROR] proxy.py not found")
        sys.exit(1)

    main()