11package io.modelcontextprotocol.kotlin.sdk.shared
22
33import io.github.oshai.kotlinlogging.KotlinLogging
4- import io.modelcontextprotocol.kotlin.sdk.CancelledNotification
5- import io.modelcontextprotocol.kotlin.sdk.EmptyRequestResult
6- import io.modelcontextprotocol.kotlin.sdk.ErrorCode
7- import io.modelcontextprotocol.kotlin.sdk.JSONRPCError
8- import io.modelcontextprotocol.kotlin.sdk.JSONRPCNotification
9- import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
10- import io.modelcontextprotocol.kotlin.sdk.JSONRPCResponse
11- import io.modelcontextprotocol.kotlin.sdk.McpError
12- import io.modelcontextprotocol.kotlin.sdk.Method
13- import io.modelcontextprotocol.kotlin.sdk.Notification
14- import io.modelcontextprotocol.kotlin.sdk.PingRequest
15- import io.modelcontextprotocol.kotlin.sdk.Progress
16- import io.modelcontextprotocol.kotlin.sdk.ProgressNotification
17- import io.modelcontextprotocol.kotlin.sdk.Request
18- import io.modelcontextprotocol.kotlin.sdk.RequestId
19- import io.modelcontextprotocol.kotlin.sdk.RequestResult
20- import io.modelcontextprotocol.kotlin.sdk.fromJSON
21- import io.modelcontextprotocol.kotlin.sdk.toJSON
4+ import io.modelcontextprotocol.kotlin.sdk.*
225import kotlinx.coroutines.CompletableDeferred
236import kotlinx.coroutines.Deferred
247import kotlinx.coroutines.TimeoutCancellationException
@@ -107,14 +90,14 @@ internal val COMPLETED = CompletableDeferred(Unit).also { it.complete(Unit) }
10790 * Implements MCP protocol framing on top of a pluggable transport, including
10891 * features like request/response linking, notifications, and progress.
10992 */
110- public abstract class Protocol < SendRequestT : Request , SendNotificationT : Notification , SendResultT : RequestResult > (
93+ public abstract class Protocol (
11194 @PublishedApi internal val options : ProtocolOptions ? ,
11295) {
11396 public var transport: Transport ? = null
11497 private set
11598
11699 @PublishedApi
117- internal val requestHandlers: MutableMap <String , suspend (request: JSONRPCRequest , extra: RequestHandlerExtra ) - > SendResultT ? > =
100+ internal val requestHandlers: MutableMap <String , suspend (request: JSONRPCRequest , extra: RequestHandlerExtra ) - > RequestResult ? > =
118101 mutableMapOf ()
119102 public val notificationHandlers: MutableMap <String , suspend (notification: JSONRPCNotification ) - > Unit > =
120103 mutableMapOf ()
@@ -143,7 +126,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
143126 /* *
144127 * A handler to invoke for any request types that do not have their own handler installed.
145128 */
146- public var fallbackRequestHandler: (suspend (request: JSONRPCRequest , extra: RequestHandlerExtra ) -> SendResultT ? )? =
129+ public var fallbackRequestHandler: (suspend (request: JSONRPCRequest , extra: RequestHandlerExtra ) -> RequestResult ? )? =
147130 null
148131
149132 /* *
@@ -158,8 +141,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
158141 }
159142
160143 setRequestHandler<PingRequest >(Method .Defined .Ping ) { request, _ ->
161- @Suppress(" UNCHECKED_CAST" )
162- EmptyRequestResult () as SendResultT
144+ EmptyRequestResult ()
163145 }
164146 }
165147
@@ -344,12 +326,12 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
344326 public abstract fun assertRequestHandlerCapability (method : Method )
345327
346328 /* *
347- * Sends a request and wait for a response.
329+ * Sends a request and waits for a response.
348330 *
349331 * Do not use this method to emit notifications! Use notification() instead.
350332 */
351333 public suspend fun <T : RequestResult > request (
352- request : SendRequestT ,
334+ request : Request ,
353335 options : RequestOptions ? = null,
354336 ): T {
355337 LOGGER .trace { " Sending request: ${request.method} " }
@@ -427,7 +409,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
427409 /* *
428410 * Emits a notification, which is a one-way message that does not expect a response.
429411 */
430- public suspend fun notification (notification : SendNotificationT ) {
412+ public suspend fun notification (notification : Notification ) {
431413 LOGGER .trace { " Sending notification: ${notification.method} " }
432414 val transport = this .transport ? : error(" Not connected" )
433415 assertNotificationCapability(notification.method)
@@ -446,7 +428,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
446428 */
447429 public inline fun <reified T : Request > setRequestHandler (
448430 method : Method ,
449- noinline block : suspend (T , RequestHandlerExtra ) -> SendResultT ? ,
431+ noinline block : suspend (T , RequestHandlerExtra ) -> RequestResult ? ,
450432 ) {
451433 setRequestHandler(typeOf<T >(), method, block)
452434 }
@@ -455,7 +437,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
455437 internal fun <T : Request > setRequestHandler (
456438 requestType : KType ,
457439 method : Method ,
458- block : suspend (T , RequestHandlerExtra ) -> SendResultT ? ,
440+ block : suspend (T , RequestHandlerExtra ) -> RequestResult ? ,
459441 ) {
460442 assertRequestHandlerCapability(method)
461443
@@ -470,8 +452,7 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
470452 EmptyRequestResult ()
471453 }
472454
473- @Suppress(" UNCHECKED_CAST" )
474- response as SendResultT
455+ response
475456 }
476457 }
477458
@@ -500,4 +481,4 @@ public abstract class Protocol<SendRequestT : Request, SendNotificationT : Notif
500481 public fun removeNotificationHandler (method : Method ) {
501482 this .notificationHandlers.remove(method.value)
502483 }
503- }
484+ }
0 commit comments