Skip to content

Commit ad31693

Browse files
authored
Refactor JSON processing to exclude "method" in serialization (#157)
1 parent 5347a7a commit ad31693

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/shared/Protocol.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,10 +465,7 @@ public abstract class Protocol(
465465
val transport = this.transport ?: error("Not connected")
466466
assertNotificationCapability(notification.method)
467467

468-
val message = JSONRPCNotification(
469-
notification.method.value,
470-
params = McpJson.encodeToJsonElement<Notification>(notification) as JsonObject,
471-
)
468+
val message = notification.toJSON()
472469
transport.send(message)
473470
}
474471

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import kotlinx.serialization.ExperimentalSerializationApi
88
import kotlinx.serialization.Serializable
99
import kotlinx.serialization.json.JsonElement
1010
import kotlinx.serialization.json.JsonObject
11+
import kotlinx.serialization.json.JsonPrimitive
1112
import kotlinx.serialization.json.decodeFromJsonElement
1213
import kotlinx.serialization.json.encodeToJsonElement
14+
import kotlinx.serialization.json.jsonObject
1315
import kotlin.concurrent.atomics.AtomicLong
1416
import kotlin.concurrent.atomics.ExperimentalAtomicApi
1517
import kotlin.concurrent.atomics.incrementAndFetch
@@ -123,9 +125,10 @@ public sealed interface Request {
123125
* @return The JSON-RPC request representation.
124126
*/
125127
internal 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
*/
165169
internal 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
*/
178182
internal 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

Comments
 (0)