@@ -18,6 +18,7 @@ import io.modelcontextprotocol.kotlin.sdk.ListRootsRequest
1818import io.modelcontextprotocol.kotlin.sdk.ListRootsResult
1919import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification
2020import io.modelcontextprotocol.kotlin.sdk.Method
21+ import io.modelcontextprotocol.kotlin.sdk.Method.Defined
2122import io.modelcontextprotocol.kotlin.sdk.PingRequest
2223import io.modelcontextprotocol.kotlin.sdk.PromptListChangedNotification
2324import io.modelcontextprotocol.kotlin.sdk.ResourceListChangedNotification
@@ -31,11 +32,11 @@ import kotlinx.serialization.json.JsonObject
3132
3233private val logger = KotlinLogging .logger {}
3334
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" )
3837 private var _onInitialized : (() -> Unit ) = {}
38+
39+ @Suppress(" ktlint:standard:backing-property-naming" )
3940 private var _onClose : () -> Unit = {}
4041
4142 init {
@@ -102,9 +103,7 @@ public open class ServerSession(
102103 * @return The result of the ping request.
103104 * @throws IllegalStateException If for some reason the method is not supported or the connection is closed.
104105 */
105- public suspend fun ping (): EmptyRequestResult {
106- return request<EmptyRequestResult >(PingRequest ())
107- }
106+ public suspend fun ping (): EmptyRequestResult = request<EmptyRequestResult >(PingRequest ())
108107
109108 /* *
110109 * Creates a message using the server's sampling capability.
@@ -116,7 +115,7 @@ public open class ServerSession(
116115 */
117116 public suspend fun createMessage (
118117 params : CreateMessageRequest ,
119- options : RequestOptions ? = null
118+ options : RequestOptions ? = null,
120119 ): CreateMessageResult {
121120 logger.debug { " Creating message with params: $params " }
122121 return request<CreateMessageResult >(params, options)
@@ -132,7 +131,7 @@ public open class ServerSession(
132131 */
133132 public suspend fun listRoots (
134133 params : JsonObject = EmptyJsonObject ,
135- options : RequestOptions ? = null
134+ options : RequestOptions ? = null,
136135 ): ListRootsResult {
137136 logger.debug { " Listing roots with params: $params " }
138137 return request<ListRootsResult >(ListRootsRequest (params), options)
@@ -141,7 +140,7 @@ public open class ServerSession(
141140 public suspend fun createElicitation (
142141 message : String ,
143142 requestedSchema : RequestedSchema ,
144- options : RequestOptions ? = null
143+ options : RequestOptions ? = null,
145144 ): CreateElicitationResult {
146145 logger.debug { " Creating elicitation with message: $message " }
147146 return request(CreateElicitationRequest (message, requestedSchema), options)
@@ -201,29 +200,33 @@ public open class ServerSession(
201200 */
202201 override fun assertCapabilityForMethod (method : Method ) {
203202 logger.trace { " Asserting capability for method: ${method.value} " }
204- when (method.value ) {
205- " sampling/createMessage " -> {
203+ when (method) {
204+ Defined . SamplingCreateMessage -> {
206205 if (clientCapabilities?.sampling == null ) {
207206 logger.error { " Client capability assertion failed: sampling not supported" }
208207 throw IllegalStateException (" Client does not support sampling (required for ${method.value} )" )
209208 }
210209 }
211210
212- " roots/list " -> {
211+ Defined . RootsList -> {
213212 if (clientCapabilities?.roots == null ) {
214213 throw IllegalStateException (" Client does not support listing roots (required for ${method.value} )" )
215214 }
216215 }
217216
218- " elicitation/create " -> {
217+ Defined . ElicitationCreate -> {
219218 if (clientCapabilities?.elicitation == null ) {
220219 throw IllegalStateException (" Client does not support elicitation (required for ${method.value} )" )
221220 }
222221 }
223222
224- " ping " -> {
223+ Defined . Ping -> {
225224 // No specific capability required
226225 }
226+
227+ else -> {
228+ throw IllegalStateException (" Server does not support $method " )
229+ }
227230 }
228231 }
229232
@@ -236,37 +239,49 @@ public open class ServerSession(
236239 */
237240 override fun assertNotificationCapability (method : Method ) {
238241 logger.trace { " Asserting notification capability for method: ${method.value} " }
239- when (method.value ) {
240- " notifications/message " -> {
242+ when (method) {
243+ Defined . NotificationsMessage -> {
241244 if (serverCapabilities.logging == null ) {
242245 logger.error { " Server capability assertion failed: logging not supported" }
243246 throw IllegalStateException (" Server does not support logging (required for ${method.value} )" )
244247 }
245248 }
246249
247- " notifications/resources/updated" ,
248- " notifications/resources/list_changed" -> {
250+ Defined .NotificationsResourcesUpdated ,
251+ Defined .NotificationsResourcesListChanged ,
252+ -> {
249253 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+ )
251257 }
252258 }
253259
254- " notifications/tools/list_changed " -> {
260+ Defined . NotificationsResourcesListChanged -> {
255261 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+ )
257265 }
258266 }
259267
260- " notifications/prompts/list_changed " -> {
268+ Defined . NotificationsPromptsListChanged -> {
261269 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+ )
263273 }
264274 }
265275
266- " notifications/cancelled" ,
267- " notifications/progress" -> {
276+ Defined .NotificationsCancelled ,
277+ Defined .NotificationsProgress ,
278+ -> {
268279 // Always allowed
269280 }
281+
282+ else -> {
283+ throw IllegalStateException (" Server does not support $method or it's not a notification method" )
284+ }
270285 }
271286 }
272287
@@ -279,45 +294,54 @@ public open class ServerSession(
279294 */
280295 override fun assertRequestHandlerCapability (method : Method ) {
281296 logger.trace { " Asserting request handler capability for method: ${method.value} " }
282- when (method.value ) {
283- " sampling/createMessage " -> {
297+ when (method) {
298+ Defined . SamplingCreateMessage -> {
284299 if (serverCapabilities.sampling == null ) {
285300 logger.error { " Server capability assertion failed: sampling not supported" }
286301 throw IllegalStateException (" Server does not support sampling (required for $method )" )
287302 }
288303 }
289304
290- " logging/setLevel " -> {
305+ Defined . LoggingSetLevel -> {
291306 if (serverCapabilities.logging == null ) {
292307 throw IllegalStateException (" Server does not support logging (required for $method )" )
293308 }
294309 }
295310
296- " prompts/get" ,
297- " prompts/list" -> {
311+ Defined .PromptsGet ,
312+ Defined .PromptsList ,
313+ -> {
298314 if (serverCapabilities.prompts == null ) {
299315 throw IllegalStateException (" Server does not support prompts (required for $method )" )
300316 }
301317 }
302318
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+ -> {
306325 if (serverCapabilities.resources == null ) {
307326 throw IllegalStateException (" Server does not support resources (required for $method )" )
308327 }
309328 }
310329
311- " tools/call" ,
312- " tools/list" -> {
330+ Defined .ToolsCall ,
331+ Defined .ToolsList ,
332+ -> {
313333 if (serverCapabilities.tools == null ) {
314334 throw IllegalStateException (" Server does not support tools (required for $method )" )
315335 }
316336 }
317337
318- " ping " , " initialize " -> {
338+ Defined . Ping , Defined . Initialize -> {
319339 // No capability required
320340 }
341+
342+ else -> {
343+ throw IllegalStateException (" Server does not support $method or it's not a request handler method" )
344+ }
321345 }
322346 }
323347
@@ -330,14 +354,16 @@ public open class ServerSession(
330354 val protocolVersion = if (SUPPORTED_PROTOCOL_VERSIONS .contains(requestedVersion)) {
331355 requestedVersion
332356 } 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+ }
334360 LATEST_PROTOCOL_VERSION
335361 }
336362
337363 return InitializeResult (
338364 protocolVersion = protocolVersion,
339365 capabilities = serverCapabilities,
340- serverInfo = serverInfo
366+ serverInfo = serverInfo,
341367 )
342368 }
343369}
0 commit comments