Skip to content

Commit 8600054

Browse files
committed
Adds alternative constructor that takes a static instructions string
1 parent 99555d2 commit 8600054

File tree

2 files changed

+33
-1
lines changed
  • kotlin-sdk-server/src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server
  • kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server

2 files changed

+33
-1
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ public open class Server(
6262
protected val options: ServerOptions,
6363
protected val instructionsProvider: (() -> String)? = null,
6464
) {
65+
/**
66+
* Alternative constructor that provides the instructions directly as a string.
67+
*
68+
* @param serverInfo Information about this server implementation (name, version).
69+
* @param options Configuration options for the server.
70+
* @param instructions Instructions from the server to the client about how to use this server.
71+
*/
72+
public constructor(
73+
serverInfo: Implementation,
74+
options: ServerOptions,
75+
instructions: String,
76+
) : this(serverInfo, options, { instructions })
77+
6578
private val sessions = atomic(persistentListOf<ServerSession>())
6679

6780
@Suppress("ktlint:standard:backing-property-naming")

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,13 +473,32 @@ class ServerTest {
473473
assertEquals("Server does not support resources capability.", exception.message)
474474
}
475475

476+
@Test
477+
fun `Server constructor should accept instructions provider parameter`() = runTest {
478+
val serverInfo = Implementation(name = "test server", version = "1.0")
479+
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
480+
val instructions = "This is a test server. Use it for testing purposes only."
481+
482+
val server = Server(serverInfo, serverOptions, { instructions })
483+
484+
// The instructions should be stored internally and used in handleInitialize
485+
// We can't directly access the private field, but we can test it through initialization
486+
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
487+
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
488+
489+
server.connect(serverTransport)
490+
client.connect(clientTransport)
491+
492+
assertEquals(instructions, client.serverInstructions)
493+
}
494+
476495
@Test
477496
fun `Server constructor should accept instructions parameter`() = runTest {
478497
val serverInfo = Implementation(name = "test server", version = "1.0")
479498
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
480499
val instructions = "This is a test server. Use it for testing purposes only."
481500

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

484503
// The instructions should be stored internally and used in handleInitialize
485504
// We can't directly access the private field, but we can test it through initialization

0 commit comments

Comments
 (0)