Skip to content

Commit 8909dee

Browse files
committed
Add convenience constructors to JSONRPCRequest
1 parent ab9ba54 commit 8909dee

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import io.modelcontextprotocol.kotlin.sdk.Implementation
1616
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage
1717
import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
1818
import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
19-
import io.modelcontextprotocol.kotlin.sdk.RequestId
2019
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
2120
import kotlinx.coroutines.Dispatchers
2221
import kotlinx.coroutines.delay
@@ -50,7 +49,7 @@ class StreamableHttpClientTransportTest {
5049
@Test
5150
fun testSendJsonRpcMessage() = runTest {
5251
val message = JSONRPCRequest(
53-
id = RequestId.StringId("test-id"),
52+
id = "test-id",
5453
method = "test",
5554
params = buildJsonObject { },
5655
)
@@ -78,7 +77,7 @@ class StreamableHttpClientTransportTest {
7877
@Test
7978
fun testStoreSessionId() = runTest {
8079
val initMessage = JSONRPCRequest(
81-
id = RequestId.StringId("test-id"),
80+
id = "test-id",
8281
method = "initialize",
8382
params = buildJsonObject {
8483
put(

kotlin-sdk-core/api/kotlin-sdk-core.api

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,8 +1143,12 @@ public final class io/modelcontextprotocol/kotlin/sdk/JSONRPCNotification$Compan
11431143

11441144
public final class io/modelcontextprotocol/kotlin/sdk/JSONRPCRequest : io/modelcontextprotocol/kotlin/sdk/JSONRPCMessage {
11451145
public static final field Companion Lio/modelcontextprotocol/kotlin/sdk/JSONRPCRequest$Companion;
1146+
public fun <init> (JLjava/lang/String;Lkotlinx/serialization/json/JsonElement;)V
1147+
public synthetic fun <init> (JLjava/lang/String;Lkotlinx/serialization/json/JsonElement;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
11461148
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/RequestId;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;Ljava/lang/String;)V
11471149
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/RequestId;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
1150+
public fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;)V
1151+
public synthetic fun <init> (Ljava/lang/String;Ljava/lang/String;Lkotlinx/serialization/json/JsonElement;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
11481152
public final fun component1 ()Lio/modelcontextprotocol/kotlin/sdk/RequestId;
11491153
public final fun component2 ()Ljava/lang/String;
11501154
public final fun component3 ()Lkotlinx/serialization/json/JsonElement;
@@ -3364,3 +3368,24 @@ public final class io/modelcontextprotocol/kotlin/sdk/shared/WebSocketMcpTranspo
33643368
public static final field MCP_SUBPROTOCOL Ljava/lang/String;
33653369
}
33663370

3371+
public class io/modelcontextprotocol/kotlin/sdk/testing/MockTransport : io/modelcontextprotocol/kotlin/sdk/shared/Transport {
3372+
public fun <init> ()V
3373+
public fun <init> (Lkotlin/jvm/functions/Function1;)V
3374+
public synthetic fun <init> (Lkotlin/jvm/functions/Function1;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
3375+
public final fun awaitMessage-ePrTys8 (JJLjava/lang/String;Lkotlin/jvm/functions/Function1;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3376+
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;
3377+
public fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3378+
public final fun getReceivedMessages (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3379+
public final fun getSentMessages (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3380+
public fun onClose (Lkotlin/jvm/functions/Function0;)V
3381+
public fun onError (Lkotlin/jvm/functions/Function1;)V
3382+
public fun onMessage (Lkotlin/jvm/functions/Function2;)V
3383+
public final fun onMessageReply (Lkotlin/jvm/functions/Function1;Lkotlin/jvm/functions/Function2;)V
3384+
public final fun onMessageReplyError (Lio/modelcontextprotocol/kotlin/sdk/Method;Lkotlin/jvm/functions/Function1;)V
3385+
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
3386+
public final fun onMessageReplyResult (Lio/modelcontextprotocol/kotlin/sdk/Method;Lkotlin/jvm/functions/Function1;)V
3387+
public fun send (Lio/modelcontextprotocol/kotlin/sdk/JSONRPCMessage;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3388+
public final fun setupInitializationResponse ()V
3389+
public fun start (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
3390+
}
3391+

kotlin-sdk-core/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/types.kt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import kotlin.concurrent.atomics.incrementAndFetch
1919
import kotlin.jvm.JvmInline
2020
import kotlin.time.ExperimentalTime
2121
import kotlin.time.Instant
22+
import kotlin.uuid.ExperimentalUuidApi
23+
import kotlin.uuid.Uuid
2224

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

@@ -232,7 +234,36 @@ public data class JSONRPCRequest(
232234
val method: String,
233235
val params: JsonElement = EmptyJsonObject,
234236
val jsonrpc: String = JSONRPC_VERSION,
235-
) : JSONRPCMessage
237+
) : JSONRPCMessage {
238+
239+
/**
240+
* Creates a JSON-RPC request with a randomly generated string ID (using UUID) and the given method and parameters.
241+
*
242+
* @param id The string ID for the request. If not provided, a random UUID string is generated.
243+
* @param method The method name for the request.
244+
* @param params The parameters for the request as a JSON element. If not provided, an empty JSON object is used.
245+
*/
246+
@OptIn(ExperimentalUuidApi::class)
247+
public constructor(
248+
id: String = Uuid.random().toHexString(),
249+
method: String,
250+
params: JsonElement = EmptyJsonObject,
251+
) : this(id = RequestId.StringId(id), method = method, params = params)
252+
253+
/**
254+
* Creates a JSON-RPC request with a numeric ID (long) generated by a counter (to ensure uniqueness)
255+
* and the given method and parameters.
256+
*
257+
* The `id` parameter is optional and defaults to a new unique long value generated
258+
* by `REQUEST_MESSAGE_ID.incrementAndFetch()`.
259+
* The `params` parameter is optional and defaults to an empty JSON object.
260+
*/
261+
public constructor(
262+
id: Long = REQUEST_MESSAGE_ID.incrementAndFetch(),
263+
method: String,
264+
params: JsonElement = EmptyJsonObject,
265+
) : this(id = RequestId.NumberId(id), method = method, params = params)
266+
}
236267

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

304335
/* Cancellation */
336+
305337
/**
306338
* This notification can be sent by either side to indicate that it is cancelling a previously issued request.
307339
*
@@ -334,6 +366,7 @@ public data class CancelledNotification(override val params: Params) :
334366
}
335367

336368
/* Initialization */
369+
337370
/**
338371
* Describes the name and version of an MCP implementation.
339372
*/
@@ -531,6 +564,7 @@ public data class InitializedNotification(override val params: Params = Params()
531564
}
532565

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

566600
/* Progress notifications */
601+
567602
/**
568603
* Represents a progress notification.
569604
*
@@ -623,6 +658,7 @@ public data class ProgressNotification(override val params: Params) :
623658
}
624659

625660
/* Pagination */
661+
626662
/**
627663
* Represents a request supporting pagination.
628664
*/
@@ -650,6 +686,7 @@ public sealed interface PaginatedResult : RequestResult {
650686
}
651687

652688
/* Resources */
689+
653690
/**
654691
* The contents of a specific resource or sub-resource.
655692
*/
@@ -892,6 +929,7 @@ public data class ResourceUpdatedNotification(override val params: Params) : Ser
892929
}
893930

894931
/* Prompts */
932+
895933
/**
896934
* Describes an argument that a prompt can accept.
897935
*/
@@ -1162,6 +1200,7 @@ public data class PromptListChangedNotification(override val params: Params = Pa
11621200
}
11631201

11641202
/* Tools */
1203+
11651204
/**
11661205
* Additional properties describing a Tool to clients.
11671206
*
@@ -1340,6 +1379,7 @@ public data class ToolListChangedNotification(override val params: Params = Para
13401379
}
13411380

13421381
/* Logging */
1382+
13431383
/**
13441384
* The severity of a log message.
13451385
*/
@@ -1399,6 +1439,7 @@ public data class LoggingMessageNotification(override val params: Params) : Serv
13991439
}
14001440

14011441
/* Sampling */
1442+
14021443
/**
14031444
* Hints to use for model selection.
14041445
*/
@@ -1643,6 +1684,7 @@ public data class CompleteResult(val completion: Completion, override val _meta:
16431684
}
16441685

16451686
/* Roots */
1687+
16461688
/**
16471689
* Represents a root directory or file that the server can operate on.
16481690
*/

0 commit comments

Comments
 (0)