11package io.modelcontextprotocol.kotlin.sdk.server
22
3+ import io.kotest.assertions.throwables.shouldThrow
4+ import io.kotest.matchers.collections.shouldBeEmpty
5+ import io.kotest.matchers.collections.shouldContain
6+ import io.kotest.matchers.collections.shouldContainExactly
7+ import io.kotest.matchers.collections.shouldHaveSize
8+ import io.kotest.matchers.nulls.shouldNotBeNull
9+ import io.kotest.matchers.shouldBe
10+ import io.kotest.matchers.throwable.shouldHaveMessage
11+ import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
12+ import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest
313import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
414import io.modelcontextprotocol.kotlin.sdk.Implementation
515import io.modelcontextprotocol.kotlin.sdk.Method
616import io.modelcontextprotocol.kotlin.sdk.Prompt
717import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
18+ import io.modelcontextprotocol.kotlin.sdk.types.McpException
819import kotlinx.coroutines.CompletableDeferred
920import kotlinx.coroutines.test.runTest
21+ import org.junit.jupiter.api.Nested
1022import org.junit.jupiter.api.Test
1123import org.junit.jupiter.api.assertThrows
1224import kotlin.test.assertEquals
@@ -21,25 +33,84 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
2133 )
2234
2335 @Test
24- fun `removePrompt should remove a prompt` () = runTest {
36+ fun `Should list no prompts by default` () = runTest {
37+ client.listPrompts() shouldNotBeNull {
38+ prompts.shouldBeEmpty()
39+ }
40+ }
41+
42+ @Test
43+ fun `Should add a prompt` () = runTest {
2544 // Add a prompt
26- val testPrompt = Prompt (" test-prompt" , " Test Prompt" , null )
45+ val testPrompt = Prompt (
46+ name = " test-prompt-with-custom-handler" ,
47+ description = " Test Prompt" ,
48+ arguments = null ,
49+ )
50+ val expectedPromptResult = GetPromptResult (
51+ description = " Test prompt description" ,
52+ messages = listOf (),
53+ )
54+
55+ server.addPrompt(testPrompt) {
56+ expectedPromptResult
57+ }
58+
59+ client.getPrompt(
60+ GetPromptRequest (
61+ name = " test-prompt-with-custom-handler" ,
62+ arguments = null ,
63+ ),
64+ ) shouldBe expectedPromptResult
65+
66+ client.listPrompts() shouldNotBeNull {
67+ prompts shouldContainExactly listOf (testPrompt)
68+ nextCursor shouldBe null
69+ _meta shouldBe EmptyJsonObject
70+ }
71+ }
72+
73+ @Test
74+ fun `Should remove a prompt` () = runTest {
75+ // given
76+ val testPrompt = Prompt (
77+ name = " test-prompt-to-remove" ,
78+ description = " Test Prompt" ,
79+ arguments = null ,
80+ )
2781 server.addPrompt(testPrompt) {
2882 GetPromptResult (
2983 description = " Test prompt description" ,
3084 messages = listOf (),
3185 )
3286 }
3387
34- // Remove the prompt
88+ client.listPrompts() shouldNotBeNull {
89+ prompts shouldContain testPrompt
90+ }
91+
92+ // when
3593 val result = server.removePrompt(testPrompt.name)
3694
37- // Verify the prompt was removed
95+ // then
3896 assertTrue(result, " Prompt should be removed successfully" )
97+ val mcpException = shouldThrow<McpException > {
98+ client.getPrompt(
99+ GetPromptRequest (
100+ name = testPrompt.name,
101+ arguments = null ,
102+ ),
103+ )
104+ }
105+ mcpException shouldHaveMessage " MCP error -32603: Prompt not found: ${testPrompt.name} "
106+
107+ client.listPrompts() shouldNotBeNull {
108+ prompts.firstOrNull { it.name == testPrompt.name } shouldBe null
109+ }
39110 }
40111
41112 @Test
42- fun `removePrompts should remove multiple prompts and send notification` () = runTest {
113+ fun `Should remove multiple prompts and send notification` () = runTest {
43114 // Add prompts
44115 val testPrompt1 = Prompt (" test-prompt-1" , " Test Prompt 1" , null )
45116 val testPrompt2 = Prompt (" test-prompt-2" , " Test Prompt 2" , null )
@@ -56,11 +127,17 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
56127 )
57128 }
58129
130+ client.listPrompts() shouldNotBeNull {
131+ prompts shouldHaveSize 2
132+ }
59133 // Remove the prompts
60134 val result = server.removePrompts(listOf (testPrompt1.name, testPrompt2.name))
61135
62136 // Verify the prompts were removed
63137 assertEquals(2 , result, " Both prompts should be removed" )
138+ client.listPrompts() shouldNotBeNull {
139+ prompts.shouldBeEmpty()
140+ }
64141 }
65142
66143 @Test
@@ -82,21 +159,55 @@ class OldSchemaServerPromptsTest : OldSchemaAbstractServerFeaturesTest() {
82159 assertFalse(promptListChangedNotificationReceived, " No notification should be sent when prompt doesn't exist" )
83160 }
84161
85- @Test
86- fun `removePrompt should throw when prompts capability is not supported` () = runTest {
162+ @Nested
163+ inner class NoPromptsCapabilitiesTests {
87164 // Create server without prompts capability
88- val serverOptions = ServerOptions (
89- capabilities = ServerCapabilities (),
90- )
91- val server = Server (
165+ val serverWithoutPrompts = Server (
92166 Implementation (name = " test server" , version = " 1.0" ),
93- serverOptions,
167+ ServerOptions (
168+ capabilities = ServerCapabilities (),
169+ ),
94170 )
95171
96- // Verify that removing a prompt throws an exception
97- val exception = assertThrows<IllegalStateException > {
98- server.removePrompt(" test-prompt" )
172+ @Test
173+ fun `RemovePrompt should throw when prompts capability is not supported` () = runTest {
174+ // Verify that removing a prompt throws an exception
175+ val exception = assertThrows<IllegalStateException > {
176+ serverWithoutPrompts.removePrompt(" test-prompt" )
177+ }
178+ assertEquals(" Server does not support prompts capability." , exception.message)
179+ }
180+
181+ @Test
182+ fun `Remove Prompts should throw when prompts capability is not supported` () = runTest {
183+ // Verify that removing a prompt throws an exception
184+ val exception = assertThrows<IllegalStateException > {
185+ serverWithoutPrompts.removePrompts(emptyList())
186+ }
187+ assertEquals(" Server does not support prompts capability." , exception.message)
188+ }
189+
190+ @Test
191+ fun `Add Prompt should throw when prompts capability is not supported` () = runTest {
192+ // Verify that removing a prompt throws an exception
193+ val exception = assertThrows<IllegalStateException > {
194+ serverWithoutPrompts.addPrompt(name = " test-prompt" ) {
195+ GetPromptResult (
196+ description = " Test prompt description" ,
197+ messages = listOf (),
198+ )
199+ }
200+ }
201+ assertEquals(" Server does not support prompts capability." , exception.message)
202+ }
203+
204+ @Test
205+ fun `Add Prompts should throw when prompts capability is not supported` () = runTest {
206+ // Verify that removing a prompt throws an exception
207+ val exception = assertThrows<IllegalStateException > {
208+ serverWithoutPrompts.addPrompts(emptyList())
209+ }
210+ assertEquals(" Server does not support prompts capability." , exception.message)
99211 }
100- assertEquals(" Server does not support prompts capability." , exception.message)
101212 }
102213}
0 commit comments