fix: reject duplicate initialize requests in ServerSession#624
fix: reject duplicate initialize requests in ServerSession#624
Conversation
There was a problem hiding this comment.
Pull request overview
This PR hardens the Kotlin server session initialization handshake by rejecting duplicate initialize requests and adds integration tests to validate the behavior (including concurrent initialization attempts).
Changes:
- Reject duplicate
initializerequests inServerSession.handleInitialize()with JSON-RPCINVALID_REQUEST(-32600). - Make
clientCapabilitiesandclientVersionread-only public properties backed byAtomicRef, usingcompareAndSet(null, ...)to atomically guard initialization. - Add JVM integration tests for first/duplicate/concurrent initialize behavior; adjust an existing client integration test to use a separate transport pair.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerSession.kt | Adds atomic initialization guard and returns INVALID_REQUEST on duplicate initialize. |
| integration-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerSessionInitializeTest.kt | New tests covering first initialize success and duplicate/concurrent initialize rejection. |
| integration-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/ClientTest.kt | Fixes test setup by using a new transport pair for a second client/server connection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // Capabilities still reflect the first client, not overwritten | ||
| assertEquals("first-client", session.clientVersion?.name) | ||
| } |
There was a problem hiding this comment.
This test claims to verify that capabilities from the first initialize are preserved, but both requests use the same default ClientCapabilities() and the assertion checks clientVersion instead. This can’t detect a regression where clientCapabilities gets overwritten—consider passing distinct capabilities per request and asserting session.clientCapabilities (or update the comment to match what’s being asserted).
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
ServerSession.handleInitialize()now rejects duplicateinitializerequests withINVALID_REQUEST(-32600) error.clientCapabilitiesandclientVersionchanged fromvartoAtomicRef-backedval, with atomiccompareAndSet(null, ...)serving as both the guard and the writeHow Has This Been Tested?
second
initializereturns error, original capabilities preservedBreaking Changes
none
Types of changes
Checklist
Additional context