diff --git a/gns3server/api/routes/mcp/__init__.py b/gns3server/api/routes/mcp/__init__.py index e0765ffe7..032c8269a 100644 --- a/gns3server/api/routes/mcp/__init__.py +++ b/gns3server/api/routes/mcp/__init__.py @@ -571,16 +571,18 @@ async def node_console( Complete workflow: 1. Call this tool with project_id and node_id to get the WebSocket URL 2. Connect to the returned URL using websocat in text mode (-t): - > websocat -t "ws://:3080/v3/projects/{project_id}/nodes/{node_id}/console/ws?token={jwt_token}" + > websocat -t --no-close "ws://:3080/v3/projects/{project_id}/nodes/{node_id}/console/ws?token={jwt_token}" 3. Send device commands with \\r\\n line endings via heredoc: - > websocat -t "ws://..." <<< $'\\r\\nenable\\r\\nshow version\\r\\nexit\\r\\n' + > websocat -t --no-close "ws://..." <<< $'\\r\\nenable\\r\\nshow version\\r\\nexit\\r\\n' 4. Receive response: websocat receives and displays device output Use 'timeout' to avoid connection hanging: - > timeout 10 websocat -t "ws://..." <<< $'commands\\r\\n' + > timeout 10 websocat -t --no-close "ws://..." <<< $'commands\\r\\n' Key points: - Use \\r\\n (not \\n) to match console protocol line endings - Use $'...' format for escape sequences in bash + - --no-close keeps the WebSocket open after stdin (heredoc) hits EOF, so + device output is not cut off before it arrives - Set a timeout to prevent hanging connections """ return await asyncio.to_thread(_run_handler_sync, get_node_console_info_handler, { diff --git a/gns3server/api/routes/mcp/nodes.py b/gns3server/api/routes/mcp/nodes.py index 7c003d80a..dcdfc96c6 100644 --- a/gns3server/api/routes/mcp/nodes.py +++ b/gns3server/api/routes/mcp/nodes.py @@ -328,7 +328,7 @@ def get_node_console_info_handler(params: dict[str, Any], gns3_ctx: dict[str, An "node_name": node.get("name"), "console_type": console_type, "ws_url": ws_url, - "command": f"websocat {ws_url}", + "command": f"websocat -t --no-close {ws_url}", } if console_type in ("vnc",): result["vnc_url"] = f"/v3/projects/{project_id}/nodes/{node_id}/console/vnc?token={gns3_ctx['jwt_token']}"