|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.server |
| 2 | + |
| 3 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 4 | +import io.modelcontextprotocol.kotlin.sdk.types.Notification |
| 5 | +import io.modelcontextprotocol.kotlin.sdk.types.PromptListChangedNotification |
| 6 | +import io.modelcontextprotocol.kotlin.sdk.types.ResourceListChangedNotification |
| 7 | +import io.modelcontextprotocol.kotlin.sdk.types.ResourceUpdatedNotification |
| 8 | +import io.modelcontextprotocol.kotlin.sdk.types.ResourceUpdatedNotificationParams |
| 9 | +import io.modelcontextprotocol.kotlin.sdk.types.ToolListChangedNotification |
| 10 | +import kotlinx.coroutines.CoroutineScope |
| 11 | +import kotlinx.coroutines.Dispatchers |
| 12 | +import kotlinx.coroutines.Job |
| 13 | +import kotlinx.coroutines.SupervisorJob |
| 14 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 15 | +import kotlinx.coroutines.launch |
| 16 | + |
| 17 | +internal class FeatureNotificationService { |
| 18 | + private val notifications = MutableSharedFlow<Notification>() |
| 19 | + private val notificationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) |
| 20 | + |
| 21 | + private val notificationSessionFeatureJobs: MutableMap<ServerSessionKey, Job> = mutableMapOf() |
| 22 | + private val notificationSessionResourceJobs: MutableMap<Pair<ServerSessionKey, FeatureKey>, Job> = mutableMapOf() |
| 23 | + |
| 24 | + private val logger = KotlinLogging.logger {} |
| 25 | + |
| 26 | + private val toolFeatureListener: FeatureListener by lazy { |
| 27 | + object : FeatureListener { |
| 28 | + override fun onListChanged() { |
| 29 | + logger.debug { "Emitting tool list changed notification" } |
| 30 | + emit(ToolListChangedNotification()) |
| 31 | + } |
| 32 | + |
| 33 | + override fun onFeatureUpdated(featureKey: FeatureKey) { |
| 34 | + logger.debug { "Skipping update for tool feature key: $featureKey" } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + private val promptFeatureListener: FeatureListener by lazy { |
| 40 | + object : FeatureListener { |
| 41 | + override fun onListChanged() { |
| 42 | + logger.debug { "Emitting prompt list changed notification" } |
| 43 | + emit(PromptListChangedNotification()) |
| 44 | + } |
| 45 | + |
| 46 | + override fun onFeatureUpdated(featureKey: FeatureKey) { |
| 47 | + logger.debug { "Skipping update for prompt feature key: $featureKey" } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private val resourceFeatureListener: FeatureListener by lazy { |
| 53 | + object : FeatureListener { |
| 54 | + override fun onListChanged() { |
| 55 | + logger.debug { "Emitting resource list changed notification" } |
| 56 | + emit(ResourceListChangedNotification()) |
| 57 | + } |
| 58 | + |
| 59 | + override fun onFeatureUpdated(featureKey: FeatureKey) { |
| 60 | + logger.debug { "Emitting resource updated notification for feature key: $featureKey" } |
| 61 | + emit(ResourceUpdatedNotification(ResourceUpdatedNotificationParams(uri = featureKey))) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + internal fun getToolFeatureListener(): FeatureListener = toolFeatureListener |
| 67 | + internal fun getPromptFeatureListener(): FeatureListener = promptFeatureListener |
| 68 | + internal fun geResourceFeatureListener(): FeatureListener = resourceFeatureListener |
| 69 | + |
| 70 | + internal fun subscribeToListChangedNotification(session: ServerSession) { |
| 71 | + logger.debug { "Subscribing to list changed notifications for sessionId: ${session.sessionId}" } |
| 72 | + notificationSessionFeatureJobs[session.sessionId] = notificationScope.launch { |
| 73 | + notifications.collect { notification -> |
| 74 | + when (notification) { |
| 75 | + is PromptListChangedNotification -> session.notification(notification) |
| 76 | + is ResourceListChangedNotification -> session.notification(notification) |
| 77 | + is ToolListChangedNotification -> session.notification(notification) |
| 78 | + else -> logger.debug { |
| 79 | + "Notification not handled for sessionId ${session.sessionId}: $notification" |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + logger.debug { "Subscribed to list changed notifications for sessionId: ${session.sessionId}" } |
| 85 | + } |
| 86 | + |
| 87 | + internal fun unsubscribeFromListChangedNotification(session: ServerSession) { |
| 88 | + logger.debug { "Unsubscribing from list changed notifications for sessionId: ${session.sessionId}" } |
| 89 | + notificationSessionFeatureJobs[session.sessionId]?.cancel() |
| 90 | + notificationSessionFeatureJobs.remove(session.sessionId) |
| 91 | + logger.debug { "Unsubscribed from list changed notifications for sessionId: ${session.sessionId}" } |
| 92 | + } |
| 93 | + |
| 94 | + internal fun subscribeToResourceUpdateNotifications(session: ServerSession, resourceKey: FeatureKey) { |
| 95 | + logger.debug { "Subscribing to resource update notifications for sessionId: ${session.sessionId}" } |
| 96 | + notificationSessionResourceJobs[session.sessionId to resourceKey] = notificationScope.launch { |
| 97 | + notifications.collect { notification -> |
| 98 | + when (notification) { |
| 99 | + is ResourceUpdatedNotification -> { |
| 100 | + if (notification.params.uri == resourceKey) { |
| 101 | + session.notification(notification) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + else -> logger.debug { |
| 106 | + "Notification not handled for session for sessionId ${session.sessionId}: $notification" |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + logger.debug { "Subscribed to resource update notifications for sessionId: ${session.sessionId}" } |
| 112 | + } |
| 113 | + |
| 114 | + internal fun unsubscribeFromResourceUpdateNotifications(session: ServerSession, resourceKey: FeatureKey) { |
| 115 | + logger.debug { "Unsubscribing from resourcec update notifications for sessionId: ${session.sessionId}" } |
| 116 | + notificationSessionResourceJobs[session.sessionId to resourceKey]?.cancel() |
| 117 | + notificationSessionResourceJobs.remove(session.sessionId to resourceKey) |
| 118 | + logger.debug { "Unsubscribed from resourcec update notifications for sessionId: ${session.sessionId}" } |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | + private fun emit(notification: Notification) { |
| 123 | + notificationScope.launch { |
| 124 | + notifications.emit(notification) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments