Files
gns3-server/gns3server/agent/gns3_copilot/tools_v2/__init__.py
T
YueGuobin 6a4cbe39c1 refactor(copilot): reuse MCP tools via a bridge; extract shared device runners
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.
2026-11-03 22:04:57 +08:00

76 lines
2.6 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 Tools Package
Most copilot tools are now adapted from the MCP server registration via
``mcp_bridge.adapted_mcp_tools()`` — the MCP tools are the single source
of truth for names, descriptions and argument schemas (see
``mcp_bridge.MCP_ALLOWED_TOOLS`` for the project-scoped filter).
This package keeps only the copilot-native tools that add behavior the
MCP counterparts lack:
- gns3_create_link: link creation with port-NAME resolution ("link_create";
shadows the adapted MCP tool of the same name)
- gns3_wait: boot wait timer ("wait_seconds"; pairs with node_start)
- packet_analysis_tool: server-side tshark pcap analysis ("packet_analysis";
no MCP counterpart by design)
The Nornir/Netmiko device command runners live in the shared layer as
plain classes (gns3_client/device_config_commands.py, device_show_commands.py,
vpcs_commands.py) behind the MCP device_config_send / device_show_run /
vpcs_config_set tools, which the copilot uses through the bridge.
Author: Yue Guobin (岳国宾)
"""
# Import main tool classes
from .gns3_create_link import GNS3LinkTool
from .gns3_wait import GNS3WaitTool
from .mcp_bridge import adapted_mcp_tools
from .packet_analysis_tool import PacketAnalysisTool
# Dynamic version management
try:
from importlib.metadata import version
__version__ = version("gns3-copilot")
except Exception:
__version__ = "unknown"
__author__ = "Yue Guobin (岳国宾)"
__description__ = "AI-powered network automation assistant for GNS3"
__url__ = "https://github.com/yueguobin/gns3-copilot"
# Export main tool classes and the MCP bridge
__all__ = [
"GNS3LinkTool",
"GNS3WaitTool",
"PacketAnalysisTool",
"adapted_mcp_tools",
]