Updated ServerPromptsTest, move AbstractTransport to it's own file (#376)
# Refactor and enhance prompt handling logic and tests, update Kotest version and modularize `AbstractTransport` - Added more post-conditions/scenarios to ServerPromptsTest - Extracted `AbstractTransport` to a dedicated file for modularity. - Improved Protocol connection state validation: When disconnected, the Protocol will throw `IllegalStateException` instead of `Error`. - Minor Kotlin fixes and suppressions to improve code quality. ## Motivation and Context Improve test scenarios in ServerPromptsTest ## How Has This Been Tested? Integration tests updated ## Breaking Changes When disconnected, the Protocol will throw `IllegalStateException` instead of `Error`. ## Types of changes <!-- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [x] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [x] Test update - [x] Refactoring ## Checklist <!-- Go over all the following points, and put an `x` in all the boxes that apply. --> - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [ ] I have added appropriate error handling - [ ] I have added or updated documentation as needed ## Additional context <!-- Add any other context, implementation notes, or design decisions -->
This commit is contained in:
parent
6afef103c9
commit
5636faf60c
4 changed files with 282 additions and 67 deletions
|
|
@ -0,0 +1,54 @@
|
|||
package io.modelcontextprotocol.kotlin.sdk.shared
|
||||
|
||||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
|
||||
/**
|
||||
* Implements [onClose], [onError] and [onMessage] functions of [Transport] providing
|
||||
* corresponding [_onClose], [_onError] and [_onMessage] properties to use for an implementation.
|
||||
*/
|
||||
@Suppress("PropertyName")
|
||||
public abstract class AbstractTransport : Transport {
|
||||
protected var _onClose: (() -> Unit) = {}
|
||||
private set
|
||||
protected var _onError: ((Throwable) -> Unit) = {}
|
||||
private set
|
||||
|
||||
// to not skip messages
|
||||
private val _onMessageInitialized = CompletableDeferred<Unit>()
|
||||
protected var _onMessage: (suspend ((JSONRPCMessage) -> Unit)) = {
|
||||
_onMessageInitialized.await()
|
||||
_onMessage.invoke(it)
|
||||
}
|
||||
private set
|
||||
|
||||
override fun onClose(block: () -> Unit) {
|
||||
val old = _onClose
|
||||
_onClose = {
|
||||
old()
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(block: (Throwable) -> Unit) {
|
||||
val old = _onError
|
||||
_onError = { e ->
|
||||
old(e)
|
||||
block(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMessage(block: suspend (JSONRPCMessage) -> Unit) {
|
||||
val old: suspend (JSONRPCMessage) -> Unit = when (_onMessageInitialized.isCompleted) {
|
||||
true -> _onMessage
|
||||
false -> { _ -> }
|
||||
}
|
||||
|
||||
_onMessage = { message ->
|
||||
old(message)
|
||||
block(message)
|
||||
}
|
||||
|
||||
_onMessageInitialized.complete(Unit)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package io.modelcontextprotocol.kotlin.sdk.shared
|
||||
|
||||
import io.modelcontextprotocol.kotlin.sdk.types.JSONRPCMessage
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
|
||||
/**
|
||||
* Describes the minimal contract for MCP transport that a client or server can communicate over.
|
||||
|
|
@ -47,53 +46,3 @@ public interface Transport {
|
|||
*/
|
||||
public fun onMessage(block: suspend (JSONRPCMessage) -> Unit)
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements [onClose], [onError] and [onMessage] functions of [Transport] providing
|
||||
* corresponding [_onClose], [_onError] and [_onMessage] properties to use for an implementation.
|
||||
*/
|
||||
@Suppress("PropertyName")
|
||||
public abstract class AbstractTransport : Transport {
|
||||
protected var _onClose: (() -> Unit) = {}
|
||||
private set
|
||||
protected var _onError: ((Throwable) -> Unit) = {}
|
||||
private set
|
||||
|
||||
// to not skip messages
|
||||
private val _onMessageInitialized = CompletableDeferred<Unit>()
|
||||
protected var _onMessage: (suspend ((JSONRPCMessage) -> Unit)) = {
|
||||
_onMessageInitialized.await()
|
||||
_onMessage.invoke(it)
|
||||
}
|
||||
private set
|
||||
|
||||
override fun onClose(block: () -> Unit) {
|
||||
val old = _onClose
|
||||
_onClose = {
|
||||
old()
|
||||
block()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onError(block: (Throwable) -> Unit) {
|
||||
val old = _onError
|
||||
_onError = { e ->
|
||||
old(e)
|
||||
block(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun onMessage(block: suspend (JSONRPCMessage) -> Unit) {
|
||||
val old: suspend (JSONRPCMessage) -> Unit = when (_onMessageInitialized.isCompleted) {
|
||||
true -> _onMessage
|
||||
false -> { _ -> }
|
||||
}
|
||||
|
||||
_onMessage = { message ->
|
||||
old(message)
|
||||
block(message)
|
||||
}
|
||||
|
||||
_onMessageInitialized.complete(Unit)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package io.modelcontextprotocol.kotlin.sdk.types
|
||||
|
||||
import io.modelcontextprotocol.kotlin.sdk.types.Icon.Theme.Dark
|
||||
import io.modelcontextprotocol.kotlin.sdk.types.Icon.Theme.Light
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
|
|
@ -27,6 +29,14 @@ public val SUPPORTED_PROTOCOL_VERSIONS: List<String> = listOf(
|
|||
public sealed interface WithMeta {
|
||||
@SerialName("_meta")
|
||||
public val meta: JsonObject?
|
||||
|
||||
@Deprecated(
|
||||
message = "Use 'meta' instead.",
|
||||
replaceWith = ReplaceWith("meta"),
|
||||
)
|
||||
@Suppress("PropertyName", "VariableNaming")
|
||||
public val _meta: JsonObject
|
||||
get() = meta ?: EmptyJsonObject
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
|
|
|
|||
|
|
@ -113,6 +113,22 @@ public data class GetPromptRequest(override val params: GetPromptRequestParams)
|
|||
@EncodeDefault
|
||||
override val method: Method = Method.Defined.PromptsGet
|
||||
|
||||
@Deprecated(
|
||||
message = "Use constructor with GetPromptRequestParams instead",
|
||||
replaceWith = ReplaceWith("GetPromptRequest(GetPromptRequestParams(name, arguments, meta))"),
|
||||
)
|
||||
public constructor(
|
||||
name: String,
|
||||
arguments: Map<String, String>? = null,
|
||||
meta: RequestMeta? = null,
|
||||
) : this(
|
||||
GetPromptRequestParams(
|
||||
name = name,
|
||||
arguments = arguments,
|
||||
meta = meta,
|
||||
),
|
||||
)
|
||||
|
||||
/**
|
||||
* The name of the prompt or prompt template to retrieve.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Reference in a new issue