Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import io.modelcontextprotocol.kotlin.sdk.Implementation
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
import io.modelcontextprotocol.kotlin.sdk.RequestId
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.TimeoutCancellationException
Expand Down Expand Up @@ -51,7 +50,7 @@ class StreamableHttpClientTransportTest {
@Test
fun testSendJsonRpcMessage() = runTest {
val message = JSONRPCRequest(
id = RequestId.StringId("test-id"),
id = "test-id",
method = "test",
params = buildJsonObject { },
)
Expand Down Expand Up @@ -79,7 +78,7 @@ class StreamableHttpClientTransportTest {
@Test
fun testStoreSessionId() = runTest {
val initMessage = JSONRPCRequest(
id = RequestId.StringId("test-id"),
id = "test-id",
method = "initialize",
params = buildJsonObject {
put(
Expand Down
25 changes: 25 additions & 0 deletions kotlin-sdk-core/api/kotlin-sdk-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -1143,8 +1143,12 @@ public final class io/modelcontextprotocol/kotlin/sdk/JSONRPCNotification$Compan

public final class io/modelcontextprotocol/kotlin/sdk/JSONRPCRequest : io/modelcontextprotocol/kotlin/sdk/JSONRPCMessage {
public static final field Companion Lio/modelcontextprotocol/kotlin/sdk/JSONRPCRequest$Companion;
public fun <init> (JLjava/lang/String;Lkotlinx/serialization/json/JsonElement;)V
public synthetic fun <init> (JLjava/lang/String;Lkotlinx/serialization/json/JsonElement;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/RequestId;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;Ljava/lang/String;)V
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/RequestId;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)V
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun component1 ()Lio/modelcontextprotocol/kotlin/sdk/RequestId;
public final fun component2 ()Ljava/lang/String;
public final fun component3 ()Lkotlinx/serialization/json/JsonElement;
Expand Down Expand Up @@ -3364,3 +3368,24 @@ public final class io/modelcontextprotocol/kotlin/sdk/shared/WebSocketMcpTranspo
public static final field MCP_SUBPROTOCOL Ljava/lang/String;
}

public class io/modelcontextprotocol/kotlin/sdk/testing/MockTransport : io/modelcontextprotocol/kotlin/sdk/shared/Transport {
public fun <init> ()V
public fun <init> (Lkotlin/jvm/functions/Function1;)V
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun awaitMessage-ePrTys8 (JJLjava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun awaitMessage-ePrTys8$default (Lio/modelcontextprotocol/kotlin/sdk/testing/MockTransport;JJLjava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getReceivedMessages (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun getSentMessages (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public fun onClose (Lkotlin/jvm/functions/Function0;)V
public fun onError (Lkotlin/jvm/functions/Function1;)V
public fun onMessage (Lkotlin/jvm/functions/Function2;)V
public final fun onMessageReply (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)V
public final fun onMessageReplyError (Lio/modelcontextprotocol/kotlin/sdk/Method;Lkotlin/jvm/functions/Function1;)V
public static synthetic fun onMessageReplyError$default (Lio/modelcontextprotocol/kotlin/sdk/testing/MockTransport;Lio/modelcontextprotocol/kotlin/sdk/Method;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
public final fun onMessageReplyResult (Lio/modelcontextprotocol/kotlin/sdk/Method;Lkotlin/jvm/functions/Function1;)V
public fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun setupInitializationResponse ()V
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
}
Comment on lines +3371 to +3390
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this came through as a side effect of other changes


Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import kotlin.concurrent.atomics.incrementAndFetch
import kotlin.jvm.JvmInline
import kotlin.time.ExperimentalTime
import kotlin.time.Instant
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

public const val LATEST_PROTOCOL_VERSION: String = "2025-03-26"

Expand Down Expand Up @@ -232,7 +234,36 @@ public data class JSONRPCRequest(
val method: String,
val params: JsonElement = EmptyJsonObject,
val jsonrpc: String = JSONRPC_VERSION,
) : JSONRPCMessage
) : JSONRPCMessage {

/**
* Creates a JSON-RPC request with a randomly generated string ID (using UUID) and the given method and parameters.
*
* @param id The string ID for the request. If not provided, a random UUID string is generated.
* @param method The method name for the request.
* @param params The parameters for the request as a JSON element. If not provided, an empty JSON object is used.
*/
@OptIn(ExperimentalUuidApi::class)
public constructor(
id: String = Uuid.random().toHexString(),
method: String,
params: JsonElement = EmptyJsonObject,
) : this(id = RequestId.StringId(id), method = method, params = params)

/**
* Creates a JSON-RPC request with a numeric ID (long) generated by a counter (to ensure uniqueness)
* and the given method and parameters.
*
* The `id` parameter is optional and defaults to a new unique long value generated
* by `REQUEST_MESSAGE_ID.incrementAndFetch()`.
* The `params` parameter is optional and defaults to an empty JSON object.
*/
public constructor(
id: Long = REQUEST_MESSAGE_ID.incrementAndFetch(),
method: String,
params: JsonElement = EmptyJsonObject,
) : this(id = RequestId.NumberId(id), method = method, params = params)
}

/**
* A notification which does not expect a response.
Expand Down Expand Up @@ -302,6 +333,7 @@ public data class JSONRPCError(val code: ErrorCode, val message: String, val dat
public sealed interface NotificationParams : WithMeta

/* Cancellation */

/**
* This notification can be sent by either side to indicate that it is cancelling a previously issued request.
*
Expand Down Expand Up @@ -334,6 +366,7 @@ public data class CancelledNotification(override val params: Params) :
}

/* Initialization */

/**
* Describes the name and version of an MCP implementation.
*/
Expand Down Expand Up @@ -531,6 +564,7 @@ public data class InitializedNotification(override val params: Params = Params()
}

/* Ping */

/**
* A ping, issued by either the server or the client, to check that the other party is still alive.
* The receiver must promptly respond, or else it may be disconnected.
Expand Down Expand Up @@ -564,6 +598,7 @@ public sealed interface ProgressBase {
}

/* Progress notifications */

/**
* Represents a progress notification.
*
Expand Down Expand Up @@ -623,6 +658,7 @@ public data class ProgressNotification(override val params: Params) :
}

/* Pagination */

/**
* Represents a request supporting pagination.
*/
Expand Down Expand Up @@ -650,6 +686,7 @@ public sealed interface PaginatedResult : RequestResult {
}

/* Resources */

/**
* The contents of a specific resource or sub-resource.
*/
Expand Down Expand Up @@ -892,6 +929,7 @@ public data class ResourceUpdatedNotification(override val params: Params) : Ser
}

/* Prompts */

/**
* Describes an argument that a prompt can accept.
*/
Expand Down Expand Up @@ -1162,6 +1200,7 @@ public data class PromptListChangedNotification(override val params: Params = Pa
}

/* Tools */

/**
* Additional properties describing a Tool to clients.
*
Expand Down Expand Up @@ -1340,6 +1379,7 @@ public data class ToolListChangedNotification(override val params: Params = Para
}

/* Logging */

/**
* The severity of a log message.
*/
Expand Down Expand Up @@ -1399,6 +1439,7 @@ public data class LoggingMessageNotification(override val params: Params) : Serv
}

/* Sampling */

/**
* Hints to use for model selection.
*/
Expand Down Expand Up @@ -1643,6 +1684,7 @@ public data class CompleteResult(val completion: Completion, override val _meta:
}

/* Roots */

/**
* Represents a root directory or file that the server can operate on.
*/
Expand Down
Loading