Skip to content

πŸš€ The fast, Pythonic way to build MCP servers and clients.

Fix skill discovery for nested supporting files (#6310)

Mahek Arab483b827f94d
diff --git a/fastmcp_slim/fastmcp/client/mixins/skills.py b/fastmcp_slim/fastmcp/client/mixins/skills.py
index 6a5c76c3a..f35129042 100644
--- a/fastmcp_slim/fastmcp/client/mixins/skills.py
+++ b/fastmcp_slim/fastmcp/client/mixins/skills.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import base64
+import bisect
import hashlib
import json
from typing import TYPE_CHECKING, cast
@@ -214,16 +215,56 @@ class ClientSkillsMixin:
Skills extension, such as FastMCP 4.1 and earlier. Pass a returned
resource's `uri` to `read_skill()` to fetch the whole skill.
+ When skill paths overlap, advertised manifests distinguish supporting
+ `SKILL.md` files from independent skills. Entries are kept if their
+ containing skill's manifest is unavailable or invalid.
+
Returns:
The `SKILL.md` resource of each skill, in the server's order.
"""
resources = await self.list_resources()
- return [
+ skills = [
resource
for resource in resources
if str(resource.uri).startswith("skill://")
and str(resource.uri).endswith("/SKILL.md")
]
+ resource_uris = {str(resource.uri) for resource in resources}
+ skill_uris = {str(resource.uri) for resource in skills}
+ # URIs sharing a prefix sort next to each other, so a skill has a
+ # nested candidate exactly when the URI after its prefix's insertion
+ # point also starts with that prefix.
+ ordered_uris = sorted(skill_uris)
+
+ def has_nested_candidate(prefix: str) -> bool:
+ start = bisect.bisect_left(ordered_uris, prefix)
+ matches = ordered_uris[start : start + 2]
+ return sum(uri.startswith(prefix) for uri in matches) > 1
+
+ supporting_uris: set[str] = set()
+ # Process parents first, so a supporting file's own name or contents
+ # cannot make it an authority over another skill's discovery.
+ for uri in sorted(skill_uris, key=lambda uri: (len(uri), uri)):
+ if uri in supporting_uris:
+ continue
+ prefix = uri.removesuffix("SKILL.md")
+ if f"{prefix}_manifest" not in resource_uris or not has_nested_candidate(
+ prefix
+ ):
+ continue
+ try:
+ manifest = await self._resource_mode_manifest(uri)
+ except (MCPError, ValueError):
+ # Manually registered resources need not have readable manifests.
+ continue
+ supporting_uris.update(
+ record.uri
+ for path, record in manifest.items()
+ if path != "SKILL.md" and record.uri.startswith(prefix)
+ )
+ return [
+ resource for resource in skills if str(resource.uri) not in supporting_uris
+ ]
async def get_skill(
self: Client,
diff --git a/fastmcp_slim/fastmcp/utilities/skills.py b/fastmcp_slim/fastmcp/utilities/skills.py
index a64f014c1..479042b74 100644
--- a/fastmcp_slim/fastmcp/utilities/skills.py
+++ b/fastmcp_slim/fastmcp/utilities/skills.py
@@ -89,23 +89,20 @@ async def list_skills(client: Client) -> list[SkillSummary]:
async def _list_skills(client: Client) -> list[SkillSummary]:
- resources = await client.list_resources()
+ resources = await client.list_skill_resources()
skills = []
for resource in resources:
uri = str(resource.uri)
- # Match skill://{name}/SKILL.md pattern
- if uri.startswith("skill://") and uri.endswith("/SKILL.md"):
- # Extract skill name from URI
- path_part = uri[len("skill://") :]
- name = path_part.rsplit("/", 1)[0]
- skills.append(
- SkillSummary(
- name=name,
- description=resource.description or "",
- uri=uri,
- )
+ path_part = uri[len("skill://") :]
+ name = path_part.rsplit("/", 1)[0]
+ skills.append(
+ SkillSummary(
+ name=name,
+ description=resource.description or "",
+ uri=uri,
)
+ )
return skills
diff --git a/tests/skills/test_list_skill_resources.py b/tests/skills/test_list_skill_resources.py
new file mode 100644
index 000000000..de50303c5
--- /dev/null
+++ b/tests/skills/test_list_skill_resources.py
@@ -0,0 +1,181 @@
+"""Resource-based skill discovery distinguishes skills from supporting files."""
+
+from pathlib import Path
+from typing import Literal
+
+import pytest
+
+from fastmcp import Client, FastMCP
+from fastmcp.server.providers.skills import (
+ SkillsDirectoryProvider as LegacyDirectoryProvider,
+)
+from fastmcp.skills import Skill
+from fastmcp.skills.provider import SkillCatalogProvider, SkillsDirectoryProvider
+from fastmcp.utilities.skills import list_skills, sync_skills
+
+pytestmark = pytest.mark.filterwarnings(
+ "ignore::fastmcp._warnings.FastMCPDeprecationWarning"
+)
+
+REVIEW = Skill(
+ name="review",
+ description="Review changes",
+ instructions="Read the examples first.",
+ files={"examples/SKILL.md": "sample", "examples/_manifest": "example"},
+)
+
+
[email protected]("provider", ["catalog", "directory"])
[email protected]("prefix", ["", "team/"])
+async def test_supporting_skill_files_are_not_discovered(
+ tmp_path: Path, provider: str, prefix: str
+) -> None:
+ path = f"{prefix}review"
+ if provider == "catalog":
+ server = FastMCP("Skills", providers=[SkillCatalogProvider({path: REVIEW})])
+ else:
+ REVIEW.to_directory(tmp_path / path)
+ server = FastMCP("Skills", providers=[SkillsDirectoryProvider(tmp_path)])
+
+ async with Client(server) as client:
+ resources = await client.list_skill_resources()
+ assert [str(resource.uri) for resource in resources] == [
+ f"skill://{path}/SKILL.md"
+ ]
+ assert await client.read_skill(str(resources[0].uri)) == REVIEW
+ assert [summary.uri for summary in await list_skills(client)] == [
+ f"skill://{path}/SKILL.md"
+ ]
+
+
[email protected]("supporting_files", ["template", "resources"])
+async def test_discovery_on_a_legacy_provider(
+ tmp_path: Path, supporting_files: Literal["template", "resources"]
+) -> None:
+ REVIEW.to_directory(tmp_path / "review")
+ server = FastMCP(
+ "Legacy",
+ providers=[
+ LegacyDirectoryProvider(tmp_path, supporting_files=supporting_files)
+ ],
+ )
+ async with Client(server) as client:
+ resources = await client.list_skill_resources()
+ assert [str(resource.uri) for resource in resources] == [
+ "skill://review/SKILL.md"
+ ]
+ assert await client.read_skill(str(resources[0].uri)) == REVIEW
+
+
+async def test_sync_installs_supporting_skill_files(tmp_path: Path) -> None:
+ server = FastMCP("Skills", providers=[SkillCatalogProvider([REVIEW])])
+ async with Client(server) as client:
+ assert [summary.name for summary in await list_skills(client)] == ["review"]
+ assert await sync_skills(client, tmp_path) == [tmp_path / "review"]
+ assert Skill.from_directory(tmp_path / "review") == REVIEW
+
+
[email protected]("mounted", [False, True])
+async def test_independent_skills_can_share_a_path_prefix(mounted: bool) -> None:
+ deploy = Skill(name="deploy", description="Deploy", instructions="Deploy the app.")
+ server = FastMCP("Parent", providers=[SkillCatalogProvider([REVIEW])])
+ if mounted:
+ server.mount(
+ FastMCP("Child", providers=[SkillCatalogProvider([deploy])]),
+ namespace="review",
+ )
+ else:
+ server.add_provider(SkillCatalogProvider({"review/deploy": deploy}))
+
+ async with Client(server) as client:
+ resources = await client.list_skill_resources()
+ assert {str(resource.uri) for resource in resources} == {
+ "skill://review/SKILL.md",
+ "skill://review/deploy/SKILL.md",
+ }
+ assert await client.read_skill("skill://review/SKILL.md") == REVIEW
+ assert await client.read_skill("skill://review/deploy/SKILL.md") == deploy
+
+
+async def test_manifest_free_resources_keep_their_order_and_metadata() -> None:
+ server = FastMCP("Manual")
+ reads = 0
+
+ @server.resource("skill://team/review/nested/SKILL.md", description="Nested")
+ def nested() -> str:
+ nonlocal reads
+ reads += 1
+ raise AssertionError("Discovery should not read skill documents")
+
+ @server.resource("skill://team/review/SKILL.md", description="Review")
+ def review() -> str:
+ nonlocal reads
+ reads += 1
+ raise AssertionError("Discovery should not read skill documents")
+
+ @server.resource("file:///tmp/SKILL.md")
+ def other_scheme() -> str:
+ return "# Not a skill resource"
+
+ @server.resource("skill://team/review/notes.md")
+ def notes() -> str:
+ return "# Notes"
+
+ async with Client(server) as client:
+ all_resources = await client.list_resources()
+ assert await client.list_skill_resources() == all_resources[:2]
+ assert [summary.description for summary in await list_skills(client)] == [
+ "Nested",
+ "Review",
+ ]
+ assert reads == 0
+
+
[email protected]("unreadable", [False, True])
+async def test_unusable_manifest_keeps_candidates(unreadable: bool) -> None:
+ server = FastMCP("Manual")
+ reads = 0
+
+ @server.resource("skill://review/SKILL.md")
+ def review() -> str:
+ return "# Review"
+
+ @server.resource("skill://review/nested/SKILL.md")
+ def nested() -> str:
+ return "# Nested"
+
+ @server.resource("skill://review/_manifest")
+ def manifest() -> str:
+ nonlocal reads
+ reads += 1
+ if unreadable:
+ raise ValueError("Manifest is unavailable")
+ return "not JSON"
+
+ async with Client(server) as client:
+ resources = await client.list_skill_resources()
+ assert [str(resource.uri) for resource in resources] == [
+ "skill://review/SKILL.md",
+ "skill://review/nested/SKILL.md",
+ ]
+ assert reads == 1
+
+
+async def test_unambiguous_skills_do_not_need_manifest_reads() -> None:
+ server = FastMCP("Manual")
+ reads = 0
+
+ @server.resource("skill://review/SKILL.md")
+ def review() -> str:
+ return "# Review"
+
+ @server.resource("skill://review/_manifest")
+ def manifest() -> str:
+ nonlocal reads
+ reads += 1
+ raise AssertionError("Unambiguous discovery should not read manifests")
+
+ async with Client(server) as client:
+ (resource,) = await client.list_skill_resources()
+ assert str(resource.uri) == "skill://review/SKILL.md"
+ assert reads == 0