@@ -18,6 +18,7 @@ import io.modelcontextprotocol.kotlin.sdk.ListRootsRequest
18
18
import io.modelcontextprotocol.kotlin.sdk.ListRootsResult
19
19
import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification
20
20
import io.modelcontextprotocol.kotlin.sdk.Method
21
+ import io.modelcontextprotocol.kotlin.sdk.Method.Defined
21
22
import io.modelcontextprotocol.kotlin.sdk.PingRequest
22
23
import io.modelcontextprotocol.kotlin.sdk.PromptListChangedNotification
23
24
import io.modelcontextprotocol.kotlin.sdk.ResourceListChangedNotification
@@ -31,11 +32,11 @@ import kotlinx.serialization.json.JsonObject
31
32
32
33
private val logger = KotlinLogging .logger {}
33
34
34
- public open class ServerSession (
35
- private val serverInfo : Implementation ,
36
- options : ServerOptions ,
37
- ) : Protocol(options) {
35
+ public open class ServerSession (private val serverInfo : Implementation , options : ServerOptions ) : Protocol(options) {
36
+ @Suppress(" ktlint:standard:backing-property-naming" )
38
37
private var _onInitialized : (() -> Unit ) = {}
38
+
39
+ @Suppress(" ktlint:standard:backing-property-naming" )
39
40
private var _onClose : () -> Unit = {}
40
41
41
42
init {
@@ -102,9 +103,7 @@ public open class ServerSession(
102
103
* @return The result of the ping request.
103
104
* @throws IllegalStateException If for some reason the method is not supported or the connection is closed.
104
105
*/
105
- public suspend fun ping (): EmptyRequestResult {
106
- return request<EmptyRequestResult >(PingRequest ())
107
- }
106
+ public suspend fun ping (): EmptyRequestResult = request<EmptyRequestResult >(PingRequest ())
108
107
109
108
/* *
110
109
* Creates a message using the server's sampling capability.
@@ -116,7 +115,7 @@ public open class ServerSession(
116
115
*/
117
116
public suspend fun createMessage (
118
117
params : CreateMessageRequest ,
119
- options : RequestOptions ? = null
118
+ options : RequestOptions ? = null,
120
119
): CreateMessageResult {
121
120
logger.debug { " Creating message with params: $params " }
122
121
return request<CreateMessageResult >(params, options)
@@ -132,7 +131,7 @@ public open class ServerSession(
132
131
*/
133
132
public suspend fun listRoots (
134
133
params : JsonObject = EmptyJsonObject ,
135
- options : RequestOptions ? = null
134
+ options : RequestOptions ? = null,
136
135
): ListRootsResult {
137
136
logger.debug { " Listing roots with params: $params " }
138
137
return request<ListRootsResult >(ListRootsRequest (params), options)
@@ -141,7 +140,7 @@ public open class ServerSession(
141
140
public suspend fun createElicitation (
142
141
message : String ,
143
142
requestedSchema : RequestedSchema ,
144
- options : RequestOptions ? = null
143
+ options : RequestOptions ? = null,
145
144
): CreateElicitationResult {
146
145
logger.debug { " Creating elicitation with message: $message " }
147
146
return request(CreateElicitationRequest (message, requestedSchema), options)
@@ -201,29 +200,33 @@ public open class ServerSession(
201
200
*/
202
201
override fun assertCapabilityForMethod (method : Method ) {
203
202
logger.trace { " Asserting capability for method: ${method.value} " }
204
- when (method.value ) {
205
- " sampling/createMessage " -> {
203
+ when (method) {
204
+ Defined . SamplingCreateMessage -> {
206
205
if (clientCapabilities?.sampling == null ) {
207
206
logger.error { " Client capability assertion failed: sampling not supported" }
208
207
throw IllegalStateException (" Client does not support sampling (required for ${method.value} )" )
209
208
}
210
209
}
211
210
212
- " roots/list " -> {
211
+ Defined . RootsList -> {
213
212
if (clientCapabilities?.roots == null ) {
214
213
throw IllegalStateException (" Client does not support listing roots (required for ${method.value} )" )
215
214
}
216
215
}
217
216
218
- " elicitation/create " -> {
217
+ Defined . ElicitationCreate -> {
219
218
if (clientCapabilities?.elicitation == null ) {
220
219
throw IllegalStateException (" Client does not support elicitation (required for ${method.value} )" )
221
220
}
222
221
}
223
222
224
- " ping " -> {
223
+ Defined . Ping -> {
225
224
// No specific capability required
226
225
}
226
+
227
+ else -> {
228
+ throw IllegalStateException (" Server does not support $method " )
229
+ }
227
230
}
228
231
}
229
232
@@ -236,37 +239,49 @@ public open class ServerSession(
236
239
*/
237
240
override fun assertNotificationCapability (method : Method ) {
238
241
logger.trace { " Asserting notification capability for method: ${method.value} " }
239
- when (method.value ) {
240
- " notifications/message " -> {
242
+ when (method) {
243
+ Defined . NotificationsMessage -> {
241
244
if (serverCapabilities.logging == null ) {
242
245
logger.error { " Server capability assertion failed: logging not supported" }
243
246
throw IllegalStateException (" Server does not support logging (required for ${method.value} )" )
244
247
}
245
248
}
246
249
247
- " notifications/resources/updated" ,
248
- " notifications/resources/list_changed" -> {
250
+ Defined .NotificationsResourcesUpdated ,
251
+ Defined .NotificationsResourcesListChanged ,
252
+ -> {
249
253
if (serverCapabilities.resources == null ) {
250
- throw IllegalStateException (" Server does not support notifying about resources (required for ${method.value} )" )
254
+ throw IllegalStateException (
255
+ " Server does not support notifying about resources (required for ${method.value} )" ,
256
+ )
251
257
}
252
258
}
253
259
254
- " notifications/tools/list_changed " -> {
260
+ Defined . NotificationsResourcesListChanged -> {
255
261
if (serverCapabilities.tools == null ) {
256
- throw IllegalStateException (" Server does not support notifying of tool list changes (required for ${method.value} )" )
262
+ throw IllegalStateException (
263
+ " Server does not support notifying of tool list changes (required for ${method.value} )" ,
264
+ )
257
265
}
258
266
}
259
267
260
- " notifications/prompts/list_changed " -> {
268
+ Defined . NotificationsPromptsListChanged -> {
261
269
if (serverCapabilities.prompts == null ) {
262
- throw IllegalStateException (" Server does not support notifying of prompt list changes (required for ${method.value} )" )
270
+ throw IllegalStateException (
271
+ " Server does not support notifying of prompt list changes (required for ${method.value} )" ,
272
+ )
263
273
}
264
274
}
265
275
266
- " notifications/cancelled" ,
267
- " notifications/progress" -> {
276
+ Defined .NotificationsCancelled ,
277
+ Defined .NotificationsProgress ,
278
+ -> {
268
279
// Always allowed
269
280
}
281
+
282
+ else -> {
283
+ throw IllegalStateException (" Server does not support $method or it's not a notification method" )
284
+ }
270
285
}
271
286
}
272
287
@@ -279,45 +294,54 @@ public open class ServerSession(
279
294
*/
280
295
override fun assertRequestHandlerCapability (method : Method ) {
281
296
logger.trace { " Asserting request handler capability for method: ${method.value} " }
282
- when (method.value ) {
283
- " sampling/createMessage " -> {
297
+ when (method) {
298
+ Defined . SamplingCreateMessage -> {
284
299
if (serverCapabilities.sampling == null ) {
285
300
logger.error { " Server capability assertion failed: sampling not supported" }
286
301
throw IllegalStateException (" Server does not support sampling (required for $method )" )
287
302
}
288
303
}
289
304
290
- " logging/setLevel " -> {
305
+ Defined . LoggingSetLevel -> {
291
306
if (serverCapabilities.logging == null ) {
292
307
throw IllegalStateException (" Server does not support logging (required for $method )" )
293
308
}
294
309
}
295
310
296
- " prompts/get" ,
297
- " prompts/list" -> {
311
+ Defined .PromptsGet ,
312
+ Defined .PromptsList ,
313
+ -> {
298
314
if (serverCapabilities.prompts == null ) {
299
315
throw IllegalStateException (" Server does not support prompts (required for $method )" )
300
316
}
301
317
}
302
318
303
- " resources/list" ,
304
- " resources/templates/list" ,
305
- " resources/read" -> {
319
+ Defined .ResourcesList ,
320
+ Defined .ResourcesTemplatesList ,
321
+ Defined .ResourcesRead ,
322
+ Defined .ResourcesSubscribe ,
323
+ Defined .ResourcesUnsubscribe ,
324
+ -> {
306
325
if (serverCapabilities.resources == null ) {
307
326
throw IllegalStateException (" Server does not support resources (required for $method )" )
308
327
}
309
328
}
310
329
311
- " tools/call" ,
312
- " tools/list" -> {
330
+ Defined .ToolsCall ,
331
+ Defined .ToolsList ,
332
+ -> {
313
333
if (serverCapabilities.tools == null ) {
314
334
throw IllegalStateException (" Server does not support tools (required for $method )" )
315
335
}
316
336
}
317
337
318
- " ping " , " initialize " -> {
338
+ Defined . Ping , Defined . Initialize -> {
319
339
// No capability required
320
340
}
341
+
342
+ else -> {
343
+ throw IllegalStateException (" Server does not support $method or it's not a request handler method" )
344
+ }
321
345
}
322
346
}
323
347
@@ -330,14 +354,16 @@ public open class ServerSession(
330
354
val protocolVersion = if (SUPPORTED_PROTOCOL_VERSIONS .contains(requestedVersion)) {
331
355
requestedVersion
332
356
} else {
333
- logger.warn { " Client requested unsupported protocol version $requestedVersion , falling back to $LATEST_PROTOCOL_VERSION " }
357
+ logger.warn {
358
+ " Client requested unsupported protocol version $requestedVersion , falling back to $LATEST_PROTOCOL_VERSION "
359
+ }
334
360
LATEST_PROTOCOL_VERSION
335
361
}
336
362
337
363
return InitializeResult (
338
364
protocolVersion = protocolVersion,
339
365
capabilities = serverCapabilities,
340
- serverInfo = serverInfo
366
+ serverInfo = serverInfo,
341
367
)
342
368
}
343
369
}
0 commit comments