99
1010from mcp .server .fastmcp .exceptions import ToolError
1111from mcp .server .fastmcp .utilities .func_metadata import FuncMetadata , func_metadata
12- from mcp .types import ToolAnnotations
12+ from mcp .types import ServerNotification , ToolAnnotations , ToolListChangedNotification
1313
1414if TYPE_CHECKING :
1515 from mcp .server .fastmcp .server import Context
@@ -35,6 +35,7 @@ class Tool(BaseModel):
3535 annotations : ToolAnnotations | None = Field (
3636 None , description = "Optional annotations for the tool"
3737 )
38+ enabled : bool = Field (default = True , description = "Whether the tool is enabled" )
3839
3940 @classmethod
4041 def from_function (
@@ -100,6 +101,32 @@ async def run(
100101 except Exception as e :
101102 raise ToolError (f"Error executing tool { self .name } : { e } " ) from e
102103
104+ async def enable (
105+ self , context : Context [ServerSessionT , LifespanContextT ] | None = None
106+ ) -> None :
107+ """Enable the tool and notify clients."""
108+ if not self .enabled :
109+ self .enabled = True
110+ if context and context .session :
111+ notification = ToolListChangedNotification (
112+ method = "notifications/tools/list_changed"
113+ )
114+ server_notification = ServerNotification .model_validate (notification )
115+ await context .session .send_notification (server_notification )
116+
117+ async def disable (
118+ self , context : Context [ServerSessionT , LifespanContextT ] | None = None
119+ ) -> None :
120+ """Disable the tool and notify clients."""
121+ if self .enabled :
122+ self .enabled = False
123+ if context and context .session :
124+ notification = ToolListChangedNotification (
125+ method = "notifications/tools/list_changed"
126+ )
127+ server_notification = ServerNotification .model_validate (notification )
128+ await context .session .send_notification (server_notification )
129+
103130
104131def _is_async_callable (obj : Any ) -> bool :
105132 while isinstance (obj , functools .partial ):
0 commit comments