π The fast, Pythonic way to build MCP servers and clients.
tasks: register task tools hidden by search and CodeMode (#5262)
nate nowackedc991e4ffb0
diff --git a/fastmcp_slim/fastmcp/server/providers/base.py b/fastmcp_slim/fastmcp/server/providers/base.pyindex 7941039dd..83c68792b 100644--- a/fastmcp_slim/fastmcp/server/providers/base.py+++ b/fastmcp_slim/fastmcp/server/providers/base.py@@ -31,7 +31,7 @@ from __future__ import annotationsfrom collections.abc import AsyncIterator, Sequencefrom contextlib import asynccontextmanagerfrom functools import partial-from typing import TYPE_CHECKING, Any, Literal, cast+from typing import TYPE_CHECKING, Any, Literal, TypeVar, castfrom typing_extensions import Self@@ -54,6 +54,18 @@ if TYPE_CHECKING:from fastmcp.tools.base import Tool+_C = TypeVar("_C", bound=FastMCPComponent)+++def _listed(before: Sequence[_C], after: Sequence[_C]) -> Sequence[_C]:+ return after+++def _keep_hidden(before: Sequence[_C], after: Sequence[_C]) -> Sequence[_C]:+ listed = {c.key for c in after}+ return [*after, *(c for c in before if c.key not in listed)]++class Provider:"""Base class for dynamic component providers.@@ -512,30 +524,49 @@ class Provider:self._list_prompts,))- tools = cast("Sequence[Tool]", results[0])- resources = cast("Sequence[Resource]", results[1])- templates = cast("Sequence[ResourceTemplate]", results[2])- prompts = cast("Sequence[Prompt]", results[3])-- # Apply provider's own transforms sequentially- # For tasks, we need the fully-transformed names- for transform in self.transforms:- tools = await transform.list_tools(tools)- resources = await transform.list_resources(resources)- templates = await transform.list_resource_templates(templates)- prompts = await transform.list_prompts(prompts)-+ components = [component for result in results for component in result]return [c- for c in [- *tools,- *resources,- *templates,- *prompts,- ]+ for c in await self._apply_task_transforms(components)if c.task_config.supports_tasks()]+ async def _apply_task_transforms(+ self, components: Sequence[FastMCPComponent]+ ) -> list[FastMCPComponent]:+ """Apply this provider's transforms to components bound for Docket.++ Registration needs the names components are called by, so renaming+ transforms apply. Catalog transforms (search, CodeMode) only replace+ what is *listed*: the components they hide stay callable, so they are+ kept alongside whatever the catalog transform returns.+ """+ from fastmcp.prompts.base import Prompt+ from fastmcp.resources.base import Resource+ from fastmcp.resources.template import ResourceTemplate+ from fastmcp.server.transforms.catalog import CatalogTransform+ from fastmcp.tools.base import Tool++ tools: Sequence[Tool] = [c for c in components if isinstance(c, Tool)]+ resources: Sequence[Resource] = [+ c for c in components if isinstance(c, Resource)+ ]+ templates: Sequence[ResourceTemplate] = [+ c for c in components if isinstance(c, ResourceTemplate)+ ]+ prompts: Sequence[Prompt] = [c for c in components if isinstance(c, Prompt)]++ for transform in self.transforms:+ keep = _keep_hidden if isinstance(transform, CatalogTransform) else _listed+ tools = keep(tools, await transform.list_tools(tools))+ resources = keep(resources, await transform.list_resources(resources))+ templates = keep(+ templates, await transform.list_resource_templates(templates)+ )+ prompts = keep(prompts, await transform.list_prompts(prompts))++ return [*tools, *resources, *templates, *prompts]+# -------------------------------------------------------------------------# Lifecycle methods# -------------------------------------------------------------------------diff --git a/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py b/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.pyindex 9d0b0a92d..4289ab0c7 100644--- a/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py+++ b/fastmcp_slim/fastmcp/server/providers/fastmcp_provider.py@@ -554,28 +554,9 @@ class FastMCPProvider(Provider):# Get tasks with child server's transforms already appliedcomponents = list(await self.server.get_tasks())- # Separate by type for this provider's transform application- tools = [c for c in components if isinstance(c, Tool)]- resources = [c for c in components if isinstance(c, Resource)]- templates = [c for c in components if isinstance(c, ResourceTemplate)]- prompts = [c for c in components if isinstance(c, Prompt)]-- # Apply this provider's transforms sequentially- for transform in self.transforms:- tools = await transform.list_tools(tools)- resources = await transform.list_resources(resources)- templates = await transform.list_resource_templates(templates)- prompts = await transform.list_prompts(prompts)-- # Filter to only task-eligible components (same as base Provider)return [c- for c in [- *tools,- *resources,- *templates,- *prompts,- ]+ for c in await self._apply_task_transforms(components)if c.task_config.supports_tasks()]diff --git a/fastmcp_slim/fastmcp/server/providers/local_provider/local_provider.py b/fastmcp_slim/fastmcp/server/providers/local_provider/local_provider.pyindex 8336b08ed..ebb662571 100644--- a/fastmcp_slim/fastmcp/server/providers/local_provider/local_provider.py+++ b/fastmcp_slim/fastmcp/server/providers/local_provider/local_provider.py@@ -455,27 +455,9 @@ class LocalProvider("""components = list(self._components.values())- # Separate by component type for transform application- tools = [c for c in components if isinstance(c, Tool)]- resources = [c for c in components if isinstance(c, Resource)]- templates = [c for c in components if isinstance(c, ResourceTemplate)]- prompts = [c for c in components if isinstance(c, Prompt)]-- # Apply this provider's transforms sequentially- for transform in self.transforms:- tools = await transform.list_tools(tools)- resources = await transform.list_resources(resources)- templates = await transform.list_resource_templates(templates)- prompts = await transform.list_prompts(prompts)-return [c- for c in [- *tools,- *resources,- *templates,- *prompts,- ]+ for c in await self._apply_task_transforms(components)if c.task_config.supports_tasks()]diff --git a/fastmcp_slim/fastmcp/server/providers/wrapped_provider.py b/fastmcp_slim/fastmcp/server/providers/wrapped_provider.pyindex f33e9d539..035037c4c 100644--- a/fastmcp_slim/fastmcp/server/providers/wrapped_provider.py+++ b/fastmcp_slim/fastmcp/server/providers/wrapped_provider.py@@ -103,37 +103,12 @@ class _WrappedProvider(Provider):async def get_tasks(self) -> Sequence[FastMCPComponent]:"""Delegate to inner's get_tasks and apply wrapper's transforms."""- # Import here to avoid circular imports- from fastmcp.prompts.base import Prompt- from fastmcp.resources.base import Resource- from fastmcp.resources.template import ResourceTemplate- from fastmcp.tools.base import Tool-# Get tasks from inner (already has inner's transforms)components = list(await self._inner.get_tasks())- # Apply this wrapper's transforms to the components- # We need to apply transforms per component type- tools = [c for c in components if isinstance(c, Tool)]- resources = [c for c in components if isinstance(c, Resource)]- templates = [c for c in components if isinstance(c, ResourceTemplate)]- prompts = [c for c in components if isinstance(c, Prompt)]-- # Apply this wrapper's transforms sequentially- for transform in self.transforms:- tools = await transform.list_tools(tools)- resources = await transform.list_resources(resources)- templates = await transform.list_resource_templates(templates)- prompts = await transform.list_prompts(prompts)-return [c- for c in [- *tools,- *resources,- *templates,- *prompts,- ]+ for c in await self._apply_task_transforms(components)if c.task_config.supports_tasks()]diff --git a/fastmcp_slim/fastmcp/server/server.py b/fastmcp_slim/fastmcp/server/server.pyindex 6f999f69c..001b3ce3e 100644--- a/fastmcp_slim/fastmcp/server/server.py+++ b/fastmcp_slim/fastmcp/server/server.py@@ -808,7 +808,7 @@ class FastMCP(# are inherited from AggregateProvider which handles aggregation and namespacingasync def get_tasks(self) -> Sequence[FastMCPComponent]:- """Get task-eligible components with all transforms applied.+ """Get task-eligible components with server-level transforms applied.Overrides AggregateProvider.get_tasks() to apply server-level transformsafter aggregation. AggregateProvider handles provider-level namespacing.@@ -816,24 +816,10 @@ class FastMCP(# Get tasks from AggregateProvider (handles aggregation and namespacing)components = list(await super().get_tasks())- # Separate by component type for server-level transform application- tools = [c for c in components if isinstance(c, Tool)]- resources = [c for c in components if isinstance(c, Resource)]- templates = [c for c in components if isinstance(c, ResourceTemplate)]- prompts = [c for c in components if isinstance(c, Prompt)]-- # Apply server-level transforms sequentially- for transform in self.transforms:- tools = await transform.list_tools(tools)- resources = await transform.list_resources(resources)- templates = await transform.list_resource_templates(templates)- prompts = await transform.list_prompts(prompts)-return [- *tools,- *resources,- *templates,- *prompts,+ c+ for c in await self._apply_task_transforms(components)+ if c.task_config.supports_tasks()]def add_transform(self, transform: Transform) -> None:diff --git a/tests/tasks/server/test_task_catalog_transforms.py b/tests/tasks/server/test_task_catalog_transforms.pynew file mode 100644index 000000000..b3d5bcce3--- /dev/null+++ b/tests/tasks/server/test_task_catalog_transforms.py@@ -0,0 +1,112 @@+"""Task-enabled tools hidden by a catalog transform stay registered with Docket.++Search and CodeMode replace the tool listing with synthetic discovery tools, but+the tools they hide remain callable. Registration has to follow what is+callable, not what is listed.+"""++from __future__ import annotations++from collections.abc import Sequence++import pytest++from fastmcp import Client, FastMCP+from fastmcp.experimental.transforms.code_mode import CodeMode+from fastmcp.server.transforms import Namespace+from fastmcp.server.transforms.catalog import CatalogTransform+from fastmcp.server.transforms.search import BM25SearchTransform, RegexSearchTransform+from fastmcp.tools.base import Tool+from fastmcp.utilities.tasks import TaskConfig+from fastmcp_tasks import TasksExtension+from tests.tasks.task_helpers import run_task, running_task_server+++def make_server() -> FastMCP:+ mcp = FastMCP("catalog-tasks")+ mcp.add_extension(TasksExtension())++ @mcp.tool(task=TaskConfig(mode="optional"))+ async def slow_thing(n: int) -> dict:+ return {"n": n}++ return mcp+++ "transform",+ [BM25SearchTransform(), RegexSearchTransform(), CodeMode()],+ ids=["bm25", "regex", "code_mode"],+)+async def test_server_level_catalog_transform_keeps_hidden_task_tool(transform):+ mcp = make_server()+ mcp.add_transform(transform)++ assert [c.name for c in await mcp.get_tasks()] == ["slow_thing"]++ async with running_task_server(mcp):+ final = await run_task(mcp, "slow_thing", {"n": 2})+ assert final.status == "completed"++ async with Client(mcp) as client:+ result = await client.call_tool("slow_thing", {"n": 1})+ assert result.data == {"n": 1}+++async def test_provider_level_search_keeps_hidden_task_tool():+ mcp = make_server()+ mcp.providers[0].add_transform(BM25SearchTransform())++ assert [c.name for c in await mcp.get_tasks()] == ["slow_thing"]+++async def test_mounted_child_search_keeps_hidden_task_tool():+ child = FastMCP("child")++ @child.tool(task=True)+ async def slow_thing(n: int) -> int:+ return n++ child.add_transform(BM25SearchTransform())+ parent = FastMCP("parent")+ parent.add_extension(TasksExtension())+ parent.mount(child, namespace="child")++ assert [c.name for c in await parent.get_tasks()] == ["child_slow_thing"]++ async with running_task_server(parent):+ final = await run_task(parent, "child_slow_thing", {"n": 3})+ assert final.status == "completed"+++async def test_renaming_transforms_still_apply_alongside_search():+ mcp = make_server()+ mcp.add_transform(Namespace("api"))+ mcp.add_transform(BM25SearchTransform())++ assert [c.name for c in await mcp.get_tasks()] == ["api_slow_thing"]+++async def test_search_listing_still_hides_task_tool():+ mcp = make_server()+ mcp.add_transform(BM25SearchTransform())++ names = [t.name for t in await mcp.list_tools()]+ assert "slow_thing" not in names+++async def test_task_tool_added_by_catalog_transform_is_registered():+ async def synthetic_slow(n: int) -> int:+ return n++ class AddsTaskTool(CatalogTransform):+ async def transform_tools(self, tools: Sequence[Tool]) -> Sequence[Tool]:+ return [Tool.from_function(synthetic_slow, task=True)]++ mcp = make_server()+ mcp.add_transform(AddsTaskTool())++ assert sorted(c.name for c in await mcp.get_tasks()) == [+ "slow_thing",+ "synthetic_slow",+ ]