Skip to content

Commit 5ecd4a2

Browse files
committed
fix: skip proxy server startup when port 8081 is already in use
When Claude Desktop and Claude Code both load the MCP, the second instance crashed because uvicorn couldn't bind port 8081. Now checks port availability first and skips startup gracefully, leaving all MCP tools fully operational against the already-running proxy.
1 parent cdf7ba1 commit 5ecd4a2

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/fpd_mcp/main.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,12 +1716,24 @@ async def run_hybrid_server(enable_always_on: bool = True, proxy_port: int = 808
17161716

17171717
# Start proxy server immediately if always-on mode is enabled
17181718
if enable_always_on:
1719-
logger.info(f"Always-on mode: Starting HTTP proxy server immediately on port {proxy_port}")
1720-
_proxy_server_task = asyncio.create_task(_run_proxy_server(proxy_port))
1721-
_proxy_server_running = True
1722-
# Brief wait to ensure server starts cleanly
1723-
await asyncio.sleep(0.5)
1724-
logger.info(f"Proxy server started successfully on port {proxy_port}")
1719+
import socket
1720+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
1721+
port_free = s.connect_ex(("127.0.0.1", proxy_port)) != 0
1722+
1723+
if not port_free:
1724+
logger.info(
1725+
"Port %d already in use — skipping proxy server startup "
1726+
"(another instance is running; MCP tools are still fully available)",
1727+
proxy_port,
1728+
)
1729+
_proxy_server_running = True # treat as running so tools work
1730+
else:
1731+
logger.info(f"Always-on mode: Starting HTTP proxy server immediately on port {proxy_port}")
1732+
_proxy_server_task = asyncio.create_task(_run_proxy_server(proxy_port))
1733+
_proxy_server_running = True
1734+
# Brief wait to ensure server starts cleanly
1735+
await asyncio.sleep(0.5)
1736+
logger.info(f"Proxy server started successfully on port {proxy_port}")
17251737
else:
17261738
# Legacy on-demand mode: proxy starts on first download request
17271739
logger.info(f"On-demand mode: Proxy will start on first document request (port {proxy_port})")

0 commit comments

Comments
 (0)