Skip to content

Commit 2588881

Browse files
committed
fix function invokes
1 parent 36cd51a commit 2588881

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/commonMain/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerAnnotations.kt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import kotlin.reflect.full.findAnnotation
1818
import kotlin.reflect.full.hasAnnotation
1919
import kotlin.reflect.full.valueParameters
2020
import kotlin.reflect.typeOf
21+
import kotlin.text.get
2122

2223
/**
2324
* Extension function to register tools from class methods annotated with [McpTool].
@@ -34,6 +35,8 @@ public inline fun <reified T : Any> Server.registerAnnotatedTools(instance: T) {
3435
.filter { it.hasAnnotation<McpTool>() }
3536
.forEach { function ->
3637
val annotation = function.findAnnotation<McpTool>()!!
38+
// val functionResult = function.call(instance, 2.0, 3.0)
39+
// print(functionResult)
3740
registerToolFromAnnotatedFunction(instance, function, annotation)
3841
}
3942
}
@@ -100,13 +103,19 @@ public fun <T : Any> Server.registerToolFromAnnotatedFunction(
100103
inputSchema = inputSchema
101104
) { request ->
102105
try {
103-
// Note: In a real implementation, we would use reflection to call the function
104-
// However, due to limitations in Kotlin reflection in Kotlin/Common, we use a workaround
105-
// This code is not used in tests - the tests use a mocked version of the Tools mechanism
106-
107-
// Placeholder for reflection-based function call - not actually executed in tests
108-
val result = CallToolResult(content = listOf(TextContent("Operation completed")), isError = false)
109-
106+
107+
// Use reflection to call the annotated function with the provided arguments
108+
val result = try {
109+
val arguments = function.valueParameters.map { param ->
110+
val paramName = param.name ?: "param${param.index}"
111+
val jsonValue = request.arguments[paramName]
112+
convertJsonValueToKotlinType(jsonValue, param.type)
113+
}
114+
function.call(instance, *arguments.toTypedArray())
115+
} catch (e: Exception) {
116+
throw IllegalArgumentException("Error invoking function ${function.name}: ${e.message}", e)
117+
}
118+
110119
// Handle the result
111120
when (result) {
112121
is CallToolResult -> result

src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/server/ServerAnnotationsTest.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class ServerAnnotationsTest {
2727

2828
// Sample annotated class for testing
2929
class TestToolsProvider {
30-
30+
3131
@McpTool(
3232
name = "echo_string",
3333
description = "Echoes back the input string"
@@ -37,7 +37,7 @@ class ServerAnnotationsTest {
3737
): String {
3838
return "Echoed: $input"
3939
}
40-
40+
4141
@McpTool(
4242
name = "failing_tool",
4343
description = "A tool that always fails"
@@ -67,7 +67,7 @@ class ServerAnnotationsTest {
6767
): CallToolResult {
6868
return CallToolResult(content = listOf(TextContent("Default name test: $input")))
6969
}
70-
70+
7171
@McpTool(
7272
name = "test_optional",
7373
description = "Tests optional parameters"
@@ -95,13 +95,13 @@ class ServerAnnotationsTest {
9595
"Float: $floatParam, List size: ${listParam.size}"
9696
return CallToolResult(content = listOf(TextContent(result)))
9797
}
98-
98+
9999
@McpTool(
100100
name = "type_override",
101101
description = "Test explicit type overrides"
102102
)
103103
fun testTypeOverride(
104-
@McpParam(description = "Parameter with explicit type", type = "object")
104+
@McpParam(description = "Parameter with explicit type", type = "object")
105105
complexParam: String
106106
): String {
107107
return "Received parameter: $complexParam"
@@ -116,7 +116,7 @@ class ServerAnnotationsTest {
116116
): String {
117117
return "Processed: $input"
118118
}
119-
119+
120120
@McpTool(
121121
name = "return_string_list",
122122
description = "Returns a list of strings"

0 commit comments

Comments
 (0)