28
28
ListResourcesResult ,
29
29
ListToolsRequest ,
30
30
ListToolsResult ,
31
+ LoggingCapability ,
31
32
LoggingLevel ,
32
33
PingRequest ,
33
34
ProgressNotification ,
34
35
Prompt ,
35
36
PromptMessage ,
36
37
PromptReference ,
38
+ PromptsCapability ,
37
39
ReadResourceRequest ,
38
40
ReadResourceResult ,
39
41
Resource ,
40
42
ResourceReference ,
43
+ ResourcesCapability ,
41
44
ServerCapabilities ,
42
45
ServerResult ,
43
46
SetLevelRequest ,
44
47
SubscribeRequest ,
45
48
TextContent ,
46
49
Tool ,
50
+ ToolsCapability ,
47
51
UnsubscribeRequest ,
48
52
)
49
53
54
58
)
55
59
56
60
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
+
57
73
class Server :
58
74
def __init__ (self , name : str ):
59
75
self .name = name
60
76
self .request_handlers : dict [type , Callable [..., Awaitable [ServerResult ]]] = {
61
77
PingRequest : _ping_handler ,
62
78
}
63
79
self .notification_handlers : dict [type , Callable [..., Awaitable [None ]]] = {}
80
+ self .notification_options = NotificationOptions ()
64
81
logger .debug (f"Initializing server '{ name } '" )
65
82
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 :
67
88
"""Create initialization options from this server instance."""
68
89
69
90
def pkg_version (package : str ) -> str :
@@ -81,20 +102,51 @@ def pkg_version(package: str) -> str:
81
102
return types .InitializationOptions (
82
103
server_name = self .name ,
83
104
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
+ ),
85
109
)
86
110
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 :
88
116
"""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 ()
92
143
93
144
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 ,
98
150
)
99
151
100
152
@property
0 commit comments