Skip to content

🚀 The fast, Pythonic way to build MCP servers and clients.

fix!: Resolve skill files within their configured directories (#5459)

Jeremiah Lowinb5c9b56fda3c
diff --git a/docs/servers/providers/skills.mdx b/docs/servers/providers/skills.mdx
index 58792fb69..7c2e2d478 100644
--- a/docs/servers/providers/skills.mdx
+++ b/docs/servers/providers/skills.mdx
@@ -111,7 +111,7 @@ skill://pdf-processing/examples/sample.pdf
```
<Note>
-Supporting-file access is confined to the skill directory. Requested paths are validated before any filesystem access: attempts to traverse out with `..`, inject an absolute path, or smuggle a null byte are rejected with a clear error, and symlinks that resolve outside the skill directory are refused.
+Skill files, including the main file, must resolve within the skill directory. Links to files within that directory are supported. Directory providers discover skill folders that resolve within each configured root; select a folder directly with `SkillProvider` to use a skill stored elsewhere.
</Note>
## Provider Architecture
diff --git a/fastmcp_slim/fastmcp/server/providers/skills/directory_provider.py b/fastmcp_slim/fastmcp/server/providers/skills/directory_provider.py
index c390b42f5..4c80e1dd1 100644
--- a/fastmcp_slim/fastmcp/server/providers/skills/directory_provider.py
+++ b/fastmcp_slim/fastmcp/server/providers/skills/directory_provider.py
@@ -6,6 +6,8 @@ from collections.abc import Sequence
from pathlib import Path
from typing import Literal
+from mcp.shared.path_security import PathEscapeError, safe_join
+
from fastmcp.resources.base import Resource
from fastmcp.resources.template import ResourceTemplate
from fastmcp.server.providers.aggregate import AggregateProvider
@@ -86,6 +88,14 @@ class SkillsDirectoryProvider(AggregateProvider):
continue
for skill_dir in root.iterdir():
+ try:
+ skill_dir = safe_join(root, skill_dir.name)
+ except (PathEscapeError, OSError, RuntimeError):
+ logger.warning(
+ f"Skipping skill directory that cannot be used: {skill_dir.name}"
+ )
+ continue
+
if not skill_dir.is_dir():
continue
@@ -110,7 +120,7 @@ class SkillsDirectoryProvider(AggregateProvider):
)
self.providers.append(provider)
seen_skill_names.add(skill_name)
- except (FileNotFoundError, PermissionError, OSError):
+ except (FileNotFoundError, PermissionError, OSError, PathEscapeError):
logger.exception(f"Failed to load skill: {skill_dir.name}")
self._discovered = True
diff --git a/fastmcp_slim/fastmcp/server/providers/skills/skill_provider.py b/fastmcp_slim/fastmcp/server/providers/skills/skill_provider.py
index 41a8c766b..f27f6dafb 100644
--- a/fastmcp_slim/fastmcp/server/providers/skills/skill_provider.py
+++ b/fastmcp_slim/fastmcp/server/providers/skills/skill_provider.py
@@ -54,7 +54,7 @@ class SkillResource(Resource):
if self.is_manifest:
return self._generate_manifest()
else:
- main_file_path = self.skill_info.path / self.skill_info.main_file
+ main_file_path = safe_join(self.skill_info.path, self.skill_info.main_file)
return main_file_path.read_text(encoding="utf-8")
def _generate_manifest(self) -> str:
@@ -225,11 +225,11 @@ class SkillProvider(Provider):
def _load_skill(self) -> None:
"""Load and parse the skill directory."""
- main_file = self._skill_path / self._main_file_name
-
if not self._skill_path.exists():
raise FileNotFoundError(f"Skill directory not found: {self._skill_path}")
+ main_file = safe_join(self._skill_path, self._main_file_name)
+
if not main_file.exists():
raise FileNotFoundError(
f"Main skill file not found: {main_file}. "
diff --git a/tests/server/providers/test_skill_main_files.py b/tests/server/providers/test_skill_main_files.py
new file mode 100644
index 000000000..30a05e081
--- /dev/null
+++ b/tests/server/providers/test_skill_main_files.py
@@ -0,0 +1,154 @@
+"""Main files and directory entries in skill providers."""
+
+from pathlib import Path
+
+import pytest
+from mcp.shared.path_security import PathEscapeError, safe_join
+from mcp_types import TextResourceContents
+
+from fastmcp import Client, FastMCP
+from fastmcp.server.providers.skills import SkillProvider, SkillsDirectoryProvider
+
+
+def make_skill(path: Path, content: str = "# Instructions") -> Path:
+ path.mkdir()
+ (path / "SKILL.md").write_text(content, encoding="utf-8")
+ return path
+
+
+def test_main_file_must_resolve_inside_skill_directory(tmp_path: Path) -> None:
+ skill = make_skill(tmp_path / "skill")
+ outside = tmp_path / "instructions.md"
+ outside.write_text("# Other instructions", encoding="utf-8")
+ (skill / "SKILL.md").unlink()
+ (skill / "SKILL.md").symlink_to(outside)
+
+ with pytest.raises(PathEscapeError):
+ SkillProvider(skill)
+
+
+async def test_main_file_is_checked_again_when_read(tmp_path: Path) -> None:
+ skill = make_skill(tmp_path / "skill")
+ provider = SkillProvider(skill)
+ resource = await provider.get_resource("skill://skill/SKILL.md")
+ assert resource is not None
+ outside = tmp_path / "instructions.md"
+ outside.write_text("# Other instructions", encoding="utf-8")
+ (skill / "SKILL.md").unlink()
+ (skill / "SKILL.md").symlink_to(outside)
+
+ with pytest.raises(PathEscapeError):
+ await resource.read()
+
+
[email protected]("entry_link", [False, True])
+async def test_directory_keeps_other_skills_when_an_entry_is_outside(
+ tmp_path: Path, entry_link: bool
+) -> None:
+ root = tmp_path / "skills"
+ root.mkdir()
+ make_skill(root / "ordinary")
+ outside = make_skill(tmp_path / "other")
+ if entry_link:
+ (root / "linked").symlink_to(outside, target_is_directory=True)
+ else:
+ linked = make_skill(root / "linked")
+ (linked / "SKILL.md").unlink()
+ (linked / "SKILL.md").symlink_to(outside / "SKILL.md")
+
+ provider = SkillsDirectoryProvider(root)
+ resources = await provider.list_resources()
+
+ assert {resource.name for resource in resources} == {
+ "ordinary/SKILL.md",
+ "ordinary/_manifest",
+ }
+
+
[email protected]("main_file_name", ["SKILL.md", "docs/MAIN.md"])
+async def test_main_file_links_within_skill_directory_remain_readable(
+ tmp_path: Path, main_file_name: str
+) -> None:
+ skill = tmp_path / "skill"
+ skill.mkdir()
+ content = "# Instructions\n\nRésumé"
+ target = skill / "instructions.md"
+ target.write_text(content, encoding="utf-8")
+ main_file = skill / main_file_name
+ main_file.parent.mkdir(exist_ok=True)
+ main_file.symlink_to(target)
+ server = FastMCP()
+ server.add_provider(SkillProvider(skill, main_file_name=main_file_name))
+
+ async with Client(server) as client:
+ result = await client.read_resource(f"skill://skill/{main_file_name}")
+
+ assert isinstance(result[0], TextResourceContents)
+ assert result[0].text == content
+
+
+async def test_explicit_skill_root_can_be_a_directory_link(tmp_path: Path) -> None:
+ actual = make_skill(tmp_path / "actual")
+ link = tmp_path / "selected"
+ link.symlink_to(actual, target_is_directory=True)
+ resources = await SkillProvider(link).list_resources()
+
+ assert {resource.name for resource in resources} == {
+ "actual/SKILL.md",
+ "actual/_manifest",
+ }
+
+
+async def test_directory_links_within_root_remain_discoverable(tmp_path: Path) -> None:
+ root = tmp_path / "skills"
+ root.mkdir()
+ actual = make_skill(root / "actual")
+ (root / "linked").symlink_to(actual, target_is_directory=True)
+ resources = await SkillsDirectoryProvider(root).list_resources()
+
+ assert {resource.name for resource in resources} == {
+ "actual/SKILL.md",
+ "actual/_manifest",
+ }
+
+
+async def test_directory_keeps_the_selected_target_when_a_link_changes(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ root = tmp_path / "skills"
+ root.mkdir()
+ selected = make_skill(root / "selected", "# Selected instructions")
+ other = make_skill(tmp_path / "other", "# Other instructions")
+ link = root / "linked"
+ link.symlink_to(selected, target_is_directory=True)
+
+ def select_path(base: Path, name: str) -> Path:
+ resolved = safe_join(base, name)
+ if name == "linked":
+ link.unlink()
+ link.symlink_to(other, target_is_directory=True)
+ return resolved
+
+ monkeypatch.setattr(
+ "fastmcp.server.providers.skills.directory_provider.safe_join", select_path
+ )
+ resources = await SkillsDirectoryProvider(root).list_resources()
+ assert {resource.name for resource in resources} == {
+ "selected/SKILL.md",
+ "selected/_manifest",
+ }
+
+
+async def test_directory_keeps_valid_skills_when_a_link_cannot_resolve(
+ tmp_path: Path,
+) -> None:
+ root = tmp_path / "skills"
+ root.mkdir()
+ make_skill(root / "ordinary")
+ unresolved = root / "unresolved"
+ unresolved.symlink_to(unresolved, target_is_directory=True)
+ resources = await SkillsDirectoryProvider(root).list_resources()
+ assert {resource.name for resource in resources} == {
+ "ordinary/SKILL.md",
+ "ordinary/_manifest",
+ }