π The fast, Pythonic way to build MCP servers and clients.
Pass resource security policy through FileSystemProvider discovery (#5498)
Asem ahmed502512e0190d
diff --git a/fastmcp_slim/fastmcp/server/providers/filesystem_discovery.py b/fastmcp_slim/fastmcp/server/providers/filesystem_discovery.pyindex 6ffba52e9..d01bbfbda 100644--- a/fastmcp_slim/fastmcp/server/providers/filesystem_discovery.py+++ b/fastmcp_slim/fastmcp/server/providers/filesystem_discovery.py@@ -372,6 +372,7 @@ def extract_components(module: ModuleType) -> list[FastMCPComponent]:annotations=meta.annotations,meta=meta.meta,auth=meta.auth,+ security=meta.security,)else:resource = Resource.from_function(diff --git a/tests/fs/test_discovery.py b/tests/fs/test_discovery.pyindex 4f88bd34e..99fa6a07f 100644--- a/tests/fs/test_discovery.py+++ b/tests/fs/test_discovery.py@@ -5,6 +5,7 @@ from pathlib import Pathfrom fastmcp.prompts.base import Promptfrom fastmcp.resources.base import Resource+from fastmcp.resources.security import INHERIT_SECURITYfrom fastmcp.resources.template import FunctionResourceTemplate, ResourceTemplatefrom fastmcp.server.providers.filesystem_discovery import (discover_and_import,@@ -291,6 +292,37 @@ def get_profile(user_id: str) -> dict:assert isinstance(component, FunctionResourceTemplate)assert component.uri_template == "users://{user_id}/profile"+ def test_extract_resource_template_preserves_security(self, tmp_path: Path):+ """Resource templates discovered from files should keep their security policy."""+ template_file = tmp_path / "secure_templates.py"+ template_file.write_text(+ """\+from fastmcp.resources import ResourceSecurity, resource++POLICY = ResourceSecurity(exempt_params={"ref"})++@resource("git://diff/{ref}", security=POLICY)+def exempt(ref: str) -> str:+ return ref++@resource("git://show/{ref}", security=None)+def unscreened(ref: str) -> str:+ return ref++@resource("git://log/{ref}")+def default(ref: str) -> str:+ return ref+"""+ )++ module = import_module_from_file(template_file)+ components = extract_components(module)++ templates = {c.name: c for c in components if isinstance(c, ResourceTemplate)}+ assert templates["exempt"].security is module.POLICY+ assert templates["unscreened"].security is None+ assert templates["default"].security is INHERIT_SECURITY+class TestDiscoverAndImport:"""Tests for discover_and_import function."""diff --git a/tests/fs/test_provider.py b/tests/fs/test_provider.pyindex 05f85631b..3b3b4ef40 100644--- a/tests/fs/test_provider.py+++ b/tests/fs/test_provider.py@@ -512,6 +512,31 @@ def get_profile(user_id: str) -> str:result = await client.read_resource("users://123/profile")assert "123" in str(result)+ async def test_resource_template_security_via_provider(self, tmp_path: Path):+ """FileSystemProvider should honor the security policy of @resource templates."""+ (tmp_path / "secure_templates.py").write_text(+ """\+from fastmcp.resources import ResourceSecurity, resource++@resource("git://diff/{ref}", security=ResourceSecurity(exempt_params={"ref"}))+def exempt(ref: str) -> str:+ return f"exempt:{ref}"++@resource("git://log/{ref}")+def screened(ref: str) -> str:+ return f"screened:{ref}"+"""+ )++ mcp = FastMCP("TestServer", providers=[FileSystemProvider(tmp_path)])++ async with Client(mcp) as client:+ result = await client.read_resource("git://diff/..%2Fx")+ assert result[0].text == "exempt:../x"++ with pytest.raises(Exception, match="Resource not found"):+ await client.read_resource("git://log/..%2Fx")+async def test_provider_with_prompts(self, tmp_path: Path):"""FileSystemProvider should work with prompts."""(tmp_path / "analyze.py").write_text(