@@ -42,7 +42,7 @@ import kotlin.reflect.typeOf
42
42
import kotlin.time.Duration
43
43
import kotlin.time.Duration.Companion.milliseconds
44
44
45
- private val LOGGER = KotlinLogging .logger { }
45
+ private val logger = KotlinLogging .logger { }
46
46
47
47
public const val IMPLEMENTATION_NAME : String = " mcp-ktor"
48
48
@@ -212,6 +212,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
212
212
}
213
213
}
214
214
215
+ logger.info { " Starting transport" }
215
216
return transport.start()
216
217
}
217
218
@@ -229,29 +230,29 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
229
230
}
230
231
231
232
private suspend fun onNotification (notification : JSONRPCNotification ) {
232
- LOGGER .trace { " Received notification: ${notification.method} " }
233
+ logger .trace { " Received notification: ${notification.method} " }
233
234
234
235
val handler = notificationHandlers[notification.method] ? : fallbackNotificationHandler
235
236
236
237
if (handler == null ) {
237
- LOGGER .trace { " No handler found for notification: ${notification.method} " }
238
+ logger .trace { " No handler found for notification: ${notification.method} " }
238
239
return
239
240
}
240
241
try {
241
242
handler(notification)
242
243
} catch (cause: Throwable ) {
243
- LOGGER .error(cause) { " Error handling notification: ${notification.method} " }
244
+ logger .error(cause) { " Error handling notification: ${notification.method} " }
244
245
onError(cause)
245
246
}
246
247
}
247
248
248
249
private suspend fun onRequest (request : JSONRPCRequest ) {
249
- LOGGER .trace { " Received request: ${request.method} (id: ${request.id} )" }
250
+ logger .trace { " Received request: ${request.method} (id: ${request.id} )" }
250
251
251
252
val handler = requestHandlers[request.method] ? : fallbackRequestHandler
252
253
253
254
if (handler == = null ) {
254
- LOGGER .trace { " No handler found for request: ${request.method} " }
255
+ logger .trace { " No handler found for request: ${request.method} " }
255
256
try {
256
257
transport?.send(
257
258
JSONRPCResponse (
@@ -263,15 +264,15 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
263
264
),
264
265
)
265
266
} catch (cause: Throwable ) {
266
- LOGGER .error(cause) { " Error sending method not found response" }
267
+ logger .error(cause) { " Error sending method not found response" }
267
268
onError(cause)
268
269
}
269
270
return
270
271
}
271
272
272
273
try {
273
274
val result = handler(request, RequestHandlerExtra ())
274
- LOGGER .trace { " Request handled successfully: ${request.method} (id: ${request.id} )" }
275
+ logger .trace { " Request handled successfully: ${request.method} (id: ${request.id} )" }
275
276
276
277
transport?.send(
277
278
JSONRPCResponse (
@@ -280,7 +281,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
280
281
),
281
282
)
282
283
} catch (cause: Throwable ) {
283
- LOGGER .error(cause) { " Error handling request: ${request.method} (id: ${request.id} )" }
284
+ logger .error(cause) { " Error handling request: ${request.method} (id: ${request.id} )" }
284
285
285
286
try {
286
287
transport?.send(
@@ -293,7 +294,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
293
294
),
294
295
)
295
296
} catch (sendError: Throwable ) {
296
- LOGGER .error(sendError) {
297
+ logger .error(sendError) {
297
298
" Failed to send error response for request: ${request.method} (id: ${request.id} )"
298
299
}
299
300
// Optionally implement fallback behavior here
@@ -302,7 +303,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
302
303
}
303
304
304
305
private fun onProgress (notification : ProgressNotification ) {
305
- LOGGER .trace {
306
+ logger .trace {
306
307
" Received progress notification: token=${notification.params.progressToken} , progress=${notification.params.progress} /${notification.params.total} "
307
308
}
308
309
val progress = notification.params.progress
@@ -315,7 +316,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
315
316
val error = Error (
316
317
" Received a progress notification for an unknown token: ${McpJson .encodeToString(notification)} " ,
317
318
)
318
- LOGGER .error { error.message }
319
+ logger .error { error.message }
319
320
onError(error)
320
321
return
321
322
}
@@ -390,9 +391,9 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
390
391
* Do not use this method to emit notifications! Use notification() instead.
391
392
*/
392
393
public suspend fun <T : RequestResult > request (request : Request , options : RequestOptions ? = null): T {
393
- LOGGER .trace { " Sending request: ${request.method} " }
394
+ logger .trace { " Sending request: ${request.method} " }
394
395
val result = CompletableDeferred <T >()
395
- val transport = this @Protocol. transport ? : throw Error (" Not connected" )
396
+ val transport = transport ? : throw Error (" Not connected" )
396
397
397
398
if (this @Protocol.options?.enforceStrictCapabilities == true ) {
398
399
assertCapabilityForMethod(request.method)
@@ -402,7 +403,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
402
403
val messageId = message.id
403
404
404
405
if (options?.onProgress != null ) {
405
- LOGGER .trace { " Registering progress handler for request id: $messageId " }
406
+ logger .trace { " Registering progress handler for request id: $messageId " }
406
407
_progressHandlers .update { current ->
407
408
current.put(messageId, options.onProgress)
408
409
}
@@ -452,12 +453,12 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
452
453
val timeout = options?.timeout ? : DEFAULT_REQUEST_TIMEOUT
453
454
try {
454
455
withTimeout(timeout) {
455
- LOGGER .trace { " Sending request message with id: $messageId " }
456
+ logger .trace { " Sending request message with id: $messageId " }
456
457
this @Protocol.transport?.send(message)
457
458
}
458
459
return result.await()
459
460
} catch (cause: TimeoutCancellationException ) {
460
- LOGGER .error { " Request timed out after ${timeout.inWholeMilliseconds} ms: ${request.method} " }
461
+ logger .error { " Request timed out after ${timeout.inWholeMilliseconds} ms: ${request.method} " }
461
462
cancel(
462
463
McpError (
463
464
ErrorCode .Defined .RequestTimeout .code,
@@ -474,7 +475,7 @@ public abstract class Protocol(@PublishedApi internal val options: ProtocolOptio
474
475
* Emits a notification, which is a one-way message that does not expect a response.
475
476
*/
476
477
public suspend fun notification (notification : Notification ) {
477
- LOGGER .trace { " Sending notification: ${notification.method} " }
478
+ logger .trace { " Sending notification: ${notification.method} " }
478
479
val transport = this .transport ? : error(" Not connected" )
479
480
assertNotificationCapability(notification.method)
480
481
0 commit comments