11package io.modelcontextprotocol.kotlin.sdk.server
22
3- import io.modelcontextprotocol.kotlin.sdk.CallToolRequest
4- import io.modelcontextprotocol.kotlin.sdk.CallToolResult
5- import io.modelcontextprotocol.kotlin.sdk.Implementation
6- import io.modelcontextprotocol.kotlin.sdk.ServerCapabilities
7- import io.modelcontextprotocol.kotlin.sdk.TextContent
8- import io.modelcontextprotocol.kotlin.sdk.Tool
3+ import io.modelcontextprotocol.kotlin.sdk.*
94import kotlinx.coroutines.test.runTest
10- import kotlinx.serialization.json.JsonObject
11- import kotlinx.serialization.json.JsonPrimitive
12- import kotlinx.serialization.json.buildJsonObject
13- import kotlinx.serialization.json.put
14- import kotlinx.serialization.json.putJsonObject
15- import kotlin.RuntimeException
5+ import kotlinx.serialization.json.*
166import kotlin.test.Test
177import kotlin.test.assertEquals
188import kotlin.test.assertNotNull
199import kotlin.test.assertTrue
20- import kotlin.reflect.KFunction
21- import kotlin.reflect.KParameter
22- import kotlin.reflect.full.findAnnotation
23- import kotlin.reflect.full.functions
2410
2511@Suppress(" UNUSED_PARAMETER" )
2612class ServerAnnotationsTest {
@@ -135,93 +121,14 @@ class ServerAnnotationsTest {
135121 capabilities = ServerCapabilities (tools = ServerCapabilities .Tools (listChanged = true ))
136122 )
137123 val server = Server (Implementation (" test" , " 1.0.0" ), serverOptions)
138-
139- // Instead of using reflection with registerAnnotatedTools, we'll register tools directly
140-
141- // Register mock tools that mimic what registerAnnotatedTools would do
142- server.addTool(
143- name = " echo_string" ,
144- description = " Echoes back the input string" ,
145- inputSchema = Tool .Input (
146- properties = buildJsonObject {
147- putJsonObject(" input" ) {
148- put(" type" , " string" )
149- put(" description" , " The string to echo" )
150- }
151- },
152- required = listOf (" input" )
153- )
154- ) { request ->
155- val input = (request.arguments[" input" ] as ? JsonPrimitive )?.content ? : " "
156- CallToolResult (content = listOf (TextContent (" Echoed: $input " )))
157- }
158-
159- server.addTool(
160- name = " add_numbers" ,
161- description = " Adds two numbers together" ,
162- inputSchema = Tool .Input (
163- properties = buildJsonObject {
164- putJsonObject(" a" ) {
165- put(" type" , " number" )
166- put(" description" , " First number" )
167- }
168- putJsonObject(" b" ) {
169- put(" type" , " number" )
170- put(" description" , " Second number" )
171- }
172- },
173- required = listOf (" a" , " b" )
174- )
175- ) { request ->
176- val a = (request.arguments[" a" ] as ? Number )?.toDouble() ? : 0.0
177- val b = (request.arguments[" b" ] as ? Number )?.toDouble() ? : 0.0
178- CallToolResult (content = listOf (TextContent (" Sum: ${a + b} " )))
179- }
180-
181- server.addTool(
182- name = " testDefaultName" ,
183- description = " Test with default name" ,
184- inputSchema = Tool .Input (
185- properties = buildJsonObject {
186- putJsonObject(" input" ) {
187- put(" type" , " string" )
188- }
189- },
190- required = listOf (" input" )
191- )
192- ) { request ->
193- val input = (request.arguments[" input" ] as ? JsonPrimitive )?.content ? : " "
194- CallToolResult (content = listOf (TextContent (" Default name test: $input " )))
195- }
196-
197- server.addTool(
198- name = " test_optional" ,
199- description = " Tests optional parameters" ,
200- inputSchema = Tool .Input (
201- properties = buildJsonObject {
202- putJsonObject(" required" ) {
203- put(" type" , " string" )
204- put(" description" , " Required parameter" )
205- }
206- putJsonObject(" optional" ) {
207- put(" type" , " string" )
208- put(" description" , " Optional parameter" )
209- }
210- },
211- required = listOf (" required" )
212- )
213- ) { request ->
214- val required = (request.arguments[" required" ] as ? JsonPrimitive )?.content ? : " "
215- val optional = (request.arguments[" optional" ] as ? JsonPrimitive )?.content ? : " default value"
216- CallToolResult (content = listOf (TextContent (" Required: $required , Optional: $optional " )))
217- }
218-
124+ server.registerAnnotatedTools(TestToolsProvider ())
125+
219126 // Get the list of registered tools
220127 val toolsResult = server.handleListTools()
221128 val registeredTools = toolsResult.tools
222129
223130 // Verify that tools were properly registered
224- assertEquals(4 , registeredTools.size, " Should have registered 4 tools" )
131+ assertEquals(9 , registeredTools.size, " Should have registered 9 tools" )
225132
226133 // Check echo_string tool
227134 val echoTool = registeredTools.find { it.name == " echo_string" }
@@ -250,8 +157,8 @@ class ServerAnnotationsTest {
250157 val multiTypesTool = registeredTools.find { it.name == " test_multiple_types" }
251158 assertNotNull(multiTypesTool, " test_multiple_types tool should be registered" )
252159 assertEquals(
253- listOf (" stringParam" , " intParam" , " boolParam" ),
254- multiTypesTool.inputSchema.required?.sorted()
160+ setOf (" stringParam" , " intParam" , " boolParam" ),
161+ multiTypesTool.inputSchema.required?.sorted()!! .toSet()
255162 )
256163
257164 // Check parameter type inference
@@ -643,4 +550,4 @@ class ServerAnnotationsTest {
643550 assertTrue(content is TextContent )
644551 assertEquals(" Echoed: hello from single registration" , (content as TextContent ).text)
645552 }
646- }
553+ }
0 commit comments