27
27
ListResourcesResult ,
28
28
ListToolsRequest ,
29
29
ListToolsResult ,
30
+ LoggingCapability ,
30
31
LoggingLevel ,
31
32
PingRequest ,
32
33
ProgressNotification ,
33
34
Prompt ,
34
35
PromptReference ,
36
+ PromptsCapability ,
35
37
ReadResourceRequest ,
36
38
ReadResourceResult ,
37
39
Resource ,
38
40
ResourceReference ,
41
+ ResourcesCapability ,
39
42
ServerCapabilities ,
40
43
ServerResult ,
41
44
SetLevelRequest ,
42
45
SubscribeRequest ,
43
46
Tool ,
47
+ ToolsCapability ,
44
48
UnsubscribeRequest ,
45
49
)
46
50
51
55
)
52
56
53
57
58
+ class NotificationOptions :
59
+ def __init__ (
60
+ self ,
61
+ prompts_changed : bool = False ,
62
+ resources_changed : bool = False ,
63
+ tools_changed : bool = False ,
64
+ ):
65
+ self .prompts_changed = prompts_changed
66
+ self .resources_changed = resources_changed
67
+ self .tools_changed = tools_changed
68
+
69
+
54
70
class Server :
55
71
def __init__ (self , name : str ):
56
72
self .name = name
57
73
self .request_handlers : dict [type , Callable [..., Awaitable [ServerResult ]]] = {
58
74
PingRequest : _ping_handler ,
59
75
}
60
76
self .notification_handlers : dict [type , Callable [..., Awaitable [None ]]] = {}
77
+ self .notification_options = NotificationOptions ()
61
78
logger .debug (f"Initializing server '{ name } '" )
62
79
63
- def create_initialization_options (self ) -> types .InitializationOptions :
80
+ def create_initialization_options (
81
+ self ,
82
+ notification_options : NotificationOptions | None = None ,
83
+ experimental_capabilities : dict [str , dict [str , Any ]] | None = None ,
84
+ ) -> types .InitializationOptions :
64
85
"""Create initialization options from this server instance."""
65
86
66
87
def pkg_version (package : str ) -> str :
@@ -78,20 +99,51 @@ def pkg_version(package: str) -> str:
78
99
return types .InitializationOptions (
79
100
server_name = self .name ,
80
101
server_version = pkg_version ("mcp_python" ),
81
- capabilities = self .get_capabilities (),
102
+ capabilities = self .get_capabilities (
103
+ notification_options or NotificationOptions (),
104
+ experimental_capabilities or {},
105
+ ),
82
106
)
83
107
84
- def get_capabilities (self ) -> ServerCapabilities :
108
+ def get_capabilities (
109
+ self ,
110
+ notification_options : NotificationOptions ,
111
+ experimental_capabilities : dict [str , dict [str , Any ]],
112
+ ) -> ServerCapabilities :
85
113
"""Convert existing handlers to a ServerCapabilities object."""
86
-
87
- def get_capability (req_type : type ) -> dict [str , Any ] | None :
88
- return {} if req_type in self .request_handlers else None
114
+ prompts_capability = None
115
+ resources_capability = None
116
+ tools_capability = None
117
+ logging_capability = None
118
+
119
+ # Set prompt capabilities if handler exists
120
+ if ListPromptsRequest in self .request_handlers :
121
+ prompts_capability = PromptsCapability (
122
+ listChanged = notification_options .prompts_changed
123
+ )
124
+
125
+ # Set resource capabilities if handler exists
126
+ if ListResourcesRequest in self .request_handlers :
127
+ resources_capability = ResourcesCapability (
128
+ subscribe = False , listChanged = notification_options .resources_changed
129
+ )
130
+
131
+ # Set tool capabilities if handler exists
132
+ if ListToolsRequest in self .request_handlers :
133
+ tools_capability = ToolsCapability (
134
+ listChanged = notification_options .tools_changed
135
+ )
136
+
137
+ # Set logging capabilities if handler exists
138
+ if SetLevelRequest in self .request_handlers :
139
+ logging_capability = LoggingCapability ()
89
140
90
141
return ServerCapabilities (
91
- prompts = get_capability (ListPromptsRequest ),
92
- resources = get_capability (ListResourcesRequest ),
93
- tools = get_capability (ListToolsRequest ),
94
- logging = get_capability (SetLevelRequest ),
142
+ prompts = prompts_capability ,
143
+ resources = resources_capability ,
144
+ tools = tools_capability ,
145
+ logging = logging_capability ,
146
+ experimental = experimental_capabilities ,
95
147
)
96
148
97
149
@property
@@ -169,9 +221,7 @@ def decorator(func: Callable[[], Awaitable[list[Resource]]]):
169
221
170
222
async def handler (_ : Any ):
171
223
resources = await func ()
172
- return ServerResult (
173
- ListResourcesResult (resources = resources )
174
- )
224
+ return ServerResult (ListResourcesResult (resources = resources ))
175
225
176
226
self .request_handlers [ListResourcesRequest ] = handler
177
227
return func
@@ -216,7 +266,6 @@ async def handler(req: ReadResourceRequest):
216
266
217
267
return decorator
218
268
219
-
220
269
def set_logging_level (self ):
221
270
from mcp_python .types import EmptyResult
222
271
0 commit comments