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