mirror of
https://github.com/GNS3/gns3-server.git
synced 2026-11-22 14:05:19 +02:00
The copilot and the MCP service maintained two parallel wrapper layers (LangChain BaseTool classes vs @mcp.tool() functions) over the same handlers in gns3_client/api_handlers.py. Descriptions and parameters drifted apart (compute_id and link_type were missing on the copilot side). This makes the MCP registration the single source of truth and removes the duplicated wrappers. tools_v2/mcp_bridge.py adapts the MCP server's tool registration into LangChain tools: names, descriptions and argument schemas come from the MCP tools. The pool is filtered to project-scoped tools (MCP_ALLOWED_TOOLS) because the copilot runs inside one open project — project management, compute/appliance/image administration and server info tools stay out. The bridge carries the copilot's request JWT into the MCP contextvars (username/token_version decoded from the token so ticket-minting tools work) and runs the async MCP functions from the copilot's sync tool node on either thread flavor. Mode toolsets (teaching / lab_automation / troubleshooting_injection) are now composed from adapted MCP tools plus a few copilot-native ones that add value the MCP versions lack: link_create (port-name resolution), wait_seconds, packet_analysis(+skills), and the topology reader. Eight orphaned LangChain wrappers are deleted. The device command tools had the dependency inverted (the MCP handlers instantiated the copilot LangChain classes and round-tripped through JSON strings). The Nornir/Netmiko classes moved verbatim (git mv) into the shared layer as plain runners — DeviceConfigCommandRunner, DeviceShowCommandRunner, VpcsCommandRunner — with _run(json_string) becoming run(params, gns3_ctx). The optional Jinja2 template rendering moved from the MCP handlers into the runners (and into utils/device_configs.render_device_template), so both entry paths get it; the MCP handlers are now pure delegation. This also drops the json.dumps/json.loads detour the MCP path used to take. test_tool_params consistency gate extended: a handler that forwards the whole params dict to a callee is a wildcard (same rationale as the params.items() case) — the per-key check cannot see through delegation.
108 lines
3.7 KiB
Python
108 lines
3.7 KiB
Python
# SPDX-License-Identifier: GPL-3.0-or-later
|
|
#
|
|
# GNS3-Copilot - AI-powered Network Lab Assistant for GNS3
|
|
#
|
|
# This file is part of GNS3-Copilot project.
|
|
#
|
|
# GNS3-Copilot is free software: you can redistribute it and/or modify it
|
|
# under the terms of the GNU General Public License as published by the
|
|
# Free Software Foundation, either version 3 of the License, or (at your
|
|
# option) any later version.
|
|
#
|
|
# GNS3-Copilot is distributed in the hope that it will be useful, but
|
|
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
# for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with GNS3-Copilot. If not, see <https://www.gnu.org/licenses/>.
|
|
#
|
|
# Copyright (C) 2025 Yue Guobin (岳国宾)
|
|
# Author: Yue Guobin (岳国宾)
|
|
#
|
|
# Project Home: https://github.com/yueguobin/gns3-copilot
|
|
#
|
|
"""
|
|
|
|
GNS3-Copilot wait tool.
|
|
|
|
node_start returns as soon as the start commands are accepted — nodes
|
|
keep booting in the background. This tool gives the agent a deliberate
|
|
pause it controls itself (instead of a hard-coded progress bar inside the
|
|
start tool), so the usual flow is: node_start → wait_seconds → check
|
|
node status / run show commands.
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
import time
|
|
from typing import Any
|
|
|
|
from langchain.tools import BaseTool
|
|
from langchain_core.callbacks import CallbackManagerForToolRun
|
|
|
|
# Configure logging
|
|
logger = logging.getLogger(__name__)
|
|
|
|
# Hard ceiling so a hallucinated "wait 999999" cannot wedge the agent loop.
|
|
MAX_WAIT_SECONDS = 600
|
|
# Log a liveness line every few seconds so long waits are visible in the
|
|
# server log.
|
|
LOGBOOK_TICK = 5
|
|
|
|
|
|
class GNS3WaitTool(BaseTool):
|
|
"""
|
|
A LangChain tool that sleeps for a given number of seconds.
|
|
|
|
**Input**:
|
|
A JSON object with seconds (integer, 1-600).
|
|
Example:
|
|
{"seconds": 30}
|
|
|
|
**Output**:
|
|
{"waited": 30}
|
|
"""
|
|
|
|
name: str = "wait_seconds"
|
|
description: str = """
|
|
Pause execution for a given number of seconds (1-600), then continue.
|
|
Use after node_start (which returns immediately) to let nodes
|
|
boot before checking status: VPCS/IOU ~15-30s, IOS/IOL routers
|
|
~60-120s, heavy NOS images (XRd, SR Linux) 2-5min. Prefer several
|
|
short waits with a status check in between over one long blind wait.
|
|
Input: JSON with seconds (integer).
|
|
Returns: {"waited": <seconds>}.
|
|
"""
|
|
|
|
def _run(
|
|
self,
|
|
tool_input: str,
|
|
run_manager: CallbackManagerForToolRun | None = None,
|
|
) -> dict[str, Any]:
|
|
try:
|
|
input_data = json.loads(tool_input)
|
|
seconds = input_data.get("seconds")
|
|
if isinstance(seconds, str) and seconds.strip().isdigit():
|
|
seconds = int(seconds.strip())
|
|
if not isinstance(seconds, int) or isinstance(seconds, bool):
|
|
return {"error": "seconds must be an integer (1-600)."}
|
|
if not 1 <= seconds <= MAX_WAIT_SECONDS:
|
|
return {"error": f"seconds must be between 1 and {MAX_WAIT_SECONDS}."}
|
|
|
|
logger.info("Waiting %d seconds...", seconds)
|
|
waited = 0
|
|
while waited < seconds:
|
|
tick = min(LOGBOOK_TICK, seconds - waited)
|
|
time.sleep(tick)
|
|
waited += tick
|
|
logger.info("Waited %d/%d seconds", waited, seconds)
|
|
return {"waited": waited}
|
|
|
|
except json.JSONDecodeError as e:
|
|
logger.error("Invalid JSON input: %s", e)
|
|
return {"error": f"Invalid JSON input: {e}"}
|
|
except Exception as e:
|
|
logger.error("Wait tool failed: %s", e)
|
|
return {"error": f"Wait tool failed: {e!s}"}
|