π The fast, Pythonic way to build MCP servers and clients.
fix(config): apply tool transforms before tag filters on single servers (#6214)
ayush02c121cbeca2
diff --git a/fastmcp_slim/fastmcp/mcp_config.py b/fastmcp_slim/fastmcp/mcp_config.pyindex e597e12d3..a05c5a438 100644--- a/fastmcp_slim/fastmcp/mcp_config.py+++ b/fastmcp_slim/fastmcp/mcp_config.py@@ -148,14 +148,17 @@ class _TransformingMCPServerMixin(BaseModel):)wrapped_mcp_server = create_proxy(client, name=server_name)- if self.include_tags is not None:- wrapped_mcp_server.enable(tags=self.include_tags, only=True)- if self.exclude_tags is not None:- wrapped_mcp_server.disable(tags=self.exclude_tags)+ # Add tool transforms FIRST - they may add/modify tags. Then add+ # enabled filters - they filter based on tags. This mirrors the order+ # used by the multi-server composite path.if self.tools:wrapped_mcp_server.add_transform(ToolTransform(_coerce_tool_transform_configs(self.tools)))+ if self.include_tags is not None:+ wrapped_mcp_server.enable(tags=self.include_tags, only=True)+ if self.exclude_tags is not None:+ wrapped_mcp_server.disable(tags=self.exclude_tags)return wrapped_mcp_server, transportdiff --git a/tests/test_mcp_config.py b/tests/test_mcp_config.pyindex 9da72bf1f..c71762980 100644--- a/tests/test_mcp_config.py+++ b/tests/test_mcp_config.py@@ -1533,3 +1533,90 @@ async def test_script(tmp_path: Path) -> AsyncGenerator[Path, Any]:yield Path(f.name)pass+++@requires_subprocess[email protected](retries=3)+async def test_single_server_config_transform_added_tags_filtering(tmp_path: Path):+ """Tags added by a `tools` transform are visible to include_tags/exclude_tags+ on a single-server config, matching the multi-server behavior (#6196)."""+ server_script = inspect.cleandoc("""+ from fastmcp import FastMCP++ mcp = FastMCP()++ @mcp.tool+ def add(a: int, b: int) -> int:+ return a + b++ @mcp.tool+ def subtract(a: int, b: int) -> int:+ return a - b++ if __name__ == '__main__':+ mcp.run()+ """)++ script_path = tmp_path / "test.py"+ script_path.write_text(server_script)++ async def list_tool_names(server_options: dict) -> set[str]:+ config = {+ "mcpServers": {+ "test": {"command": "python", "args": [str(script_path)]}+ | server_options+ }+ }+ client = Client(config)+ async with client:+ return {tool.name for tool in await client.list_tools()}++ included = await list_tool_names(+ {"tools": {"add": {"tags": ["keep"]}}, "include_tags": ["keep"]}+ )+ assert included == {"add"}++ excluded = await list_tool_names(+ {"tools": {"add": {"tags": ["drop"]}}, "exclude_tags": ["drop"]}+ )+ assert excluded == {"subtract"}+++async def test_single_server_transform_added_tags_filtering_in_memory():+ """Same as the subprocess test above, but in-memory so it runs everywhere.++ The transforming single-server path must apply the `tools` transform+ before the include_tags/exclude_tags filters β tags the transform adds+ are invisible to filters registered first.+ """+ from fastmcp.client.transports import FastMCPTransport++ backend = FastMCP()++ @backend.tool+ def add(a: int, b: int) -> int:+ return a + b++ @backend.tool+ def subtract(a: int, b: int) -> int:+ return a - b++ config = TransformingStdioMCPServer(+ command="unused",+ args=[],+ tools={"add": {"tags": ["keep"]}},+ include_tags=["keep"],+ )++ with patch.object(+ StdioMCPServer,+ "to_transport",+ lambda self: FastMCPTransport(mcp=backend),+ ):+ wrapped, _ = config._to_server_and_underlying_transport()++ client = Client(FastMCPTransport(mcp=wrapped))+ async with client:+ tool_names = {tool.name for tool in await client.list_tools()}++ assert tool_names == {"add"}