-
Notifications
You must be signed in to change notification settings - Fork 209
fix: reject duplicate initialize requests in ServerSession #624
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
Changes from all commits
Commits
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
152 changes: 152 additions & 0 deletions
152
...c/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerSessionInitializeTest.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,152 @@ | ||
| package io.modelcontextprotocol.kotlin.sdk.server | ||
|
|
||
| import io.modelcontextprotocol.kotlin.sdk.shared.InMemoryTransport | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ClientCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.types.Implementation | ||
| import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.types.InitializeRequestParams | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCError | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage | ||
| import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCResponse | ||
| import io.modelcontextprotocol.kotlin.sdk.types.LATEST_PROTOCOL_VERSION | ||
| import io.modelcontextprotocol.kotlin.sdk.types.RPCError | ||
| import io.modelcontextprotocol.kotlin.sdk.types.ServerCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.types.toJSON | ||
| import kotlinx.coroutines.CompletableDeferred | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.joinAll | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.test.runTest | ||
| import kotlinx.coroutines.withContext | ||
| import org.junit.jupiter.api.Test | ||
| import java.util.concurrent.CopyOnWriteArrayList | ||
| import kotlin.test.assertEquals | ||
| import kotlin.test.assertNotNull | ||
| import kotlin.test.assertNull | ||
| import kotlin.test.assertTrue | ||
|
|
||
| class ServerSessionInitializeTest { | ||
|
|
||
| private fun createSession(): ServerSession = ServerSession( | ||
| serverInfo = Implementation(name = "test-server", version = "1.0"), | ||
| options = ServerOptions(capabilities = ServerCapabilities()), | ||
| instructions = null, | ||
| ) | ||
|
|
||
| private fun createInitializeRequest(clientName: String = "test-client"): InitializeRequest = InitializeRequest( | ||
| InitializeRequestParams( | ||
| protocolVersion = LATEST_PROTOCOL_VERSION, | ||
| capabilities = ClientCapabilities(), | ||
| clientInfo = Implementation(name = clientName, version = "1.0"), | ||
| ), | ||
| ) | ||
|
|
||
| @Test | ||
| fun `should handle first initialize request successfully`() = runTest { | ||
| val session = createSession() | ||
| val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair() | ||
|
|
||
| assertNull(session.clientCapabilities) | ||
| assertNull(session.clientVersion) | ||
|
|
||
| val responseDone = CompletableDeferred<JSONRPCResponse>() | ||
| clientTransport.onMessage { message -> | ||
| if (message is JSONRPCResponse) { | ||
| responseDone.complete(message) | ||
| } | ||
| } | ||
|
|
||
| session.connect(serverTransport) | ||
| clientTransport.send(createInitializeRequest().toJSON()) | ||
|
|
||
| val response = responseDone.await() | ||
| assertNotNull(response.result) | ||
| assertNotNull(session.clientCapabilities) | ||
| assertEquals("test-client", session.clientVersion?.name) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should reject duplicate initialize request`() = runTest { | ||
| val session = createSession() | ||
| val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair() | ||
|
|
||
| val responses = CopyOnWriteArrayList<JSONRPCMessage>() | ||
| val secondResponseDone = CompletableDeferred<Unit>() | ||
|
|
||
| clientTransport.onMessage { message -> | ||
| when (message) { | ||
| is JSONRPCResponse, is JSONRPCError -> { | ||
| responses.add(message) | ||
| if (responses.size == 2) secondResponseDone.complete(Unit) | ||
| } | ||
|
|
||
| else -> {} | ||
| } | ||
| } | ||
|
|
||
| session.connect(serverTransport) | ||
|
|
||
| // First initialize should succeed | ||
| clientTransport.send(createInitializeRequest(clientName = "first-client").toJSON()) | ||
|
|
||
| // Second initialize should be rejected | ||
| clientTransport.send(createInitializeRequest(clientName = "second-client").toJSON()) | ||
|
|
||
| secondResponseDone.await() | ||
|
|
||
| assertEquals(2, responses.size) | ||
| assertTrue(responses[0] is JSONRPCResponse, "First response should be success") | ||
| assertTrue(responses[1] is JSONRPCError, "Second response should be error") | ||
| assertEquals(RPCError.ErrorCode.INVALID_REQUEST, (responses[1] as JSONRPCError).error.code) | ||
|
|
||
| // Capabilities still reflect the first client, not overwritten | ||
| assertEquals("first-client", session.clientVersion?.name) | ||
| } | ||
|
|
||
| @Test | ||
| fun `should reject concurrent initialize requests - only first succeeds`() = runTest { | ||
| val session = createSession() | ||
| val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair() | ||
|
|
||
| val n = 10 | ||
| val allResponsesDone = CompletableDeferred<Unit>() | ||
| val successes = CopyOnWriteArrayList<JSONRPCResponse>() | ||
| val errors = CopyOnWriteArrayList<JSONRPCError>() | ||
|
|
||
| clientTransport.onMessage { message -> | ||
| when (message) { | ||
| is JSONRPCResponse -> successes.add(message) | ||
| is JSONRPCError -> errors.add(message) | ||
| else -> {} | ||
| } | ||
| if (successes.size + errors.size == n) { | ||
| allResponsesDone.complete(Unit) | ||
| } | ||
| } | ||
|
|
||
| session.connect(serverTransport) | ||
|
|
||
| // Use Dispatchers.Default for true parallelism on JVM | ||
| withContext(Dispatchers.Default) { | ||
| val barrier = CompletableDeferred<Unit>() | ||
| val jobs = (1..n).map { i -> | ||
| launch { | ||
| barrier.await() | ||
| clientTransport.send( | ||
| createInitializeRequest(clientName = "client-$i").toJSON(), | ||
| ) | ||
| } | ||
| } | ||
| barrier.complete(Unit) | ||
| jobs.joinAll() | ||
| } | ||
|
|
||
| allResponsesDone.await() | ||
|
|
||
| assertEquals(1, successes.size, "Exactly one initialize should succeed") | ||
| assertEquals(n - 1, errors.size, "All other initializes should be rejected") | ||
| errors.forEach { error -> | ||
| assertEquals(RPCError.ErrorCode.INVALID_REQUEST, error.error.code) | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.