@@ -9,6 +9,7 @@ import kotlinx.serialization.Serializable
9
9
import kotlinx.serialization.json.JsonElement
10
10
import kotlinx.serialization.json.JsonObject
11
11
import kotlinx.serialization.json.JsonPrimitive
12
+ import kotlinx.serialization.json.buildJsonObject
12
13
import kotlinx.serialization.json.decodeFromJsonElement
13
14
import kotlinx.serialization.json.encodeToJsonElement
14
15
import kotlinx.serialization.json.jsonObject
@@ -125,10 +126,11 @@ public sealed interface Request {
125
126
* @return The JSON-RPC request representation.
126
127
*/
127
128
internal fun Request.toJSON (): JSONRPCRequest {
128
- val encoded = JsonObject (McpJson .encodeToJsonElement(this ).jsonObject.minus(" method" ))
129
+ val fullJson = McpJson .encodeToJsonElement(this ).jsonObject
130
+ val params = JsonObject (fullJson.filterKeys { it != " method" })
129
131
return JSONRPCRequest (
130
132
method = method.value,
131
- params = encoded ,
133
+ params = params ,
132
134
jsonrpc = JSONRPC_VERSION ,
133
135
)
134
136
}
@@ -139,8 +141,7 @@ internal fun Request.toJSON(): JSONRPCRequest {
139
141
* @return The decoded [Request] or null
140
142
*/
141
143
internal fun JSONRPCRequest.fromJSON (): Request {
142
- val requestData = JsonObject (params.jsonObject.plus(" method" to JsonPrimitive (method)))
143
-
144
+ val requestData = JsonObject (params.jsonObject + (" method" to JsonPrimitive (method)))
144
145
val deserializer = selectRequestDeserializer(method)
145
146
return McpJson .decodeFromJsonElement(deserializer, requestData)
146
147
}
@@ -159,6 +160,7 @@ public open class CustomRequest(override val method: Method) : Request
159
160
@Serializable(with = NotificationPolymorphicSerializer ::class )
160
161
public sealed interface Notification {
161
162
public val method: Method
163
+ public val params: NotificationParams ?
162
164
}
163
165
164
166
/* *
@@ -167,10 +169,9 @@ public sealed interface Notification {
167
169
* @return The JSON-RPC notification representation.
168
170
*/
169
171
internal fun Notification.toJSON (): JSONRPCNotification {
170
- val encoded = JsonObject (McpJson .encodeToJsonElement<Notification >(this ).jsonObject.minus(" method" ))
171
172
return JSONRPCNotification (
172
- method.value,
173
- params = encoded
173
+ method = method .value,
174
+ params = McpJson .encodeToJsonElement(params),
174
175
)
175
176
}
176
177
@@ -180,7 +181,10 @@ internal fun Notification.toJSON(): JSONRPCNotification {
180
181
* @return The decoded [Notification].
181
182
*/
182
183
internal fun JSONRPCNotification.fromJSON (): Notification {
183
- val data = JsonObject (params.jsonObject.plus(" method" to JsonPrimitive (method)))
184
+ val data = buildJsonObject {
185
+ put(" method" , JsonPrimitive (method))
186
+ put(" params" , params)
187
+ }
184
188
return McpJson .decodeFromJsonElement<Notification >(data)
185
189
}
186
190
@@ -295,6 +299,12 @@ public data class JSONRPCError(
295
299
val data : JsonObject = EmptyJsonObject ,
296
300
) : JSONRPCMessage
297
301
302
+ /* *
303
+ * Base interface for notification parameters with optional metadata.
304
+ */
305
+ @Serializable
306
+ public sealed interface NotificationParams : WithMeta
307
+
298
308
/* Cancellation */
299
309
/* *
300
310
* This notification can be sent by either side to indicate that it is cancelling a previously issued request.
@@ -307,19 +317,24 @@ public data class JSONRPCError(
307
317
*/
308
318
@Serializable
309
319
public data class CancelledNotification (
310
- /* *
311
- * The ID of the request to cancel.
312
- *
313
- * It MUST correspond to the ID of a request previously issued in the same direction.
314
- */
315
- val requestId : RequestId ,
316
- /* *
317
- * An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
318
- */
319
- val reason : String? ,
320
- override val _meta : JsonObject = EmptyJsonObject ,
321
- ) : ClientNotification, ServerNotification, WithMeta {
320
+ override val params : Params ,
321
+ ) : ClientNotification, ServerNotification {
322
322
override val method: Method = Method .Defined .NotificationsCancelled
323
+
324
+ @Serializable
325
+ public data class Params (
326
+ /* *
327
+ * The ID of the request to cancel.
328
+ *
329
+ * It MUST correspond to the ID of a request previously issued in the same direction.
330
+ */
331
+ val requestId : RequestId ,
332
+ /* *
333
+ * An optional string describing the reason for the cancellation. This MAY be logged or presented to the user.
334
+ */
335
+ val reason : String? = null ,
336
+ override val _meta : JsonObject = EmptyJsonObject ,
337
+ ) : NotificationParams
323
338
}
324
339
325
340
/* Initialization */
@@ -408,7 +423,7 @@ public sealed interface ServerResult : RequestResult
408
423
*/
409
424
@Serializable
410
425
public data class UnknownMethodRequestOrNotification (
411
- override val method : Method ,
426
+ override val method : Method , override val params : NotificationParams ? = null ,
412
427
) : ClientNotification, ClientRequest, ServerNotification, ServerRequest
413
428
414
429
/* *
@@ -506,8 +521,15 @@ public data class InitializeResult(
506
521
* This notification is sent from the client to the server after initialization has finished.
507
522
*/
508
523
@Serializable
509
- public class InitializedNotification : ClientNotification {
524
+ public data class InitializedNotification (
525
+ override val params : Params = Params (),
526
+ ) : ClientNotification {
510
527
override val method: Method = Method .Defined .NotificationsInitialized
528
+
529
+ @Serializable
530
+ public data class Params (
531
+ override val _meta : JsonObject = EmptyJsonObject ,
532
+ ) : NotificationParams
511
533
}
512
534
513
535
/* Ping */
@@ -528,7 +550,7 @@ public sealed interface ProgressBase {
528
550
/* *
529
551
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
530
552
*/
531
- public val progress: Int
553
+ public val progress: Double
532
554
533
555
/* *
534
556
* Total number of items to a process (or total progress required), if known.
@@ -553,7 +575,7 @@ public open class Progress(
553
575
/* *
554
576
* The progress thus far. This should increase every time progress is made, even if the total is unknown.
555
577
*/
556
- override val progress : Int ,
578
+ override val progress : Double ,
557
579
558
580
/* *
559
581
* Total number of items to a process (or total progress required), if known.
@@ -571,18 +593,32 @@ public open class Progress(
571
593
*/
572
594
@Serializable
573
595
public data class ProgressNotification (
574
- override val progress : Int ,
575
- /* *
576
- * The progress token,
577
- * which was given in the initial request,
578
- * used to associate this notification with the request that is proceeding.
579
- */
580
- public val progressToken : ProgressToken ,
581
- @Suppress(" PropertyName" ) val _meta : JsonObject = EmptyJsonObject ,
582
- override val total : Double? ,
583
- override val message : String? ,
584
- ) : ClientNotification, ServerNotification, ProgressBase {
596
+ override val params : Params ,
597
+ ) : ClientNotification, ServerNotification {
585
598
override val method: Method = Method .Defined .NotificationsProgress
599
+
600
+ @Serializable
601
+ public data class Params (
602
+ /* *
603
+ * The progress thus far. This should increase every time progress is made, even if the total is unknown.
604
+ */
605
+ override val progress : Double ,
606
+ /* *
607
+ * The progress token,
608
+ * which was given in the initial request,
609
+ * used to associate this notification with the request that is proceeding.
610
+ */
611
+ val progressToken : ProgressToken ,
612
+ /* *
613
+ * Total number of items to process (or total progress required), if known.
614
+ */
615
+ override val total : Double? = null ,
616
+ /* *
617
+ * An optional message describing the current progress.
618
+ */
619
+ override val message : String? = null ,
620
+ override val _meta : JsonObject = EmptyJsonObject ,
621
+ ) : NotificationParams, ProgressBase
586
622
}
587
623
588
624
/* Pagination */
@@ -784,8 +820,15 @@ public class ReadResourceResult(
784
820
* Servers may issue this without any previous subscription from the client.
785
821
*/
786
822
@Serializable
787
- public class ResourceListChangedNotification : ServerNotification {
823
+ public data class ResourceListChangedNotification (
824
+ override val params : Params = Params (),
825
+ ) : ServerNotification {
788
826
override val method: Method = Method .Defined .NotificationsResourcesListChanged
827
+
828
+ @Serializable
829
+ public data class Params (
830
+ override val _meta : JsonObject = EmptyJsonObject ,
831
+ ) : NotificationParams
789
832
}
790
833
791
834
/* *
@@ -821,13 +864,18 @@ public data class UnsubscribeRequest(
821
864
*/
822
865
@Serializable
823
866
public data class ResourceUpdatedNotification (
824
- /* *
825
- * The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.
826
- */
827
- val uri : String ,
828
- override val _meta : JsonObject = EmptyJsonObject ,
829
- ) : ServerNotification, WithMeta {
867
+ override val params : Params ,
868
+ ) : ServerNotification {
830
869
override val method: Method = Method .Defined .NotificationsResourcesUpdated
870
+
871
+ @Serializable
872
+ public data class Params (
873
+ /* *
874
+ * The URI of the resource that has been updated. This might be a sub-resource of the one that the client actually subscribed to.
875
+ */
876
+ val uri : String ,
877
+ override val _meta : JsonObject = EmptyJsonObject ,
878
+ ) : NotificationParams
831
879
}
832
880
833
881
/* Prompts */
@@ -1044,8 +1092,15 @@ public class GetPromptResult(
1044
1092
* Servers may issue this without any previous subscription from the client.
1045
1093
*/
1046
1094
@Serializable
1047
- public class PromptListChangedNotification : ServerNotification {
1095
+ public data class PromptListChangedNotification (
1096
+ override val params : Params = Params (),
1097
+ ) : ServerNotification {
1048
1098
override val method: Method = Method .Defined .NotificationsPromptsListChanged
1099
+
1100
+ @Serializable
1101
+ public data class Params (
1102
+ override val _meta : JsonObject = EmptyJsonObject ,
1103
+ ) : NotificationParams
1049
1104
}
1050
1105
1051
1106
/* Tools */
@@ -1223,8 +1278,15 @@ public data class CallToolRequest(
1223
1278
* Servers may issue this without any previous subscription from the client.
1224
1279
*/
1225
1280
@Serializable
1226
- public class ToolListChangedNotification : ServerNotification {
1281
+ public data class ToolListChangedNotification (
1282
+ override val params : Params = Params (),
1283
+ ) : ServerNotification {
1227
1284
override val method: Method = Method .Defined .NotificationsToolsListChanged
1285
+
1286
+ @Serializable
1287
+ public data class Params (
1288
+ override val _meta : JsonObject = EmptyJsonObject ,
1289
+ ) : NotificationParams
1228
1290
}
1229
1291
1230
1292
/* Logging */
@@ -1252,22 +1314,27 @@ public enum class LoggingLevel {
1252
1314
*/
1253
1315
@Serializable
1254
1316
public data class LoggingMessageNotification (
1255
- /* *
1256
- * The severity of this log message.
1257
- */
1258
- val level : LoggingLevel ,
1317
+ override val params : Params ,
1318
+ ) : ServerNotification {
1319
+ override val method: Method = Method .Defined .NotificationsMessage
1259
1320
1260
- /* *
1261
- * An optional name of the logger issuing this message.
1262
- */
1263
- val logger : String? = null ,
1321
+ @Serializable
1322
+ public data class Params (
1323
+ /* *
1324
+ * The severity of this log message.
1325
+ */
1326
+ val level : LoggingLevel ,
1327
+ /* *
1328
+ * An optional name of the logger issuing this message.
1329
+ */
1330
+ val logger : String? = null ,
1331
+ /* *
1332
+ * The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
1333
+ */
1334
+ val data : JsonElement ,
1335
+ override val _meta : JsonObject = EmptyJsonObject ,
1336
+ ) : NotificationParams
1264
1337
1265
- /* *
1266
- * The data to be logged, such as a string message or an object. Any JSON serializable type is allowed here.
1267
- */
1268
- val data : JsonObject = EmptyJsonObject ,
1269
- override val _meta : JsonObject = EmptyJsonObject ,
1270
- ) : ServerNotification, WithMeta {
1271
1338
/* *
1272
1339
* A request from the client to the server to enable or adjust logging.
1273
1340
*/
@@ -1281,8 +1348,6 @@ public data class LoggingMessageNotification(
1281
1348
) : ClientRequest, WithMeta {
1282
1349
override val method: Method = Method .Defined .LoggingSetLevel
1283
1350
}
1284
-
1285
- override val method: Method = Method .Defined .NotificationsMessage
1286
1351
}
1287
1352
1288
1353
/* Sampling */
@@ -1578,8 +1643,15 @@ public class ListRootsResult(
1578
1643
* A notification from the client to the server, informing it that the list of roots has changed.
1579
1644
*/
1580
1645
@Serializable
1581
- public class RootsListChangedNotification : ClientNotification {
1646
+ public data class RootsListChangedNotification (
1647
+ override val params : Params = Params (),
1648
+ ) : ClientNotification {
1582
1649
override val method: Method = Method .Defined .NotificationsRootsListChanged
1650
+
1651
+ @Serializable
1652
+ public data class Params (
1653
+ override val _meta : JsonObject = EmptyJsonObject ,
1654
+ ) : NotificationParams
1583
1655
}
1584
1656
1585
1657
/* *
0 commit comments