From db9772ca7a60e2f235a25cf4f18e19dbe196f28b Mon Sep 17 00:00:00 2001 From: YueGuobin Date: Sat, 6 Jun 2026 00:48:22 +0800 Subject: [PATCH] fix: resolve MCP server URL host via default route IP when bound to 0.0.0.0 When GNS3 server is configured to listen on 0.0.0.0 (all interfaces), _server_url() was hardcoding 127.0.0.1, making the WebSocket console URL unreachable from remote MCP clients. Use the UDP connect trick (connect to 8.8.8.8:80 without sending data) to discover the default route interface IP, which is the address remote clients can actually reach. --- gns3server/api/routes/mcp/__init__.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/gns3server/api/routes/mcp/__init__.py b/gns3server/api/routes/mcp/__init__.py index 00a2770fb..8cf8d0bcb 100644 --- a/gns3server/api/routes/mcp/__init__.py +++ b/gns3server/api/routes/mcp/__init__.py @@ -30,6 +30,7 @@ import contextvars import json import asyncio import logging +import socket from typing import Any, Annotated from urllib.parse import parse_qs @@ -94,8 +95,14 @@ async def _validate_token(token: str) -> bool: def _server_url() -> str: cfg = Config.instance().settings host = cfg.Server.host - if host == "0.0.0.0": - host = "127.0.0.1" + if host in ("0.0.0.0", "::"): + try: + with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: + s.settimeout(0.1) + s.connect(("8.8.8.8", 80)) + host = s.getsockname()[0] + except OSError: + host = "127.0.0.1" scheme = "https" if cfg.Server.enable_ssl else "http" return f"{scheme}://{host}:{cfg.Server.port}"