|
20 | 20 | import subprocess |
21 | 21 | import platform |
22 | 22 | import atexit |
| 23 | +import socket |
23 | 24 | from datetime import datetime, timezone |
24 | 25 | from typing import Dict, Any |
25 | 26 |
|
@@ -105,6 +106,18 @@ def _bootstrap_shared_memory() -> Dict[str, Any]: |
105 | 106 |
|
106 | 107 | _spawned_procs = [] |
107 | 108 |
|
| 109 | +def _port_in_use(host: str, port: int, timeout: float = 1.0) -> bool: |
| 110 | + """ |
| 111 | + Raw TCP connect check — works even for SSE/streaming servers that don't |
| 112 | + respond correctly to JSON-RPC probes (e.g. windows-mcp already running). |
| 113 | + Returns True if something is listening on that port, regardless of protocol. |
| 114 | + """ |
| 115 | + try: |
| 116 | + with socket.create_connection((host, port), timeout=timeout): |
| 117 | + return True |
| 118 | + except OSError: |
| 119 | + return False |
| 120 | + |
108 | 121 | def _wait_until_ready(check_fn, timeout=20, interval=0.5) -> bool: |
109 | 122 | deadline = time.time() + timeout |
110 | 123 | while time.time() < deadline: |
@@ -153,11 +166,19 @@ def start_services() -> None: |
153 | 166 | client.fs_available |
154 | 167 | ) |
155 | 168 | if platform.system() == 'Windows': |
156 | | - _start_service( |
157 | | - 'windows-mcp', |
158 | | - 'uvx windows-mcp serve --transport sse --host localhost --port 8000', |
159 | | - client.win_available |
160 | | - ) |
| 169 | + # windows-mcp uses SSE transport — its endpoint doesn't respond to |
| 170 | + # JSON-RPC initialize probes, so win_available() returns False even |
| 171 | + # when it's already running (e.g. survived a terminal close). |
| 172 | + # Check the port directly first before attempting a new launch. |
| 173 | + if _port_in_use('localhost', 8000): |
| 174 | + print('[BRIDGE] windows-mcp already running on port 8000') |
| 175 | + else: |
| 176 | + _start_service( |
| 177 | + 'windows-mcp', |
| 178 | + 'uvx windows-mcp serve --transport sse --host localhost --port 8000', |
| 179 | + lambda: _port_in_use('localhost', 8000), |
| 180 | + timeout=15 |
| 181 | + ) |
161 | 182 | else: |
162 | 183 | print('[BRIDGE] caios_mcp_client.py not found — MCP servers not auto-started') |
163 | 184 |
|
|
0 commit comments