Adds 'instructions' string to the Server and propagates to the InitializeResult (#290)

<!-- Provide a brief summary of your changes -->

## Motivation and Context
The
[schema](cca15204f6/schema/2025-03-26/schema.ts (L192))
for the InitializeResult contains an optional 'instructions' string

## Additional context
The instructions string is included in the schema for for version
"2025-03-26"

The instructions in the Server class are given as a provider function
with the result of the provider propagated to the underlying
ServerSession. As the ServerSession change is meant to allow for a
'Singleton scoped' server class, this provides the ability to
dynamically modify the instructions at the server level but have them be
immutable per session.
 
This also refactors the `serverInfo` and `instructions` properties to be
protected. This `Server` class is open and meant to be overridden, there
is no harm in having these immutable properties available to subclasses.

This adds the serverInstructions to the client as returned from the
InitializeResult. This is used to verify the response in the Server
tests.
This commit is contained in:
Jeff Clyne 2025-10-03 11:12:37 -05:00 • committed by GitHub
commit 2f1b659efd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -19,6 +19,7 @@ import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.runTest
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertNull
import org.junit.jupiter.api.assertThrows
import kotlin.test.assertEquals
import kotlin.test.assertFalse
@ -471,4 +472,59 @@ class ServerTest {
}
assertEquals("Server does not support resources capability.", exception.message)
}
@Test
fun `Server constructor should accept instructions provider parameter`() = runTest {
val serverInfo = Implementation(name = "test server", version = "1.0")
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
val instructions = "This is a test server. Use it for testing purposes only."
val server = Server(serverInfo, serverOptions, { instructions })
// The instructions should be stored internally and used in handleInitialize
// We can't directly access the private field, but we can test it through initialization
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
server.connect(serverTransport)
client.connect(clientTransport)
assertEquals(instructions, client.serverInstructions)
}
@Test
fun `Server constructor should accept instructions parameter`() = runTest {
val serverInfo = Implementation(name = "test server", version = "1.0")
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
val instructions = "This is a test server. Use it for testing purposes only."
val server = Server(serverInfo, serverOptions, instructions)
// The instructions should be stored internally and used in handleInitialize
// We can't directly access the private field, but we can test it through initialization
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
server.connect(serverTransport)
client.connect(clientTransport)
assertEquals(instructions, client.serverInstructions)
}
@Test
fun `Server constructor should work without instructions parameter`() = runTest {
val serverInfo = Implementation(name = "test server", version = "1.0")
val serverOptions = ServerOptions(capabilities = ServerCapabilities())
// Test that server works when instructions parameter is omitted (defaults to null)
val server = Server(serverInfo, serverOptions)
val (clientTransport, serverTransport) = InMemoryTransport.createLinkedPair()
val client = Client(clientInfo = Implementation(name = "test client", version = "1.0"))
server.connect(serverTransport)
client.connect(clientTransport)
assertNull(client.serverInstructions)
}
}