Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -1242,7 +1242,12 @@ public data class Tool(
* Optional additional tool information.
*/
val annotations: ToolAnnotations?,
) {

/**
* Optional metadata for the tool.
*/
override val _meta: JsonObject = EmptyJsonObject,
) : WithMeta {
@Serializable
public data class Input(val properties: JsonObject = EmptyJsonObject, val required: List<String>? = null) {
@OptIn(ExperimentalSerializationApi::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.modelcontextprotocol.kotlin.sdk
import io.kotest.assertions.json.shouldEqualJson
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.buildJsonObject
import kotlin.test.Test
Expand Down Expand Up @@ -44,6 +45,9 @@ class ToolSerializationTest {
}
},
"required": ["temperature", "conditions", "humidity"]
},
"_meta": {
"_for_test_only": true
}
}
""".trimIndent()
Expand Down Expand Up @@ -91,6 +95,9 @@ class ToolSerializationTest {
},
required = listOf("temperature", "conditions", "humidity"),
),
_meta = buildJsonObject {
put("_for_test_only", JsonPrimitive(true))
},
)

//region Serialize
Expand Down Expand Up @@ -411,6 +418,7 @@ class ToolSerializationTest {
name: String = "get_weather",
title: String? = null,
outputSchema: String? = null,
@Suppress("LocalVariableName") _meta: String? = null,
): String {
val stringBuilder = StringBuilder()

Expand Down Expand Up @@ -453,6 +461,14 @@ class ToolSerializationTest {
)
}

stringBuilder
.appendLine(",")
.append(
"""
"_meta": ${_meta ?: "{}"}
""".trimIndent(),
)

stringBuilder
.appendLine()
.appendLine("}")
Expand All @@ -464,6 +480,7 @@ class ToolSerializationTest {
name: String = "get_weather",
title: String? = null,
outputSchema: Tool.Output? = null,
@Suppress("LocalVariableName") _meta: JsonObject = EmptyJsonObject,
): Tool = Tool(
name = name,
title = title,
Expand All @@ -482,6 +499,7 @@ class ToolSerializationTest {
required = listOf("location"),
),
outputSchema = outputSchema,
_meta = _meta,
)

//endregion Private Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.modelcontextprotocol.kotlin.sdk.server
import io.github.oshai.kotlinlogging.KotlinLogging
import io.modelcontextprotocol.kotlin.sdk.CallToolRequest
import io.modelcontextprotocol.kotlin.sdk.CallToolResult
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject
import io.modelcontextprotocol.kotlin.sdk.GetPromptRequest
import io.modelcontextprotocol.kotlin.sdk.GetPromptResult
import io.modelcontextprotocol.kotlin.sdk.Implementation
Expand Down Expand Up @@ -32,6 +33,7 @@ import kotlinx.collections.immutable.minus
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.persistentMapOf
import kotlinx.collections.immutable.toPersistentSet
import kotlinx.serialization.json.JsonObject

private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -217,6 +219,7 @@ public open class Server(
* @param inputSchema The expected input schema for the tool.
* @param outputSchema The optional expected output schema for the tool.
* @param toolAnnotations Optional additional tool information.
* @param _meta Optional metadata as a [JsonObject].
* @param handler A suspend function that handles executing the tool when called by the client.
* @throws IllegalStateException If the server does not support tools.
*/
Expand All @@ -227,9 +230,10 @@ public open class Server(
title: String? = null,
outputSchema: Tool.Output? = null,
toolAnnotations: ToolAnnotations? = null,
@Suppress("LocalVariableName") _meta: JsonObject = EmptyJsonObject,
handler: suspend (CallToolRequest) -> CallToolResult,
) {
val tool = Tool(name, title, description, inputSchema, outputSchema, toolAnnotations)
val tool = Tool(name, title, description, inputSchema, outputSchema, toolAnnotations, _meta)
addTool(tool, handler)
}

Expand Down
Loading