Skip to content

Commit 3e682ec

Browse files
committed
Modifies top level Server instructions to be a provider so it can be sourced dynamically for each session if desired
1 parent 74aeaff commit 3e682ec

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

kotlin-sdk-server/api/kotlin-sdk-server.api

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ public final class io/modelcontextprotocol/kotlin/sdk/server/RegisteredTool {
4545
}
4646

4747
public class io/modelcontextprotocol/kotlin/sdk/server/Server {
48-
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Ljava/lang/String;)V
49-
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Ljava/lang/String;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
48+
public fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;)V
49+
public synthetic fun <init> (Lio/modelcontextprotocol/kotlin/sdk/Implementation;Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;Lkotlin/jvm/functions/Function0;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
5050
public final fun addPrompt (Lio/modelcontextprotocol/kotlin/sdk/Prompt;Lkotlin/jvm/functions/Function2;)V
5151
public final fun addPrompt (Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function2;)V
5252
public static synthetic fun addPrompt$default (Lio/modelcontextprotocol/kotlin/sdk/server/Server;Ljava/lang/String;Ljava/lang/String;Ljava/util/List;Lkotlin/jvm/functions/Function2;ILjava/lang/Object;)V
@@ -60,7 +60,7 @@ public class io/modelcontextprotocol/kotlin/sdk/server/Server {
6060
public final fun addTools (Ljava/util/List;)V
6161
public final fun close (Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
6262
public final fun connect (Lio/modelcontextprotocol/kotlin/sdk/shared/Transport;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
63-
protected final fun getInstructions ()Ljava/lang/String;
63+
protected final fun getInstructionsProvider ()Lkotlin/jvm/functions/Function0;
6464
protected final fun getOptions ()Lio/modelcontextprotocol/kotlin/sdk/server/ServerOptions;
6565
public final fun getPrompts ()Ljava/util/Map;
6666
public final fun getResources ()Ljava/util/Map;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,14 @@ public class ServerOptions(public val capabilities: ServerCapabilities, enforceS
5353
*
5454
* @param serverInfo Information about this server implementation (name, version).
5555
* @param options Configuration options for the server.
56-
* @param instructions Optional instructions from the server to the client about how to use this server.
56+
* @param instructionsProvider Optional provider for instructions from the server to the client about how to use
57+
* this server. The provider is called each time a new session is started to support dynamic instructions.
5758
*/
5859

5960
public open class Server(
6061
protected val serverInfo: Implementation,
6162
protected val options: ServerOptions,
62-
protected val instructions: String? = null,
63+
protected val instructionsProvider: (() -> String)? = null,
6364
) {
6465
private val sessions = atomic(persistentListOf<ServerSession>())
6566

@@ -96,7 +97,7 @@ public open class Server(
9697
* @return The initialized and connected server session.
9798
*/
9899
public suspend fun connect(transport: Transport): ServerSession {
99-
val session = ServerSession(serverInfo, options, instructions)
100+
val session = ServerSession(serverInfo, options, instructionsProvider?.invoke())
100101

101102
// Internal handlers for tools
102103
if (options.capabilities.tools != null) {

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class ServerTest {
479479
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
480480
val instructions = "This is a test server. Use it for testing purposes only."
481481

482-
val server = Server(serverInfo, serverOptions, instructions)
482+
val server = Server(serverInfo, serverOptions) { instructions }
483483

484484
// The instructions should be stored internally and used in handleInitialize
485485
// We can't directly access the private field, but we can test it through initialization
@@ -492,7 +492,6 @@ class ServerTest {
492492
assertEquals(instructions, client.serverInstructions)
493493
}
494494

495-
496495
@Test
497496
fun `Server constructor should work without instructions parameter`() = runTest {
498497
val serverInfo = Implementation(name = "test server", version = "1.0")

0 commit comments

Comments
 (0)