-
Notifications
You must be signed in to change notification settings - Fork 176
Add server session id #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tiginamaria
wants to merge
1
commit into
main
Choose a base branch
from
tigina/session-id
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+191
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,12 @@ package io.modelcontextprotocol.kotlin.sdk.server | |
| import io.github.oshai.kotlinlogging.KotlinLogging | ||
| import io.modelcontextprotocol.kotlin.sdk.CallToolRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.CallToolResult | ||
| import io.modelcontextprotocol.kotlin.sdk.CreateElicitationRequest.RequestedSchema | ||
| import io.modelcontextprotocol.kotlin.sdk.CreateElicitationResult | ||
| import io.modelcontextprotocol.kotlin.sdk.CreateMessageRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.CreateMessageResult | ||
| import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject | ||
| import io.modelcontextprotocol.kotlin.sdk.EmptyRequestResult | ||
| import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.GetPromptResult | ||
| import io.modelcontextprotocol.kotlin.sdk.Implementation | ||
|
|
@@ -13,23 +18,27 @@ import io.modelcontextprotocol.kotlin.sdk.ListResourceTemplatesRequest | |
| import io.modelcontextprotocol.kotlin.sdk.ListResourceTemplatesResult | ||
| import io.modelcontextprotocol.kotlin.sdk.ListResourcesRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.ListResourcesResult | ||
| import io.modelcontextprotocol.kotlin.sdk.ListRootsResult | ||
| import io.modelcontextprotocol.kotlin.sdk.ListToolsRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.ListToolsResult | ||
| import io.modelcontextprotocol.kotlin.sdk.LoggingMessageNotification | ||
| import io.modelcontextprotocol.kotlin.sdk.Method | ||
| import io.modelcontextprotocol.kotlin.sdk.Prompt | ||
| import io.modelcontextprotocol.kotlin.sdk.PromptArgument | ||
| import io.modelcontextprotocol.kotlin.sdk.ReadResourceRequest | ||
| import io.modelcontextprotocol.kotlin.sdk.ReadResourceResult | ||
| import io.modelcontextprotocol.kotlin.sdk.Resource | ||
| import io.modelcontextprotocol.kotlin.sdk.ResourceUpdatedNotification | ||
| import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities | ||
| import io.modelcontextprotocol.kotlin.sdk.TextContent | ||
| import io.modelcontextprotocol.kotlin.sdk.Tool | ||
| import io.modelcontextprotocol.kotlin.sdk.ToolAnnotations | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.ProtocolOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.RequestOptions | ||
| import io.modelcontextprotocol.kotlin.sdk.shared.Transport | ||
| import kotlinx.atomicfu.atomic | ||
| import kotlinx.atomicfu.update | ||
| import kotlinx.collections.immutable.persistentListOf | ||
| import kotlinx.collections.immutable.persistentMapOf | ||
| import kotlinx.coroutines.CancellationException | ||
| import kotlinx.serialization.json.JsonObject | ||
|
|
||
|
|
@@ -45,7 +54,7 @@ public class ServerOptions(public val capabilities: ServerCapabilities, enforceS | |
| ProtocolOptions(enforceStrictCapabilities = enforceStrictCapabilities) | ||
|
|
||
| /** | ||
| * An MCP server on top of a pluggable transport. | ||
| * An MCP server is responsible for storing features and handling new connections. | ||
| * | ||
| * This server automatically responds to the initialization flow as initiated by the client. | ||
| * You can register tools, prompts, and resources using [addTool], [addPrompt], and [addResource]. | ||
|
|
@@ -79,7 +88,24 @@ public open class Server( | |
| block: Server.() -> Unit = {}, | ||
| ) : this(serverInfo, options, { instructions }, block) | ||
|
|
||
| private val sessions = atomic(persistentListOf<ServerSession>()) | ||
| private val sessionRegistry = atomic(persistentMapOf<String, ServerSession>()) | ||
|
|
||
| /** | ||
| * Returns a read-only view of the current server sessions. | ||
| */ | ||
| public val sessions: Map<String, ServerSession> | ||
| get() = sessionRegistry.value | ||
|
|
||
| /** | ||
| * Gets a server session by its ID. | ||
| */ | ||
| public fun getSession(sessionId: String): ServerSession? = sessions[sessionId] | ||
|
|
||
| /** | ||
| * Gets a server session by its ID or throws an exception if the session doesn't exist. | ||
| */ | ||
| public fun getSessionOrThrow(sessionId: String): ServerSession = | ||
| sessions[sessionId] ?: throw IllegalArgumentException("Session not found: $sessionId") | ||
|
|
||
| @Suppress("ktlint:standard:backing-property-naming") | ||
| private var _onInitialized: (() -> Unit) = {} | ||
|
|
@@ -107,7 +133,10 @@ public open class Server( | |
|
|
||
| public suspend fun close() { | ||
| logger.debug { "Closing MCP server" } | ||
| sessions.value.forEach { session -> session.close() } | ||
| sessions.forEach { (sessionId, session) -> | ||
| logger.info { "Closing session $sessionId" } | ||
| session.close() | ||
| } | ||
| _onClose() | ||
| } | ||
|
|
||
|
|
@@ -171,12 +200,12 @@ public open class Server( | |
| // Register cleanup handler to remove session from list when it closes | ||
| session.onClose { | ||
| logger.debug { "Removing closed session from active sessions list" } | ||
| sessions.update { list -> list.remove(session) } | ||
| sessionRegistry.update { sessions -> sessions.remove(session.sessionId) } | ||
| } | ||
| logger.debug { "Server session connecting to transport" } | ||
| session.connect(transport) | ||
| logger.debug { "Server session successfully connected to transport" } | ||
| sessions.update { sessions -> sessions.add(session) } | ||
| sessionRegistry.update { sessions -> sessions.put(session.sessionId, session) } | ||
|
|
||
| _onConnect() | ||
| return session | ||
|
|
@@ -535,4 +564,124 @@ public open class Server( | |
| // If you have resource templates, return them here. For now, return empty. | ||
| return ListResourceTemplatesResult(listOf()) | ||
| } | ||
|
|
||
| // Start the ServerSession redirection section | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.ping] request for session by provided [sessionId]. | ||
| * @param sessionId The session ID to ping | ||
| */ | ||
| public suspend fun ping(sessionId: String): EmptyRequestResult { | ||
| val session = getSessionOrThrow(sessionId) | ||
| return session.ping() | ||
|
Comment on lines
+575
to
+576
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a pattern! It makes sense to make a method like |
||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.createMessage] request for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to create a message. | ||
| * @param params The parameters for creating a message. | ||
| * @param options Optional request options. | ||
| * @return The created message result. | ||
| * @throws IllegalStateException If the server does not support sampling or if the request fails. | ||
| */ | ||
| public suspend fun createMessage( | ||
| sessionId: String, | ||
| params: CreateMessageRequest, | ||
| options: RequestOptions? = null, | ||
| ): CreateMessageResult { | ||
| val session = getSessionOrThrow(sessionId) | ||
| return session.request(params, options) | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.listRoots] request for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to list roots for. | ||
| * @param params JSON parameters for the request, usually empty. | ||
| * @param options Optional request options. | ||
| * @return The list of roots. | ||
| * @throws IllegalStateException If the server or client does not support roots. | ||
| */ | ||
| public suspend fun listRoots( | ||
| sessionId: String, | ||
| params: JsonObject = EmptyJsonObject, | ||
| options: RequestOptions? = null, | ||
| ): ListRootsResult { | ||
| val session = getSessionOrThrow(sessionId) | ||
| return session.listRoots(params, options) | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.createElicitation] request for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to create elicitation for. | ||
| * @param message The elicitation message. | ||
| * @param requestedSchema The requested schema for the elicitation. | ||
| * @param options Optional request options. | ||
| * @return The created elicitation result. | ||
| * @throws IllegalStateException If the server does not support elicitation or if the request fails. | ||
| */ | ||
| public suspend fun createElicitation( | ||
| sessionId: String, | ||
| message: String, | ||
| requestedSchema: RequestedSchema, | ||
| options: RequestOptions? = null, | ||
| ): CreateElicitationResult { | ||
| val session = getSessionOrThrow(sessionId) | ||
| return session.createElicitation(message, requestedSchema, options) | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.sendLoggingMessage] for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to send the logging message to. | ||
| * @param notification The logging message notification. | ||
| */ | ||
| public suspend fun sendLoggingMessage(sessionId: String, notification: LoggingMessageNotification) { | ||
| val session = getSessionOrThrow(sessionId) | ||
| session.sendLoggingMessage(notification) | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.sendResourceUpdated] for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to send the resource updated notification to. | ||
| * @param notification Details of the updated resource. | ||
| */ | ||
| public suspend fun sendResourceUpdated(sessionId: String, notification: ResourceUpdatedNotification) { | ||
| val session = getSessionOrThrow(sessionId) | ||
| session.sendResourceUpdated(notification) | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.sendResourceListChanged] for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to send the resource list changed notification to. | ||
| */ | ||
| public suspend fun sendResourceListChanged(sessionId: String) { | ||
| val session = getSessionOrThrow(sessionId) | ||
| session.sendResourceListChanged() | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.sendToolListChanged] for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to send the tool list changed notification to. | ||
| */ | ||
| public suspend fun sendToolListChanged(sessionId: String) { | ||
| val session = getSessionOrThrow(sessionId) | ||
| session.sendToolListChanged() | ||
| } | ||
|
|
||
| /** | ||
| * Triggers [ServerSession.sendPromptListChanged] for session by provided [sessionId]. | ||
| * | ||
| * @param sessionId The session ID to send the prompt list changed notification to. | ||
| */ | ||
| public suspend fun sendPromptListChanged(sessionId: String) { | ||
| val session = getSessionOrThrow(sessionId) | ||
| session.sendPromptListChanged() | ||
| } | ||
| // End the ServerSession redirection section | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's create a class for a session registry with a Type alias for the sessionId type. To better comply with the Single Responsibility Principle and potentially implement atomic operations on the session