Skip to content

Commit bbb2877

Browse files
committed
Update server kdoc with notification information
1 parent 97df266 commit bbb2877

File tree

1 file changed

+35
-17
lines changed
  • kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server

1 file changed

+35
-17
lines changed

kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/Server.kt

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class ServerOptions(public val capabilities: ServerCapabilities, enforceS
6363
* You can register tools, prompts, and resources using [addTool], [addPrompt], and [addResource].
6464
* The server will then automatically handle listing and retrieval requests from the client.
6565
*
66+
* In case the server supports feature list notification or resource substitution,
67+
* the server will automatically send notifications for all connected clients.
68+
* Currently, after subscription to a resource, the server will NOT send the subscription configuration
69+
* as this response shema is not defined in the protocol.
70+
*
6671
* @param serverInfo Information about this server implementation (name, version).
6772
* @param options Configuration options for the server.
6873
* @param instructionsProvider Optional provider for instructions from the server to the client about how to use
@@ -91,17 +96,6 @@ public open class Server(
9196
block: Server.() -> Unit = {},
9297
) : this(serverInfo, options, { instructions }, block)
9398

94-
private val sessionRegistry = ServerSessionRegistry()
95-
96-
@OptIn(ExperimentalTime::class)
97-
private val notificationService = FeatureNotificationService()
98-
99-
/**
100-
* Provides a snapshot of all sessions currently registered in the server
101-
*/
102-
public val sessions: Map<ServerSessionKey, ServerSession>
103-
get() = sessionRegistry.sessions
104-
10599
@Suppress("ktlint:standard:backing-property-naming")
106100
private var _onInitialized: (() -> Unit) = {}
107101

@@ -111,6 +105,11 @@ public open class Server(
111105
@Suppress("ktlint:standard:backing-property-naming")
112106
private var _onClose: () -> Unit = {}
113107

108+
@OptIn(ExperimentalTime::class)
109+
private val notificationService = FeatureNotificationService()
110+
111+
private val sessionRegistry = ServerSessionRegistry()
112+
114113
private val toolRegistry = FeatureRegistry<RegisteredTool>("Tool").apply {
115114
if (options.capabilities.tools?.listChanged ?: false) {
116115
addListener(notificationService.toolListChangedListener)
@@ -130,10 +129,27 @@ public open class Server(
130129
}
131130
}
132131

132+
/**
133+
* Provides a snapshot of all sessions currently registered in the server
134+
*/
135+
public val sessions: Map<ServerSessionKey, ServerSession>
136+
get() = sessionRegistry.sessions
137+
138+
/**
139+
* Provides a snapshot of all tools currently registered in the server
140+
*/
133141
public val tools: Map<String, RegisteredTool>
134142
get() = toolRegistry.values
143+
144+
/**
145+
* Provides a snapshot of all prompts currently registered in the server
146+
*/
135147
public val prompts: Map<String, RegisteredPrompt>
136148
get() = promptRegistry.values
149+
150+
/**
151+
* Provides a snapshot of all resources currently registered in the server
152+
*/
137153
public val resources: Map<String, RegisteredResource>
138154
get() = resourceRegistry.values
139155

@@ -209,10 +225,12 @@ public open class Server(
209225
if (options.capabilities.resources?.subscribe ?: false) {
210226
session.setRequestHandler<SubscribeRequest>(Method.Defined.ResourcesSubscribe) { request, _ ->
211227
handleSubscribeResources(session, request)
228+
// Does not return any confirmation as the structure is not stated in the protocol
212229
null
213230
}
214231
session.setRequestHandler<UnsubscribeRequest>(Method.Defined.ResourcesUnsubscribe) { request, _ ->
215232
handleUnsubscribeResources(session, request)
233+
// Does not return any confirmation as the structure is not stated in the protocol
216234
null
217235
}
218236
}
@@ -521,7 +539,7 @@ public open class Server(
521539
}
522540

523541
// --- Internal Handlers ---
524-
private suspend fun handleSubscribeResources(session: ServerSession, request: SubscribeRequest) {
542+
private fun handleSubscribeResources(session: ServerSession, request: SubscribeRequest) {
525543
if (options.capabilities.resources?.subscribe ?: false) {
526544
logger.debug { "Subscribing to resources" }
527545
notificationService.subscribeToResourceUpdate(session, request.params.uri)
@@ -530,7 +548,7 @@ public open class Server(
530548
}
531549
}
532550

533-
private suspend fun handleUnsubscribeResources(session: ServerSession, request: UnsubscribeRequest) {
551+
private fun handleUnsubscribeResources(session: ServerSession, request: UnsubscribeRequest) {
534552
if (options.capabilities.resources?.subscribe ?: false) {
535553
logger.debug { "Unsubscribing from resources" }
536554
notificationService.unsubscribeFromResourceUpdate(session, request.params.uri)
@@ -539,7 +557,7 @@ public open class Server(
539557
}
540558
}
541559

542-
private suspend fun handleListTools(): ListToolsResult {
560+
private fun handleListTools(): ListToolsResult {
543561
val toolList = tools.values.map { it.tool }
544562
return ListToolsResult(tools = toolList, nextCursor = null)
545563
}
@@ -573,7 +591,7 @@ public open class Server(
573591
}
574592
}
575593

576-
private suspend fun handleListPrompts(): ListPromptsResult {
594+
private fun handleListPrompts(): ListPromptsResult {
577595
logger.debug { "Handling list prompts request" }
578596
return ListPromptsResult(prompts = prompts.values.map { it.prompt })
579597
}
@@ -589,7 +607,7 @@ public open class Server(
589607
return prompt.messageProvider(request)
590608
}
591609

592-
private suspend fun handleListResources(): ListResourcesResult {
610+
private fun handleListResources(): ListResourcesResult {
593611
logger.debug { "Handling list resources request" }
594612
return ListResourcesResult(resources = resources.values.map { it.resource })
595613
}
@@ -605,7 +623,7 @@ public open class Server(
605623
return resource.readHandler(request)
606624
}
607625

608-
private suspend fun handleListResourceTemplates(): ListResourceTemplatesResult {
626+
private fun handleListResourceTemplates(): ListResourceTemplatesResult {
609627
// If you have resource templates, return them here. For now, return empty.
610628
return ListResourceTemplatesResult(listOf())
611629
}

0 commit comments

Comments
 (0)