π The fast, Pythonic way to build MCP servers and clients.
Install mounted skills under their own name in sync_skills (#6078)
nate nowack5e8dafd8297b
diff --git a/fastmcp_slim/fastmcp/utilities/skills.py b/fastmcp_slim/fastmcp/utilities/skills.pyindex 80baefae4..1ff807ef6 100644--- a/fastmcp_slim/fastmcp/utilities/skills.py+++ b/fastmcp_slim/fastmcp/utilities/skills.py@@ -12,10 +12,13 @@ from typing import TYPE_CHECKINGimport mcp_typesfrom fastmcp._warnings import FastMCPDeprecationWarning+from fastmcp.utilities.logging import get_loggerif TYPE_CHECKING:from fastmcp.client import Client+logger = get_logger(__name__)+@dataclassclass SkillSummary:@@ -154,11 +157,14 @@ async def download_skill() -> Path:"""Download a skill and all its files to a local directory.- Creates a subdirectory named after the skill containing all files.+ Creates a subdirectory named after the skill containing all files. A+ skill served at a nested path, such as a mounted server's `ops/deploy`,+ is installed as `deploy`, since agents look for skill folders one level+ deep.Args:client: Connected FastMCP client- skill_name: Name of the skill to download+ skill_name: Name or path of the skill to downloadtarget_dir: Directory where skill folder will be createdoverwrite: If True, overwrite existing skill directory. If False(default), raise FileExistsError if directory exists.@@ -185,10 +191,13 @@ async def download_skill(```"""target_dir = Path(target_dir).expanduser().resolve()- skill_dir = (target_dir / skill_name).resolve()+ segments = skill_name.split("/")+ skill_dir = (target_dir / segments[-1]).resolve()- # Security: ensure skill_dir stays within target_dir- if not skill_dir.is_relative_to(target_dir):+ # Security: reject relative segments and ensure skill_dir stays within target_dir+ if any(segment in ("", ".", "..") for segment in segments) or (+ not skill_dir.is_relative_to(target_dir)+ ):raise ValueError(f"Skill name {skill_name!r} would escape the target directory")# Check if directory exists@@ -244,6 +253,10 @@ async def sync_skills() -> list[Path]:"""Download all available skills from a server.+ Each skill is installed in a folder named after it. When two skills share+ a name, such as `ops/deploy` and `web/deploy`, only the first is+ installed.+Args:client: Connected FastMCP clienttarget_dir: Directory where skill folders will be created@@ -264,8 +277,18 @@ async def sync_skills("""skills = await list_skill_summaries(client)downloaded = []+ installed_names: set[str] = set()for skill in skills:+ folder_name = skill.name.rsplit("/", 1)[-1]+ if folder_name in installed_names:+ logger.warning(+ "Skipping skill %r: a skill named %r was already installed",+ skill.name,+ folder_name,+ )+ continue+ installed_names.add(folder_name)try:path = await download_skill(client, skill.name, target_dir, overwrite=overwritediff --git a/tests/utilities/test_skills.py b/tests/utilities/test_skills.pyindex b5309c79c..8dfeffd65 100644--- a/tests/utilities/test_skills.py+++ b/tests/utilities/test_skills.py@@ -483,3 +483,35 @@ class TestPathTraversal:async with Client(skills_server) as client:with pytest.raises(ValueError, match="would escape the target directory"):await download_skill(client, malicious_name, target)+++class TestMountedSkills:+ @pytest.fixture+ def mounted_server(self, skills_dir: Path) -> FastMCP:+ parent = FastMCP("parent")+ for namespace in ("ops", "web"):+ child = FastMCP(namespace, providers=[SkillsDirectoryProvider(skills_dir)])+ parent.mount(child, namespace=namespace)+ return parent++ async def test_download_installs_nested_skill_by_name(+ self, mounted_server: FastMCP, tmp_path: Path+ ):+ async with Client(mounted_server) as client:+ path = await download_skill(client, "ops/pdf-processing", tmp_path)++ assert path == tmp_path / "pdf-processing"+ assert (path / "SKILL.md").exists()++ async def test_sync_installs_first_of_colliding_names(+ self, mounted_server: FastMCP, tmp_path: Path+ ):+ async with Client(mounted_server) as client:+ results = await sync_skills(client, tmp_path)++ assert sorted(path.name for path in results) == [+ "code-review",+ "nested-skill",+ "pdf-processing",+ ]+ assert not (tmp_path / "ops").exists()