@@ -8,8 +8,10 @@ import kotlinx.serialization.ExperimentalSerializationApi
88import kotlinx.serialization.Serializable
99import kotlinx.serialization.json.JsonElement
1010import kotlinx.serialization.json.JsonObject
11+ import kotlinx.serialization.json.JsonPrimitive
1112import kotlinx.serialization.json.decodeFromJsonElement
1213import kotlinx.serialization.json.encodeToJsonElement
14+ import kotlinx.serialization.json.jsonObject
1315import kotlin.concurrent.atomics.AtomicLong
1416import kotlin.concurrent.atomics.ExperimentalAtomicApi
1517import kotlin.concurrent.atomics.incrementAndFetch
@@ -123,9 +125,10 @@ public sealed interface Request {
123125 * @return The JSON-RPC request representation.
124126 */
125127internal fun Request.toJSON (): JSONRPCRequest {
128+ val encoded = JsonObject (McpJson .encodeToJsonElement(this ).jsonObject.minus(" method" ))
126129 return JSONRPCRequest (
127130 method = method.value,
128- params = McpJson .encodeToJsonElement( this ) ,
131+ params = encoded ,
129132 jsonrpc = JSONRPC_VERSION ,
130133 )
131134}
@@ -135,10 +138,11 @@ internal fun Request.toJSON(): JSONRPCRequest {
135138 *
136139 * @return The decoded [Request] or null
137140 */
138- internal fun JSONRPCRequest.fromJSON (): Request ? {
139- val serializer = selectRequestDeserializer(method)
140- val params = params
141- return McpJson .decodeFromJsonElement<Request >(serializer, params)
141+ internal fun JSONRPCRequest.fromJSON (): Request {
142+ val requestData = JsonObject (params.jsonObject.plus(" method" to JsonPrimitive (method)))
143+
144+ val deserializer = selectRequestDeserializer(method)
145+ return McpJson .decodeFromJsonElement(deserializer, requestData)
142146}
143147
144148/* *
@@ -163,7 +167,7 @@ public sealed interface Notification {
163167 * @return The JSON-RPC notification representation.
164168 */
165169internal fun Notification.toJSON (): JSONRPCNotification {
166- val encoded = McpJson .encodeToJsonElement<Notification >(this )
170+ val encoded = JsonObject ( McpJson .encodeToJsonElement<Notification >(this ).jsonObject.minus( " method " ) )
167171 return JSONRPCNotification (
168172 method.value,
169173 params = encoded
@@ -176,7 +180,8 @@ internal fun Notification.toJSON(): JSONRPCNotification {
176180 * @return The decoded [Notification].
177181 */
178182internal fun JSONRPCNotification.fromJSON (): Notification {
179- return McpJson .decodeFromJsonElement<Notification >(params)
183+ val data = JsonObject (params.jsonObject.plus(" method" to JsonPrimitive (method)))
184+ return McpJson .decodeFromJsonElement<Notification >(data)
180185}
181186
182187/* *
0 commit comments