@@ -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
@@ -204,6 +204,7 @@ public abstract class Protocol(
204
204
}
205
205
}
206
206
207
+ logger.info { " Starting transport" }
207
208
return transport.start()
208
209
}
209
210
@@ -221,29 +222,29 @@ public abstract class Protocol(
221
222
}
222
223
223
224
private suspend fun onNotification (notification : JSONRPCNotification ) {
224
- LOGGER .trace { " Received notification: ${notification.method} " }
225
+ logger .trace { " Received notification: ${notification.method} " }
225
226
226
227
val handler = notificationHandlers[notification.method] ? : fallbackNotificationHandler
227
228
228
229
if (handler == null ) {
229
- LOGGER .trace { " No handler found for notification: ${notification.method} " }
230
+ logger .trace { " No handler found for notification: ${notification.method} " }
230
231
return
231
232
}
232
233
try {
233
234
handler(notification)
234
235
} catch (cause: Throwable ) {
235
- LOGGER .error(cause) { " Error handling notification: ${notification.method} " }
236
+ logger .error(cause) { " Error handling notification: ${notification.method} " }
236
237
onError(cause)
237
238
}
238
239
}
239
240
240
241
private suspend fun onRequest (request : JSONRPCRequest ) {
241
- LOGGER .trace { " Received request: ${request.method} (id: ${request.id} )" }
242
+ logger .trace { " Received request: ${request.method} (id: ${request.id} )" }
242
243
243
244
val handler = requestHandlers[request.method] ? : fallbackRequestHandler
244
245
245
246
if (handler == = null ) {
246
- LOGGER .trace { " No handler found for request: ${request.method} " }
247
+ logger .trace { " No handler found for request: ${request.method} " }
247
248
try {
248
249
transport?.send(
249
250
JSONRPCResponse (
@@ -255,15 +256,15 @@ public abstract class Protocol(
255
256
)
256
257
)
257
258
} catch (cause: Throwable ) {
258
- LOGGER .error(cause) { " Error sending method not found response" }
259
+ logger .error(cause) { " Error sending method not found response" }
259
260
onError(cause)
260
261
}
261
262
return
262
263
}
263
264
264
265
try {
265
266
val result = handler(request, RequestHandlerExtra ())
266
- LOGGER .trace { " Request handled successfully: ${request.method} (id: ${request.id} )" }
267
+ logger .trace { " Request handled successfully: ${request.method} (id: ${request.id} )" }
267
268
268
269
transport?.send(
269
270
JSONRPCResponse (
@@ -273,7 +274,7 @@ public abstract class Protocol(
273
274
)
274
275
275
276
} catch (cause: Throwable ) {
276
- LOGGER .error(cause) { " Error handling request: ${request.method} (id: ${request.id} )" }
277
+ logger .error(cause) { " Error handling request: ${request.method} (id: ${request.id} )" }
277
278
278
279
try {
279
280
transport?.send(
@@ -286,14 +287,14 @@ public abstract class Protocol(
286
287
)
287
288
)
288
289
} catch (sendError: Throwable ) {
289
- LOGGER .error(sendError) { " Failed to send error response for request: ${request.method} (id: ${request.id} )" }
290
+ logger .error(sendError) { " Failed to send error response for request: ${request.method} (id: ${request.id} )" }
290
291
// Optionally implement fallback behavior here
291
292
}
292
293
}
293
294
}
294
295
295
296
private fun onProgress (notification : ProgressNotification ) {
296
- LOGGER .trace { " Received progress notification: token=${notification.params.progressToken} , progress=${notification.params.progress} /${notification.params.total} " }
297
+ logger .trace { " Received progress notification: token=${notification.params.progressToken} , progress=${notification.params.progress} /${notification.params.total} " }
297
298
val progress = notification.params.progress
298
299
val total = notification.params.total
299
300
val message = notification.params.message
@@ -304,7 +305,7 @@ public abstract class Protocol(
304
305
val error = Error (
305
306
" Received a progress notification for an unknown token: ${McpJson .encodeToString(notification)} " ,
306
307
)
307
- LOGGER .error { error.message }
308
+ logger .error { error.message }
308
309
onError(error)
309
310
return
310
311
}
@@ -382,9 +383,9 @@ public abstract class Protocol(
382
383
request : Request ,
383
384
options : RequestOptions ? = null,
384
385
): T {
385
- LOGGER .trace { " Sending request: ${request.method} " }
386
+ logger .trace { " Sending request: ${request.method} " }
386
387
val result = CompletableDeferred <T >()
387
- val transport = this @Protocol. transport ? : throw Error (" Not connected" )
388
+ val transport = transport ? : throw Error (" Not connected" )
388
389
389
390
if (this @Protocol.options?.enforceStrictCapabilities == true ) {
390
391
assertCapabilityForMethod(request.method)
@@ -394,7 +395,7 @@ public abstract class Protocol(
394
395
val messageId = message.id
395
396
396
397
if (options?.onProgress != null ) {
397
- LOGGER .trace { " Registering progress handler for request id: $messageId " }
398
+ logger .trace { " Registering progress handler for request id: $messageId " }
398
399
_progressHandlers .update { current ->
399
400
current.put(messageId, options.onProgress)
400
401
}
@@ -427,7 +428,7 @@ public abstract class Protocol(
427
428
428
429
val notification = CancelledNotification (
429
430
params = CancelledNotification .Params (
430
- requestId = messageId,
431
+ requestId = messageId,
431
432
reason = reason.message ? : " Unknown"
432
433
)
433
434
)
@@ -444,12 +445,12 @@ public abstract class Protocol(
444
445
val timeout = options?.timeout ? : DEFAULT_REQUEST_TIMEOUT
445
446
try {
446
447
withTimeout(timeout) {
447
- LOGGER .trace { " Sending request message with id: $messageId " }
448
+ logger .trace { " Sending request message with id: $messageId " }
448
449
this @Protocol.transport?.send(message)
449
450
}
450
451
return result.await()
451
452
} catch (cause: TimeoutCancellationException ) {
452
- LOGGER .error { " Request timed out after ${timeout.inWholeMilliseconds} ms: ${request.method} " }
453
+ logger .error { " Request timed out after ${timeout.inWholeMilliseconds} ms: ${request.method} " }
453
454
cancel(
454
455
McpError (
455
456
ErrorCode .Defined .RequestTimeout .code,
@@ -466,7 +467,7 @@ public abstract class Protocol(
466
467
* Emits a notification, which is a one-way message that does not expect a response.
467
468
*/
468
469
public suspend fun notification (notification : Notification ) {
469
- LOGGER .trace { " Sending notification: ${notification.method} " }
470
+ logger .trace { " Sending notification: ${notification.method} " }
470
471
val transport = this .transport ? : error(" Not connected" )
471
472
assertNotificationCapability(notification.method)
472
473
0 commit comments