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.
This commit is contained in:
YueGuobin committed 2026-11-03 22:04:57 +08:00
1 parent 9c27a293d0
commit 6a4cbe39c1
34 files changed
+986 -2772

No files matched your search

+75 -43
View File
@@ -16,9 +16,9 @@ from gns3server.agent.gns3_copilot.utils.device_configs import (
merge_duplicate_device_configs,
)
VPCS_MOD = "gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko"
DISPLAY_MOD = "gns3server.agent.gns3_copilot.tools_v2.display_tools_nornir"
CONFIG_MOD = "gns3server.agent.gns3_copilot.tools_v2.config_tools_nornir"
VPCS_MOD = "gns3server.agent.gns3_copilot.gns3_client.vpcs_commands"
DISPLAY_MOD = "gns3server.agent.gns3_copilot.gns3_client.device_show_commands"
CONFIG_MOD = "gns3server.agent.gns3_copilot.gns3_client.device_config_commands"
PROJECT_ID = "0c0fde25-6ead-4413-a283-ea8fd2324291"
@@ -50,10 +50,10 @@ def _aggregated_result(output_by_device):
class TestVPCSNodeTypeGuard:
def test_non_vpcs_node_is_rejected(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
with patch(f"{VPCS_MOD}.get_device_ports_from_topology", return_value=_topology_ports("iou")) as topo:
result = VPCSCommands()._run(
result = VpcsCommandRunner().run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -68,10 +68,10 @@ class TestVPCSNodeTypeGuard:
assert "not a VPCS node" in result[0]["error"]
def test_missing_node_type_is_rejected(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
with patch(f"{VPCS_MOD}.get_device_ports_from_topology", return_value={"PC1": {"port": 5000}}):
result = VPCSCommands()._run(
result = VpcsCommandRunner().run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -83,18 +83,18 @@ class TestVPCSNodeTypeGuard:
assert "unknown-type" in result[0]["error"]
def test_vpcs_node_passes_the_guard(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
tool = VPCSCommands()
tool = VpcsCommandRunner()
nornir = MagicMock()
host_result = MagicMock(failed=False)
host_result.result = "OK"
nornir.run.return_value = {"PC1": host_result}
with (
patch(f"{VPCS_MOD}.get_device_ports_from_topology", return_value=_topology_ports("vpcs")),
patch.object(VPCSCommands, "_initialize_nornir", return_value=nornir),
patch.object(VpcsCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = tool._run(
result = tool.run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -106,18 +106,18 @@ class TestVPCSNodeTypeGuard:
assert result[0]["output"] == "OK"
def test_execution_failure_reports_failed_with_error(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
tool = VPCSCommands()
tool = VpcsCommandRunner()
nornir = MagicMock()
host_result = MagicMock(failed=True)
host_result.result = "Command failed (ReadTimeout)"
nornir.run.return_value = {"PC1": host_result}
with (
patch(f"{VPCS_MOD}.get_device_ports_from_topology", return_value=_topology_ports("vpcs")),
patch.object(VPCSCommands, "_initialize_nornir", return_value=nornir),
patch.object(VpcsCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = tool._run(
result = tool.run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -138,7 +138,7 @@ class TestVPCSConsoleHostPropagation:
"""
def test_remote_compute_hostname_reaches_nornir_inventory(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
with patch(
f"{VPCS_MOD}.get_device_ports_from_topology",
@@ -147,7 +147,7 @@ class TestVPCSConsoleHostPropagation:
"PC2": {"port": 5001, "node_type": "vpcs"},
},
):
hosts_data = VPCSCommands()._prepare_device_hosts_data(
hosts_data = VpcsCommandRunner()._prepare_device_hosts_data(
[
{"device_name": "PC1", "commands": ["ip 10.0.0.1/24"]},
{"device_name": "PC2", "commands": ["ip 10.0.0.2/24"]},
@@ -167,10 +167,10 @@ class TestDeviceToolErrorContract:
"""
def test_topology_level_error_has_status(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
with patch(f"{VPCS_MOD}.get_device_ports_from_topology", side_effect=ValueError("topology unreachable")):
result = VPCSCommands()._run(
result = VpcsCommandRunner().run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -181,15 +181,15 @@ class TestDeviceToolErrorContract:
assert result == [{"status": "failed", "error": "topology unreachable"}]
def test_config_tool_topology_level_error_has_status(self):
from gns3server.agent.gns3_copilot.tools_v2.config_tools_nornir import (
ExecuteMultipleDeviceConfigCommands,
from gns3server.agent.gns3_copilot.gns3_client.device_config_commands import (
DeviceConfigCommandRunner,
)
with patch(
"gns3server.agent.gns3_copilot.tools_v2.config_tools_nornir.get_device_ports_from_topology",
"gns3server.agent.gns3_copilot.gns3_client.device_config_commands.get_device_ports_from_topology",
side_effect=ValueError("no valid devices"),
):
result = ExecuteMultipleDeviceConfigCommands()._run(
result = DeviceConfigCommandRunner().run(
json.dumps(
{
"project_id": "0c0fde25-6ead-4413-a283-ea8fd2324291",
@@ -217,13 +217,45 @@ class TestDeviceToolErrorContract:
assert "required" in result[0]["error"]
def test_template_render_error_has_status(self):
from gns3server.agent.mcp.device_config import _render_template
from gns3server.agent.gns3_copilot.utils.device_configs import render_device_template
result = _render_template("{{ unclosed", [{"device_name": "R1", "vars": {"n": 1}}])
result = render_device_template("{{ unclosed", [{"device_name": "R1", "vars": {"n": 1}}])
assert len(result) == 1
assert result[0]["status"] == "failed"
assert "Template rendering failed" in result[0]["error"]
def test_template_flows_through_the_config_runner(self):
"""The optional Jinja2 template reaches the execution layer rendered."""
from gns3server.agent.gns3_copilot.gns3_client.device_config_commands import (
DeviceConfigCommandRunner,
)
nornir = MagicMock()
nornir.run.return_value = _aggregated_result({"R1": "ok", "R2": "ok"})
with (
patch(
f"{CONFIG_MOD}.get_device_ports_from_topology",
return_value={"R1": {"port": 5000, "node_type": "iou"}, "R2": {"port": 5001, "node_type": "iou"}},
),
patch.object(DeviceConfigCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = DeviceConfigCommandRunner().run(
{
"project_id": PROJECT_ID,
"template": "interface lo{{ n }}\nip address {{ ip }} 255.255.255.255",
"device_configs": [
{"device_name": "R1", "vars": {"n": 0, "ip": "1.1.1.1"}},
{"device_name": "R2", "vars": {"n": 1, "ip": "2.2.2.2"}},
],
}
)
assert nornir.run.call_args.kwargs["device_configs_map"] == {
"R1": ["interface lo0", "ip address 1.1.1.1 255.255.255.255"],
"R2": ["interface lo1", "ip address 2.2.2.2 255.255.255.255"],
}
assert [row["device_name"] for row in result] == ["R1", "R2"]
class TestDuplicateDeviceMerging:
"""
@@ -236,17 +268,17 @@ class TestDuplicateDeviceMerging:
"""
def test_show_run_merges_same_device_entries(self):
from gns3server.agent.gns3_copilot.tools_v2.display_tools_nornir import (
ExecuteMultipleDeviceCommands,
from gns3server.agent.gns3_copilot.gns3_client.device_show_commands import (
DeviceShowCommandRunner,
)
nornir = MagicMock()
nornir.run.return_value = _aggregated_result({"R1": "merged output", "R2": "r2 output"})
with (
patch(f"{DISPLAY_MOD}.get_device_ports_from_topology", return_value=_topology_two_devices()),
patch.object(ExecuteMultipleDeviceCommands, "_initialize_nornir", return_value=nornir),
patch.object(DeviceShowCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = ExecuteMultipleDeviceCommands()._run(
result = DeviceShowCommandRunner().run(
json.dumps(
{
"project_id": PROJECT_ID,
@@ -271,8 +303,8 @@ class TestDuplicateDeviceMerging:
assert result[0]["diagnostic_commands"] == ["show ip route", "show ip interface brief"]
def test_config_send_merges_same_device_entries(self):
from gns3server.agent.gns3_copilot.tools_v2.config_tools_nornir import (
ExecuteMultipleDeviceConfigCommands,
from gns3server.agent.gns3_copilot.gns3_client.device_config_commands import (
DeviceConfigCommandRunner,
)
nornir = MagicMock()
@@ -281,9 +313,9 @@ class TestDuplicateDeviceMerging:
patch(
f"{CONFIG_MOD}.get_device_ports_from_topology", return_value={"R1": {"port": 5000, "node_type": "iou"}}
),
patch.object(ExecuteMultipleDeviceConfigCommands, "_initialize_nornir", return_value=nornir),
patch.object(DeviceConfigCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = ExecuteMultipleDeviceConfigCommands()._run(
result = DeviceConfigCommandRunner().run(
json.dumps(
{
"project_id": PROJECT_ID,
@@ -303,7 +335,7 @@ class TestDuplicateDeviceMerging:
assert result[0]["config_commands"] == ["int lo0", "ip address 1.1.1.1 255.255.255.255", "exit"]
def test_vpcs_merges_same_device_entries(self):
from gns3server.agent.gns3_copilot.tools_v2.vpcs_tools_netmiko import VPCSCommands
from gns3server.agent.gns3_copilot.gns3_client.vpcs_commands import VpcsCommandRunner
nornir = MagicMock()
host_result = MagicMock(failed=False)
@@ -311,9 +343,9 @@ class TestDuplicateDeviceMerging:
nornir.run.return_value = {"PC1": host_result}
with (
patch(f"{VPCS_MOD}.get_device_ports_from_topology", return_value=_topology_ports("vpcs")),
patch.object(VPCSCommands, "_initialize_nornir", return_value=nornir),
patch.object(VpcsCommandRunner, "_initialize_nornir", return_value=nornir),
):
result = VPCSCommands()._run(
result = VpcsCommandRunner().run(
json.dumps(
{
"project_id": PROJECT_ID,
@@ -332,8 +364,8 @@ class TestDuplicateDeviceMerging:
def test_blocked_commands_accumulate_across_duplicate_entries(self):
"""Blocked-command info must not last-win between same-device entries."""
from gns3server.agent.gns3_copilot.tools_v2.display_tools_nornir import (
ExecuteMultipleDeviceCommands,
from gns3server.agent.gns3_copilot.gns3_client.device_show_commands import (
DeviceShowCommandRunner,
)
def fake_filter(commands):
@@ -347,10 +379,10 @@ class TestDuplicateDeviceMerging:
patch(
f"{DISPLAY_MOD}.get_device_ports_from_topology", return_value={"R1": {"port": 5000, "node_type": "iou"}}
),
patch.object(ExecuteMultipleDeviceCommands, "_initialize_nornir", return_value=nornir),
patch.object(DeviceShowCommandRunner, "_initialize_nornir", return_value=nornir),
patch(f"{DISPLAY_MOD}.filter_forbidden_commands", side_effect=fake_filter),
):
result = ExecuteMultipleDeviceCommands()._run(
result = DeviceShowCommandRunner().run(
json.dumps(
{
"project_id": PROJECT_ID,
@@ -368,11 +400,11 @@ class TestDuplicateDeviceMerging:
def test_entry_without_device_name_fails_the_whole_batch(self):
"""An unaddressable entry must abort with an error, not vanish."""
from gns3server.agent.gns3_copilot.tools_v2.display_tools_nornir import (
ExecuteMultipleDeviceCommands,
from gns3server.agent.gns3_copilot.gns3_client.device_show_commands import (
DeviceShowCommandRunner,
)
result = ExecuteMultipleDeviceCommands()._run(
result = DeviceShowCommandRunner().run(
json.dumps(
{
"project_id": PROJECT_ID,
+11
View File
@@ -138,6 +138,17 @@ def _get_handler_params(handler_name):
and sub.func.value.id in ("params", "params_data")
):
return {"*"}
# Same for a handler that delegates the WHOLE params dict to a callee
# (e.g. device_config_send_handler → DeviceConfigCommandRunner().run()):
# the callee decides what to read, so the per-key check cannot see
# through it. isinstance(params, dict) is a type check, not a forward.
for sub in ast.walk(node):
if (
isinstance(sub, ast.Call)
and not (isinstance(sub.func, ast.Name) and sub.func.id == "isinstance")
and any(isinstance(a, ast.Name) and a.id == "params" for a in sub.args)
):
return {"*"}
# Found the handler function, search for params.get("xxx")
for sub in ast.walk(node):
if not isinstance(sub, ast.Call):
+196
View File
@@ -0,0 +1,196 @@
# 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
#
"""
Tests for the copilot ← MCP tool bridge (tools_v2/mcp_bridge.py).
The bridge reuses the MCP server's tool registration as the single source
of truth for the copilot's LangChain tools. These tests pin the filter
(project-scoped allowlist, no administration tools), the drift guard on
requested names, the args-model generation, and the sync/async context
bridging.
"""
import asyncio
import json
from typing import Any
import pytest
from gns3server.agent.gns3_copilot.tools_v2 import mcp_bridge
from gns3server.agent.gns3_copilot.tools_v2.mcp_bridge import (
MCP_ALLOWED_TOOLS,
NATIVE_TOOL_NAMES,
_run_coro_sync,
_unwrap_mcp_content,
adapted_mcp_tools,
)
def test_allowlist_excludes_administration_tools() -> None:
"""The copilot runs inside one project: management tools stay out."""
banned = {
"project_list",
"project_create",
"project_delete",
"project_open",
"project_close",
"template_create",
"template_update",
"template_delete",
"compute_list",
"compute_get",
"appliance_list",
"appliance_install",
"image_list",
"image_delete",
"image_prune",
"server_version",
"server_statistics",
}
assert banned & MCP_ALLOWED_TOOLS == set()
def test_every_allowed_tool_exists_on_the_mcp_server() -> None:
"""The allowlist must not reference tools the MCP server does not register."""
from gns3server.agent.mcp import mcp
registered = {tool.name for tool in mcp._tool_manager.list_tools()}
assert MCP_ALLOWED_TOOLS <= registered
def test_adapted_pool_is_allowlist_minus_native_shadows() -> None:
tools = adapted_mcp_tools()
names = {tool.name for tool in tools}
assert names == MCP_ALLOWED_TOOLS - NATIVE_TOOL_NAMES
# link_create is served by the copilot-native wrapper (port-name
# resolution) and must not be adapted.
assert "link_create" not in names
def test_adapted_subset_preserves_order_and_drift_guard() -> None:
tools = adapted_mcp_tools(names=["node_start", "node_stop"])
assert [tool.name for tool in tools] == ["node_start", "node_stop"]
with pytest.raises(RuntimeError, match="not available to the copilot"):
adapted_mcp_tools(names=["node_start", "no_such_tool"])
def test_adapted_args_model_matches_mcp_schema() -> None:
tool = next(t for t in adapted_mcp_tools() if t.name == "node_create")
fields = tool.args_schema.model_fields
# required parameter carried over from the MCP JSON schema
assert "project_id" in fields
assert fields["project_id"].is_required()
# optional parameters keep their MCP defaults
assert fields["compute_id"].default == "local"
assert fields["x"].default == 0
# **kwargs-style MCP tools surface a plain dict "kwargs" parameter
node_update = next(t for t in adapted_mcp_tools() if t.name == "node_update")
assert "kwargs" in node_update.args_schema.model_fields
def test_unwrap_mcp_content() -> None:
assert _unwrap_mcp_content([{"type": "text", "text": json.dumps({"a": 1})}]) == {"a": 1}
assert _unwrap_mcp_content([{"type": "text", "text": "plain text"}]) == "plain text"
assert _unwrap_mcp_content({"already": "a dict"}) == {"already": "a dict"}
assert _unwrap_mcp_content("bare string") == "bare string"
def test_run_coro_sync_without_running_loop() -> None:
async def value() -> int:
return 42
assert _run_coro_sync(value()) == 42
def test_run_coro_sync_inside_running_loop() -> None:
"""Sync tool code executing on the event loop thread must still work."""
async def value() -> int:
return 7
async def main() -> int:
return _run_coro_sync(value())
assert asyncio.run(main()) == 7
class _FakeMcpTool:
"""Minimal stand-in for an mcp.server.mcpserver Tool."""
name = "fake_probe"
description = "Fake tool for bridge tests."
parameters: dict[str, Any] = {
"properties": {"x": {"type": "integer", "description": "a number"}},
"required": ["x"],
"type": "object",
}
async def fn(self, x: int) -> list[dict[str, str]]:
# Report the contextvars the bridge was supposed to set, proving
# they survived the thread/loop hop into the handler context.
from gns3server.agent.mcp import _jwt_token_var, _jwt_token_version_var, _jwt_username_var
return [
{
"type": "text",
"text": json.dumps(
{
"x": x,
"token": _jwt_token_var.get(),
"username": _jwt_username_var.get(),
"token_version": _jwt_token_version_var.get(),
}
),
}
]
class _FakeTokenData:
username = "tester"
token_version = 3
def test_invoke_bridges_copilot_context_into_mcp_handler(monkeypatch: pytest.MonkeyPatch) -> None:
"""The copilot's JWT must reach the MCP contextvars inside the handler."""
monkeypatch.setattr(
mcp_bridge, "build_gns3_ctx", lambda: {"jwt_token": "copilot-jwt", "server_url": "http://127.0.0.1:3080"}
)
monkeypatch.setattr(mcp_bridge.auth_service, "get_token_data", lambda token: _FakeTokenData())
tool = mcp_bridge._adapt(_FakeMcpTool())
result = tool.invoke({"x": 5})
assert result == {"x": 5, "token": "copilot-jwt", "username": "tester", "token_version": 3}
def test_invoke_without_token_returns_error(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(mcp_bridge, "build_gns3_ctx", lambda: None)
tool = mcp_bridge._adapt(_FakeMcpTool())
result = tool.invoke({"x": 5})
assert "error" in result