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.
.../src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerSessionInitializeTest.kt
Show resolved
Hide resolved
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
185bc6a to
39a0212
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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