Skip to content

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

Deduplicate versions in PromptsAsTools and ResourcesAsTools list tools (#5331)

Asem ahmedb2de499dc8ce
diff --git a/fastmcp_slim/fastmcp/server/transforms/prompts_as_tools.py b/fastmcp_slim/fastmcp/server/transforms/prompts_as_tools.py
index 81c421bc3..1d01d1a78 100644
--- a/fastmcp_slim/fastmcp/server/transforms/prompts_as_tools.py
+++ b/fastmcp_slim/fastmcp/server/transforms/prompts_as_tools.py
@@ -29,7 +29,7 @@ from mcp_types import TextContent
from fastmcp.server.dependencies import get_context
from fastmcp.server.transforms import GetToolNext, Transform
from fastmcp.tools.base import Tool
-from fastmcp.utilities.versions import VersionSpec
+from fastmcp.utilities.versions import VersionSpec, dedupe_with_versions
if TYPE_CHECKING:
from fastmcp.server.providers.base import Provider
@@ -100,7 +100,9 @@ class PromptsAsTools(Transform):
and optional arguments.
"""
ctx = get_context()
- prompts = await ctx.fastmcp.list_prompts()
+ prompts = dedupe_with_versions(
+ await ctx.fastmcp.list_prompts(), lambda p: p.name
+ )
result: list[dict[str, Any]] = []
for p in prompts:
diff --git a/fastmcp_slim/fastmcp/server/transforms/resources_as_tools.py b/fastmcp_slim/fastmcp/server/transforms/resources_as_tools.py
index 9861fc6c9..416c5724d 100644
--- a/fastmcp_slim/fastmcp/server/transforms/resources_as_tools.py
+++ b/fastmcp_slim/fastmcp/server/transforms/resources_as_tools.py
@@ -30,7 +30,7 @@ from mcp_types import ToolAnnotations
from fastmcp.server.dependencies import get_context
from fastmcp.server.transforms import GetToolNext, Transform
from fastmcp.tools.base import Tool
-from fastmcp.utilities.versions import VersionSpec
+from fastmcp.utilities.versions import VersionSpec, dedupe_with_versions
_DEFAULT_ANNOTATIONS = ToolAnnotations(read_only_hint=True)
@@ -104,8 +104,12 @@ class ResourcesAsTools(Transform):
placeholders like {name}.
"""
ctx = get_context()
- resources = await ctx.fastmcp.list_resources()
- templates = await ctx.fastmcp.list_resource_templates()
+ resources = dedupe_with_versions(
+ await ctx.fastmcp.list_resources(), lambda r: str(r.uri)
+ )
+ templates = dedupe_with_versions(
+ await ctx.fastmcp.list_resource_templates(), lambda t: t.uri_template
+ )
result: list[dict[str, Any]] = []
diff --git a/tests/server/transforms/test_prompts_as_tools.py b/tests/server/transforms/test_prompts_as_tools.py
index 1583d3b11..b670b89bf 100644
--- a/tests/server/transforms/test_prompts_as_tools.py
+++ b/tests/server/transforms/test_prompts_as_tools.py
@@ -99,6 +99,31 @@ class TestListPromptsTool:
lang_arg = next(a for a in args if a["name"] == "language")
assert lang_arg["required"] is False
+ async def test_lists_highest_version_only(self):
+ """list_prompts shows one entry per name, like prompts/list does."""
+ mcp = FastMCP("Test")
+
+ @mcp.prompt(name="greet", version="1.0")
+ def greet_v1(name: str) -> str:
+ """Greet v1."""
+ return f"Hi {name}"
+
+ @mcp.prompt(name="greet", version="2.0")
+ def greet_v2(name: str, formal: str = "no") -> str:
+ """Greet v2."""
+ return f"Hello {name}"
+
+ mcp.add_transform(PromptsAsTools(mcp))
+
+ async with Client(mcp) as client:
+ result = await client.call_tool("list_prompts", {})
+ prompts = json.loads(result.data)
+
+ assert len(prompts) == 1
+ assert prompts[0]["name"] == "greet"
+ assert prompts[0]["description"] == "Greet v2."
+ assert [a["name"] for a in prompts[0]["arguments"]] == ["name", "formal"]
+
async def test_empty_when_no_prompts(self):
"""list_prompts returns empty list when no prompts exist."""
mcp = FastMCP("Test")
diff --git a/tests/server/transforms/test_resources_as_tools.py b/tests/server/transforms/test_resources_as_tools.py
index 620a87c76..f6771b1d7 100644
--- a/tests/server/transforms/test_resources_as_tools.py
+++ b/tests/server/transforms/test_resources_as_tools.py
@@ -119,6 +119,50 @@ class TestListResourcesTool:
assert uris == ["config://app"]
assert templates == ["file://{path}"]
+ async def test_lists_highest_version_only(self):
+ """list_resources shows one entry per URI, like resources/list does."""
+ mcp = FastMCP("Test")
+
+ @mcp.resource("config://app", version="1.0")
+ def config_v1() -> str:
+ """Config v1."""
+ return "v1"
+
+ @mcp.resource("config://app", version="2.0")
+ def config_v2() -> str:
+ """Config v2."""
+ return "v2"
+
+ @mcp.resource("file://{path}", version="1.0")
+ def file_v1(path: str) -> str:
+ """File v1."""
+ return path
+
+ @mcp.resource("file://{path}", version="2.0")
+ def file_v2(path: str) -> str:
+ """File v2."""
+ return path
+
+ mcp.add_transform(ResourcesAsTools(mcp))
+
+ async with Client(mcp) as client:
+ result = await client.call_tool("list_resources", {})
+ resources = json.loads(result.data)
+
+ assert resources == [
+ {
+ "uri": "config://app",
+ "name": "config_v2",
+ "description": "Config v2.",
+ "mime_type": "text/plain",
+ },
+ {
+ "uri_template": "file://{path}",
+ "name": "file_v2",
+ "description": "File v2.",
+ },
+ ]
+
async def test_empty_when_no_resources(self):
"""list_resources returns empty list when no resources exist."""
mcp = FastMCP("Test")