Repository navigation
Fix response result deserialization to use the original request method (#601) #999
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
Open
remote-controlled-man
wants to merge
6
commits into
modelcontextprotocol:main
Choose a base branch
from
remote-controlled-man:fix/kotlin-sdk-601-request-result-deserialization
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9429c40
Fix response result deserialization to use the original request metho…
remote-controlled-man 8d58d2f
Merge branch 'main' into fix/kotlin-sdk-601-request-result-deserializ…
devcrocod a674a88
Preserve response diagnostics and copied wire results (#601)
remote-controlled-man f6f65ac
Decode task envelopes before overlapping result shapes (#601)
remote-controlled-man 7a7d38b
Preserve custom result decoding failures (#601)
remote-controlled-man 44c0630
Defer task payload shape validation (#601)
remote-controlled-man 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
93 changes: 93 additions & 0 deletions
93
...textprotocol/kotlin/sdk/client/streamable/http/StreamableHttpClientTransportReplayTest.kt
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 |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package io.modelcontextprotocol.kotlin.sdk.client.streamable.http | ||
|
|
||
| import io.kotest.matchers.nulls.shouldNotBeNull | ||
| import io.kotest.matchers.shouldBe | ||
| import io.ktor.client.HttpClient | ||
| import io.ktor.client.engine.mock.MockEngine | ||
| import io.ktor.client.engine.mock.respond | ||
| import io.ktor.client.plugins.sse.SSE | ||
| import io.ktor.http.ContentType | ||
| import io.ktor.http.HttpHeaders | ||
| import io.ktor.http.HttpMethod | ||
| import io.ktor.http.HttpStatusCode | ||
| import io.ktor.http.headersOf | ||
| import io.ktor.utils.io.ByteReadChannel | ||
| import io.modelcontextprotocol.kotlin.sdk.client.StreamableHttpClientTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.Protocol | ||
| import io.modelcontextprotocol.kotlin.sdk.types.GetTaskPayloadRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.types.GetTaskPayloadRequestParams | ||
| import io.modelcontextprotocol.kotlin.sdk.types.GetTaskPayloadResult | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Method | ||
| import kotlinx.coroutines.runBlocking | ||
| import kotlinx.serialization.json.boolean | ||
| import kotlinx.serialization.json.jsonPrimitive | ||
| import kotlin.test.Test | ||
| import kotlin.time.Duration.Companion.seconds | ||
|
|
||
| /** | ||
| * Regression test for https://github.com/modelcontextprotocol/kotlin-sdk/issues/601 on the | ||
| * Streamable HTTP SSE replay path. | ||
| * | ||
| * Lives in `jvmTest` (not `commonTest`) because it drives a full `Protocol.request` round trip | ||
| * over a MockEngine: under `runTest`'s virtual clock the request's internal timeout fires while | ||
| * the real-dispatcher HTTP delivery is still in flight, so the test needs `runBlocking`. | ||
| */ | ||
| class StreamableHttpClientTransportReplayTest { | ||
|
|
||
| @Test | ||
| fun `should deserialize a replayed SSE response by the original request method`() = runBlocking { | ||
| // When a response arrives on the SSE stream with a different id, the transport rewrites it | ||
| // to the POSTed request's id. The rewrite must preserve the raw result JSON so that | ||
| // Protocol can decode the result according to the original request's method instead of | ||
| // guessing from the JSON shape. | ||
| val protocol = object : Protocol(null) { | ||
| override fun assertCapabilityForMethod(method: Method) = Unit | ||
| override fun assertNotificationCapability(method: Method) = Unit | ||
| override fun assertRequestHandlerCapability(method: Method) = Unit | ||
| } | ||
|
|
||
| val mockEngine = MockEngine { request -> | ||
| if (request.method == HttpMethod.Post) { | ||
| val sseContent = buildString { | ||
| appendLine("id: ev-1") | ||
| appendLine("event: message") | ||
| appendLine( | ||
| """data: {"jsonrpc":"2.0","id":"server-side-id",""" + | ||
| """"result":{"content":[{"type":"text","text":"task output"}],"isError":false}}""", | ||
| ) | ||
| appendLine() | ||
| } | ||
|
|
||
| respond( | ||
| content = ByteReadChannel(sseContent), | ||
| status = HttpStatusCode.OK, | ||
| headers = headersOf( | ||
| HttpHeaders.ContentType, | ||
| ContentType.Text.EventStream.toString(), | ||
| ), | ||
| ) | ||
| } else { | ||
| respond("", HttpStatusCode.OK) | ||
| } | ||
| } | ||
| val httpClient = HttpClient(mockEngine) { | ||
| install(SSE) { | ||
| reconnectionTime = 1.seconds | ||
| } | ||
| } | ||
| val transport = StreamableHttpClientTransport(httpClient, url = "http://localhost:8080/mcp") | ||
|
|
||
| protocol.connect(transport) | ||
|
|
||
| val result = protocol.request<GetTaskPayloadResult>( | ||
| GetTaskPayloadRequest(GetTaskPayloadRequestParams(taskId = "task-42")), | ||
| ) | ||
|
|
||
| // The tasks/result payload is CallToolResult-shaped; it must still surface as | ||
| // GetTaskPayloadResult with the raw payload preserved. | ||
| result["content"].shouldNotBeNull() | ||
| result["isError"]?.jsonPrimitive?.boolean shouldBe false | ||
|
|
||
| transport.close() | ||
| } | ||
| } |
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
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
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 PR description no longer matches the code, and this repo squash-merges with
squash_merge_commit_message: PR_BODY, so the description becomes the permanent commit message. The "How" section's last bullet still claims "Streamable HTTP replay rewrites the replay id in the raw JSON before decoding (decodeIncomingMessage) instead of copying an already shape-decoded message, at both SSE sites" — butStreamableHttpClientTransport.ktis not in the diff at all; both SSE sites still callmsg.copy(id = replayMessageId)(lines 468 and 517) and thiscopyoverride is what actually carries the raw JSON across the id rewrite. The "Intentional behavior change" and "Tests" sections are stale too: for the 21 mapped methods a bad payload now surfaces as the realSerializationExceptionrather than reaching the caller as a cast failure, andProtocolResultDeserializationTesthas 13 tests, not 7.Failure scenario: someone bisecting a future regression in the SSE replay path lands on this squashed commit, reads that the id rewrite happens in the raw JSON via
decodeIncomingMessage, and greps for a function and a transport change that never existed — while the real invariant (copy()preservesrawResultonly whileresultis referentially unchanged) goes unmentioned in history.Suggested fix: update the PR body before merge — replace the transport bullet with the
JSONRPCResponse.copy()raw-JSON preservation mechanism, correct the behavior-change paragraph to say decode errors now propagate for known methods, and refresh the test counts.