-
Notifications
You must be signed in to change notification settings - Fork 136
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
+13
−11
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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, | ||
) | ||
} | ||
|
@@ -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) | ||
} | ||
|
||
/** | ||
|
@@ -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 | ||
|
@@ -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))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we refactor schema instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, because the scheme is correct |
||
return McpJson.decodeFromJsonElement<Notification>(data) | ||
} | ||
|
||
/** | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
toRequest
, 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.Copilot uses AI. Check for mistakes.