π The fast, Pythonic way to build MCP servers and clients.
Don't evaluate properties when registering MCPMixin methods (#5620)
Asem ahmed8e64209d0838
diff --git a/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py b/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.pyindex 350359630..460e169c8 100644--- a/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py+++ b/fastmcp_slim/fastmcp/contrib/mcp_mixin/mcp_mixin.py@@ -178,15 +178,18 @@ class MCPMixin:def _get_methods_to_register(self, registration_type: str):"""Retrieves all methods marked for a specific registration type."""- return [- (- getattr(self, method_name),- getattr(getattr(self, method_name), registration_type).copy(),- )- for method_name in dir(self)- if callable(getattr(self, method_name))- and hasattr(getattr(self, method_name), registration_type)- ]+ methods = []+ for method_name in dir(self):+ # Look for the marker without reading the attribute, so properties+ # and other descriptors on the instance are never evaluated.+ static_attr = inspect.getattr_static(self, method_name, None)+ func = getattr(static_attr, "__func__", static_attr)+ if not hasattr(func, registration_type):+ continue+ method = getattr(self, method_name)+ if callable(method):+ methods.append((method, getattr(method, registration_type).copy()))+ return methodsdef register_tools(self,diff --git a/tests/contrib/test_mcp_mixin.py b/tests/contrib/test_mcp_mixin.pyindex 1bce287bf..972bf6795 100644--- a/tests/contrib/test_mcp_mixin.py+++ b/tests/contrib/test_mcp_mixin.py@@ -344,6 +344,57 @@ class TestMCPMixin:assert prompt.meta == {"priority": "high", "category": "analysis"}+class TestMCPMixinRegistrationDoesNotReadAttributes:+ """Registration should only look at methods, not evaluate other attributes."""++ async def test_register_all_does_not_evaluate_properties(self):+ evaluated: list[str] = []++ class MyMixin(MCPMixin):+ @property+ def connection(self) -> str:+ evaluated.append("connection")+ raise RuntimeError("not connected")++ @mcp_tool()+ def sample_tool(self):+ pass++ @mcp_resource(uri="test://resource")+ def sample_resource(self):+ return "data"++ @mcp_prompt()+ def sample_prompt(self):+ return "prompt text"++ mcp = FastMCP()+ MyMixin().register_all(mcp)++ assert evaluated == []+ assert [t.name for t in await mcp.list_tools()] == ["sample_tool"]+ assert [str(r.uri) for r in await mcp.list_resources()] == ["test://resource"]+ assert [p.name for p in await mcp.list_prompts()] == ["sample_prompt"]++ async def test_static_and_class_methods_are_still_registered(self):+ class MyMixin(MCPMixin):+ @staticmethod+ @mcp_tool()+ def static_tool():+ pass++ @classmethod+ @mcp_tool()+ def class_tool(cls):+ pass++ mcp = FastMCP()+ MyMixin().register_tools(mcp)++ names = {t.name for t in await mcp.list_tools()}+ assert names == {"static_tool", "class_tool"}++class TestMCPMixinKwargsSync:"""Verify that the valid-kwarg sets stay in sync with from_function signatures."""