diff --git a/src/fastmcp/server/server.py b/src/fastmcp/server/server.py index d72c0351a..70b2079ff 100644 --- a/src/fastmcp/server/server.py +++ b/src/fastmcp/server/server.py @@ -41,6 +41,7 @@ from starlette.requests import Request from starlette.responses import Response from starlette.routing import BaseRoute, Route +import fastmcp import fastmcp.server import fastmcp.settings from fastmcp.exceptions import NotFoundError @@ -161,7 +162,9 @@ class FastMCP(Generic[LifespanResultT]): self.dependencies = dependencies self._cache = TimedCache( - expiration=datetime.timedelta(seconds=cache_expiration_seconds or 0) + expiration=datetime.timedelta( + seconds=self.settings.cache_expiration_seconds + ) ) self._mounted_servers: dict[str, MountedServer] = {} self._additional_http_routes: list[BaseRoute] = [] diff --git a/src/fastmcp/settings.py b/src/fastmcp/settings.py index 04c830b27..1a3fc443a 100644 --- a/src/fastmcp/settings.py +++ b/src/fastmcp/settings.py @@ -170,7 +170,7 @@ class ServerSettings(BaseSettings): ), ] = [] - # cache settings (for checking mounted servers) + # cache settings (for getting attributes from servers, used to avoid repeated calls) cache_expiration_seconds: float = 0 # StreamableHTTP settings diff --git a/tests/server/test_mount.py b/tests/server/test_mount.py index 090666ccc..97c2efd3e 100644 --- a/tests/server/test_mount.py +++ b/tests/server/test_mount.py @@ -304,6 +304,19 @@ class TestDynamicChanges: tools = await main_app.get_tools() assert "sub_temp_tool" not in tools + async def test_cache_expiration(self): + main_app = FastMCP("MainApp", cache_expiration_seconds=2) + sub_app = FastMCP("SubApp") + tools = await main_app.get_tools() + assert len(tools) == 0 + + @sub_app.tool + def sub_tool(): + return "sub_tool" + + tools = await main_app.get_tools() + assert len(tools) == 0 + class TestResourcesAndTemplates: """Test mounting with resources and resource templates."""