2828 ListResourcesResult ,
2929 ListToolsRequest ,
3030 ListToolsResult ,
31+ LoggingCapability ,
3132 LoggingLevel ,
3233 PingRequest ,
3334 ProgressNotification ,
3435 Prompt ,
3536 PromptMessage ,
3637 PromptReference ,
38+ PromptsCapability ,
3739 ReadResourceRequest ,
3840 ReadResourceResult ,
3941 Resource ,
4042 ResourceReference ,
43+ ResourcesCapability ,
4144 ServerCapabilities ,
4245 ServerResult ,
4346 SetLevelRequest ,
4447 SubscribeRequest ,
4548 TextContent ,
4649 Tool ,
50+ ToolsCapability ,
4751 UnsubscribeRequest ,
4852)
4953
5458)
5559
5660
61+ class NotificationOptions :
62+ def __init__ (
63+ self ,
64+ prompts_changed : bool = False ,
65+ resources_changed : bool = False ,
66+ tools_changed : bool = False ,
67+ ):
68+ self .prompts_changed = prompts_changed
69+ self .resources_changed = resources_changed
70+ self .tools_changed = tools_changed
71+
72+
5773class Server :
5874 def __init__ (self , name : str ):
5975 self .name = name
6076 self .request_handlers : dict [type , Callable [..., Awaitable [ServerResult ]]] = {
6177 PingRequest : _ping_handler ,
6278 }
6379 self .notification_handlers : dict [type , Callable [..., Awaitable [None ]]] = {}
80+ self .notification_options = NotificationOptions ()
6481 logger .debug (f"Initializing server '{ name } '" )
6582
66- def create_initialization_options (self ) -> types .InitializationOptions :
83+ def create_initialization_options (
84+ self ,
85+ notification_options : NotificationOptions | None = None ,
86+ experimental_capabilities : dict [str , dict [str , Any ]] | None = None ,
87+ ) -> types .InitializationOptions :
6788 """Create initialization options from this server instance."""
6889
6990 def pkg_version (package : str ) -> str :
@@ -81,20 +102,51 @@ def pkg_version(package: str) -> str:
81102 return types .InitializationOptions (
82103 server_name = self .name ,
83104 server_version = pkg_version ("mcp_python" ),
84- capabilities = self .get_capabilities (),
105+ capabilities = self .get_capabilities (
106+ notification_options or NotificationOptions (),
107+ experimental_capabilities or {},
108+ ),
85109 )
86110
87- def get_capabilities (self ) -> ServerCapabilities :
111+ def get_capabilities (
112+ self ,
113+ notification_options : NotificationOptions ,
114+ experimental_capabilities : dict [str , dict [str , Any ]],
115+ ) -> ServerCapabilities :
88116 """Convert existing handlers to a ServerCapabilities object."""
89-
90- def get_capability (req_type : type ) -> dict [str , Any ] | None :
91- return {} if req_type in self .request_handlers else None
117+ prompts_capability = None
118+ resources_capability = None
119+ tools_capability = None
120+ logging_capability = None
121+
122+ # Set prompt capabilities if handler exists
123+ if ListPromptsRequest in self .request_handlers :
124+ prompts_capability = PromptsCapability (
125+ listChanged = notification_options .prompts_changed
126+ )
127+
128+ # Set resource capabilities if handler exists
129+ if ListResourcesRequest in self .request_handlers :
130+ resources_capability = ResourcesCapability (
131+ subscribe = False , listChanged = notification_options .resources_changed
132+ )
133+
134+ # Set tool capabilities if handler exists
135+ if ListToolsRequest in self .request_handlers :
136+ tools_capability = ToolsCapability (
137+ listChanged = notification_options .tools_changed
138+ )
139+
140+ # Set logging capabilities if handler exists
141+ if SetLevelRequest in self .request_handlers :
142+ logging_capability = LoggingCapability ()
92143
93144 return ServerCapabilities (
94- prompts = get_capability (ListPromptsRequest ),
95- resources = get_capability (ListResourcesRequest ),
96- tools = get_capability (ListToolsRequest ),
97- logging = get_capability (SetLevelRequest ),
145+ prompts = prompts_capability ,
146+ resources = resources_capability ,
147+ tools = tools_capability ,
148+ logging = logging_capability ,
149+ experimental = experimental_capabilities ,
98150 )
99151
100152 @property
0 commit comments