Files
gns3-server/gns3server/agent/mcp/skills.py
T
yueguobin 21fb84e91c fix(skills): load categories independently and support non-git checkouts
SkillsManager refused to start when the local skills directory was not a
git repository (cloning into a non-empty path fails), and reload_skills()
returned early when injection/ was missing, so device and feature skills
never loaded. reload_packet_analysis_protocols() had the opposite bug:
an empty result wiped the registry while still reporting success.

- initialize(): accept a non-empty directory without .git as a local-only
  source (skip automatic updates, keep loading files); degrade to local
  files when GitPython is unavailable.
- _update_if_needed(): re-create/clone when the local directory vanished
  or moved, instead of reusing a stale handle forever.
- reload_skills(): injection, device and feature load independently; a
  missing category keeps its previously loaded entries.
- reload_packet_analysis_protocols(): an empty/missing directory keeps
  the existing registry and reports failure.
- reload_skills_repository(): drop the console guard cache so
  config/console_guard.txt applies on reload.

MCP device_skill_get now exposes the category filter, documents the
single-file (topic-less) layout, and returns a sections list for
index/summary of skills that have no topics/issues. An uninitialized
skills repository fails with a clear error instead of an empty list.
2026-11-01 23:09:38 +08:00

88 lines
3.4 KiB
Python

#
# Copyright (C) 2026 GNS3 Technologies Inc.
# Author: Yue Guobin
#
# This program 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.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
"""
device_skill_get — the MCP surface of the GNS3-Skills repository.
The copilot's DeviceSkillsTool already proved the access pattern
(list -> topic index -> single topic; bodies load ONLY on an explicit
topic request). This handler reuses the same framework-free core —
get_skill() / list_available_skills() over SKILLS_REGISTRY — instead of
the LangChain tool shell, so both surfaces stay in step by construction
and share the SkillsManager hot reload.
The protocol is multi-call by design: list -> a device's topic index ->
one topic body. The caller picks the key from the list and passes it
back unchanged; there is no name guessing in this layer.
"""
import logging
from typing import Any
log = logging.getLogger(__name__)
def device_skills_handler(params: dict[str, Any], gns3_ctx: dict[str, Any]) -> dict[str, Any]:
"""List or fetch skills from the GNS3-Skills repository.
action "list" -> every loaded skill (device_type, name, topic_count).
Otherwise one skill by the key the list returned.
"""
try:
from gns3server.agent.gns3_copilot.skills import registry
except Exception as e:
log.debug("device_skill_get: skills registry unavailable: %s", e)
return {"status": "failed", "error": f"Skills subsystem unavailable: {e}"}
# First call on a fresh server may race the background repository
# load; triggering the (idempotent, retryable) init avoids returning
# an empty list just because the clone has not finished yet.
if not registry.SKILLS_REGISTRY and registry.get_skills_manager() is None:
return {
"status": "failed",
"error": "Skills repository is not initialized. Configure the GNS3-Skills repository "
"(skills_repo_url) and retry — the initial clone may still be running.",
}
action = params.get("action")
if not action:
# No explicit action: list when nothing to get, get otherwise.
action = "list" if not params.get("device_type") else "get"
if action == "list":
skills = registry.list_available_skills(params.get("category"))
return {"status": "ok", "count": len(skills), "skills": skills}
device_type = params.get("device_type")
if not device_type:
return {
"status": "failed",
"error": "Provide device_type (the skill key)",
"hint": "device_skill_get(action='list') enumerates what is loaded",
}
result = registry.get_skill(
device_type,
category=params.get("category"),
detail=params.get("detail", "full"),
issue=params.get("issue"),
topic=params.get("topic"),
)
if "error" in result:
return {"status": "failed", **result}
return {"status": "ok", "skill": result}