Skip to content

Refactor JSON processing to exclude "method" in serialization #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,7 @@ public abstract class Protocol(
val transport = this.transport ?: error("Not connected")
assertNotificationCapability(notification.method)

val message = JSONRPCNotification(
notification.method.value,
params = McpJson.encodeToJsonElement<Notification>(notification) as JsonObject,
)
val message = notification.toJSON()
transport.send(message)
}

Expand Down
19 changes: 12 additions & 7 deletions src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonElement
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.decodeFromJsonElement
import kotlinx.serialization.json.encodeToJsonElement
import kotlinx.serialization.json.jsonObject
import kotlin.concurrent.atomics.AtomicLong
import kotlin.concurrent.atomics.ExperimentalAtomicApi
import kotlin.concurrent.atomics.incrementAndFetch
Expand Down Expand Up @@ -123,9 +125,10 @@ public sealed interface Request {
* @return The JSON-RPC request representation.
*/
internal fun Request.toJSON(): JSONRPCRequest {
val encoded = JsonObject(McpJson.encodeToJsonElement(this).jsonObject.minus("method"))
return JSONRPCRequest(
method = method.value,
params = McpJson.encodeToJsonElement(this),
params = encoded,
jsonrpc = JSONRPC_VERSION,
)
}
Expand All @@ -135,10 +138,11 @@ internal fun Request.toJSON(): JSONRPCRequest {
*
* @return The decoded [Request] or null
*/
internal fun JSONRPCRequest.fromJSON(): Request? {
val serializer = selectRequestDeserializer(method)
val params = params
return McpJson.decodeFromJsonElement<Request>(serializer, params)
internal fun JSONRPCRequest.fromJSON(): Request {
val requestData = JsonObject(params.jsonObject.plus("method" to JsonPrimitive(method)))

val deserializer = selectRequestDeserializer(method)
return McpJson.decodeFromJsonElement(deserializer, requestData)
Comment on lines +141 to +145
Copy link
Preview

Copilot AI Jul 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function signature changed from returning Request? to Request, removing null safety. This is a breaking change that could cause issues if callers were handling null returns. Consider maintaining the nullable return type or ensuring all call sites are updated to handle potential exceptions.

Suggested change
internal fun JSONRPCRequest.fromJSON(): Request {
val requestData = JsonObject(params.jsonObject.plus("method" to JsonPrimitive(method)))
val deserializer = selectRequestDeserializer(method)
return McpJson.decodeFromJsonElement(deserializer, requestData)
internal fun JSONRPCRequest.fromJSON(): Request? {
val requestData = JsonObject(params.jsonObject.plus("method" to JsonPrimitive(method)))
val deserializer = selectRequestDeserializer(method)
return try {
McpJson.decodeFromJsonElement(deserializer, requestData)
} catch (e: Exception) {
null
}

Copilot uses AI. Check for mistakes.

}

/**
Expand All @@ -163,7 +167,7 @@ public sealed interface Notification {
* @return The JSON-RPC notification representation.
*/
internal fun Notification.toJSON(): JSONRPCNotification {
val encoded = McpJson.encodeToJsonElement<Notification>(this)
val encoded = JsonObject(McpJson.encodeToJsonElement<Notification>(this).jsonObject.minus("method"))
return JSONRPCNotification(
method.value,
params = encoded
Expand All @@ -176,7 +180,8 @@ internal fun Notification.toJSON(): JSONRPCNotification {
* @return The decoded [Notification].
*/
internal fun JSONRPCNotification.fromJSON(): Notification {
return McpJson.decodeFromJsonElement<Notification>(params)
val data = JsonObject(params.jsonObject.plus("method" to JsonPrimitive(method)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we refactor schema instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the scheme is correct

return McpJson.decodeFromJsonElement<Notification>(data)
}

/**
Expand Down
Loading